Using width, max-width and margin: auto;
As mentioned in the previous chapter; a block-level element always takes up the full width available (stretches out to the left and right as far as it can).
Setting the width
of a block-level element will prevent it from stretching out to the edges of its container. Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins:
This <div> element has a width of 500px, and margin set to auto.
Note: The problem with the <div>
above occurs when the browser window is smaller than the width of the element. The browser then adds a horizontal scrollbar to the page. Continue reading CSS Layout – Width