Why is the image not rendered exactly in the middle of the enclosing DIV in this example?
div {
border: 1px solid gray;
line-height: 100px;
}
img {
height: 96px;
vertical-align: middle;
}
<div>
<img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Russula_emetica_117475.jpg"> Foo
</div>
If we run this example, we see that the image is closer to the bottom border than the top border.
Why does this happen?
What is the right way to vertically align an image in a DIV so that it is exactly in the middle of the DIV vertically?
If we check the documentation we will read this:
middle
Aligns the middle of the element with the baseline plus half the x-height of the parent.
So we need to identify these values in order to understand the alignement. If we refer to the above figure we can clearly see the baseline and we can also see the line-height (defined by 100px in our case). You can aslo notice that the middle is not the middle of the div but the middle of the text defined by the different values (font-family, font-size, etc).
To use easy words: your reference of alignment is no the div but the text inside the div.
To make it easier let keep the line-height with the default value and define a font-size instead (the line-height will be then equal to the font-size):
div {
border: 1px solid gray;
font-size:50px;
}
img {
height: 46px;
vertical-align: middle;
}
<div>
<img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Russula_emetica_117475.jpg"> Foo
</div>
with a different font-family
<div style="font-family:arial">
<img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Russula_emetica_117475.jpg"> Foo
</div>
You can cleary see that the middle is far from the middle of the div and if we change the font-family the alignment will also change.
In order to align content inside a div that contain text better rely on flexbox for example:
div {
border: 1px solid gray;
font-size:50px;
display:flex;
align-items:center;
}
img {
height: 46px;
}
<div>
<img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Russula_emetica_117475.jpg"> Foo
</div>
that the element would be where it is required, he needs to specify it
img {
height: 96px;
width: 120px;
margin-left: calc(50% - 60px);
vertical-align: middle;
}
<div>
<img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Russula_emetica_117475.jpg"> Foo
</div>
You could just add equal padding on the top and bottom of the div. This helps you to align the image in the center. There are also many other ways to center align items.
Please go through the article https://vanseodesign.com/css/vertical-centering/
div {
border: 1px solid gray;
padding: 20px 0;
}
img {
height: 96px;
}
<div>
<img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Russula_emetica_117475.jpg"> Foo
</div>
What is the right way to vertically align an image in a DIV ?
you can do it with vertical-align: middle;
div {
border: 1px solid gray;
}
img {
height: 96px;
vertical-align: middle;
}
<div>
<img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Russula_emetica_117475.jpg" />
<span class="caption">Foo</span>
</div>
or you can use flexbox
div {
border: 1px solid gray;
display:flex;
align-items:center;
}
img {
height: 96px;
}
<div>
<img src="https://upload.wikimedia.org/wikipedia/commons/0/02/Russula_emetica_117475.jpg" />
<span class="caption">Foo</span>
</div>
Related
I am stuck with a CSS problem and I am calling out for your expertise to help me!
I am trying to align text and image. Text and image should be vertically centered, left aligned and fit right next to each other. Both elements should be contained inside the wrapper div that can have varying width.
Below is code that I have so far:
<style>
.cell {
width: 150px;
height: 200px;
box-shadow: 0 0 0.5em #999;
white-space: nowrap;
}
.text_element {
height: 100%;
display: inline-block;
white-space: normal;
float: left;
}
.tooltip_element {
height: 100%;
display: inline-block;
}
</style>
<div class="cell">
<div class="text_element"> This is some random, random text.</div>
<div class="tooltip_element">
<img src="http://www.sainsburysbank.co.uk/library/default/images/life-insurance/icon-tooltip.png"/>
</div>
</div>
Above code produces next image when the wrapper content is 500px:
In the above image elements fit correctly next to each other, but aren't vertically aligned.
For width 200px, we get another problem however:
Here we get empty space between text and image, which shouldn't be there, as image should fit right next to the text. Furthermore image element is now outside the div.
Note that:
wrapper content can have varying width
solution should work in all browsers (no flex solutions)
no JS, only CSS can be used
Thank you very much for your help!
EDIT:
Text should be left aligned!
Ok, here it is:
.cell {
width: 210px;
height: 200px;
box-shadow: 0 0 0.5em #999;
white-space: nowrap;
padding-right: 30px;
}
.text_element {
display: inline-block;
white-space: normal;
float: left;
text-align: justify;
position: relative;
}
.tooltip_element {
position: absolute;
left: 100%;
top: 50%;
transform: translateY(-50%);
}
<div class="cell">
<div class="text_element">This is some random, random text. Moar random text. And moar, moar random text...
<div class="tooltip_element">
<img src="http://www.sainsburysbank.co.uk/library/default/images/life-insurance/icon-tooltip.png" />
</div>
</div>
</div>
Please note I included the tooltip element inside the text element. So I could vertically align them. If they must be siblings, I'd need to wrap them both in a container for vertical centering without flex-box.
If you prefer jsFiddle, to play around with cell width, here it is.
You can use css table https://jsfiddle.net/2Lzo9vfc/247/
CSS
.cell {
width: 100%;
height: 200px;
box-shadow: 0 0 0.5em #999;
word-break:break-all;
}
.text_element {
display: table-cell;
vertical-align: middle;
}
.tooltip_element {
display: table-cell;
vertical-align: middle;
}
Here a small demo of my issue...
<html>
<body>
<span style="border: 1px solid black; background-color: #CCCCFF"> My text
<img style="width: 1em; height: 1em; vertical-align: middle;"
src="http://files.softicons.com/download/social-media-icons/stucco-social-media-icons-by-bradley-siefert/png/64x64/stucco-facebook.png"> My text </span>
</body>
</html>
As you can see I have styled the image vertical-align: middle but still the image is misplaced at the bottom.
How do I get it centered between the top and bottom borders?
EDIT: Here's a good rundown of this issue:
http://phrogz.net/css/vertical-align/
How do I get it centered between the top and bottom borders?
You can try using the CSS flexbox.
Check the following example:
span {
display: inline-flex;
align-items: center;
padding: 5px;
}
img {
width: 20px;
margin: 0 5px;
}
<span style="border: 1px solid black; background-color: #CCCCFF">
My text
<img src="http://files.softicons.com/download/social-media-icons/stucco-social-media-icons-by-bradley-siefert/png/64x64/stucco-facebook.png">
My text
</span>
Using vertical-align: middle adjust the image with the texts height, and as you can see in my screen-dump, the image is exactly between the text's top/bottom (I added 2 green lines to visualize it).
The "empty" space between the text and the top border, is how a particular font (and this is different for different fonts) arranges the characters within its internal height.
Now, for what ever reason, if you want to "break" this and adjust for example an image within a fonts internal height (as in your question, between the top/bottom borders), you need to add "margin" somewhere.
To let the text flow as normal as possible, you could:
add a few pixels margin/padding to the image element
add a few extra lines of pixels with full transpareny on the image
Note: Based on how perfect you want this, you need to calculate the "margin" on the font you use (it can/will likely be different), the font size, and you also might want to check on different OS's as well.
To "force text/image" to center, you could:
use display: table (work on IE8/9)
use display: flex (don't work on IE8/9)
Here is samples using "padding" and display: table:
/* padding */
span img {
padding-bottom: 3px;
width: 20px;
vertical-align: middle;
}
/* display table */
div {
display: table-cell;
vertical-align: middle;
padding: 5px;
}
div span {
vertical-align: middle;
}
div img {
width: 20px;
vertical-align: middle;
}
<span style="padding: 5px;border: 1px solid black; background-color: #CCCCFF"> This one
<img
src="http://files.softicons.com/download/social-media-icons/stucco-social-media-icons-by-bradley-siefert/png/64x64/stucco-facebook.png"> use padding </span>
<br /><br />
<div style="border: 1px solid black; background-color: #CCCCFF">
<span>This one</span>
<img src="http://files.softicons.com/download/social-media-icons/stucco-social-media-icons-by-bradley-siefert/png/64x64/stucco-facebook.png">
<span>use display: table</span>
</div>
As in this JS Fiddle, I gave the wrapping span height value of 1.3em, turned its display to inline-block so that the height value will take effect, then put a margin-top:0.1em for the image
<span style="display:inline-block;height:1.3em; border: 1px solid black; background-color: #CCCCFF"> My text
<img style="width: 1em; height: 1em; margin-top:.1em;"
src="http://files.softicons.com/download/social-media-icons/stucco-social-media-icons-by-bradley-siefert/png/64x64/stucco-facebook.png"> My text </span>
Note that you can set both the parent span and the img height to 1em but keep the display:inline-block;, thusthe img will fit inside the span witouth margins.
you can try this one:
span
{
display:inline-block;height:1.2em;
}
DEMO HERE
try this
<html><body>
<span style="border: 1px solid black; background-color: #CCCCFF"> My text
<img style="width: 1em; height: 1em; vertical-align: top; margin-top:1px"
src="http://files.softicons.com/download/social-media-icons/stucco-social-media-icons-by-bradley-siefert/png/64x64/stucco-facebook.png"> My text </span>
</body></html>`enter code here`
Give padding-bottom to your image
<img style="padding-bottom:2px; width: 1em; height: 1em; vertical-align: middle;" src="http://files.softicons.com/download/social-media-icons/stucco-social-media-icons-by-bradley-siefert/png/64x64/stucco-facebook.png">
I am working on a menu system which consists of div's (width: 275px, height: 75px, border: 1px solid) among each other. I want the whole div to be clickable, which I do with an a tag and display:block property (see code below). Some of the menu entries are multi-lined text, but I can't align them vertically. The basic code is:
.div {
width: 275px;
height: 75px;
border: 1px solid #000;
text-align: center;
float: right;
}
<div class="div">
<a style="display: block; width: 100%; height: 100%" href="#">link..</a>
</div>
I have tried the following:
line-height: 75px: that doesn't work with multi-lined text
display: table and vertical-align: middle does not work with the 100% width and height of the -tag.
I have really tried a lot other code with wrapper div's, tables, ... but with no success.
Otherwise, I do not want to make use of javascript (onclick="location.href").
Thanks!
You can do it with what you've already tried: 'display:table-cell; vertical-align: middle;', you just have to set the height to 75px instead of 100%.
Use display:table-cell, to achieve vertically centred multi-lined text.
JSFiddle Demo
CSS:
div {
width: 300px;
height: 200px;
border: 1px solid #000;
text-align: center;
display: table-cell;
vertical-align: middle;
}
I have a container which is 100px by 100px. I have put an image in it which I don't know the dimensions, except the fact that I have set the width to 100px. I would like to find a way in CSS to vertically align this image middle in it's container. I have stuck overflow:hidden on the container to prevent it from showing anything outside the square.
I have found something on here on how to do the opposite (width, not height).
Wrap the image inside two divs that will mimic an html table. The outer div will have display: table property, and the inner will have display: table-cell property. You can set the vertical-align property of the inner div to be top, middle, or bottom.
HTML:
<div class="table">
<div class="table-cell middle">
<img src="https://www.google.ca/images/srpr/logo3w.png" alt="" />
</div>
</div>
CSS:
.table {
display: table;
width: 100px;
height: 100px;
border: 1px solid blue;
}
.table-cell {
display: table-cell;
}
.middle {
vertical-align: middle;
}
img {
width: 100%;
outline: 1px solid red;
}
Here's the fiddle.
You would need to know the height of the image to vertically align it as you would then use this CSS:
div {
position:relative;
overflow:hidden;
}
div img {
position:absolute;
top:50%;
margin-top:-XXXpx /*(where XXX px is half the height of the image)*/
}
I have a div centered in the site and containing a picture. The div extends its height to fit the picture as expected. Then I add some text and I expect it to lay out on the bottom of the div. But in addition to that the div also resizes a little bit so its height is higher than the height of the picture. Here is the exmaple:
Html:
<div class="siteContent">
<img class="debug" src="../../Content/themes/base/images/pageTitle.png" />
<span>
aaa
</span>
</div>
CSS:
body
{
margin: 0;
}
.siteContent
{
width: 960px;
margin-left: auto;
margin-right: auto;
border: thin solid;
}
.debug
{
border: thin solid;
}
The result:
The unwanted effect is marked red.
I was able to fix this for IE 8 and Firefox by modifiing the CSS like this:
body
{
margin: 0;
}
.siteContent
{
width: 960px;
margin-left: auto;
margin-right: auto;
border: thin solid;
position: relative;
}
.siteContent span{
position: absolute;
bottom: 0px;
}
.debug
{
border: thin solid;
}
After runnig this I got the right result in Mozilla:
However this does not work for Chrome and Safary. How can make it work for all major browsers ?
this happens because the image is aligned to the baseline of the text. try to set vertical-align: bottom or vertical-align: text-bottom for your image to solve this.
I think this is what you want: http://jsfiddle.net/WEF49/3/
Tested in IE, chrome and FF. This way img and span are bottom aligned and div do not have any extra space. Bad side is that div height is fixed to img height. If you do not mind fixed height on div this is cleanest solution.
html:
<div class="siteContent">
<img class="debug" src="http://t0.gstatic.com/images?q=tbn:ANd9GcRzjvZihxljFMlEt_IOypeOWYCgPgEvVrZ_DnwVTws5uR_iC_oFIg" />
<span>
aaa
</span>
</div>
css:
.siteContent
{
width: 960px;
height: 213px;
margin-left: auto;
margin-right: auto;
border: thin solid;
overflow:hidden;
}
.debug
{
border: thin solid;
}
Try adding overflow:hidden to the div without height
+
<span style="clear:both">
aaa
</span>
+
<img src="..." style="clear:both" />
take the style="*" to your css file (ofcource )