P粉2037924682023-08-22 12:35:59
You need to add the vertical-align
attribute to both child divs.
If .small
is always shorter, just apply this property to .small
.
However, this property should be applied to both .small
and .big
if any of them may be the highest.
.container{ border: 1px black solid; width: 320px; height: 120px; } .small{ display: inline-block; width: 40%; height: 30%; border: 1px black solid; background: aliceblue; vertical-align: top; } .big { display: inline-block; border: 1px black solid; width: 40%; height: 50%; background: beige; vertical-align: top; }
Vertical alignment affects inline or table cell boxes. This property has many different values. See https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align for more details.
P粉8232680062023-08-22 09:52:17
Because by default, vertical-align
is set to baseline.
Use insteadvertical-align:top
:
.small{ display: inline-block; width: 40%; height: 30%; border: 1px black solid; background: aliceblue; vertical-align:top; /* <---- this */ }
http://jsfiddle.net/Lighty_46/RHM5L/9/
Or as @f00644 said, you can also apply float
to child elements.