This question already has answers here:
Vertically align text next to an image?
(26 answers)
Closed 7 years ago.
I want both the image and text centered horizontally and vertically. What is the best way to go about this? I have tried float but it doesn't seem to be working. See the above image for ideal result
HTML:
<div class="clearfix" id="one">
<img class="imac" src="imac.png">
<p1>
I want to work in Computer Design, changing the way people
interact with thoughtfully considered software and hardware
experiences.
</p1>
</div>
CSS:
#one{
background-color: #4E5B71;
width: 100%;
height: auto;
padding: 15px;
}
.clearfix{
overflow: auto;
}
p1{
font-family: AvenirNext-Regular;
font-size: 24px;
color: #FFFFFF;
line-height: 50px;
vertical-align: middle;
display: inline-block;
}
imac{
width: 100% auto;
height: auto;
float:left;
vertical-align: middle;
}
SOLUTION 1
Demo: https://jsfiddle.net/2Lzo9vfc/78/
HTML
<div class="clearfix" id="one"> <img class="imac" src="http://placehold.it/70x50"> <p> I want to work in Computer Design, changing the way people interact with thoughtfully considered software and hardware experiences. </p>
</div>
CSS
#one{
background-color: #4E5B71;
width: 100%;
height: auto;
padding: 15px;
display: table;
}
.clearfix{
overflow: auto;
}
p{
font-family: AvenirNext-Regular;
font-size: 16px;
color: #FFFFFF;
vertical-align: middle;
display: table-cell;
}
.imac {
vertical-align: middle;
display: table-cell;
}
SOLUTION 2 using flexbox
Demo: https://jsfiddle.net/2Lzo9vfc/81/
CSS
#one{
background-color: #4E5B71;
width: 100%;
height: auto;
padding: 15px;
display: -webkit-flex;
display: flex;
align-items:center;
}
.clearfix{
overflow: auto;
}
p{
font-family: AvenirNext-Regular;
font-size: 16px;
color: #FFFFFF;
}
The vertical-align property does not work in that context. It is a bit counter-intuitive but vertical-align will only effectively work in a table layout.
You have a few ways to solve your predicament, but on the topic of tables, it might not be a bad idea to use a table to assist with your alignment. For example, you could put create a table with one row <tr> and four table cells <td> and apply your vertical-align to the table cells. The image would be in cell two, and the paragraph in cell three. You would then apply the desired width to the cells to ensure correct horizontal alignment.
PS: There is no <p1> tag. You should be using just <p>.
To center the text, you should use text-align: center.
p1{
font-family: AvenirNext-Regular;
font-size: 24px;
color: #FFFFFF;
line-height: 50px;
display: inline-block;
text-align: center;
}
You can also use a paragraph to wrap the image and control it. You can use the same paragraph formatting as the text below it or give it its own class. Just make sure it also has text-align: center programmed in it.
<p1><img class="imac" src="imac.png" /></p1>
Related
Ok, so i have been looking around for someone with the same problem as me, but didn't find any(almost 100% shure some of you guys are going to link to to one).
I have managed to center a div inside a div which again is inside footer(pretty shure overkill). But my problem is that i have centered two images with two lines of text connected to them. I want the text to be displayed vertically centered(with the image in mind), and not in the bottom right corner of the images, like now.
Pretty shure it's something simple, but here is a link:
http://jsfiddle.net/rdsdmuw8/
<footer>
<div id="footer">
<div id="sosial">
<img src="bilder/telefon.jpg" style="height:50%;">
+47 930 98 907
<img src="bilder/mail.png" style="height:50%; margin-left:20%; margin-top:20px;">
Bryter-pedersen#hotmail.com
</div>
</div>
</footer>
*{
margin: 0px;
padding: 0px;
color: #fff;
}
footer {
width:100%;
height: 80px;
background-color: red;
}
#footer{
height: 100%;
}
#sosial {
text-align: center;
vertical-align: middle;
}
#sosial a{
list-style-type: none;
text-decoration: none;
}
In order to vertically align the img elements next to the anchors set vertical-align: middle for both of the elements.
#sosial img,
#sosial a {
vertical-align: middle;
}
In order to vertically center all the containing elements within the footer, you can use the table-cell/table approach.
#footer {
height: 100%;
width: 100%;
display: table;
}
#sosial {
text-align: center;
display: table-cell;
vertical-align: middle;
}
Updated Exmaple
I removed the inline CSS styling in the example. You can use img:nth-of-type() to apply the margin to the second element. Just throwing options out there.
#sosial img:nth-of-type(2) {
margin-left:50px;
}
if you know what is the height you want for the images you can use, in my example is 50px:
#sosial a {
list-style-type: none;
text-decoration: none;
line-height: 50px;
display: inline-block;
height: 100%;
vertical-align: middle;
}
I came across this behavior recently when a client reported that some of the buttons on a page had vertically centered text while others did not.
As it turns out, buttons will vertically center text inside them but links won't. Example here: http://jsfiddle.net/valentin/7EjtD/
a, button{
height: 200px;
background-color: #ff6400;
color: #fff;
font-weight: bold;
display: inline-block;
vertical-align: top;
border: 0;
padding: 20px;
font-family: sans-serif;
text-decoration: none;
}
Is there any way to add this behavior to links as well outside of using line-height?
Button aligns to the middle because is its default behavior. Your fiddle is aligning top actually. To make it work you can wrap your elements on an display:table element, like a div. Then set the button and the link to be display:table-cell. Then your vertical-align will work. Like this:
<div class="wrapper">
LINK
<button>BUTTON</button>
</div>
And the css:
*{
box-sizing: border-box;
}
div.wrapper {
display:table;
}
a, button{
height: 200px;
background-color: #ff6400;
color: #fff;
font-weight: bold;
display: table-cell;
vertical-align: middle;
border: 0;
padding: 20px;
font-family: sans-serif;
text-decoration: none;
}
Buttons are inline-block elements, while anchors are just inline. You can use padding to achieve the same effect:
a
{
padding: 91px 20px; /* <---(height-fontSize)/2 */
height: auto;
}
JSFiddle
TableData (TD AKA cell) are pretty damn good at default text centering ;)
live demo
a{
display: table-cell;
vertical-align: middle;
}
for clean-code-sake i'd use a special class like:
a.buttonAlike{
Sorry folks! Didn't see he wanted to avoid line-height. Original post:
Add line-height equal to the height of your element. In this case:
line-height: 200px;
DEMO: JSFIDDLE
I need to vertically align the text into a div that have a certain height.
If you go here you can see my problem: http://onofri.org/example/example3/
As you can see I have a #titleBox div that contain the text: Promoting Investment in Agriculture
The #titleBox have a specific height that is 40px. I want vertically align the text in the green div in such a way that is at the center.
This is my HTML and CSS code but don't work well:
<div id="container">
<div id="titleBox">
<p id="myTitle">Promoting Investment in Agriculture</p>
</div>
</div>
#titleBox{
margin: 0 auto;
width: 350px;
background-color: #6da662;
height: 40px;
vertical-align: middle;
}
#myTitle{
/* consente di posizionare un elemento al centro del suo contenitore */
margin: 0 auto;
overflow: hidden;
color: #fff;
font-size: 16.5px;
font-weight: bold;
text-align: center;
}
What have I to do to solve?
Tnx
Andrea
Try to add these styles to #myTitle:
vertical-align: middle;
line-height: 40px;
Here's another solution - add these styles to your page.
You should definitely get familiar with the display:table-cell property for vertical centering.
#titleBox{
display:table;
}
#myTitle{
display:table-cell;
vertical-align: middle;
}
If you want to be really concise, you can do it all in one element:
<div id="titleBox">Promoting Investment in Agriculture</div>
and apply the following CSS:
#titleBox {
margin: 0 auto;
width: 350px;
background-color: #6da662;
line-height: 40px;
vertical-align: middle;
text-align: center;
color: #fff;
font-size: 16.5px;
font-weight: bold;
}
See fiddle: http://jsfiddle.net/audetwebdesign/2PxsZ/
I want to have a vertical centralized text.
But the problem is: When I increase the font-size, different browsers render a different font-family (font stack) and its not keep middle alignment.
http://jsfiddle.net/rpNnh/1/
Thanks!
see the solution in jsfiddle
In detail
Html code
<div>
<p>text+<br>sdffd</p>
</div>
<br/><br/>
Switch Font
CSS
div{
width: 200px;
height: 200px;
border: 1px solid black;
display: table; //added
}
p{
line-height: 20px; //changed
font-size: 18px;
text-align: center;
display: table-cell; //added
vertical-align: middle; //added
}
p.times{
font-family: "verdana"; //changed
}
You can vertically align a text in middle by placing this CSS on the div:
text-align: center;
display: table-cell;
vertical-align: middle;
I have fixed height divs that contain text in them. I would like the text to be vertically aligned in the middle of the div, but the problem lies in the fact that some of the text is single-line, and some splits itself over onto two lines. For IE8, Chrome and Firefox, using display: table-cell and vertical-align: middle provides the solution I need:
JS Fiddle is here. Take the asterisk off the width: 300px to see the formatting when the text is on one line.
However, IE7 does not support the display: table-cell property. The only solutions I have found to this apply only to single lines, and not to text that may be 1 or 2 lines. How can I have it display in IE7 as it does in more modern browsers, without the use of any scripts?
How about an IE7 CSS call putting position:relative on the div, and absolute on the h6, and keep the code for vertical-align for modern browsers.
http://jsfiddle.net/yap59cn3/
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="ie7.css">
<![endif]-->
ie7.css
div
{
/* Use inheritance, and override only the declarations needed. */
position:relative;
}
h6
{
height:auto; /* override inherited css */
position:absolute;
top:45%;
}
The goal is to make IE7 "presentable" -- no matter what you do, it will never look as pretty as a modern browser. To me, it's not worth the headache (not even a little).
Personally I've started to (ab)use padding to get vertical aligns. It's especially handy if you use fixed height, since you can offset the height with the value of the padding to get a perfect full-height element.
Note: This solution only works if you know what text will come in the <h6> in advance. If you dynamically add it, I'd suggest wordcounting to try to figure out if it's gonna wrap or not.
Solution:
HTML
<div>
<h6 class="OneLineVertCentered">Here is some text. Look at this lovely text. Isn't it nice?</h6>
</div>
<div style="margin-top: 1em;"> <!-- Margin only for displaying the boxes properly -->
<h6 class="TwoLineVertCentered">Here is some text. Look at this <br />
lovely two-line text. Isn't it nice?</h6>
</div>
CSS
div {
background-color: yellow;
height: 30px;
width: 200px;
width: 300px;
}
h6.OneLineVertCentered,
h6.TwoLineVertCentered {
font-size: 12px;
line-height: 1em;
}
h6.OneLineVertCentered {
padding-top: 10px;
}
h6.TwoLineVertCentered {
padding-top: 3px;
}
Fiddle:
http://jsfiddle.net/Snorbuckle/CnmKN/
Snippet (same as fiddle):
div {
background-color: yellow;
height: 30px;
width: 200px;
width: 300px;
}
h6.OneLineVertCentered,
h6.TwoLineVertCentered {
font-size: 12px;
line-height: 1em;
}
h6.OneLineVertCentered {
padding-top: 10px;
}
h6.TwoLineVertCentered {
padding-top: 3px;
}
<div>
<h6 class="OneLineVertCentered">Here is some text.
Look at this lovely text. Isn't it nice?</h6>
</div>
<div style="margin-top: 1em;">
<h6 class="TwoLineVertCentered">Here is some text. Look at this <br />
lovely two-line text. Isn't it nice?</h6>
</div>
You can use a helper span element to vertical align your text like the following example:
html
<div class="container">
<span class="aligner"></span>
<h3>Text to be aligned center in the beloved ie7</h3>
</div>
css
div.container {
background-color: #000;
color: #fff;
height: 300px;
width: 250px;
position:relative;
margin:12px auto;
text-align:center;
}
.aligner {
display: inline-block;
height: 100%;
content: ' ';
margin-right: -0.25em;
vertical-align: middle;
}
h3 {
display: inline-block;
vertical-align: middle;
}
Fiddle: http://jsfiddle.net/groumisg/dbx4rr0f/
Normally, we would use a pseudo element for this, but ie7 (what a surprise!) does not support :after, :before...etc. Also, note that ie7 does not support display: inline-block for elements that are not inline by default, like div. To use display: inline-block for a div you would have to use the following hack:
div {
display: inline-block;
*display: inline;
zoom: 1;
}
as suggested here Inline block doesn't work in internet explorer 7, 6
You should be able to accomplish this with line-height and vertical-align: middle;.
div {
background-color: yellow;
height: 30px;
line-height: 30px;
width: 200px;
*width: 300px;
}
h6 {
font-size: 12px;
line-height: 1em;
height: 30px;
vertical-align: middle;
}
check this out
http://jsfiddle.net/CnmKN/59/
CSS Code
div {
background-color: yellow;
height: 30px;
width: 200px;
*width: 300px;
display:table;
}
h6 {
font-size: 12px;
line-height: 1em;
display: table-cell;
vertical-align: middle;
height:90px;
}
I know two other methods to vertically center elements than with table-cell:
1) With line-height:
.element {
height: 60px;
line-height: 60px
}
This will only work if the text is in a single line.
2) position absolute/margin auto
.parentElement {
position: relative;
}
.element {
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
}
You maybe will have to use height (auto or a value) and display inline/inline-block. Just try.
Key point is not to use pixels for alignment, use only %-s.
Works even on IE5 :)
here is Demo
.wrapper{
position: relative;
width: 100%;
height: 200px; /* change this value to see alignment*/
background-color: red;
margin: 0 auto;
}
.cell{
position: absolute;
display:block;
background-color: blue;
left:50%;
top:50%; /*this puches element half down*/
margin-left:-100px; /* this is the half size of cell width:200px;*/
margin-top: -.5em; /*this is the half size of font size*/
width: 200px;
color: #fff;
text-align:center;
}
<div class='wrapper'>
<div class='cell'>vertically aligned text</div>
</div>
div {
background-color: yellow;
height: 400px;
width: 200px;
display: table-cell;
vertical-align: middle;
width: 300px;
}
h6 {
font-size: 12px;
line-height: 1em;
height: 30px;
}