Recently, a client asked that his site be percent-based rather than pixel-based. The percent was to be set to 80%. As you guys know, it is very easy to center the container if it is pixel-based but how do you center a percent-based main container?
#container { width:80%; margin:0px auto; }
That does not center the container :(
You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.
To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.
Easiest way to center something regardless of page width is margin: auto; in your CSS, with height and width defined. Save this answer.
You can set float div center by using margin property. you does not need to use float property but you can use margin property and set left or right margin value auto by this way.
The margin property supports percentage values:
margin-left: 10%; margin-right: 10%;
#container { width:80%; position:absolute; margin-left:-40%; left:50%; }
or simply
#container { width:80%; margin-left:10%; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With