I want a full with (of the backgrond) of the my Navbar, this nav is inside a Main tag with margins. But with nav: width: 100% don’t work, only stretches to the right. Heres my code:
in the way you are structuring your page you need position: absolute; on your nav element.
You need to have one thing in mind when adding widths with %, that % is relative to it’s parent. If the parent is 1000px wide, and you put an element (a nav for example) with 100% width your element will be 1000px too because is 100% of it’s parent. In the same way if you set a witdh of 50% your element will be 500px wide (50% of 1000px).
The only way to make en element bigger than it’s parent is by absolute positioning the element, but I suggest you try to avoid unless is necessary, is nothing fun dealing with too many absolute elements .
Instead, you can re-think your html structure, removing the parent margin/padding to avoid the issue and use margins in the other elements, or even better moving your nav outside that container.
Thanks a lot. amazing explanation! Can you respond one more question? If i wanna have margins in every section of the page you suggest to write manually each everyone? i mean, NOT write body { margin 0 1em }
No, just how you did it first is fine, using a container (your main div) with margins, just the nav needs the full width, by placing it outside you wont have the issue, this will do the trick:
body
–Nav (no margin)
–container (margin or padding)
----all your sections (no margin).