Fixed gradient background with css

前端 未结 4 1598
盖世英雄少女心
盖世英雄少女心 2020-12-24 05:49

I would like for my page to have a gradient background flowing from top to bottom. I want the background to act like a fixed image in that the gradient stretches from the to

相关标签:
4条回答
  • 2020-12-24 06:18

    If you wish to do this using CSS3 gradients, try using the background-attachment property

    For example, if you are applying your gradients to #background, then add this after the CSS gradient.

    background-attachment: fixed;

    Note: You must add background-attachment after the background properties.

    Your entire code might look like this:

    #background {
      background: #1e5799;
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1e5799), color-stop(100%,#7db9e8));
      background: -webkit-linear-gradient(top,  #1e5799 0%, #7db9e8 100%);
      background:    -moz-linear-gradient(top,  #1e5799 0%, #7db9e8 100%);
      background:     -ms-linear-gradient(top,  #1e5799 0%, #7db9e8 100%);
      background:      -o-linear-gradient(top,  #1e5799 0%, #7db9e8 100%);
      background:         linear-gradient(to bottom, #1e5799 0%, #7db9e8 100%);
      filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 );
      background-attachment: fixed;
    }
    
    0 讨论(0)
  • 2020-12-24 06:20
    html {
      height: 100%;
      /* fallback */
      background-color: #1a82f7;
      background: url(images/linear_bg_2.png);
      background-repeat: repeat-x;
    
      /* Safari 4-5, Chrome 1-9 */
      background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#1a82f7), to(#2F2727));
    
      /* Safari 5.1, Chrome 10+ */
      background: -webkit-linear-gradient(top, #2F2727, #1a82f7);
    
      /* Firefox 3.6+ */
      background: -moz-linear-gradient(top, #2F2727, #1a82f7);
    
      /* IE 10 */
      background: -ms-linear-gradient(top, #2F2727, #1a82f7);
    
      /* Opera 11.10+ */
      background: -o-linear-gradient(top, #2F2727, #1a82f7);
    }
    

    http://css-tricks.com/examples/CSS3Gradient/
    http://css-tricks.com/css3-gradients/

    Depending on what browsers you support, you may or may not want an image fallback. If not, you might want to include the filter and -ms-filter syntax instead to allow for IE 6-8. Even without this or an image it will fallback to the background-color

    0 讨论(0)
  • 2020-12-24 06:27

    Another way of doing this (with actual image):

    body {
      background-attachment: local; // or 'fixed' here
      background-image: url(fancy.jpg);
      background-size: 100% 100%;
      overflow:auto;
      box-sizing:border-box;
      width:100%;
      height:100%;
      margin:0; 
    }
    
    0 讨论(0)
  • 2020-12-24 06:31

    I guess using the ::before pseudo-element can be an option as well, thanks for the background-attachment @ScottA

    0 讨论(0)
提交回复
热议问题