Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an inline-block box stick to the top? [duplicate]

Tags:

html

css

layout

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...

like image 305
mxcd Avatar asked Feb 13 '14 22:02

mxcd


People also ask

How do you align inline block elements to top?

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.

How do you make inline blocks not wrap?

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.

What is the difference between inline block and flex?

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.

What is the difference between inline block and inline block?

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.


2 Answers

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

like image 116
Hashem Qolami Avatar answered Nov 01 '22 12:11

Hashem Qolami


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/

like image 35
mayabelle Avatar answered Nov 01 '22 11:11

mayabelle