html - 在另一个中心 div

标签 html css

我无法将一个 div 的内容置于另一个 div 的中心,因为内容没有显示。

#searchbkg {
  postition: relative;
  width: 100%;
  height: 700px;
  background-color: #85e085;
}
#searchcentre {
  position: absolute;
  width: 50%;
  margin: 0 auto;
}
<div id="searchbkg">
  <div id="searchcentre">Test</div>
</div>

绿框出现,但里面没有文字。

最佳答案

您的文字看起来不错,但它不会居中,因为您在内部 div 上有 position: absolute;。将其更改为 position: relative; 它将水平居中。如果您需要文本在 div 中居中,您还可以应用 text-align: center;

#searchbkg {
  position: relative;
  width: 100%;
  height: 700px;
  background-color: #85e085;
}
#searchcentre {
  position: relative;
  width: 50%;
  margin: 0 auto;
  text-align: center;
  border: 1px solid red;
}
<div id="searchbkg">
  <div id="searchcentre">This is a centered div!</div>
</div>

关于html - 在另一个中心 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38832672/

相关文章:

html - CSS:如何使用渐变过渡切换两个背景图像?

html - CSS 根据屏幕大小更改内容

asp.net - 如何制作最大尺寸设置为包含 DIV 的 GridView

javascript - 固定背景的移动视差

css - Chrome 不支持 BUTTON 标签的 "writing-mode"?

javascript - 为什么我网站上的头像链接会泄露?

html - 仅在三个侧面有多个插入框阴影和 alpha 透明度的阴影

javascript相关的调试工具或库?

html - Markdown 中的多行项目符号列表

javascript - 电话间隙没有状态栏?