Why is my margin not working with position: fixed?

JSFiddle Demo

I have a div for the title and a div for wrapping the content.

For some reason, I cannot have a field at the bottom of the header, which forces the content wrapper to press down. He just ignores him completely, and I have no idea why.

Does anyone know what is happening with this? He does not do this when I take the position: fixed; the attribute is in the header, but I want it to be fixed at the top, so when the scroll of the header is always displayed.

Hoping someone can explain why this is happening, or at least what it is called, so I can do it.

body { margin: 0; padding: 0; font-family: arial; background: #5A9910; text-align: center; } /* ==========================Main wrap for page==*/ div.wrap { width: 100%; margin: 0 auto; text-align: left; } /* ==========================Header with logo==*/ div.header { position: fixed; width: 100%; background: #ffffff; -webkit-box-shadow: 0 8px 6px -6px #333; -moz-box-shadow: 0 8px 6px -6px #333; box-shadow: 0 8px 6px -6px #333; } /* ==========================Header logo image==*/ img.headerlogo { width: 30%; } /* ==========================Content wrapper==*/ div.contentwrap { width: 80%; height: 1600px; background: #ccc; margin: 0 auto; } 
 <div class="wrap"> <div class="header"> <p>Header</p> </div> <div class="contentwrap"> <p>Test</p> </div> </div> 
+7
source share
5 answers

I think you need to explicitly declare the position of a fixed div.

 div.header { position: fixed; width: 100%; background: #ffffff; top:20px; -webkit-box-shadow: 0 8px 6px -6px #333; -moz-box-shadow: 0 8px 6px -6px #333; box-shadow: 0 8px 6px -6px #333; } 

and assign a marker in the content div

 div.contentwrap { width: 80%; height: 1600px; background: #ccc; margin: 80px auto; } 

check this script if it works as you need: https://jsfiddle.net/0cmvg92m/3/

+5
source

When you set an element to position: fixed; , you remove it from the "normal document flow". Imagine your site is a river, and you look from top to bottom from top to bottom. Each element is a rock in this river. Any margin that you apply to your elements is like a force field around one of these rocks.

When you set position: fixed; on one of these stones, you essentially pull it out of the river so that the stone, in fact, floats above the river in the air. Scala and the river water flow will still look the same for you, because you are up, looking straight down, but the stone no longer interacts with the river flow.

If you applied margin to this stone, this force field around the rock no longer holds water from it, because the stone hovers in the air, thanks to the position: fixed; property position: fixed; . Thus, there is no water or other stones (other elements) from which you must distance yourself. Your rock strength field (the field of your element) presses on the thin air, so nothing in the river will be affected.

But a picture is worth a thousand words, as they say:

Kicking the tower of pisa

This man doesn't really kick the Leaning Tower of Pisa, but it looks like that! In this example, the background of the image, including the Leaning Tower of Pisa, represents your page (or your "normal document flow"), and the person is an element (or stone from our last example) using position: fixed; .

Read more about the position property here and, more relevant,.

One way to fix this is to set the title to top: 20px; z-index: 2; top: 20px; z-index: 2; to make sure that it is located above and above each other element on the z plane, and then give your body position: relative; and margin-top equal to the height of the header (in this case, 52px ) plus the value of the header top property. To increase the space between your headline and your body, simply increase the amount of margin-top . This is sometimes called a “sticky approach”. Here is a demo:

 body { margin: 0; padding: 0; font-family: arial; background: #5A9910; text-align: center; } /* ==========================Main wrap for page==*/ div.wrap { width: 100%; margin: 0 auto; text-align: left; } /* ==========================Header with logo==*/ div.header { position: fixed; top: 20px; z-index: 2; width: 100%; background: #ffffff; -webkit-box-shadow: 0 8px 6px -6px #333; -moz-box-shadow: 0 8px 6px -6px #333; box-shadow: 0 8px 6px -6px #333; } /* ==========================Header logo image==*/ img.headerlogo { width: 30%; } /* ==========================Content wrapper==*/ div.contentwrap { width: 80%; height: 1600px; background: #ccc; margin: 0 auto; position: relative; margin-top: 72px; } 
 <div class="wrap"> <div class="header"> <p>Header</p> </div> <div class="contentwrap"> <p>Test</p> </div> </div> 

PS position: fixed; - CSS property (more precisely, a pair of property values), and not an HTML attribute.

+27
source

Margin does not work because the position of the header is fixed.

You should use padding-top on your contentwrap.

0
source

Your title has a position:fixed property. Therefore, the margin you apply to the title does not affect the content section.

To solve this problem, you need to specify either margin or an addition to the contentwrap element

0
source

The css that worked with me is

 #toaster-container { width: 99%; height: 98%; margin: 15px; position: fixed; top: 0; } 

but I'm not sure how this works, and I don’t know if it will work with scaling

one more thing above css make the width as full screen (100%). perhaps this will lead to a violation of some properties from above, from below, to the right and to the left

0
source

Source: https://habr.com/ru/post/1233729/


All Articles