简体   繁体   中英

display: inline-block not working correctly

I have two divs which I would like to have placed next to each other. This works when I give both divs the display: inline-block; property, but once I add a <p> tag into one of the divs the placement of that div goes out of whack. This is what i'm working with:

HTML:

<div class = "icon_container">
    <button><img src="images/favorite.png" class = "profile_icons"/></button><p>1234</p>
</div>
<div class = "icon_container">
    <button><img src="images/tool.png" class = "profile_icons"/></button>
</div>

CSS:

.icon_container {
    height: 150px;
    display: inline-block;
}

Fiddle:

https://jsfiddle.net/qLysghjf/

Try adding a vertical align to your icon_container class:

.icon_container {
    height: 100px;
    display: inline-block;
    background-color: red;
    vertical-align: top;
}

https://jsfiddle.net/qLysghjf/2/

Well, according to this post by @robertnyman, to make inline-block element vertically align right, it needs vertical-align: top; . I had tried in your fiddle get the result as: https://jsfiddle.net/qLysghjf/3/

So the css is:

.icon_container {
  height: 100px;
  display: inline-block;
  background-color: red;
  vertical-align: top;
}

Please add float:left in .icon_container css class. Hope this will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM