Hide scrollable content behind transparent fixed position divs when scrolling the page?

后端 未结 10 1011
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 09:16

I have a div called header that is set up with a fixed position. The problem is when I scroll the page the content of the page shows up behind the header (the header is tran

相关标签:
10条回答
  • 2020-12-01 09:55

    I was having a similar issue, and found a solution for my case. It should apply whether you are using a full screen background image, or a solid color (including white).

    HTML

    <div id="full-size-background"></div>
     <div id="header">
      <p>Some text that should be fixed to the top</p>
    </div>
    <div id="body-text">
      <p>Some text that should be scrollable</p>
    </div>
    

    CSS

    #full-size-background {
    z-index:-1;
    background-image:url(image.jpg);
    background-position:fixed;
    position:fixed;
    top:0px;
    left:0px;
    width:100%;
    height:100%;
    }
    #header {
    position:fixed;
    background-image:url(image.jpg);
    height:150px;
    width:100%;
    }
    #body-text {
    margin-top:150px;
    }
    

    This gives me the look of a full page image with a transparent fixed header and when the body content scrolls, it hides behind the header. The images appear seamless.

    You could do the same thing with a solid color background, though, arguably, it would have been easier.

    2 notes: the header has a set height, I have only tested in FF and Chrome.

    0 讨论(0)
  • 2020-12-01 09:55

    I fixed this problem using the background property with a color, you can use var even if you'd like to

    .header{
        width:100%;
        position:fixed;
        z-index:10;
        background:blue;
        /* background: var(--my-var-value);  You can do this if needed*/
    }
    
    0 讨论(0)
  • 2020-12-01 10:02

    The header's z-index is set to 1000, so the z-index of the container would have to be 1001 if you want it to stack on top of the header. https://codepen.io/richiegarcia/pen/OJypzrL

    #header {
        margin:0 auto;
        position: fixed;
        width:100%;
        z-index:1000;
    }
    #topmenu {
        background-color:#0000FF;
        height:24px;
        filter: alpha(opacity=50);
        opacity: 0.5;
    }
    
    #leftlinks {
        padding: 4px;
        padding-left: 10px;
        float: left;
    }
    
    #rightlinks {
        padding: 4px;
        padding-right: 10px;
        float: right;
    }
    
    #containerfixedtop {
        width: 100%;
        height: 20px;
    }
    
    #contentfixedtop {
        margin: 0 auto;
        background-color: #DAA520;
        width: 960px;
        height:20px;
    }
    
    #container {
        position: relative;
        top: 68px;
        width: 100%;
        height: 2000px;
        overflow: auto;
        z-index:1001;
    }
    
    #content {
        margin: 0 auto;
        background-color: #DAA520;
        width: 960px;
        height: 2000px;
    }
    
    0 讨论(0)
  • 2020-12-01 10:05
    1. I have fixed background image
    2. The header background is transparent
    3. I don't want my content to override my transparent header

    I came up with a solution scrolling the div instead the body:

    <div>
        <div class="header"></div>
        <div class="content"></div>
    </div>
    
    
    .header { position: fixed; ... }
    .content { position: fixed; height: calc(100% - HEADER_HEIGHT); overflow: scroll; }
    
    0 讨论(0)
  • 2020-12-01 10:07

    Does #header have a set height?

    #header {position: fixed; height: 100px; }
    #container {position: absolute; top: 100px; bottom: 0; overflow: auto; }
    

    Pretty sure this wouldn't work in IE though...

    0 讨论(0)
  • 2020-12-01 10:09

    You are probably looking for z-index. It allows you to specify the vertical order of elements on the page, so an element with z-index: 10 is floating above (visually) an element with z-index: 5.

    Give the content z-index: 5 and see if it works.

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