简体   繁体   English

方框,线性渐变,css 和 javascript

[英]Box, linear gradient, css and javascript

I'm working with react JS and i look for a solution for linear gradient in different Box.我正在使用 React JS,我正在寻找不同 Box 中线性渐变的解决方案。 When i have three same box in a row.当我连续三个相同的盒子时。 I'have an example: When I've three same box like this: enter image description here我有一个例子:当我有三个这样的相同框时:在此处输入图像描述

It Should be like that:应该是这样的:

enter image description here在此处输入图像描述

Do you have anea idea about this?你对此有什么想法吗?

Thanks;谢谢;

 .box { width: 200px; height: 200px; margin: 0 auto; }.box--gradient { background-image: linear-gradient(#2a6496, #fff 70%, #011852); }
 <div class="box box--gradient"></div>

You can give the background to the container, and have another element who will cover all unused space from the child elements (and hide the background in that area)您可以为容器提供背景,并让另一个元素覆盖子元素中所有未使用的空间(并隐藏该区域中的背景)

 html, body { background-color: white; }.cont { display:flex; background: linear-gradient(to right, red, yellow, green, blue) no-repeat; }.spacer { width: 100%; background-color: white; }.box { width: 100px; height: 100px; flex-shrink:0; display: flex; justify-content:center; align-items:center; }
 <h1>One box</h1> <div class="cont"> <div class="box">1</div> <div class="spacer"></div> </div> <h1>Three boxes</h1> <div class="cont"> <div class="box">1</div> <div class="box">2</div> <div class="box">3</div> <div class="spacer"></div> </div>

Update更新

According to last comment I understand there gonna be few different gradients boxes in a row, while up to 3 can come in a row, and in this case they should collapce.根据最后的评论,我知道连续会有几个不同的渐变框,而最多可以连续出现 3 个,在这种情况下它们应该折叠。 in that case I offer this:在这种情况下,我提供这个:

 .cont { display: flex; overflow: auto; }.grad-1 { background-image: linear-gradient(to right, red, yellow, green); }.grad-2 { background-image: linear-gradient(to right, blue, purple, pink); }.grad-3 { background-image: linear-gradient(to right, white,silver, black); } [class^=grad]{ width: 100px; height: 100px; flex-shrink:0; background-size: 300% 100%; background-position: top left; }.grad-1+.grad-1,.grad-2+.grad-2,.grad-3+.grad-3 { background-position: top center; }.grad-1+.grad-1+.grad-1,.grad-2+.grad-2+.grad-2,.grad-3+.grad-3+.grad-3 { background-position: top right; }
 <div class="cont"> <div class="grad-1"></div> <div class="grad-1"></div> <div class="grad-3"></div> <div class="grad-2"></div> <div class="grad-2"></div> <div class="grad-2"></div> <div class="grad-3"></div> <div class="grad-1"></div> <div class="grad-1"></div> <div class="grad-3"></div> <div class="grad-3"></div> <div class="grad-3"></div> </div>

You can either add a big background and change its position您可以添加一个大背景并更改其 position

 body { display: flex; }.box { width: 100px; height: 100px; background: linear-gradient(to right, blue, red); background-size: 1300%; }.box:nth-child(1) { background-position: 0 0; }.box:nth-child(2) { background-position: 8.33% 0; }.box:nth-child(3) { background-position: 16.66% 0; }.box:nth-child(4) { background-position: 25% 0; }.box:nth-child(5) { background-position: 33.33% 0; }.box:nth-child(6) { background-position: 41.66% 0; }.box:nth-child(7) { background-position: 50% 0; }.box:nth-child(8) { background-position: 58.33% 0; }.box:nth-child(9) { background-position: 66.66% 0; }.box:nth-child(10) { background-position: 75% 0; }.box:nth-child(11) { background-position: 83.33% 0; }.box:nth-child(12) { background-position: 91.66% 0; }.box:nth-child(13) { background-position: 100% 0; }
 <div class="box">1</div> <div class="box">2</div> <div class="box">3</div> <div class="box">4</div> <div class="box">5</div> <div class="box">6</div> <div class="box">7</div> <div class="box">8</div> <div class="box">9</div> <div class="box">10</div> <div class="box">11</div> <div class="box">12</div> <div class="box">13</div>

Or Add your boxes to a container that have this background或将您的盒子添加到具有此背景的容器中

 .container { display: flex; background: linear-gradient(to right, blue, red); }.box { width: 100%; height: 100px; display: flex; justify-content: center; align-items: center; color: white; font-size: 2rem; }
 <div class="container"> <div class="box">1</div> <div class="box">2</div> <div class="box">3</div> </div>

A simple solution:一个简单的解决方案:

You have 3 boxes so, add a color for the first one, a gradient for the second and a color for the last one... (check my demo)你有 3 个盒子,所以,为第一个添加一个颜色,为第二个添加一个渐变,为最后一个添加一个颜色......(查看我的演示)

App.js应用程序.js

import React from 'react';
import './style.css';

export default function App() {
  return (
      <div>
        <div className="row">
          <div className="box"> </div>
          <div className="box"> </div>
          <div className="box"> </div>
        </div>
      </div>
  );
}

Styles.css Styles.css

.row {
  display: flex;
}
.row .box:first-of-type {
  height: 150px;
  width: 33%;
  border: 1px solid black;
  background: #44107a;
}
.row .box {
  height: 150px;
  width: 33%;
  border: 1px solid black;
  background-image: linear-gradient(90deg, #44107a 0%, #ff0032 100%);
}
.row .box:last-of-type {
  height: 150px;
  width: 33%;
  border: 1px solid black;
  background: #ff0032;
}

Demo: Stackblitz演示: Stackblitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM