Reputation: 10219
I have this code and the text is properly centered.
HTML
<div class="holder">
<img src="http://placehold.it/100x150/" style="float: left;" />
<a href="#" class="centered"> some text</a>
</div>
CSS
.holder {
border: 1px solid red;
padding: 10px;
width: 300px;
display: table;
}
a {
display: table-cell;
vertical-align: middle;
}
img {
width: 100px;
}
How can I make the text aligned to left, next to the image?
Upvotes: 0
Views: 74
Reputation: 1136
Set your anchor to 100% and add some padding if needed:
a {
display: table-cell;
vertical-align: middle;
width: 100%;
padding-left: 15px;
}
Demo: http://jsfiddle.net/2P8Yj/20/
Upvotes: 1
Reputation: 1025
Try this http://jsfiddle.net/2P8Yj/18/
I have added the display: table to body the css is as follows
body{display:table}
.holder { border: 1px solid red; padding: 10px; width: 300px;display: table-row;}
a { display: table-cell;
vertical-align: middle; }
img { width: 100px;}
Instead of body you can have one more div above it.
Upvotes: 1
Reputation: 1477
Not sure if changing the display: table-cell;
to display: inline-block;
is ok in your situation.
But you can accomplish the same effect with this:
a {
display: inline-block;
vertical-align: middle;
}
img {
width: 100px;
display: inline-block;
vertical-align: middle;
}
Upvotes: 0
Reputation: 1216
You could put img
and a
each in a cell
to create a "proper" table:
HTML:
<div class="holder">
<div class="cell">
<img src="http://placehold.it/100x150/" style="float: left;" />
</div>
<div class="cell">
<a href="#" class="centered"> some text</a>
</div>
</div>
CSS:
.holder {
border: 1px solid red;
padding: 10px;
width: 300px;
display: table;
}
.cell {
display:table-cell;
vertical-align: middle;
text-align:left;
}
.cell:nth-child(1) {
width:105px; /*just to show, better to use padding for second cell*/
}
img {
width: 100px;
}
Upvotes: 0
Reputation: 360
Try this JsFiddle: JsFiddle
This are the changes in your CSS, notice the height I have added to the .holder
and the line-height to your a
:
.holder {
border: 1px solid red;
padding: 10px;
width: 300px;
height:150px;
display: block;
}
a {
line-height:150px;
padding-left:20px;
}
img {
width: 100px;
}
Upvotes: 0
Reputation: 1729
Add a width to your a:
a {
display: table-cell;
vertical-align: middle;
width:200px;
}
Upvotes: 2
Reputation: 6499
You could add position:relative
and left:-80px
to your text CSS.
Upvotes: -1