仅使用CSS创建漂亮的水平线

时间:2013-03-05 03:57:50

标签: css css3

有关如何在photoshop中执行此操作的教程here

enter image description here

我只想用CSS做这件事。我能越接近fiddle

hr.fancy-line { 
    border: 0; 
    height: 1px; 
    background-image: -webkit-linear-gradient(left, rgba(0,0,0,0), rgba(215,215,215,0.75), rgba(0,0,0,0)); 
    background-image: -moz-linear-gradient(left, rgba(0,0,0,0), rgba(215,215,215,0.75), rgba(0,0,0,0)); 
    background-image: -ms-linear-gradient(left, rgba(0,0,0,0), rgba(215,215,215,0.75), rgba(0,0,0,0)); 
    background-image: -o-linear-gradient(left, rgba(0,0,0,0), rgba(215,215,215,0.75), rgba(0,0,0,0)); 
    box-shadow: 0px -2px 4px rgba(136,136,136,0.75);
}
<hr class="fancy-line"></hr>

对阴影做渐变似乎非常困难。 我有什么想法可以改善这个吗?

3 个答案:

答案 0 :(得分:52)

我会将radial-gradient用于伪元素而不是box-shadow,因为它会向边缘逐渐变细。

radial-gradient放在<hr>上方,使其缩小一半。然后在<hr>正下方放置另一个psuedo元素,其颜色与背景相同,高度足以覆盖渐变的其余部分。

Updated JSFiddle


<强> CSS

hr.fancy-line { 
    border: 0; 
    height: 1px;

}
hr.fancy-line:before {
    top: -0.5em;
    height: 1em;
}
hr.fancy-line:after {
    content:'';
    height: 0.5em;
    top: 1px;
}

hr.fancy-line:before, hr.fancy-line:after {
    content: '';
    position: absolute;
    width: 100%;
}

hr.fancy-line, hr.fancy-line:before {
    background: radial-gradient(ellipse at center, rgba(0,0,0,0.1) 0%,rgba(0,0,0,0) 75%);
}

body, hr.fancy-line:after {
    background: #f4f4f4;
}

答案 1 :(得分:3)

请查看https://codepen.io/ibrahimjabbari/pen/ozinB。 该网站提供18种风格的水平线条。有些看起来很棒。

以下是一个例子。

hr.style17 {
    border-top: 1px solid #8c8b8b;
    text-align: center;
}
hr.style17:after {
    content: '§';
    display: inline-block;
    position: relative;
    top: -14px;
    padding: 0 10px;
    background: #f0f0f0;
    color: #8c8b8b;
    font-size: 18px;
    -webkit-transform: rotate(60deg);
    -moz-transform: rotate(60deg);
    transform: rotate(60deg);
}

enter image description here

答案 2 :(得分:1)

以下是实现此目的的最简单形式:

hr{ 
   border: 0; 
   height: 1px; 
   background-image: -webkit-linear-gradient(left, #f0f0f0, #8c8b8b, #f0f0f0);
   background-image: -moz-linear-gradient(left, #f0f0f0, #8c8b8b, #f0f0f0);
   background-image: -ms-linear-gradient(left, #f0f0f0, #8c8b8b, #f0f0f0);
   background-image: -o-linear-gradient(left, #f0f0f0, #8c8b8b, #f0f0f0); 
}