html - 无法将段落居中::在 div 之前

标签 html css sass

div.st-header-image {
  width: 100%;
  background-color: #a7b885;
  margin: 0px;
  text-align: center;
  overflow: hidden;
}

div.st-header-image p.st-description {
  margin: 0px;
  color: red;
  font-size: 40px;
  line-height: 40px;
  vertical-align: middle;
}

div.st-header-image ::before {
  content: " ";
  padding-top: 40%;
  display: inline-block;
}
<body>
<div class="st-header-image">
        <p class="st-description">Header Image</p>
</div>
</body>

我正在尝试制作一个需要在具有::before 样式的 div 内部的段落,以便在我增加或减少分辨率时它会改变大小。

我尝试使用不同的溢出,不同的显示...还尝试使用 calc((100% - 40px)/2) 来定位顶部/底部来修复它,但它似乎也不起作用。

div.st-header-image
  {
    width:100%;
    background-color: rgb(167, 184, 133);
    margin:0px;
    text-align: center;
    overflow: hidden;

      p.st-description
      {
          margin:0px;
          color:red;
          font-size:40px;
          line-height: 40px;
          vertical-align: middle;
      }
      ::before
      {
        content: " ";
        padding-top: 40%;
        display: inline-block;
      } 
  }

p 元素位于类为 st-header-image 的 div 中

Div 是响应式的,但段落一直显示在 div 下方而不是在它的中心...

最佳答案

你想要完成的是让 div 在顶部有一个响应空间,并且在它的响应期间段落坚持在中间。 我修复了没有 ::before 伪元素的代码。

可以使用 div 的填充和一点定位来实现相同的功能。

我在 repl.it 上分享了代码 here

这是您需要的 CSS:

div.st-header-image {
  width: 100%;
  background-color: #a7b885;
  margin: 0px;
  text-align: center;
  overflow: hidden;
  padding-top: 40%;
  position: relative;
}

p.st-description {
  margin: 0px;
  color: red;
  font-size: 40px;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  text-align: center;
  padding-top: 15%;
}

关于html - 无法将段落居中::在 div 之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57268679/

相关文章:

css - 向 slider 添加过渡缓和

css - 无法编译现有元素,因为 Compass 找不到它

javascript - 单击链接后更改其 CSS 属性的元素的正确名称是什么

html - 将两个链接放在同一个框中

javascript - 从 textarea 执行 javascript

html - CSS 在表格下方显示 div

javascript - 在 Javascript 中更改 CSS 显示样式

CSS 菜单栏没有子菜单

css - 如何更改 CSS 样式取决于浏览器

sass - 在 Scout 中设置自定义图像路径