Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center vertically a unknown height text in a unknown height div

Tags:

html

css

center

I know this is a common issue, and I've already asked a similar one. But I could not find the solution this time. I am trying to vertically center a text which can have different height in a DIV which can have different height. And I am trying to solve this only with CSS, without touching the HTML.

Example : http://jsfiddle.net/F8TtE/

 <div id="test">
    <div id="sumup">

        <h1 class="titre">Title</h1>
        <div id="date">hello guys</div>
    </div>
 </div>

My goal is to have the text centered vertically and horizontally whatever its size.

like image 618
Damien Avatar asked Nov 05 '13 14:11

Damien


People also ask

How do I vertically center a div inside another div?

Vertically centering div items inside another div Just set the container to display:table and then the inner items to display:table-cell . Set a height on the container, and then set vertical-align:middle on the inner items. This has broad compatibility back as far as the days of IE9.

How will you horizontally and vertically center a div inside a div?

First position the div's top left corner at the center of the page (using position: fixed; top: 50%; left: 50% ). Then, translate moves it up by 50% of the div's height to center it vertically on the page. Finally, translate also moves the div to the right by 50% of it's width to center it horizontally.

How do I center text vertically in HTML?

For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.


2 Answers

Here it is :

#test {
    text-align:center;
}
#test::before {
    content: '';
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}
#sumup {
    display: inline-block;
    vertical-align: middle;
}

#test {
    height : 180px;
    text-align:center;
    background: yellow;
}

#sumup {
   	background-color: #123456;
}

#test:before {
    content: '';
    display: inline-block;
    height: 100%;
    vertical-align: middle;
  }
  #sumup {
    display: inline-block;
    vertical-align: middle;
  }
<div id="test">
		<div id="sumup">
			
			<h1 class="titre">Title</h1>
			<div id="date">hello guys</div>
		</div>
</div>

EDIT: It's now 2015 and thankfully the web changes. Assuming you don't support obsolete browsers, it's usually simpler and cleaner to vertically center elements with the Flex model.

#test {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

#test {
    height : 180px;
    background: yellow;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

#sumup {
   	background-color: #123456;
}
<div id="test">
		<div id="sumup">
			<h1 class="titre">Title</h1>
			<div id="date">hello guys</div>
		</div>
</div>
like image 75
Denys Séguret Avatar answered Oct 24 '22 15:10

Denys Séguret


Here's another way using display:table-cell since I don't quite understand how dystroy's answer works.

#test {
    width:100%;
    height : 400px;
    display:table;
}

#sumup {
    width : 100%;

    height : 100%;
    background-color: #123456;
    display:table-cell;
    vertical-align:middle;
}

http://jsfiddle.net/F8TtE/3/

like image 27
TreeTree Avatar answered Oct 24 '22 14:10

TreeTree