没有表格的 3 动态宽度列布局

发布于 2024-12-08 12:46:05 字数 456 浏览 0 评论 0原文

我试图实现以下布局,但没有成功:

在此处输入图像描述

要求是:

  1. 所有三列,红色,绿色和蓝色必须具有动态宽度,即它们的宽度必须根据其内容而变化。
  2. 灰色容器必须居中,其宽度也必须是动态的,具体取决于三列的内容。
  3. 绿色列的 HTML 代码必须位于源代码中其他两列之前。

除了第三个要求之外,使用表格就像蛋糕一样简单,但我无法使用 DIV 和 CSS 来做到这一点。 我发现的最接近的是“完美的 3 列液体布局(百分比宽度)< /a>”,但它有百分比宽度,这不适合我的需要。

I'm trying to achieve the following layout without success:

enter image description here

The requirements are:

  1. All three columns, red, green and blue, must have dynamic widths, i.e. their width must change according to their content.
  2. The gray container must be centered and its width must be dynamic as well, depending on the content of the three columns.
  3. The HTML code for the green column must be before the other two columns in the source code.

This is easy as cake using tables, except for the third requirement, but I just can't manage to do it using DIVs and CSS.
The closest thing I've found is "The Perfect 3 Column Liquid Layout (Percentage widths)", but it has percentage widths, which doesn't suit what I need.

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

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

发布评论

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

评论(2

雨轻弹 2024-12-15 12:46:05

如今,使用 Flex-box 可以轻松实现这一点。
干得好。无需使用 widthheight 属性即可满足您的所有 3 个要求。所有容器都会根据其内容进行调整。

body {text-align:center; font: 16px arial}
wrap {display: inline-flex;  flex-direction: column; background: lightgrey;}
header {margin-bottom: 10px; background: white; }
footer {margin-top: 10px; background: white}
content {display: flex; flex-direction: row-reverse}
ctnr {display: flex;}
centerCol {margin: 0 10px; background: green}
rightCol {background: blue}
leftCol {background: red}
[pad] {padding: 10px; border: 1px solid black;}
<wrap pad>
<header pad>Header</header>
<content>
<ctnr>
<centerCol pad><br><br><br>Center column<br><br><br><br><br><br><br></centerCol>
<rightCol pad>Right</rightCol>
</ctnr>
<ctnr>
<leftCol pad>Left</leftCol>
</ctnr>
</content>
<footer pad>Footer</footer>
</wrap>

This is easily doable nowadays with flex-box.
Here you go. All your 3 requirements are met without a single use of the properties width or height. All containers adjust to their content.

body {text-align:center; font: 16px arial}
wrap {display: inline-flex;  flex-direction: column; background: lightgrey;}
header {margin-bottom: 10px; background: white; }
footer {margin-top: 10px; background: white}
content {display: flex; flex-direction: row-reverse}
ctnr {display: flex;}
centerCol {margin: 0 10px; background: green}
rightCol {background: blue}
leftCol {background: red}
[pad] {padding: 10px; border: 1px solid black;}
<wrap pad>
<header pad>Header</header>
<content>
<ctnr>
<centerCol pad><br><br><br>Center column<br><br><br><br><br><br><br></centerCol>
<rightCol pad>Right</rightCol>
</ctnr>
<ctnr>
<leftCol pad>Left</leftCol>
</ctnr>
</content>
<footer pad>Footer</footer>
</wrap>

沉默的熊 2024-12-15 12:46:05

我找到了如何将中心列放在代码中

http://jsfiddle.net/gamepreneur/bj6bU/< /a>

html

<div class="main">
    <div class="right-float">
        <div class="green">
            green
        </div>
        <div class="blue">
            blue
        </div>
    </div>
    <div class="red">
        red
    </div>
</div>

css

.right-float {
    float:right
}
.green {
    float:left;
}
.blue {
    float:right
}
.red {
    float:left;
}

我只是不知道如何做剩下的...而且现在是中午 12:40(在我写这篇文章的时候)

i found how to put the the center column first in the code

http://jsfiddle.net/gamepreneur/bj6bU/

html

<div class="main">
    <div class="right-float">
        <div class="green">
            green
        </div>
        <div class="blue">
            blue
        </div>
    </div>
    <div class="red">
        red
    </div>
</div>

css

.right-float {
    float:right
}
.green {
    float:left;
}
.blue {
    float:right
}
.red {
    float:left;
}

I'm just not sure how to do the rest... plus it's 12:40pm (at the time that i am writing this)

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