I try to make the blue container stick to the top. How can I manage that?
HTML:
<div class="container">
<div class="a">blue</div>
<div class="b">green</div>
</div>
CSS:
.a {
width:100px;
height:400px;
display:inline-block;
background-color:blue;
}
.b {
width:400px;
height:600px;
display:inline-block;
background-color: green;
}
.container {
vertical-align:top;
}
http://jsfiddle.net/xswa4/
vertical-align: top;
doesn't work...
The vertical-align property defines the vertical alignment of an inline element. The "top" value aligns the top of the aligned subtree with the top of the line box. We must apply the vertical-align property to the "small-box" only to make it start at the top of the container.
As a result, the elements can sit next to each other. The major difference between display: inline; and display: inline-block; is that, display: inline-block; also allows us to set the width and height of the element. We can prevent inline-block divs from wrapping by adding suitable Bootstrap classes.
Block: Displays an element as a block element. It starts on a new line and takes up take up as much horizontal space as they can. Block-level elements do not appear in the same line, but breaks the existing line and appear in the next line. Flex: Flex displays an element as a flexible structure.
Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.
You should use vertical-align:top;
on the element .a
itself, not the parent .container
:
.a {
display:inline-block;
background-color:blue;
vertical-align:top;
}
JSFiddle Demo
Your vertical-align
needs to be on the blue container, not the parent container.
.a
{
width:100px;
height:400px;
display:inline-block;
background-color:blue;
vertical-align: top;
}
Fiddle: http://jsfiddle.net/xswa4/3/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With