关于这个问题
css rainbow built using gradient colors
有没有办法通过使用多个innerHTML
而不是一个来做到这一点?
您可能会问,为什么仅出于教育目的就只用一个就能做到这一点。
这是我所做的,但看起来并不好。
任何想法都会受到赞赏。
wordsToBeTyped
divs
答案 0 :(得分:1)
我使用float:right
并使用宽度来均衡列。根据您的参考示例使用相同的梯度。希望对您有帮助。
.mainDiv {
clear: both;
}
.rainbow1 {
background: linear-gradient(78deg, red 5%, transparent 7%), linear-gradient(69deg, orange 10%, transparent 12%), linear-gradient(60deg, yellow 15%, transparent 17%), linear-gradient(51deg, green 20%, transparent 22%);
width: 50%;
float: left;
height: 100px;
}
.rainbow2 {
background: linear-gradient(43deg, blue 25%, transparent 27%), linear-gradient(35deg, indigo 30%, transparent 32%), linear-gradient(28deg, violet 35%, transparent 37%);
width: 50%;
height: 100px;
}
<div class="mainDiv">
<div class="rainbow1"></div>
<div class="rainbow2"></div>
</div>
使用width:33%
更新了三列的代码。随附摘要供您参考。
.mainDiv {
clear: both;
position: relative;
}
.rainbow1 {
background: linear-gradient(78deg, red 5%, transparent 7%), linear-gradient(69deg, orange 10%, transparent 12%), linear-gradient(60deg, yellow 15%, transparent 17%);
height: 100px;
width: 33%;
position: relative;
z-index: 2;
}
.rainbow2 {
background: linear-gradient(51deg, green 20%, transparent 22%), linear-gradient(43deg, blue 25%, transparent 27%);
height: 100px;
width: 33%;
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
.rainbow3 {
background: linear-gradient(35deg, indigo 30%, transparent 32%), linear-gradient(28deg, violet 35%, transparent 37%);
height: 100px;
width: 33%;
position: absolute;
top: 0;
left: 0;
}
<div class="mainDiv">
<div class="rainbow1"></div>
<div class="rainbow2"></div>
<div class="rainbow3"></div>
</div>
答案 1 :(得分:1)
由于您要考虑多个div,因此我将摆脱渐变并考虑偏斜变换和纯背景颜色具有重叠部分。
这里是3格:
.rainbow {
position:relative;
width:200px;
height:100px;
}
.rainbow > div {
position:absolute;
top:0;
bottom:0;
width:100%;
right:100%;
transform-origin:top;
}
.rainbow > div:nth-last-child(1) {
background:red;
transform:skewX(8deg);
}
.rainbow > div:nth-last-child(2) {
background:blue;
transform:skewX(16deg);
}
.rainbow > div:nth-last-child(3) {
background:yellow;
transform:skewX(24deg);
}
body {
margin: 0;
}
<div class="rainbow">
<div></div>
<div></div>
<div></div>
</div>
您可以轻松缩放到任意数量的div。
.rainbow {
position:relative;
width:200px;
height:100px;
}
.rainbow > div {
position:absolute;
top:0;
bottom:0;
width:100%;
right:100%;
transform-origin:top;
}
.rainbow > div:nth-last-child(1) {
background:red;
transform:skewX(8deg);
}
.rainbow > div:nth-last-child(2) {
background:blue;
transform:skewX(16deg);
}
.rainbow > div:nth-last-child(3) {
background:yellow;
transform:skewX(24deg);
}
.rainbow > div:nth-last-child(4) {
background:pink;
transform:skewX(32deg);
}
.rainbow > div:nth-last-child(5) {
background:purple;
transform:skewX(40deg);
}
body {
margin: 0;
}
<div class="rainbow">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
要淡入淡出,可以考虑使用blur
过滤器:
.rainbow {
position:relative;
width:200px;
height:100px;
overflow:hidden;
}
.rainbow > div {
position:absolute;
top:0;
bottom:-5px;
width:100%;
right:100%;
transform-origin:top;
filter:blur(2px);
}
.rainbow > div:nth-last-child(1) {
background:red;
transform:skewX(8deg);
}
.rainbow > div:nth-last-child(2) {
background:blue;
transform:skewX(16deg);
}
.rainbow > div:nth-last-child(3) {
background:yellow;
transform:skewX(24deg);
}
.rainbow > div:nth-last-child(4) {
background:pink;
transform:skewX(32deg);
}
.rainbow > div:nth-last-child(5) {
background:purple;
transform:skewX(40deg);
}
body {
margin: 0;
}
<div class="rainbow">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
答案 2 :(得分:0)
下面是一个例子:
div[class*="rainbow"] {
width: 0;
height: 0;
border-style: solid;
position: absolute;
}
.rainbowRed {
border-width: 200px 0 0 20px;
border-color: transparent transparent transparent red;
z-index: 7;
}
.rainbowOrange {
border-width: 200px 0 0 40px;
border-color: transparent transparent transparent orange;
z-index: 6;
}
.rainbowYellow {
border-width: 200px 0 0 70px;
border-color: transparent transparent transparent yellow;
z-index: 5;
}
.rainbowGreen {
border-width: 200px 0 0 110px;
border-color: transparent transparent transparent green;
z-index: 4;
}
.rainbowBlue {
border-width: 200px 0 0 160px;
border-color: transparent transparent transparent blue;
z-index: 3;
}
.rainbowIndigo {
border-width: 200px 0 0 220px;
border-color: transparent transparent transparent indigo;
z-index: 2;
}
.rainbowViolet {
border-width: 200px 0 0 290px;
border-color: transparent transparent transparent violet;
z-index: 1;
}
<div class="rainbowRed"></div>
<div class="rainbowOrange"></div>
<div class="rainbowYellow"></div>
<div class="rainbowGreen"></div>
<div class="rainbowBlue"></div>
<div class="rainbowIndigo"></div>
<div class="rainbowViolet"></div>