Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gokulkrishh/6e09757bbaa5be2ff184 to your computer and use it in GitHub Desktop.
Save gokulkrishh/6e09757bbaa5be2ff184 to your computer and use it in GitHub Desktop.
Align an element horizontally & vertically center in css
<div class="container horizontal">
<div>Text in aligned horizontally</div>
</div>
<div class="container vertical-horizontal">
<div>Text in aligned horizontal & vertically</div>
</div>
.container {
width: 300px;
height: 200px;
border: 2px solid;
margin: 20px auto;
}
.horizontal div {
text-align: center;
margin: 0 auto;
border: 2px solid red;
}
.vertical-horizontal {
position: relative;
}
.vertical-horizontal div {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
text-align: center;
width: 90%;
height: 10%;
border: 2px solid red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment