Centering a div with CSS

Centering a div with auto margin

Adipisicing laudantium tenetur sunt maiores architecto

Lorem consectetur adipisicing laudantium tenetur accusamus cumque dolore pariatur.

bulbs, example image for code explanation

Consectetur adipisicing laudantium maiores architecto fuga quo corrupti quaerat exercitationem cupiditate obcaecati tempora accusamus pariatur.

div.center {
	width: 70%;
	margin: 0 auto;
}

To center a div (with margin), you need to assign an “Auto” margin both from the left and right sides.

Since the <div> is a block-level element in HTML, you don’t need to write display: block; in your CSS again. Because it’s already a block-level element by born.

In this example- I give it (div) a 70% width for better visualization of centering the div. But you don’t need to give it a width value to center align a div.

Centering a div vertically and horizontally

If you prefer to watch instead of reading, you can check this video. Otherwise, keep reading.

Adipisicing laudantium tenetur architecto

Lorem consectetur adipisicing laudantium tenetur.

bulbs, example image for code explanation

Consectetur adipisicing laudantium maiores architecto tempora accusamus pariatur.

div.wrapper {
	background: #4285F4;
	min-height: 700px;
	display: flex;
	justify-content: center; /*aligns the content(child) horizontally (x-axis) center*/
	align-items: center; /*aligns the content (child) vertically (y-axis) center*/
}

div.center {
	background: #EA4335;
	max-width: 700px;
	padding: 20px;    
}
<div class="wrapper">
	<div class="center">
		<h3>Adipisicing laudantium tenetur architecto</h3>
		<p>Lorem consectetur adipisicing laudantium tenetur.</p>
		<img class="nolazy" src="image.jpg">
		<p>Consectetur adipisicing laudantium maiores architecto.</p>
	</div>
</div>  

Centering a div without flexbox (vertically & horizontally)

Adipisicing laudantium tenetur architecto

Lorem consectetur adipisicing laudantium tenetur.

bulbs, example image for code explanation

Consectetur adipisicing laudantium maiores architecto tempora accusamus pariatur.

div.wrapper {
 background: #FBBC05;
 min-height: 700px;
 position: relative;
}

div.center {
 background: #34A853;
 width: 700px;
 padding: 20px;
 position: absolute;
 top: 50%; /*pushing the content by 50% from top*/
 left: 50%; /*pushing the content by 50% from left*/
 transform: translate(-50%, -50%);
/*pushing BACK the content by 50% to the left and top
of its own width and height accordingly*/
}
<div class="wrapper">
	<div class="center">
		<h3>Adipisicing laudantium tenetur architecto</h3>
		<p>Lorem consectetur adipisicing laudantium tenetur.</p>
		<img class="nolazy" src="image.jpg">
		<p>Consectetur adipisicing laudantium maiores architecto.</p>
	</div>
</div>  

1 common issue fixed for centering a div with CSS absolute positioning

In the above example, I used width: 700px; for the child <div> (.center).

But if you use a relative length instead of an absolute length, the <div> will become smaller based on the top & left distance. Let me clarify more.

If you use max-width instead of width, then your <div> will become smaller by the absolute positioning and depending on the top & left distance.

Because the <div> is a block-level element in HTML. So it will not only start with a new line but also shrink horizontally to display its whole content (just like a paragraph behaves).

If you need to use the max-width anyhow, then there is a fix for this issue. After you assign the max-width, also you have to write anyone of the following CSS properties:

width: fit-content; or width: max-content;

div.center {
max-width: 700px;
width: fit-content; /*this property will solve the issue*/
/*or*/
/*width: max-content;*/

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

Learn about centering more HTML elements