Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css position fixed on header hides content

I am making a new website, and i have a problem with the header... I set the header's position to fixed, and that works but the content below the header is hidden. I tried to move the content down with margin-top: 10px, but all it did was move the header down.

Link to jsfiddle:

http://jsfiddle.net/vwzhda41/

like image 728
Jojo01 Avatar asked Oct 30 '15 15:10

Jojo01


People also ask

How do you fix a header position in CSS?

Answer: Use CSS fixed positioning You can easily create sticky or fixed header and footer using the CSS fixed positioning. Simply apply the CSS position property with the value fixed in combination with the top and bottom property to place the element on the top or bottom of the viewport accordingly.

How do I fix header position?

To fix the position of the header in the webpage, use position: fixed, and to fix it at top use to top:0. The fixed-top can overlay other elements. So to avoid it add margin-top to the other contents.

How do I keep my position fixed in HTML?

To create a fixed top menu, use position:fixed and top:0 . Note that the fixed menu will overlay your other content. To fix this, add a margin-top (to the content) that is equal or larger than the height of your menu.


3 Answers

Give padding-top:58px; to the .responsiveContainer and add top:0; to the .header.

Jsfiddle

.responsiveContainer {
    width: 100%;
    // Add padding top
    padding-top: 58px;
}

.header {
    background-color: #000000;
    padding: 10px;
    padding-left: 0;
    padding-right: 0;
    box-shadow: 0 5px 0 #232323;
    text-align: center;
    width: 100%;
    position: fixed;
    //   Add top 0
    top: 0;
}

According to MDN:

fixed

Do not leave space for the element. Instead, position it at a specified position relative to the screen's viewport and don't move it when scrolled. When printing, position it at that fixed position on every page. This value always create a new stacking context.

like image 169
Alex Avatar answered Oct 13 '22 20:10

Alex


Try to use

padding-top: 58px;/*the height of the header*/` 

instead of

margin-top:10px;
like image 32
Aminesrine Avatar answered Oct 13 '22 20:10

Aminesrine


You need put the <div class="header"> inside of a div with defined height, like:

<div class="heightTest">
<div class="header">
    <div class="navbar">
        <ul>
          ...
        </ul>
    </div>
</div>
</div>

and css:

.heightTest{height:90px;}

Jsfiddle: http://jsfiddle.net/vwzhda41/2/

like image 35
Eduardo Shoiti Fujiwara Avatar answered Oct 13 '22 20:10

Eduardo Shoiti Fujiwara