Do center alignment in HTML and CSS

Description

The following code shows how to do center alignment.

Example


<html>
<head>
<style type="text/css" title="text/css">
div {<!--   w  w  w  . j  a  va2  s  . c  o  m-->
position: relative;
background: black;
}

#cm {
position: absolute;
}

* .centeredMargin {
width: 200px;
left: 0;
margin-left: auto;
right: 0;
margin-right: auto;
background: red;
}

* .verticalMargin {
height: 40px;
top: 0;
margin-top: auto;
bottom: 0;
margin-bottom: auto;
background: yellow;
}
</style>
</head>
<body>

<div>
<p id="cm" class="centeredMargin verticalMargin">Horizontally &amp; Vertically Centered</p>
</div>

</body>
</html>

Click to view the demo

The code above generates the following result.

Do center alignment in HTML and CSS
Home »
  HTML CSS Tutorial »
    Layout »
      Position
...
Top Align Static Block in HTML and CSS
Top offset Static Block in HTML and CSS
Align Center for static block in HTML and C...
Align Left and top for Static Block in HTML...
Align Right for static block in HTML and CS...
Bottom Align Static Block with margin-botto...
Center Static Block in HTML and CSS
Left Align Sized Static Block with margin-r...
Right align Sized Static Block in HTML and ...
Stretch Static Block right alignment in HTM...
Place Sized Relative layout block in HTML a...
Position block to the bottom right of the r...
Use negative value in left for relative pos...
Use position relative with top and left val...
Use relative position with top and left in ...
Use relative position with top and left set...
Use relative position with top and left: va...
Clear after floating element in HTML and CS...
Clear right for above float right element i...
Compare clear both with not clear elements ...
Make a new line br to clear right in HTML a...
Make element clear from the both side in HT...
Make element overlap with clear left for fl...
Do center alignment in HTML and CSS
Do Left-top Alignment in HTML and CSS
Do Right-bottom Alignment in HTML and CSS
Four column layout in HTML and CSS
Get implied width and height when all four ...
Make container grow height as child div gro...
Make div grow as children grow in HTML and ...
One column layout with header and footer in...
One column three parts layout:header, conte...
Position Image in relation to containing el...
Right-top Align and Offset in HTML and CSS
Set display to inline table for div element...
Set top to 100px in HTML and CSS
Set up four column layout in HTML and CSS
Set width for floating left and right eleme...
...