Reputation: 813
First of all, my apologies. I know there are various solutions posted for this issue here, but for the life of me I can't get any of them to work.
For a responsive website I'm trying to center an h1 in a div.
Centering horizontally is not an issue, but I'm having problems getting it centered vertically. Presumably I'm doing something wrong or misunderstanding the explanations I found here (or maybe both).
So as I'm probably interpreting earlier solutions given the wrong way, could someone please explain what exactly I have to add to the code beneath to get the h1 to center vertically?
(To make this question relevant to as much people as possible, I've already stripped the code of all my previous attempts to do so myself.)
CSS:
html, body {
height: 100%;
margin: 0;
}
#section1 {
min-height: 90%;
text-align:center
}
HTML:
<div class="section" id="section1">
<h1>Lorem ipsum</h1>
</div>
Upvotes: 67
Views: 161178
Reputation: 1324757
I'm trying to center an h1 in a div.
In 2024, align-content: center;
is enough to vertically center an element inside a div:
#section1 {
height: 160px;
background-color: lightblue;
align-content: center; /* vertical centering*/
}
body {
height: 100%;
margin: 0;
}
#section1 {
height: 160px;
background-color: lightblue;
align-content: center; /* vertical centering*/
}
/* the below is for horizontal centering (irrelevant to the question) */
div {
width: fit-content;
margin: auto;
}
#section2 {
height: 30px;
background-color: red;
}
<div class="section" id="section1">
<h1>Lorem ipsum</h1>
</div>
<div class="section" id="section2">
Other section (section2)
</div>
See also "CSS finally adds vertical centering in 2024" by James Smith:
align-content
works in the default layout in 2024, allowing vertical centering with 1 CSS propertySupported since Chrome: 123 | Firefox: 125 | Safari: 17.4
The status quo for CSS alignment is to switch to flexbox or grid because align-content doesn’t work in the default layout (flow).
In 2024, browsers have implementedalign-content
for flow layout.
So no more table cell, absolute positioning, inline content, single-line or multi-line flexbox, grid content or grid cell, or auto-margin!
Upvotes: 1
Reputation: 727
Flexbox is a solid well-supported way to center an h1 tag inside div.
<div class="section" id="section1">
<h1>Lorem ipsum</h1>
</div>
This is the OP:s HTML. Let's keep it like that. Now working with CSS we can add display flex and some properties. Here is a working code snippet that shows how flexbox can do vertical alignment.
#root {
width: 90vw;
height: 90vh;
border: 2px solid green;
}
#section1 {
display: flex;
flex-direction: row;
flex: 1;
min-height: 90%;
border: 2px solid red;
background-color: orange;
text-align: center;
/* this does align h1 if h1 is wider than its containing text */
}
#section1>h1 {
flex: 1;
/* this makes the h1 take all available width in its containing div, and makes text-align: center property work inside section1 */
background-color: #666333;
align-self: center/* this is finally the property that vertically aligns the h1 title inside its containing div */
}
<!DOCTYPE html>
<html>
<header>Demo</header>
<div id="root">
<div id="section1">
<h1>Title Centered</h1>
</div>
</div>
</html>
Since the accepted answer's CSS3 option vertically aligns the containing div and not the h1 tag as requested, this answer shows how that h1 can be vertically aligned inside a pre-sized, larger containing div.
Upvotes: 1
Reputation: 33
Just use padding top and bottom, it will automatically center the content vertically.
Upvotes: -2
Reputation: 58432
you can achieve vertical aligning with display:table-cell
:
#section1 {
height: 90%;
text-align:center;
display:table;
width:100%;
}
#section1 h1 {display:table-cell; vertical-align:middle}
Update - CSS3
For an alternate way to vertical align, you can use the following css 3 which should be supported in all the latest browsers:
#section1 {
height: 90%;
width:100%;
display:flex;
align-items: center;
justify-content: center;
}
Upvotes: 158
Reputation: 9460
HTML
<div id='sample'>
<span class='vertical'>Test Message</span>
</div>
CSS
#sample
{
height:100px;
width:100%;
background-color:#003366;
display:table;
text-align: center;
}
.vertical
{
color:white;
display:table-cell;
vertical-align:middle;
}
Fiddle : Demo
Upvotes: 1
Reputation: 487
I've had success putting text within span tags and then setting vertical-align: middle on that span. Don't know how cross-browser compliant this is though, I've only tested it in webkit browsers.
Upvotes: 1
Reputation: 9348
You can achieve this with the display
property:
html, body {
height:100%;
margin:0;
padding:0;
}
#section1 {
width:100%; /*full width*/
min-height:90%;
text-align:center;
display:table; /*acts like a table*/
}
h1{
margin:0;
padding:0;
vertical-align:middle; /*middle centred*/
display:table-cell; /*acts like a table cell*/
}
Demo: http://jsfiddle.net/a3Kns/
Upvotes: 5