Computer >> Computer tutorials >  >> Programming >> CSS

How do I center <div> tag using CSS?


Example

<html>
<head>
   <title>div in center without css</title>
</head>
<body>
<div>
   THIS IS ONE PARAGRAPH. HERE WE ARE NOT USE ANY ALIGNMENT.
</div>
<div align="center" style="border: 2px solid blue">
   THIS PARAGRAPH IS IN CENTER
</div>
</body>
</html>

Example

<html>
<head>
<title>div in center with css</title>
   <link href="../../Content/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
   THIS IS ONE PARAGRAPH. HERE WE ARE NOT USE ANY ALIGNMENT.
</div>
<div class="mydiv">
   THIS PARAGRAPH IS IN CENTER
</div>
</body>
</html>

Style.css

body {
   background-color:Gray;
}

.mydiv {
   padding:10px;
   margin:auto;
   width:30%;
   border:4px solid black;
}

If you want to text is also center in the box.then write text-align:center in .mydiv class within CSS file.