【问题标题】:Multiple background colors with css only仅使用 css 的多种背景颜色
【发布时间】:2016-02-22 14:02:19
【问题描述】:

我想要一个元素有多种背景颜色? 类比:

<div class="multiplebackground">
  <div style="background-color:red"></div>
  <div style="background-color:green"></div>
  <div style="background-color:blue"></div>
</div>

但如果我只有 1 个带有一些文本内容的 div:

<div class="multiplebackground">
  ...
</div>

.multiplebackground {
  ???
}

还是不可能?

【问题讨论】:

标签: html css


【解决方案1】:

您可以使用渐变来实现这一点。您只需要从一种颜色混合到相同颜色,然后在零像素之间混合到下一种颜色,依此类推。

.Neapolitan {
  height: 200px;
  width: 200px;
  background: linear-gradient(to bottom, red 0%, red 33%, green 33%, green 66%, blue 66%, blue 100%);
}
<div class="Neapolitan">

</div>

(前缀替代和 IE 特定代码可用于旧版浏览器)

【讨论】:

    【解决方案2】:

    您可以通过linear-gradient 做到这一点

    body, html {
      margin: 0;
      padding: 0;
    }
    
    .multiplebackground {
      min-height: 100vh;
      background: linear-gradient(to bottom, red 0%, red 33%, green 33%, green 66%, blue 66%, blue 100%);
      background-size: 100%;
      background-repeat: no-repeat;
    }
    &lt;div class="multiplebackground"&gt;&lt;/div&gt;

    【讨论】:

      【解决方案3】:

      是的...使用带有色标的线性渐变

      .multiplebackground {
        height: 50px;
        background: linear-gradient(to right, red, red 33%, blue 33%, blue 66%, green 66%);
      }
      <div class="multiplebackground">
      
      </div>

      【讨论】:

        【解决方案4】:

        您可以使用 css3 渐变背景属性来做到这一点!

        background: linear-gradient(to right, 
                    red 33%, 
                    green 33%, 
                    green 66%, 
                    blue 66%); 
        

        请注意,我将green 定义了两次,从 33% 到 66%。那是因为我必须定义它从哪里开始和在哪里结束,所以我在每个渐变之间都有一个锐利的边缘

        【讨论】:

          【解决方案5】:

          可能是线性渐变

          http://www.w3schools.com/css/css3_gradients.asp

          body, html {
          margin: 0;
            padding: 0;
          }
          
             #grad {
          background: red; /* For browsers that do not support gradients */
          background: -webkit-linear-gradient(red, yellow, green); /* For Safari 5.1 to 6.0 */
          background: -o-linear-gradient(red, yellow, green); /* For Opera 11.1 to 12.0 */
          background: -moz-linear-gradient(red, yellow, green); /* For Firefox 3.6 to 15 */
          background: linear-gradient(red, yellow, green); /* Standard syntax */
          }
          &lt;div id="grad"&gt;&lt;/div&gt;

          【讨论】:

          • 大多数现代浏览器不需要前缀。只有标准语法可以正常工作。 Get rid of prefixes
          • 是的,但旧浏览器仍在使用
          • 是的,我同意。但是数量非常少。如上述文章所述,-o- 前缀浏览器占全球总用户群的 0.25%。其他人也是如此。我更喜欢这个,因为我希望我的代码看起来整洁:)
          • 歌剧在俄罗斯很受欢迎,我想几年前有30%的市场份额。但是在他们克隆 chrome 之后,我认为人气并没有那么大......
          【解决方案6】:

          使用 CSS,您可以(截至目前)只设置不同的第一行样式。

          p:first-line{ background-color:coral;}

          如果你想要类似 :nth-line() 的东西,你必须用 javascript 来实现。

          这是一个例子:http://codepen.io/jnowland/pen/AifjK/

          var nthLine = function () {
              var content = $('h1').text();
              var words = content.split(" ");
              var cache = words[0]; //because we miss the first word as we need to detect the height.
              var lines = [];
              $("header").append('<h1 id="sample">' + words[0] + '</h1>');
              var size = $('#sample').height();
              var newSize = size;
              for (var i = 1; i < words.length; i++) {
                  var sampleText = $('#sample').html();
                  cache = cache + ' ' + words[i];
                  marker = [i];
                  $('#sample').html(sampleText + ' ' + words[i]);
                  var newSize = $('#sample').height();
                  if (size !== newSize) {
                      cache = cache.substring(0, cache.length - (words[i].length + 1));
                      lines.push(cache);
                      cache = words[i];
                      size = $('#sample').height();
                  }
              }
              lines.push(cache);
              cache = ''
              for (var i = 0; i < lines.length; i++) {
                  cache = cache + ' <span class="line-' + [i] + '">' + lines[i] + '</span>'
              }
              $('#sample').remove();
              cache = cache.substring(1);
              //prints the result.
              $('h1').html(cache);
          };
          nthLine();
          $(window).resize(nthLine);
          

          【讨论】:

          • 正如您在问题中看到的那样。我想使用“仅 css”并且只有 1 个元素。
          • @alexey 是的,我知道,这就是我这样回答的原因。解决问题的唯一方法是将文本拆分为多个元素。如果您想要多种颜色,则使用“线性渐变”解决方案将更加复杂,然后您将设置样式,就好像所有行都是块一样。 (没有文字的地方会有背景颜色)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-31
          • 1970-01-01
          • 2011-12-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多