html - 如何锁定 LI 标签中的 DIV 使其无法调整大小?

标签 html css linear-gradients

我想创建一个响应式或可调整大小的背景图案,其中有六个彩色 strip 。我尝试过使用具有多种颜色的 li ,但我的问题是在调整窗口大小时无法锁定我的 li 。也就是说, strip 没有填满整个窗口尺寸。我怎样才能实现这个目标?

下面以片段的形式提供了我的代码。

html,
body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  margin: 0;
  padding: 0;
}
.topper {
  background-color: #ffffff;
  height: 200px;
  width: auto;
  top: 0;
  margin: 0 auto;
  padding: 0;
  display: block;
  position: relative;
  box-shadow: 3px 5px 40px 1px #000000;
  z-index: 1;
}
.bg {
  width: 100%;
  height: 100%;
  vertical-align: baseline;
  display: table-cell;
  min-width: 1366px;
  position: fixed;
  bottom: 0;
}
ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
  position: absolute;
}
ul > li {
  display: inline;
  float: left;
  bottom: 0;
  position: relative;
}
li > div {
  height: 900px;
  width: 214px;
  display: block;
}
.g {
  background-color: #4885ed;
}
.go {
  background-color: #db3236;
}
.goo {
  background-color: #f4c20d;
}
.goog {
  background-color: #4885ed;
}
.googl {
  background-color: #3cba54;
}
.google {
  background-color: #db3236;
}
<div class="topper">
</div>
<div class="bg">
  <ul>
    <li>
      <div class="g"></div>
    </li>
    <li>
      <div class="go"></div>
    </li>
    <li>
      <div class="goo"></div>
    </li>
    <li>
      <div class="goog"></div>
    </li>
    <li>
      <div class="googl"></div>
    </li>
    <li>
      <div class="google"></div>
    </li>
  </ul>
</div>

JSFiddle

最佳答案

您可以使用 linear-gradient 仅使用一个元素来完成此操作作为背景而不是使用多个 li元素。另外,正如 jiff 所指出的,您不应该使用 div里面的元素 li .

通过将元素的大小设置为等于屏幕大小并使用色标的百分比值,可以实现所需的效果。

渐变是使用以下逻辑创建的:

  • 需要六个彩色条作为背景,因此每个彩色条只能占据元素背景的六分之一。也就是说,(100%/6) 约为 16.66%。
  • 第一种颜色应从 0% 开始,到 16.66%(颜色停止)结束,因此应写成如下:

    <color> 0%, <color> 16.66%

    (在代码片段中,未写入 <color> 0%,因为假定第一种颜色从 0% 开始)

  • 第二种颜色应从第一个元素结束的位置开始,并应进一步占据 16.66%。所以它应该从 16.66% 开始,到 33.33% 结束。这就是为什么它写成 #db3236 16.66%, #db3236 33.33% .

  • 类似地,第三种颜色从第二种颜色结束的地方开始,并占据另外 16.66%。所以它的开始是 33.33%,结束是 49.99%。以同样的方式,应确定每种颜色的起点和终点,然后在 linear-gradient 中设置。 .

html,
body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  margin: 0;
  padding: 0;
}
.topper {
  background-color: #ffffff;
  height: 200px;
  width: auto;
  top: 0;
  margin: 0 auto;
  padding: 0;
  display: block;
  position: relative;
  box-shadow: 3px 5px 40px 1px #000000;
  z-index: 1;
}
.bg {
  width: 100%;
  height: 100%;
  position: fixed;
  bottom: 0;
  background: linear-gradient(to right, #4885ed 16.66%, #db3236 16.66%, #db3236 33.33%, #f4c20d 33.33%, #f4c20d 49.99%, #4885ed 49.99%, #4885ed 66.66%, #3cba54 66.66%, #3cba54 83.33%, #db3236 83.33%, #db3236 100%);
}
<div class="topper">
</div>

<div class="bg">
</div>

关于html - 如何锁定 LI 标签中的 DIV 使其无法调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36369603/

相关文章:

javascript - 当页面加载时,jquery Accordion 应该默认折叠

javascript - 使用 d3 获取事件监听器中输入元素的值

javascript - 我的 ID 为 #firstheader 的 H1 div 不会淡出

javascript - 将带有透明背景的 map 放在另一张 map 上

html - Windows 中的 Chrome 缩小 (cntrl - ) 隐藏线性渐变条纹颜色

javascript - 来自对象 javascript 的嵌套 div

css - 单选按钮内联并以标签居中

javascript - 我无法让连续的 Canvas 矩形在 js 中具有重复的线性渐变。任何人都知道如何?

用于创建动画渐变按钮背景的 CSS

html - 当按钮在 Firefox 中处于事件状态时如何防止按钮文本移动?