使用 CSS 在子 Div 之间划分元素的宽度

发布于 2024-11-19 16:08:26 字数 305 浏览 0 评论 0原文

我有不同数量的内联块 div,我希望它们总共占据其父级的 100%。这可以在没有 JavaScript 的情况下完成吗?我能想到的唯一方法是使用表格,但仅将表格用于布局目的当然是不好的做法。

|----------------------|
|{  div 1  }{  div 2  }|
           or
|{div 1}{div  2}{div 3}|
|----------------------|

我也尝试过 { display:block;浮动:左; } 但这似乎没有什么区别。

I have a varying number of inline-block divs that I want to collectively take up 100% of their parent. Can this be done without JavaScript? The only way I can think of is with a table but it's of course bad practice to use a table solely for layout purposes.

|----------------------|
|{  div 1  }{  div 2  }|
           or
|{div 1}{div  2}{div 3}|
|----------------------|

I have also tried { display:block; float:left; } but it doesn't seem to make a difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

薄荷港 2024-11-26 16:08:26

您可以在内部 div 上使用 display:table-cell 来执行此操作。为了让浏览器使内部 div 表现得像表格单元格,它还需要两层包含元素:一层充当表格,另一层充当表格行。

对于这样的结构:

   <div class="outer">
       <div class="middle">
          <div class="inner">Item 1</div> 
          <div class="inner">Item 2</div> 
          <div class="inner">Item 3</div> 
          <div class="inner">Item 4</div> 
       </div>
   </div>

使用此 CSS:

div.outer {display:table;}
div.middle {display:table-row;}
div.inner {display:table-cell;}

一个很好的结构是用 DIV 包装 UL:DIV 充当表格,UL 充当行,LI 充当表格单元格。

这种技术在较旧的浏览器中并没有得到很好的支持 - 对于任何早于 IE8 的浏览器,你完全不走运。

如果您需要更多示例代码,请告诉我!

You can use display:table-cell on your inner divs to do this. For the browser to make the inner divs behave like table cells, it also needs two layers of containing elements: one to acts as the table, and another to act as the table-row.

For a structure like this:

   <div class="outer">
       <div class="middle">
          <div class="inner">Item 1</div> 
          <div class="inner">Item 2</div> 
          <div class="inner">Item 3</div> 
          <div class="inner">Item 4</div> 
       </div>
   </div>

Use this CSS:

div.outer {display:table;}
div.middle {display:table-row;}
div.inner {display:table-cell;}

A nice structure to use is a UL wrapped in a DIV: the DIV acts as a table, the UL as a row, and the LI's as table-cells.

This technique is not well supported in older browsers - for anything older than IE8, you're out of luck entirely.

Let me know if you need more sample code than that!

残疾 2024-11-26 16:08:26

您可以在这里利用 css3 的优势。我也面临这个问题,现在我已经使用下面的示例代码修复了这个问题

.parent-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
  -webkit-justify-content: space-around;
  flex-wrap: nowrap;
  -webkit-flex-wrap: nowrap;
}
.child-item {
  margin: 5px;
  text-align: center;
  padding: 10px;
  background-color: red;
  color: #fff;
}
<ul class="parent-container">
  <li class="child-item">1</li>
  <li class="child-item">2</li>
  <li class="child-item">3</li>
  <li class="child-item">4</li>
  <li class="child-item">5</li>
  <li class="child-item">6</li>
  <li class="child-item">7</li>
</ul>

谢谢&问候,
林格什拉姆

You can utilize css3 benefits here. I was also facing this issue now i have fixed that using below example code

.parent-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
  -webkit-justify-content: space-around;
  flex-wrap: nowrap;
  -webkit-flex-wrap: nowrap;
}
.child-item {
  margin: 5px;
  text-align: center;
  padding: 10px;
  background-color: red;
  color: #fff;
}
<ul class="parent-container">
  <li class="child-item">1</li>
  <li class="child-item">2</li>
  <li class="child-item">3</li>
  <li class="child-item">4</li>
  <li class="child-item">5</li>
  <li class="child-item">6</li>
  <li class="child-item">7</li>
</ul>

Thanks & Regards,
Lingeshram

吖咩 2024-11-26 16:08:26

接受的答案错过了一个重要的CSS属性,这是工作所必需的:

表格布局:固定;

这是正确的答案:

HTML:

<div class="outer">
    <div class="middle">
        <div class="inner">Item 1</div> 
            <div class="inner">Item 2</div> 
            <div class="inner">Item 3</div> 
            <div class="inner">Item 4</div> 
        </div>
    </div>
</div>

CSS:

div.outer {display:table; table-layout: fixed;}
div.middle {display:table-row;}
div.inner {display:table-cell;}

The accepted answer missed an important CSS property which is necessary to work:

table-layout: fixed;

This is the correct answer:

HTML:

<div class="outer">
    <div class="middle">
        <div class="inner">Item 1</div> 
            <div class="inner">Item 2</div> 
            <div class="inner">Item 3</div> 
            <div class="inner">Item 4</div> 
        </div>
    </div>
</div>

CSS:

div.outer {display:table; table-layout: fixed;}
div.middle {display:table-row;}
div.inner {display:table-cell;}
冷︶言冷语的世界 2024-11-26 16:08:26

我想阐述一下@lingeshram 的回答。 Flexbox 已经发展到现在,我认为这确实是现在的解决方法。如果您必须支持旧版浏览器,请务必先检查 caniuse

.container {
  display: flex; /* or inline-flex */
}

.col {
  flex-grow: 1;
  border: 1px solid #000;
}

.col2x {
  flex-grow: 2;
  border: 1px solid #000;
}
Evenly split three children
<div class='container'>
  <span class='col'>Inner 1</span>
  <span class='col'>Inner 2</span>
  <span class='col'>Inner 3</span>
</div>

<br>
Evenly split two children
<div class='container'>
  <span class='col'>Inner 1</span>
  <span class='col'>Inner 2</span>
</div>

<br>
Split three children, but the middle is twice the size of the others
<div class='container'>
  <span class='col'>Inner 1</span>
  <span class='col2x'>Inner 2</span>
  <span class='col'>Inner 3</span>
</div>

这是一个非常好的指南,介绍了您的不同方式可以使用弹性盒。

I'd like to expound on @lingeshram's answer. Flexboxes have come so far that I think it's really the way to do it now. If you have to support old browsers, be sure to check caniuse first.

.container {
  display: flex; /* or inline-flex */
}

.col {
  flex-grow: 1;
  border: 1px solid #000;
}

.col2x {
  flex-grow: 2;
  border: 1px solid #000;
}
Evenly split three children
<div class='container'>
  <span class='col'>Inner 1</span>
  <span class='col'>Inner 2</span>
  <span class='col'>Inner 3</span>
</div>

<br>
Evenly split two children
<div class='container'>
  <span class='col'>Inner 1</span>
  <span class='col'>Inner 2</span>
</div>

<br>
Split three children, but the middle is twice the size of the others
<div class='container'>
  <span class='col'>Inner 1</span>
  <span class='col2x'>Inner 2</span>
  <span class='col'>Inner 3</span>
</div>

Here is a pretty good guide to the different ways you can use flexbox.

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