栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将多种背景色应用于一个div

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将多种背景色应用于一个div

A
div实际上可以在没有

:before
:after
选择器的情况下进行,但第一次尝试使用线性渐变。唯一的区别是您必须指定4个位置。暗灰色从0到50%,亮灰色从50%到100%,如下所示:

background: linear-gradient(to right,  #9c9e9f 0%,#9c9e9f 50%,#f6f6f6 50%,#f6f6f6 100%);

如您所知,B div是由具有2个位置的线性渐变制成的,如下所示:

background: linear-gradient(to right,  #9c9e9f 0%,#f6f6f6 100%);

对于C div,我使用与div A相同的梯度:

background: linear-gradient(to right,  #9c9e9f 0%,#9c9e9f 50%,#33ccff 50%,#33ccff 100%);

但是这一次我使用

:after
白色背景的选择器,就像div的第二部分较小一样。 * 请注意,我在下面添加了更好的选择。

查看此jsfiddle或以下代码段,以获取完整的跨浏览器代码。

div{    position:relative;    width:80%;    height:100px;    color:red;    text-align:center;    line-height:100px;    margin-bottom:10px;}.a{    background: #9c9e9f;     background: -moz-linear-gradient(left,  #9c9e9f 0%, #9c9e9f 50%, #f6f6f6 50%, #f6f6f6 100%);     background: -webkit-gradient(linear, left top, right top, color-stop(0%,#9c9e9f), color-stop(50%,#9c9e9f), color-stop(50%,#f6f6f6), color-stop(100%,#f6f6f6));     background: -webkit-linear-gradient(left,  #9c9e9f 0%,#9c9e9f 50%,#f6f6f6 50%,#f6f6f6 100%);     background: -o-linear-gradient(left,  #9c9e9f 0%,#9c9e9f 50%,#f6f6f6 50%,#f6f6f6 100%);     background: -ms-linear-gradient(left,  #9c9e9f 0%,#9c9e9f 50%,#f6f6f6 50%,#f6f6f6 100%);     background: linear-gradient(to right,  #9c9e9f 0%,#9c9e9f 50%,#f6f6f6 50%,#f6f6f6 100%);     filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#9c9e9f', endColorstr='#f6f6f6',GradientType=1 ); }.b{    background: #9c9e9f;     background: -moz-linear-gradient(left,  #9c9e9f 0%, #f6f6f6 100%);     background: -webkit-gradient(linear, left top, right top, color-stop(0%,#9c9e9f), color-stop(100%,#f6f6f6));     background: -webkit-linear-gradient(left,  #9c9e9f 0%,#f6f6f6 100%);     background: -o-linear-gradient(left,  #9c9e9f 0%,#f6f6f6 100%);     background: -ms-linear-gradient(left,  #9c9e9f 0%,#f6f6f6 100%);     background: linear-gradient(to right,  #9c9e9f 0%,#f6f6f6 100%);     filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#9c9e9f', endColorstr='#f6f6f6',GradientType=1 ); }.c{    background: #9c9e9f;     background: -moz-linear-gradient(left,  #9c9e9f 0%, #9c9e9f 50%, #33ccff 50%, #33ccff 100%);     background: -webkit-gradient(linear, left top, right top, color-stop(0%,#9c9e9f), color-stop(50%,#9c9e9f), color-stop(50%,#33ccff), color-stop(100%,#33ccff));     background: -webkit-linear-gradient(left,  #9c9e9f 0%,#9c9e9f 50%,#33ccff 50%,#33ccff 100%);     background: -o-linear-gradient(left,  #9c9e9f 0%,#9c9e9f 50%,#33ccff 50%,#33ccff 100%);     background: -ms-linear-gradient(left,  #9c9e9f 0%,#9c9e9f 50%,#33ccff 50%,#33ccff 100%);     background: linear-gradient(to right,  #9c9e9f 0%,#9c9e9f 50%,#33ccff 50%,#33ccff 100%);     filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#9c9e9f', endColorstr='#33ccff',GradientType=1 ); }.c:after{    content:"";    position:absolute;    right:0;    bottom:0;    width:50%;    height:20%;    background-color:white;}<div >A</div><div >B</div><div >C</div>

对于C div,还有一种替代方法是不使用白色背景来隐藏第二部分的一部分。而是使第二部分透明,并使用

:after
选择器充当具有所需位置和大小的彩色背景。

有关此更新的解决方案,请参见下面的jsfiddle或代码段。

div {  position: relative;  width: 80%;  height: 100px;  color: red;  text-align: center;  line-height: 100px;  margin-bottom: 10px;}.a {  background: #9c9e9f;    background: -moz-linear-gradient(left, #9c9e9f 0%, #9c9e9f 50%, #f6f6f6 50%, #f6f6f6 100%);    background: -webkit-gradient(linear, left top, right top, color-stop(0%, #9c9e9f), color-stop(50%, #9c9e9f), color-stop(50%, #f6f6f6), color-stop(100%, #f6f6f6));    background: -webkit-linear-gradient(left, #9c9e9f 0%, #9c9e9f 50%, #f6f6f6 50%, #f6f6f6 100%);    background: -o-linear-gradient(left, #9c9e9f 0%, #9c9e9f 50%, #f6f6f6 50%, #f6f6f6 100%);    background: -ms-linear-gradient(left, #9c9e9f 0%, #9c9e9f 50%, #f6f6f6 50%, #f6f6f6 100%);    background: linear-gradient(to right, #9c9e9f 0%, #9c9e9f 50%, #f6f6f6 50%, #f6f6f6 100%);    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#9c9e9f', endColorstr='#f6f6f6', GradientType=1);  }.b {  background: #9c9e9f;    background: -moz-linear-gradient(left, #9c9e9f 0%, #f6f6f6 100%);    background: -webkit-gradient(linear, left top, right top, color-stop(0%, #9c9e9f), color-stop(100%, #f6f6f6));    background: -webkit-linear-gradient(left, #9c9e9f 0%, #f6f6f6 100%);    background: -o-linear-gradient(left, #9c9e9f 0%, #f6f6f6 100%);    background: -ms-linear-gradient(left, #9c9e9f 0%, #f6f6f6 100%);    background: linear-gradient(to right, #9c9e9f 0%, #f6f6f6 100%);    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#9c9e9f', endColorstr='#f6f6f6', GradientType=1);  }.c {  background: #9c9e9f;    background: -moz-linear-gradient(left, #9c9e9f 0%, #9c9e9f 50%, rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0) 100%);    background: -webkit-gradient(linear, left top, right top, color-stop(0%, #9c9e9f), color-stop(50%, #9c9e9f), color-stop(50%, rgba(0, 0, 0, 0)), color-stop(100%, rgba(0, 0, 0, 0)));    background: -webkit-linear-gradient(left, #9c9e9f 0%, #9c9e9f 50%, rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0) 100%);    background: -o-linear-gradient(left, #9c9e9f 0%, #9c9e9f 50%, rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0) 100%);    background: -ms-linear-gradient(left, #9c9e9f 0%, #9c9e9f 50%, rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0) 100%);    background: linear-gradient(to right, #9c9e9f 0%, #9c9e9f 50%, rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0) 100%);    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#9c9e9f', endColorstr='#ffffff00', GradientType=1);  }.c:after {  content: "";  position: absolute;  right: 0;  top: 0;  width: 50%;  height: 80%;  background-color: #33ccff;  z-index: -1}<div >A</div><div >B</div><div >C</div>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/635268.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号