admin管理员组文章数量:1354714
I am trying to place 2 divs with different height next to each other horizontally that are in a div. With my code, the second div box
stays at the bottom of the first div square
.
box
's width and height are set BUT with square
div, height and width are not set and I don't want to set it.
With this condition, how do achieve the result that I want? and If this cannot be done with CSS, using Jquery is fine as well.
HTML
<div id="wrapper">
<div id="square"></div>
<div id="box">Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text</div>
</div>
CSS
#wrapper {
display: inline-block;
border: 1px solid black;
}
#square {
float: left;
width: 50px;
height: 50px;
border: 1px solid blue;
display: inline-block;
}
#box {
border: 1px solid red;
float: right;
display: inline-block;
}
DEMO
jsfiddle
I am trying to place 2 divs with different height next to each other horizontally that are in a div. With my code, the second div box
stays at the bottom of the first div square
.
box
's width and height are set BUT with square
div, height and width are not set and I don't want to set it.
With this condition, how do achieve the result that I want? and If this cannot be done with CSS, using Jquery is fine as well.
HTML
<div id="wrapper">
<div id="square"></div>
<div id="box">Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text</div>
</div>
CSS
#wrapper {
display: inline-block;
border: 1px solid black;
}
#square {
float: left;
width: 50px;
height: 50px;
border: 1px solid blue;
display: inline-block;
}
#box {
border: 1px solid red;
float: right;
display: inline-block;
}
DEMO
jsfiddle
Share Improve this question asked Jul 16, 2017 at 5:37 H.KimH.Kim 2771 gold badge11 silver badges25 bronze badges 1- try to remove /* float: right; / / display: inline-block; */ from div with id box – knizer Commented Jul 16, 2017 at 5:43
4 Answers
Reset to default 6Try this with flexbox.
JSfiddle. CanIUse.
#wrapper {
border: 1px solid black;
display: flex;
}
#square {
min-width: 50px;
height: 50px;
border: 1px solid blue;
}
#box {
border: 1px solid red;
}
<div id="wrapper">
<div id="square"></div>
<div id="box">Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text </div>
</div>
You can use flexbox. Remove your floats and inline blocks. Wrap the boxes with display:flex. This makes immediate children flexible along the plane specified (flex-direction: row
). Specify the box that shrinks as flex:1
.
#wrapper {
display: flex;
flex-direction: row;
border: 1px solid black
}
#square {
width: 50px;
height: 50px;
border: 1px solid blue;
}
#box {
border: 1px solid red;
flex: 1;
}
https://jsfiddle/nj163q24/
There are a couple of ways to do this. If you change your html slightly, you can do something like this:
#wrapper {
display: inline-block;
border: 1px solid black;
}
#square {
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 50px;
border: 1px solid blue;
}
#box {
border: 1px solid red;
position: relative;
top: 0;
padding-left: 65px;
}
<div id="wrapper">
<div id="box">
<div id="square"></div>
Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text </div>
</div>
Just Edit #box
like :
#box {
border: 1px solid red;
float: right;<---------Remove
display: inline-block;<---------Remove
margin-left: 50px; <---------Added
}
* {
box-sizing: border-box;
}
#wrapper {
display: inline-block;
border: 1px solid black;
}
#square {
float: left;
width: 50px;
height: 50px;
border: 1px solid blue;
display: inline-block;
}
#box {
border: 1px solid red;
margin-left: 50px;
}
<div id="wrapper">
<div id="square"></div>
<div id="box">Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long textVery long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text</div>
</div>
本文标签: javascriptCSS horizontally align two divs with different heightStack Overflow
版权声明:本文标题:javascript - CSS horizontally align two divs with different height - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743971317a2570700.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论