CSS弹性特性和弹性盒子

huangapple go评论66阅读模式
英文:

css flex feature and flexbox

问题

如果我有一个包含三个 div 元素的容器 div,我想使用 flex 特性使 div1div2 并排,div3 放在它们下方。

无论宽度或高度如何,它始终分成三个部分。

<div id="container">
  <div id="div1">这是 Div 1</div>
  <div id="div2">这是 Div 2</div>
  <div id="div3">这是 Div 3</div>
</div>

如上所示。

英文:

If I have a container div and inside it, there are 3 divs, I want to make div1 and div2 next to each other and div3 below with the flex feature.

It is always divided into three parts, regardless of width or height.

&lt;div id=&quot;container&quot;&gt;
  &lt;div id=&quot;div1&quot;&gt;This is Div 1&lt;/div&gt;
  &lt;div id=&quot;div2&quot;&gt;This is Div 2&lt;/div&gt;
  &lt;div id=&quot;div3&quot;&gt;This is Div 3&lt;/div&gt;
&lt;/div&gt;

As you can see above.

答案1

得分: 2

你尝试过使用CSS的grid布局吗?

*, *::before, *::after { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
body { display: flex; }

#container {
  display: grid;
  grid-template-columns: repeat(2, auto);
  flex: 1;
}

#div1 { background: cyan; }
#div2 { background: yellow; }
#div3 { background: magenta; grid-column: span 2; }
<div id="container">
  <div id="div1">This is Div 1</div>
  <div id="div2">This is Div 2</div>
  <div id="div3">This is Div 3</div>
</div>
英文:

Have you tried the CSS grid layout?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

*, *::before, *::after { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
body { display: flex; }

#container {
  display: grid;
  grid-template-columns: repeat(2, auto);
  flex: 1;
}

#div1 { background: cyan; }
#div2 { background: yellow; }
#div3 { background: magenta; grid-column: span 2; }

<!-- language: lang-html -->

&lt;div id=&quot;container&quot;&gt;
  &lt;div id=&quot;div1&quot;&gt;This is Div 1&lt;/div&gt;
  &lt;div id=&quot;div2&quot;&gt;This is Div 2&lt;/div&gt;
  &lt;div id=&quot;div3&quot;&gt;This is Div 3&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

使用flex属性和flex-wrap属性,您可以将flex属性添加到容器中,以便div元素可以换行。

* {
  box-sizing: border-box;
}

#container {
  display: flex;
  flex-wrap: wrap;
}

#container>div {
  border: 1px solid #000;
}

#div1,
#div2 {
  flex: 0 1 50%;
}

#div3 {
  flex-grow: 1;
}
<div id="container">
  <div id="div1">This is Div 1</div>
  <div id="div2">This is Div 2</div>
  <div id="div3">This is Div 3</div>
</div>
英文:

Using flex you can add flex property to your container with flex-wrap so your div can goes to the new line.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  box-sizing: border-box;
}

#container {
  display: flex;
  flex-wrap: wrap;
}

#container&gt;div {
  border: 1px solid #000;
}

#div1,
#div2 {
  flex: 0 1 50%;
}

#div3 {
  flex-grow: 1;
}

<!-- language: lang-html -->

&lt;div id=&quot;container&quot;&gt;
  &lt;div id=&quot;div1&quot;&gt;This is Div 1&lt;/div&gt;
  &lt;div id=&quot;div2&quot;&gt;This is Div 2&lt;/div&gt;
  &lt;div id=&quot;div3&quot;&gt;This is Div 3&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月12日 23:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定