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

一个html元素可能有两种背景颜色吗?

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

一个html元素可能有两种背景颜色吗?

只需使用 线性渐变 作为背景,您就可以轻松调整方向,颜色以及每种颜色的百分比:

body {  margin: 0;  background: linear-gradient(to right, red 50%, blue 0%);  height:100vh;  text-align:center;  color:#fff;}some contentbody {  margin: 0;  background: linear-gradient(to bottom, red 60%, blue 0%);  height:100vh;  text-align:center;  color:#fff;}some content

或使用 伪元素 和简单的 背景颜色, 然后简单地控制伪元素的位置/大小即可控制两个背景:

body {  margin: 0;  background: red;  height: 100vh;  position: relative;  text-align:center;  color:#fff;}body:before {  content: "";  position: absolute;  top: 0;  bottom: 0;  left: 0;  right: 50%;  background: blue;  z-index:-1;}some contentbody {  margin: 0;  background: red;  height: 100vh;  position: relative;  text-align:center;  color:#fff;}body:before {  content: "";  position: absolute;  top: 0;  bottom: 40%;  left: 0;  right: 0;  background: blue;  z-index:-1;}some content

如果你想要更多

您可以在渐变内组合不同的颜色,也可以使用多个线性背景,以便为背景实现更复杂的颜色划分:

body {  margin: 0;  background:linear-gradient(30deg, red 50%, blue 50%, blue 70%,orange 70%) left/50% 100% no-repeat,   linear-gradient(-30deg, red 50%, blue 50%, blue 70%,orange 70%) right/50% 100% no-repeat;  height:100vh;  text-align:center;  color:#fff;}some content

您还可以对伪元素执行相同的操作,还可以使用一些CSS转换(旋转,倾斜等):

body {  margin: 0;  background: red;  height: 100vh;  position: relative;  text-align: center;  color: #fff;  overflow: hidden;}body:before {  content: "";  position: absolute;  top: 0;  bottom: 0;  left: -50%;  right: 50%;  background: blue;  transform: skew(30deg);  z-index: -1;}body:after {  content: "";  position: absolute;  top: 0;  bottom: 0;  left: 50%;  right: -50%;  background: orange;  transform: skew(-30deg);  z-index: -1;}some content


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

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

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