How can I vertically center a div element for all browsers using CSS?
Below is the best all-around solution I could build to vertically and horizontally center a fixed-width, flexible height content box. It was tested and worked for recent versions of Firefox, Opera, Chrome, and Safari.
.outer {
display: table;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
width: 400px;
/* Whatever width you want */
}
<div class="outer">
<div class="middle">
<div class="inner">
<h1>The Content</h1>
<p>Once upon a midnight dreary...</p>
</div>
</div>
</div>
How can I vertically center a div element for all browsers using CSS?
Below is the best all-around solution I could build to vertically and horizontally center a fixed-width, flexible height content box. It was tested and worked for recent versions of Firefox, Opera, Chrome, and Safari.
.outer {
display: table;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
width: 400px;
/* Whatever width you want */
}
<div class="outer">
<div class="middle">
<div class="inner">
<h1>The Content</h1>
<p>Once upon a midnight dreary...</p>
</div>
</div>
</div>
Vertically center a div - Browser Compatibility
Here are two other methods:
1. Using pseudo-element on the container
#container:before {
content:'';
display:inline-block;
vertical-align:middle;
height:100%;
}
#content {
display:inline-block;
vertical-align:middle;
}
This method should be compatible with most browsers except IE8 and below: http://caniuse.com/#search=%3Abefore
jsFiddle demo
Note that in this case, the element being vertically centered needs to have display:inline-block
2. Using display:table-cell;
#container {
display:table-cell;
vertical-align:middle;
}
This method should be compatible with 99.98% of the browsers: http://caniuse.com/#search=css%20table
jsFiddle demo
How can I vertically center a div element for all browsers using CSS?
Below is the best all-around solution I could build to vertically and horizontally center a fixed-width, flexible height content box. It was tested and worked for recent versions of Firefox, Opera, Chrome, and Safari.
.outer {
display: table;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
width: 400px;
/* Whatever width you want */
}
<div class="outer">
<div class="middle">
<div class="inner">
<h1>The Content</h1>
<p>Once upon a midnight dreary...</p>
</div>
</div>
</div>
How can I vertically align elements in a div?
Wow, this problem is popular. It's based on a misunderstanding in the vertical-align
property. This excellent article explains it:
Understanding vertical-align
, or "How (Not) To Vertically Center Content" by Gavin Kistner.
“How to center in CSS” is a great web tool which helps to find the necessary CSS centering attributes for different situations.
In a nutshell (and to prevent link rot):
- Inline elements (and only inline elements) can be vertically aligned in their context via
vertical-align: middle
. However, the “context” isn’t the whole parent container height, it’s the height of the text line they’re in. jsfiddle example - For block elements, vertical alignment is harder and strongly depends on the specific situation:
- If the inner element can have a fixed height, you can make its position
absolute
and specify itsheight
,margin-top
andtop
position. jsfiddle example - If the centered element consists of a single line and its parent height is fixed you can simply set the container’s
line-height
to fill its height. This method is quite versatile in my experience. jsfiddle example - … there are more such special cases.
- If the inner element can have a fixed height, you can make its position
How to vertically center text when using the :before modifier in CSS?
You can use flexbox.
.status-yellow {
display: inline-block;
padding: .35em .65em;
font-size: .75em;
font-weight: 400;
line-height: 1;
color: #000;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
text-transform: uppercase;
background-color: #fff;
display:flex;
align-items:center;/*for vertical align*/
justify-content:center;/*for horizontal align*/
}
.status-yellow:before {
display: inline-block;
content:"\2022";
margin-right: 0.5rem;
color: yellow;
font-size: 48px;
}
How to vertically center a div on page?
Method 1 (position, transform-translate):
* { padding: 0; margin: 0;}.parent { position: relative; left: 0; top: 0; width: 100vw; height: 100vh; background-color: gray;}.child { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); width: 10em; height: 5em; background-color: white; box-shadow: 1px 1px 10px rgba(0,0,0,.4);}
<div class="parent"> <div class="child"></div></div>
How to center an element horizontally and vertically
Approach 1 -
transform
translateX
/translateY
:Example Here / Full Screen Example
In supported browsers (most of them), you can use
top: 50%
/left: 50%
in combination withtranslateX(-50%) translateY(-50%)
to dynamically vertically/horizontally center the element.
.container { position: absolute; top: 50%; left: 50%; -moz-transform: translateX(-50%) translateY(-50%); -webkit-transform: translateX(-50%) translateY(-50%); transform: translateX(-50%) translateY(-50%);}
<div class="container"> <span>I'm vertically/horizontally centered!</span></div>
Related Topics
How to Display Multiple Lines of Text in Svg
How to Use Two Ngfor in HTML Angular 4
How to Add HTML Line Break Within an Input Text Placeholder
Having Difficulty Understanding How to Remove Some Padding
How to Remove the Default Arrow Icon from a Dropdown List (Select Element)
How to Make Flex Box Work in Safari
How to Give Multiple Conditions in *Ngif Statement in Angular 6
Line Break in HTML Select Option
How to Vertically Align Text in Input Type="Text"
Html - How to Get My Text Next to My Icon
Css Height 100% Is Not Always Equal to the Direct Parent
How to Make HTML Table Cell Editable
Can't Scroll to Top of Flex Item That Is Overflowing Container