如何正确围绕内容(间距)制作DIV包裹?
我正在尝试根据其遇到的其他内容自动更改宽度。它应该很简单,但是我的DIV拒绝宽度不到100%。我已经尝试了多种方法来做到这一点。
最终,我试图使Div在左右两侧有一点间距(因此边框是整洁的),但是Div在另一个Divs下方。
我要实现的目标
我得到的(副标题为100%宽度及以下),
我尝试了许多事情来解决此问题,并使用Overflow:隐藏,漂浮:左...什么都没有。
下面的代码。
<div class="box">
<div class="side"></div>
<div class="psubheader">Test</div>
</div>
<style>
.box {
width: 50%;
height: 1000px;
border:1px solid;
}
.psubheader {
padding-bottom:7px;
font-family: 'sans-serif';
font-size: 1.35em;
border-bottom: 1px solid #a2a9b1;
margin-left:10px;
margin-right:-10px;
}
.side {
width: 50%;
height: 1000px;
background-color:red;
float:right;
}
感谢您的帮助。
I'm trying to make a div automatically change width based on other content it encounters. It should be straightforward, but my div refuses to be anything less than 100% width. I've tried a number of ways to do this.
Ultimately I'm trying to make the div have a little spacing on the left and right sides (so the border is neater), but the div goes under the other divs.
What I'm trying to achieve
What I get (subheader is 100% width and underneath)
I've tried a number of things to fix this, using overflow:hidden, float:left... Nothing does it.
Code below.
<div class="box">
<div class="side"></div>
<div class="psubheader">Test</div>
</div>
<style>
.box {
width: 50%;
height: 1000px;
border:1px solid;
}
.psubheader {
padding-bottom:7px;
font-family: 'sans-serif';
font-size: 1.35em;
border-bottom: 1px solid #a2a9b1;
margin-left:10px;
margin-right:-10px;
}
.side {
width: 50%;
height: 1000px;
background-color:red;
float:right;
}
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除任何
float
's。然后将flex
设置为父母,并定义每个flex项目的宽度。在您的版本中,psubheader
尚未定义宽度,并被带入浮动div。我要做的是使用文本元素(无论您喜欢哪个),然后在该元素上设置边框,然后限制宽度。Remove any
float
's. Then setflex
to the parent and define the widths one each flex item. In your version, thepsubheader
had no width defined and was carried into the floated div. What I would do is use a text element (whichever you prefer) and set the border on that element, then restrict the width.