Last active
December 23, 2015 13:49
-
-
Save rcotrina94/6644780 to your computer and use it in GitHub Desktop.
How to vertically center HTML objects whitin a container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#How to vertically center HTML objects. | |
Only with pure HTML and CSS | |
Requirements: | |
- The height of the element's container must be known or specified. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="vcenter-container"> | |
<div class="vcenter-item">Any kind of content</div> | |
<div class="vcenter-item">Any kind of content</div> | |
<div class="vcenter-item">Any kind of content</div> | |
<!-- here comes the helper --> | |
<div class="vcenter-helper"></div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.vcenter-helper{ | |
display: inline-block; | |
vertical-align: middle; | |
height:100%; | |
width:1px; | |
} | |
.vcenter-container{ | |
display:block; | |
height:300px; | |
/* This value is an example, if the div that has to contain the | |
elements to be vertically centered, shouldn't be displayed as a | |
block element, you can use .vcenter-container within this | |
element, and just set 100% height to vcenter-container */ | |
width:100%; | |
} | |
.vcenter-item{ | |
display: inline-block; | |
vertical-align: middle; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment