I was practising with the Bootstrap 3 css. I planed on having a fixed footer and a fixed navigation bar on my webpage. So to do this I used margins of 5% to make the content in the middle of my page to not be covered by the footer or header. To format the text I am using the container
class which comes with bootstrap. This can be seen in the picture below.
This looked as it should. However I soon discovered that when the width of the page is expanded it increases the margin size. Like in the picture shown below.
So is there a way to limit how much the margins can extend on the container
class in bootstrap. For example something similar too
.addThisClassToTagWithClassContainer {
max-margin-top:5%;
max-margin-bottom:5%;
max-margin-left:5%;
max-margin-right:5%;
}
There is a copy of the files here if you believe this is a coding error that I have made.
2
Answers
The container class is intentionally not 100% width. It is different fixed widths depending on the width of the viewport.
If you want to work with the full width of the screen, use
.container-fluid
:This isn’t exactly a bootstrap issue – when you set
padding
andmargin
as a % value, like in your example, the % is calculated from the width of the containing element. If you want to set a fixed height, you can usepx
, or if you want it to be a consistent size relative to the height of the viewport instead, you can usevh
(5vh is equal to 5/100 — or 5% — of the viewport height).Typically, this would be a case for
px
simply because on a small screen,5vh
could be very small, and usually a navbar, for instance, would stay a pretty consistent size regardless of how tall the window is.So something like this is probably your most likely approach:
But if you do actually want to have it be relative to the height of the window, you can do this (You will probably need a higher number than 5, here to make similar to your screenshot.):
You should find either approach will prevent your margins from changing when the width of the page changes.