【问题标题】:Create a "complex" background in CSS在 CSS 中创建“复杂”背景
【发布时间】:2019-12-08 23:21:32
【问题描述】:

我想知道是否可以在 CSS3 中创建这样的背景。 背景应跨越标题 div,渐变应从白色变为黑色,与屏幕宽度无关(始终为左侧白色,右侧为黑色)。

不使用图像的原因是加载时间较长,并且当浏览器小于 1920 像素(图像宽度)时,我无法调整其宽度。

已经尝试过线性渐变,但我无法让它工作......

问候, 延斯

【问题讨论】:

  • #div { 背景颜色:黑色;背景图像:线性渐变(向右,白色,黑色);这只是创建渐变。不知道怎么降低只有渐变条的高度
  • 你没有 HTML 代码?
  • 当然有,但这有关系吗?我想将此应用于任何 div。作为背景。
  • 试试 ColorZilla 编辑器,你可以创建一些非常酷的背景 - colorzilla.com/gradient-editor

标签: html css


【解决方案1】:

如果您还想要顶部的黑色条,您应该为背景指定尺寸,停止重复并将其放置在您想要的位置(将其视为普通背景图像

div { 
  background-color: black; 
  background-image: linear-gradient(to right, white, black); 
  background-repeat:no-repeat;
  background-size:100% 20px; /*full width, 20px height*/
  background-position:0 100%; /*gradient at bottom*/
  
  /*just to give size to demo*/
  min-height:50px;
}
<div></div>

【讨论】:

  • 太棒了!非常感谢
【解决方案2】:

这里有一些 CSS 给你:

#grad {
  background: gray; /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(left, white , black); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(right, white, black); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(right, white, black); /* For Firefox 3.6 to 15 */
  background: linear-gradient(to right, white , black); /* Standard syntax */
} 

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

【讨论】:

    【解决方案3】:

    我知道 OP 的问题已得到解答。但无论如何我都会在这里发表评论,以提供更多信息来创建一个更“复杂”的背景。

    为什么是background-image? CSS 中一个基本(也是重要的)背景理论是:一个元素的背景只能有 1 个background-color,并且多个background-images 位于其上 (即使在背景图像之后声明了背景颜色,background-color 仍将放置在背景图像下方),您可以调整这些背景图像的大小、重新定位。重要的是 linear-gradient 算作 background-image不是 background-color。上面的 2 个链接确实提供了有关它的所有详细信息。

    这是一个关于 OP 问题的“更复杂”背景的快速演示,仅使用 1 个div HTML:

    div { 
      background: 
        linear-gradient(to right, white, black) 0 100%/100% 20px no-repeat,
        linear-gradient(to left, white, black) 0 0/100% 20px no-repeat,
        black;
      height: 100px;
    }
    <div></div>


    我写这篇长评论的灵感来自一个教程 https://levelup.gitconnected.com/how-to-implement-netflix-slider-with-react-and-hooks-bdb9b99d1ce4,其中有一部分是 HTML 和 CSS 中的冗长 hack,以实现我能够在一行 CSS background 中做的事情,我认为分享很酷,isn不是吗

    /* simpler */
    
    .box {
      width: 100%;
      height: 100px;
      background: linear-gradient(to right,black 0%,black 30%,transparent 75%,transparent 100%), green;
    }
    
    /* more complex */
    
    .content {
      height: 100px;
      position: relative;
    }
    
    .background {
      display: flex;
      height: 100%;
    }
    
    .left {
      background: black;
      width: 30%;
      position: relative;
    }
    
    .left:before {
      content: '';
      position: absolute;
      background-image: linear-gradient(to right,#000,transparent);
      top: 0;
      bottom: 0;
      left: 100%;
      width: 275px;
    }
    
    .right {
      background: green;
      width: 70%;
    }
    
    .content-container {
      color: white;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      padding: 30px
    }
    <!-- simpler -->
    
    <div class="box">
      <div class="content-container">content here...</div>
    </div>
    
    <hr>
    
    <!-- more complex -->
    <div class="content">
      <div class="background">
        <div class="left">left</div>
        <div class="right">right</div>
      </div>
      <div class="content-container">content here...</div>
    </div>

    【讨论】:

      猜你喜欢
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 2018-06-05
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多