html - 虚线顶部和底部边框比文本短

标签 html css border css-shapes

我想像下图那样实现顶部和底部的边框,如何使用 CSS 技巧实现?

Image

挑战是我不想要带边框的整个宽度,它也应该是响应式的。

手机版图片为http://i.imgur.com/XZTW28N.jpg它也应该适用于桌面和移动浏览器。

我尝试使用 %width 边框,但它不起作用。

我写了下面的代码,但这对我来说不是 100% 完美的答案。

HTML:

<h1>How it Works</h1

CSS:

h1:before, h1:after {
    content: "";
    height: 1px;
    background: linear-gradient(to right,  rgba(0,0,0,0) 0%,rgba(147,147,147,1) 50%,rgba(0,0,0,0) 100%);
    display: block;
    margin-bottom: 10px;
    margin-top: 10px;
}

http://jsfiddle.net/wjhnX/488/

最佳答案

我对您的 CSS 进行了一些更改:

h1{
    text-align: center;
    font-size: 70px;
}

h1:before, h1:after{
    position: relative;
    content: "";
    width: 30%;
    left: 35%;
    display: block;
    margin-bottom: 10px;
    margin-top: 10px;
    border-bottom: 5px dotted yellow;
}

DEMO

编辑:

如果你想要一个固定的宽度,你可以添加:

h1:before, h1:after{
    width: 150px;     /* You can change this value */
    left: 50%;
    transform: translateX(-50%);
}

DEMO2

关于html - 虚线顶部和底部边框比文本短,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706932/

相关文章:

jQuery 粘性标题

ios - 移除 UITabbar 上边框线

html - 如何在 Bootstrap 面板标题中向右显示所有按钮?

html - W3 Modal 立即打开和关闭

css - 如何使整个屏幕减去 x 像素的 div

html - 图片自动给定边框?

css - 如何向锯齿状边框容器添加边框

javascript - 如何以编程方式向类添加样式 - Angular 2

javascript - 带偏移量的哈希位置

html - 带空格的 CSS