html - CSS :before element is not align with my text

标签 html css

我正在使用 :before 元素在我的文本之前添加一个图像。但它没有对齐。

.page-header:before {
  content: url('../img/page-header.png');
  display: inline-block;
}

.page-header .page-title {
  padding: 0 10px;
  color: #ff4500;
  font-size: 32px;
  padding-bottom: 0px;
  padding-left: 0px;
}

.page-header .page-sub-title {
  font-size: 18px;
}
<div class="container">
  <div class="row">
    <div class="col-md-12">
      <div class="page-header">
        <span class="page-title">Title</span>
        <br/>
        <span class="page-sub-title">
                        <span>Subtitle</span><br/>
        <span>Introduction Paragraph</span>
        </span>
      </div>
    </div>
  </div>
</div>

现状:

enter image description here

期望的结果:

enter image description here

最佳答案

您可以通过在 :before 上使用 position:absolute;height:100%; 来实现这一点。

.page-header {
  padding-left: 15px;
  position: relative;
}

.page-header:before {
    content: '';
    display: inline-block;
    border: 1px solid #000;
    border-top: 0;
    border-bottom: 0;
    height: 100%;
    width: 3px;
    
    position: absolute;
    left: 0;
    top: 0;

}

.page-header .page-title {
    color: #ff4500;
    font-size: 32px;
}

.page-header .page-sub-title {
    font-size: 18px;
}
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="page-header">
                <span class="page-title">Title</span>
                <br/>
                <span class="page-sub-title">
                    <span>Subtitle</span><br/>
                    <span>Introduction Paragraph</span>
                </span>
            </div>
        </div>
    </div>
</div>

关于html - CSS :before element is not align with my text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56559449/

相关文章:

javascript - 修复窗口调整大小时带有侧边栏位置的导航栏

javascript - 逐渐增加 eclipse 刻素描元素的不透明度

css - Bootstrap 3 : Span rows in a grid

html - CSS类不继承

javascript - 从 Todo 应用程序元素中删除元素

Javascript 无法在移动设备上正确触发 - iOS/Android

html - CSS 定位 : How to make 2 overlapping divs and then 1 more div to the right of the overlapping divs

html - css 不成比例的图像缩放

javascript - 许多按钮的按钮单击动画

css - 顶栏偏离中心?