I'm currently working on a webpage, and one of the challenges I'm facing is centered horizontally a specific div on the screen. Despite my efforts, I haven't been able to achieve this as expected. Here's the HTML and CSS code I'm currently using:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="estilos.css">
</head>
<body>
<div class="mi-div">
<p>Contenido del div</p>
</div>
</body>
</html>
/* estilos.css */
.mi-div {
width: 300px;
/* this is the width*/
height: 200px;
/* this is the height*/
background-color: lightgray;
/* I assign is a background*/
/* Here's where I've attempted to center this div horizontally, but it hasn't worked so far. */
/* How can I center this div horizontally? */
}
I've explored various methods, including using
/* this is css code*/
margin: 0 auto;
and
/* this is css code*/
text-align: center;
` but none of them seem to be achieving the desired centering effect.
Could someone kindly provide a step-by-step guide or an alternative approach to achieve horizontal centering for this div? I would greatly appreciate any help or insights.
`
3
Answers
To center the div you can check below code:
I have used body to make center, instead you can warp into a another parent div and apply the same css.
Currently CSS layout is normally configured by flexbox (one dimension, horizontal or vertical) or grid (two dimensions). You need to modify the CSS like