CSS CSS淡出水平线/线条样式的div效果,没有图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9437400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 03:05:43  来源:igfitidea点击:

CSS fade out horizontal rule / line styled div effect without images

csslineshadowshapesfadeout

提问by ja_him

I'm a big fan of minimal use of images and was wondering if anyone had a tactic (or if it's possible) to create this kind of thing with pure static CSS?

我是极少使用图像的忠实粉丝,想知道是否有人有策略(或者是否有可能)用纯静态 CSS 创建这种东西?

http://www.flickr.com/photos/jahimandahalf/6780397612/

http://www.flickr.com/photos/jahimandahalf/6780397612/

I'm referring an effect of a line seemingly getting skinnier and fading out and the shadow effect underneath it.

我指的是一条看起来更细、越来越淡的线条的效果,以及它下面的阴影效果。

I was thinking it might be possible to do a CSS shape trick with it like the triangles:

我在想可能可以像三角形一样用它来做一个 CSS 形状技巧:

http://css-tricks.com/snippets/css/css-triangle/

http://css-tricks.com/snippets/css/css-triangle/

Or perhaps with rotation on box-shadow using 'transform':

或者也许使用“变换”在 box-shadow 上旋转:

zenelements.com/blog/css3-transform/

zenelements.com/blog/css3-transform/

Any ideas?

有任何想法吗?

回答by Andres Ilich

You can use CSS3's stops and the :afterpseudo-element to achieve such an effect. The trick is to add a border to the <hr>element by using the :afterpseudo-element and position it in the center of the initial gradient with a soft color that ends with the gradient.

您可以使用 CSS3 的停止和:after伪元素来实现这样的效果。诀窍是<hr>使用:after伪元素为元素添加边框,并将其放置在初始渐变的中心,并使用以渐变结束的柔和颜色。

Here is a quick demo, and another demousing some color.

这是一个快速演示,以及另一个使用某种颜色的演示

回答by Jared Christensen

