【问题标题】:CSS border above and below but not full widthCSS边框上下但不是全宽
【发布时间】:2014-11-10 20:58:52
【问题描述】:

我正在尝试为我正在开发的网站上的块引用定义全局样式。

我们的目标是设置块引用的样式,使它们看起来类似于下图。我想避免修改 DOM 结构。

使用伪类我想在元素上方和下方显示水平平行边框,但线条应该只有元素本身宽度的一半并且水平居中。

这是我到目前为止所获得的,但线条没有正确居中。

blockquote {
    margin: 0 auto;
    position: relative;
    text-align: center;
    display: table;
    font-size: 15px;
}

blockquote:before {
    content: '\A';
    height: 1px;
    width: 40%;
    position: absolute;
    background: #000;
    top: -8px;
}

blockquote:after {
    content: '\A';
    height: 1px;
    width: 40%;
    position: absolute;
    background: #000;
    bottom: -8px;
}
<blockquote>
  Neque porro quisquam e porro quisquest qui dolorem ipsum quia dolor sit<br />
 est qui dolorem ipsum quia dolor sit amet rem ipsum quia 
</blockquote>

【问题讨论】:

    标签: css styling text-styling blockquote


    【解决方案1】:

    如果宽度是固定的,您可以使用负边距左到中心元素。在你的情况下margin-left: -20%;

    blockquote { 
        margin: 0 auto;
        position: relative;
        text-align: center;
        display: table;
        font-size: 15px;
    }
    blockquote:before, blockquote:after {
        content: '';
        position: absolute;
        top: 0;
        left: 50%;         /* <-- put left edge in the middle */
        margin-left: -20%; /* <-- shift to the left by half of the width */
        width: 40%;
        height: 1px;
        background: #000;
    }
    blockquote:after {
        top: inherit;
        bottom: 0;
    }
    <blockquote>
      Neque porro quisquam e porro quisquest qui dolorem ipsum quia dolor sit<br>
     est qui dolorem ipsum quia dolor sit amet rem ipsum quia 
    </blockquote>

    【讨论】:

    • 这很好,在前后添加一个类似于 -10px 的快速边距顶部/边距底部,他也有他的间距,+1 今天学习一些东西:)
    • 是边距顶部/底部或负顶部/底部值来调整垂直间距。
    • @dcc 这可以简化一点,只使用一个伪元素和边框顶部/底部:jsfiddle.net/webtiki/krnorhwp
    • @web-tiki 在这种情况下,我认为这个伪元素会覆盖内容。
    • @dfsq 不,因为我使用了 before 伪元素,它位于块引用的内容后面。您可以通过使用开发工具进行检查来看到这一点。
    猜你喜欢
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2020-10-28
    • 2012-09-19
    • 2022-01-24
    • 2013-06-16
    相关资源
    最近更新 更多