The code below (also available as a demo on JS Fiddle) does not position the text in the middle, as I ideally would like it to. I cannot find any way to vertically centre text in a div
, even using the margin-top
attribute. How can I do this?
<div id="column-content">
<img src="http://i.stack.imgur.com/12qzO.png">
<strong>1234</strong>
yet another text content that should be centered vertically
</div>
#column-content {
display: inline-block;
border: 1px solid red;
position:relative;
}
#column-content strong {
color: #592102;
font-size: 18px;
}
img {
margin-top:-7px;
vertical-align: middle;
}
This question is related to
css
vertical-alignment
This is simply supposed to work:
#column-content {
--------
margin-top: auto;
margin-bottom: auto;
}
I tried it on your demo.
Try this:
HTML
<div><span>Text</span></div>
CSS
div {
height: 100px;
}
span {
height: 100px;
display: table-cell;
vertical-align: middle;
}
The accepted answer doesn't work for multi-line text.
I updated the JSfiddle to show CSS multiline text vertical align as explained here:
<div id="column-content">
<div>yet another text content that should be centered vertically</div>
</div>
#column-content {
border: 1px solid red;
height: 200px;
width: 100px;
}
div {
display: table-cell;
vertical-align:middle;
text-align: center;
}
It also works with <br />
in "yet another..."
To make Omar's (or Mahendra's) solution even more universal, the block of code relative to Firefox should be replaced by the following:
/* Firefox */
display: flex;
justify-content: center;
align-items: center;
The problem with Omar's code, otherwise operative, arises when you want to center the box in the screen or in its immediate ancestor. This centering is done either by setting its position to
position: relative;
or position:static;
(not with position:absolute nor fixed).
And then margin: auto; or margin-right: auto; margin-left: auto;
Under this box center aligning environment, Omar's suggestion does not work. It doesn't work either in Internet Explorer 8 (yet 7.7% market share). So for Internet Explorer 8 (and other browsers), a workaround as seen in other above solutions should be considered.
Update April 10, 2016
Flexboxes should now be used to vertically (or even horizontally) align items.
body {
height: 150px;
border: 5px solid cyan;
font-size: 50px;
display: flex;
align-items: center; /* Vertical center alignment */
justify-content: center; /* Horizontal center alignment */
}
_x000D_
Middle
_x000D_
A good guide to flexbox can be read on CSS Tricks. Thanks Ben (from comments) for pointing it out. I didn't have time to update.
A good guy named Mahendra posted a very working solution here.
The following class should make the element horizontally and vertically centered to its parent.
.absolute-center {
/* Internet Explorer 10 */
display: -ms-flexbox;
-ms-flex-pack: center;
-ms-flex-align: center;
/* Firefox */
display: -moz-box;
-moz-box-pack: center;
-moz-box-align: center;
/* Safari, Opera, and Chrome */
display: -webkit-box;
-webkit-box-pack: center;
-webkit-box-align: center;
/* W3C */
display: box;
box-pack: center;
box-align: center;
}
Add a vertical align to the CSS content #column-content strong
too:
#column-content strong {
...
vertical-align: middle;
}
Also see your updated example.
=== UPDATE ===
With a span around the other text and another vertical align:
HTML:
... <span>yet another text content that should be centered vertically</span> ...
CSS:
#column-content span {
vertical-align: middle;
}
Also see the next example.
This is the simplest way to do it if you need multiple lines. Wrap you span
'd text in another span
and specify its height with line-height
. The trick to multiple lines is resetting the inner span
's line-height
.
<span class="textvalignmiddle"><span>YOUR TEXT HERE</span></span>
.textvalignmiddle {
line-height: /* Set height */;
}
.textvalignmiddle > span {
display: inline-block;
vertical-align: middle;
line-height: 1em; /* Set line height back to normal */
}
Of course the outer span
could be a div
or what have you.
Andres Ilich has it right. Just in case someone misses his comment...
A.) If you only have one line of text:
div_x000D_
{_x000D_
height: 200px;_x000D_
line-height: 200px; /* <-- this is what you must define */_x000D_
}
_x000D_
<div>vertically centered text</div>
_x000D_
B.) If you have multiple lines of text:
div_x000D_
{_x000D_
height: 200px;_x000D_
line-height: 200px;_x000D_
}_x000D_
_x000D_
span_x000D_
{_x000D_
display: inline-block;_x000D_
vertical-align: middle;_x000D_
line-height: 18px; /* <-- adjust this */_x000D_
}
_x000D_
<div><span>vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text</span></div>
_x000D_
I know it’s totally stupid and you normally really shouldn’t use tables when not creating tables, but:
Table cells can align multiple lines of text vertically centered and even do this by default. So a solution which works quite fine could be something like this:
HTML:
<div class="box">
<table class="textalignmiddle">
<tr>
<td>lorem ipsum ...</td>
</tr>
</table>
</div>
CSS (make the table item always fit to the box div):
.box {
/* For example */
height: 300px;
}
.textalignmiddle {
width: 100%;
height: 100%;
}
See here: http://www.cssdesk.com/LzpeV
Source: Stackoverflow.com