Issue
I do not understand when vertical-align
will and won't work.
Every time I run into a use case for vertical-align
it seems to be a coin toss as to whether it will actually work. I know it has to be applied to inline elements. I have read that I must specify a line-height
for elements that do not normally have one. I have read that the height
property must have a static (non-auto/non-%) value. I have read that some (modern) browsers do not handle vertical-align
correctly if the element they are used on is not naturally an inline element. I am unclear on whether vertical-align
should be on the containing element (like text-align
) or the element I want vertically aligned.
I've created this jsfiddle to try to work out the problem, but remain confused.
#outer {
box-sizing: border-box;
border: red 1px solid;
height: 200px;
width: 400px;
text-align: center;
}
#inner {
border: blue 1px solid;
display: inline-block;
height: 200px;
vertical-align: middle;
}
#header {
border: green 1px solid;
display: inline-block;
line-height: 1em;
margin: 0;
}
<div id="outer">
<div id="inner">
<h1 id="header">
Some Text
</h1>
</div>
</div>
In the jsfiddle above I would expect #header
to be centered halfway between the top and bottom of #outer
and #inner
. Obviously, that's not the case.
Solution
Simply said: vertical-align
is only active/valid when the element it is applied to has display: inline-block
or ìnline
, which for example is useful if you want to align a bunch of images at their top border: You define them as inline-blocks
and apply vertical-align: top
to them
Here is an example:
.wrapper {
height: 250px;
border: 1px solid green;
}
.wrapper>img {
display: inline-block;
vertical-align: top;
}
<div class="wrapper">
<img src="https://placehold.it/120x40">
<img src="https://placehold.it/70x140">
<img src="https://placehold.it/80x60">
<img src="https://placehold.it/60x140">
</div>
In your fiddle, the elements are nested into each other, not next to each other, i.e. they are not siblings - there is only one child element each, so there is no alignment of siblings like in the example above.
Answered By - Johannes Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.