hr {
  height: 1px;
  margin: 50px 0;
  background: -webkit-gradient(linear, 0 0, 100% 0, from(rgba(0, 0, 0, 0)), color-stop(0.5, #333333), to(rgba(0, 0, 0, 0)));
  background: -webkit-linear-gradient(left, rgba(0, 0, 0, 0), #333333, rgba(0, 0, 0, 0));
  background: -moz-linear-gradient(left, rgba(0, 0, 0, 0), #333333, rgba(0, 0, 0, 0));
  background: -o-linear-gradient(left, rgba(0, 0, 0, 0), #333333, rgba(0, 0, 0, 0));
  background: linear-gradient(left, rgba(0, 0, 0, 0), #333333, rgba(0, 0, 0, 0));
  border: 0;
}
hr:after {
  display: block;
  content: '';
  height: 30px;
  background-image: -webkit-gradient(radial, 50% 0%, 0, 50% 0%, 116, color-stop(0%, #cccccc), color-stop(100%, rgba(255, 255, 255, 0)));
  background-image: -webkit-radial-gradient(center top, farthest-side, #cccccc 0%, rgba(255, 255, 255, 0) 100%);
  background-image: -moz-radial-gradient(center top, farthest-side, #cccccc 0%, rgba(255, 255, 255, 0) 100%);
  background-image: -o-radial-gradient(center top, farthest-side, #cccccc 0%, rgba(255, 255, 255, 0) 100%);
  background-image: radial-gradient(farthest-side at center top, #cccccc 0%, rgba(255, 255, 255, 0) 100%);
}
<hr>

回答by ja_him

Ok so I've answered my own question but I've read the Stackoverflow forums and it seems to be acceptable (if not actually encouraged!)

好的,我已经回答了我自己的问题,但我已经阅读了 Stackoverflow 论坛,这似乎是可以接受的(如果实际上没有受到鼓励!)

So...

所以...

HTML:

HTML:

<html>
<head>
<TITLE>TEST</TITLE>
<link rel="stylesheet" type="text/css" href="test.css" />
</head>
<body>

<div id="wrap">
<div id="gradient">
</div>
</div>

</body>
</html>

CSS:

CSS:

#wrap
{
overflow:hidden;
height:10px;
width:600px;
height:20px;
margin:auto;
margin-top:200px;
}


#gradient
{
height:20px;
width:580px;
margin:auto;
margin-top:-11px;
background: -moz-radial-gradient(center, ellipse cover,  rgba(10,10,10,1) 0%, rgba(8,8,8,1) 19%, rgba(3,3,3,0) 80%, rgba(1,1,1,0) 100%); /* FF3.6+ */
background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,rgba(10,10,10,1)), color-stop(19%,rgba(8,8,8,1)), color-stop(80%,rgba(3,3,3,0)), color-stop(100%,rgba(1,1,1,0))); /* Chrome,Safari4+ */
background: -webkit-radial-gradient(center, ellipse cover,  rgba(10,10,10,1) 0%,rgba(8,8,8,1) 19%,rgba(3,3,3,0) 80%,rgba(1,1,1,0) 100%); /* Chrome10+,Safari5.1+ */
background: -o-radial-gradient(center, ellipse cover,  rgba(10,10,10,1) 0%,rgba(8,8,8,1) 19%,rgba(3,3,3,0) 80%,rgba(1,1,1,0) 100%); /* Opera 12+ */
background: -ms-radial-gradient(center, ellipse cover,  rgba(10,10,10,1) 0%,rgba(8,8,8,1) 19%,rgba(3,3,3,0) 80%,rgba(1,1,1,0) 100%); /* IE10+ */
background: radial-gradient(center, ellipse cover,  rgba(10,10,10,1) 0%,rgba(8,8,8,1) 19%,rgba(3,3,3,0) 80%,rgba(1,1,1,0) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#0a0a0a', endColorstr='#00010101',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */
}

回答by Nick Beranek

In order to reproduce that horizontal rule, you can use a CSS3 linear-gradient. Just create a div with about a 3px height and apply the following CSS (change the colors as needed):

为了重现该水平规则,您可以使用 CSS3 线性渐变。只需创建一个大约 3px 高度的 div 并应用以下 CSS(根据需要更改颜色):

background: #ffffff; /* Old browsers */
background: -moz-linear-gradient(left,  #ffffff 0%, #2989d8 25%, #207cca 75%, #ffffff 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right top, color-stop(0%,#ffffff), color-stop(25%,#2989d8), color-stop(75%,#207cca), color-stop(100%,#ffffff)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(left,  #ffffff 0%,#2989d8 25%,#207cca 75%,#ffffff 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(left,  #ffffff 0%,#2989d8 25%,#207cca 75%,#ffffff 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(left,  #ffffff 0%,#2989d8 25%,#207cca 75%,#ffffff 100%); /* IE10+ */
background: linear-gradient(left,  #ffffff 0%,#2989d8 25%,#207cca 75%,#ffffff 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ffffff',GradientType=1 ); /* IE6-9 */

Keep in mind that filterdoes not support color stops, so you may want an image fall back for < IE10.

请记住,filter它不支持颜色停止,因此您可能希望图像在 < IE10 后退。

Build your own CSS3 gradient here: http://www.colorzilla.com/gradient-editor/

在此处构建您自己的 CSS3 渐变:http: //www.colorzilla.com/gradient-editor/

回答by Biswajit

This code create a normal hr but the difference will is it will have both end with fade effect(gradient black to blue)

此代码创建了一个正常的 hr,但不同之处在于它的两端都有淡入淡出效果(渐变黑色到蓝色)

hr{
border-top-color: black;
margin-bottom: 25px;
width: 80%;
}
hr::before{
display: block;
margin-left: -10%;
margin-top: -1px;
content:"";
background: linear-gradient(to left, black, blue);
width: 10%;
height: 1px;
}
hr::after{
display: block;
margin-left: 100%;
margin-top: -1px;
content:"";
background: linear-gradient(to right, black, blue);
width: 10%;
height: 1px;
}