单个html元素可以有两种背景颜色吗?

sad*_*eee 0 html css background background-color

我想在同一个css类中添加2种不同的背景颜色.

.stepwizard-row:before {
    top: 14px;
    bottom: 0;
    position: absolute;
    content: " ";
    width: 100%;
    height: 4px;
    background-color: #d6d6c2;
    z-order: 0;

}
Run Code Online (Sandbox Code Playgroud)

前50%(考虑总宽度)和剩余的背景颜色是否可以有一种背景颜色?如果它无法实现,有人能建议我实现这一目标吗?

Tem*_*fif 11

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

body {
  margin: 0;
  background: linear-gradient(to right, red 50%, blue 0%);
  
  height:100vh;
  text-align:center;
  color:#fff;
}
Run Code Online (Sandbox Code Playgroud)
some content
Run Code Online (Sandbox Code Playgroud)

body {
  margin: 0;
  background: linear-gradient(to bottom, red 60%, blue 0%);
  
  height:100vh;
  text-align:center;
  color:#fff;
}
Run Code Online (Sandbox Code Playgroud)
some content
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)
some content
Run Code Online (Sandbox Code Playgroud)

body {
  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;
}
Run Code Online (Sandbox Code Playgroud)
some content
Run Code Online (Sandbox Code Playgroud)

如果你想要更多

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

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;
}
Run Code Online (Sandbox Code Playgroud)
some content
Run Code Online (Sandbox Code Playgroud)

您也可以使用伪元素执行相同的操作,并使用一些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;
}
Run Code Online (Sandbox Code Playgroud)
some content
Run Code Online (Sandbox Code Playgroud)