CSS Introduction

CSS Tutorial What is CSS CSS Syntax CSS Selector How to include CSS CSS Comments

CSS Attributes

CSS Background CSS Border CSS Display CSS Float CSS Font CSS Color CSS Hover CSS Important CSS Line-height CSS Margin CSS Opacity CSS Filter CSS Images CSS Overflow CSS Padding CSS Position CSS Vertical align CSS White space CSS Width Word-wrap in CSS Box-shadow in CSS Text-transform in CSS CSS Outline CSS Visibility CSS Counters CSS Clear fix CSS Icons CSS Justify-content Text-decoration in CSS CSS Lists CSS nth selector CSS Sticky CSS Background-clip CSS Checkbox-style CSS Letter-spacing CSS Navigation bar CSS Overlay CSS Root CSS Specificity CSS Text-indent CSS Text-stroke CSS Zoom CSS Order CSS Descendent selector CSS Clip CSS calc() CSS Background-blend-mode CSS radio-button CSS Superscript and subscript CSS Text-effects CSS Text-align CSS Variables CSS Page-break-before CSS Page-break-inside CSS Page-break-after CSS Content property CSS Word-spacing CSS Animation CSS @keyframes rules CSS Pseudo-classes CSS Pseudo-elements CSS Radial-gradient CSS Translate CSS Gradients CSS z-index CSS Loaders CSS Units CSS Transition CSS Masking CSS Arrow CSS Pagination

Misc

What is Bootstrap CSS What is CSS used for Difference between HTML and CSS How to center a table in CSS What is a CSS File How to center a button in CSS How to change background color in CSS How to change the font in CSS How to change font size in CSS How to resize an image in CSS How to get rid of bullet pioints in CSS Is CSS a programming language Grid Vs Flexbox in CSS How to edit CSS in WordPress How to use google fonts in CSS

CSS Align

CSS Center Align Elements

CSS center align is used to create align by using block element. You should specify margin: auto;properties. The element will take up the specified width and the remaining space. It will split equally between the two margins: Let us see an example of CSS align:
<!DOCTYPE html>  
<html>  
<head>  
<style>  
.center {  
    margin: auto;  
    width: 60%;  
    border: 3px solid blue;  
    padding: 10px;  
}  
</style>  
</head>  
<body>  
<h2>Center Align Elements</h2>  
<div class="center">  
  <p><b>Note: </b> margin:auto will not work in IE8, unless a !DOCTYPE is declared.</p>  
</div>  
</body>  
</html>
Try Now

Center Align Text

Center aligns text CSS property is used to create text in center. You should use text-align: center;element. Example
.center {  
    text-align: center;  
    border: 1px solid green;  
}

Center Align Image

Center aligns image CSS property is used to create text center. You should use margin:auto; element. Example
img {  
    display: block;  
    margin: 0 auto;  
}

Left and Right Align by using position

There is one method for aligning elements. You can use position: absolute; Let us see an example of CSS left and right align by using position:
<!DOCTYPE html>  
<html>  
<head>  
<style>  
.right {  
    position: absolute;  
    right: 0px;  
    width: 300px;  
    border: 3px solid #73AD21;  
    padding: 10px;  
}  
</style>  
</head>  
<body>  
<h2>Right Align</h2>  
<div class="right">  
  <p>Hello friends we are learning. Hello friends we are learning.</p>  
</div>  
</body>  
</html>
Try Now

Center vertically using padding

There are various way to center an element vertically in CSS. A simple solution is to use top and bottom padding. Example
.center {  
    padding: 70px 0;  
    border: 3px solid green;  
}



ADVERTISEMENT
ADVERTISEMENT