I have a div
30px
high and 500px
wide. This div
can contain two lines of text one under the other, and is styled (padded) accordingly. But sometimes it only contains one line, and I want it to be centered. Is this possible?
You may try to use in your CSS the property vertical-align in order to center it verticaly
div {
vertical-align:middle;
}
if it's a size problem, please notice that 2 text lines and a padding style have great chance to have a height superior to 30px.
For example, if your font size is 12 px and your div padding is 5 px, a one text line div height will be 5px (padding-top) + 12px + 5 px (padding-bottom) = 22px < 30px so no problem,
With a 2 text lines div, it will be 5px +12px *2 (2 lines) + 5px = 34px > 30px and your div height will be automatically changed.
Try either to increase your div height (maybe 40px) or to reduce your padding.
Hope it will help
To center horizontally, use text-align:center
.
To center vertically, one can only use vertical-align:middle
if there is another element in the same row that it is being aligned to.
See it working here.
We use an empty span with a height of 100%, and then put the content in the next element with a vertical-align:middle.
There are other techniques such as using table-cell or putting the content in an absolutely positioned element with top, bottom, left, and right all set to zero, but they all suffer from cross browser compatibility issues.
I believe you want the text to be vertically centered inside your div, not (only) horizontally. The only reliable way I know of doing this is using:
display: table-cell;
vertical-align: middle;
on your div, and it works with any number of lines. You can see a test here: http://jsfiddle.net/qMtZV/1/
Be sure to check browser support of this property, as it is not supported — for example — in IE7 or earlier.
UPDATE 02/10/2016
Five years later this technique is still valid, but I believe there are better and more solid solutions to this problem. Since Flexbox support is good nowadays, you might want to do something along these lines: http://codepen.io/michelegera/pen/gPZpqE.
<div>
is set to position: relative;
. –
Singlecross relative
or to the (default) value of static
. It won’t center the lines when position is absolute
or fixed
. –
Ducks For the parent div,use following CSS:
style="display: flex;align-items: center;"
HTML
<div class="outside">
<div class="inside">
<p>Centered Text</p>
</div>
</div>
CSS
.outside {
position: absolute;
display: table;
}
.inside {
display: table-cell;
vertical-align: middle;
text-align: center;
}
If you have text inside a <div>
:
text-align: center;
vertical-align: middle;
display: table-cell;
div {
height: 256px;
width: 256px;
display: table-cell;
text-align: center;
line-height: 256px;
}
The trick is to set the line-height
equal to the height
of the div
.
You may try to use in your CSS the property vertical-align in order to center it verticaly
div {
vertical-align:middle;
}
if it's a size problem, please notice that 2 text lines and a padding style have great chance to have a height superior to 30px.
For example, if your font size is 12 px and your div padding is 5 px, a one text line div height will be 5px (padding-top) + 12px + 5 px (padding-bottom) = 22px < 30px so no problem,
With a 2 text lines div, it will be 5px +12px *2 (2 lines) + 5px = 34px > 30px and your div height will be automatically changed.
Try either to increase your div height (maybe 40px) or to reduce your padding.
Hope it will help
Adding line height helps too.
text-align: center;
line-height: 18px; /* for example. */
I may be missing something here, but have you tried:
text-align:center;
??
I've looked around and the
display: table-cell;
vertical-align: middle;
seems to be the most popular solution
<div class="header">
<div class="phone">☎ 052 824 134</div>
<div class="adres">✯ бул. Княз Борис I, No 115 - Дворец на Културата и Спорта</div>
using flexbox To center div
.header{
background: #ff6400;
height:100px;
box-sizing: border-box;
display: flex;
justify-content:'center';
flex-direction: column;
}
.phone{
color: white;
font-size: 20px;
padding: 9px 0 0 125px;
}
.adres{
display: inline-block;
color: white;
font-size: 20px;
width: 50%;
margin: 0 auto;
text-align:'center';
}
}
© 2022 - 2024 — McMap. All rights reserved.