如何正确围绕内容(间距)制作DIV包裹?

发布于 2025-01-21 13:17:20 字数 1046 浏览 1 评论 0原文

我正在尝试根据其遇到的其他内容自动更改宽度。它应该很简单,但是我的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.

enter image description here

What I'm trying to achieve

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅 2025-01-28 13:17:20

删除任何float's。然后将flex设置为父母,并定义每个flex项目的宽度。在您的版本中,psubheader尚未定义宽度,并被带入浮动div。我要做的是使用文本元素(无论您喜欢哪个),然后在该元素上设置边框,然后限制宽度。

.box {
  width: 100%;
  height: 1000px;
  border: 1px solid;
  display: flex;
  flex-flow: row-reverse;
}

.psubheader {
  padding-bottom: 7px;
  font-family: 'sans-serif';
  font-size: 1.35em;
  width: 50%;
  border-bottom: 1px solid #a2a9b1;
  margin-left: 10px;
  margin-right: -10px;
}

.side {
  width: 50%;
  height: 1000px;
  background-color: red;
}

h5 {
  margin: 0;
  border-bottom: solid 1px #a2a9b1;
  width: 95%;
}
<div class="box">
  <div class="side"></div>
  <div class="psubheader">
    <h5 class="tst">Test</h5>
  </div>
</div>

Remove any float's. Then set flex to the parent and define the widths one each flex item. In your version, the psubheader 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.

.box {
  width: 100%;
  height: 1000px;
  border: 1px solid;
  display: flex;
  flex-flow: row-reverse;
}

.psubheader {
  padding-bottom: 7px;
  font-family: 'sans-serif';
  font-size: 1.35em;
  width: 50%;
  border-bottom: 1px solid #a2a9b1;
  margin-left: 10px;
  margin-right: -10px;
}

.side {
  width: 50%;
  height: 1000px;
  background-color: red;
}

h5 {
  margin: 0;
  border-bottom: solid 1px #a2a9b1;
  width: 95%;
}
<div class="box">
  <div class="side"></div>
  <div class="psubheader">
    <h5 class="tst">Test</h5>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文