简体   繁体   English

CSS Flexbox 列溢出

[英]CSS Flexbox columns are overflowing

I want to build a layout with CSS flexbox and I got into a problem while building a calculator.我想用 CSS flexbox 构建一个布局,但在构建计算器时遇到了问题。

I have a #controller that contains all my buttons on the calculator.我有一个#controller ,其中包含我在计算器上的所有按钮。 The "=" button's size is twice the other buttons (vertically). “=”按钮的大小是其他按钮的两倍(垂直方向)。

The bottom of the layout is like this (I cant upload pictures)...排版底部是这样的(我上传不了图片)...

| | 4 | 4 | 5 | 5 | 6 | 6 | / | / |
| | 1 | 1 | 2 | 2 | 3 | 3 | = | = |
|. |。 | | 0 | 0 | % | % | = | = |

So I created div "rows" for the normal buttons in which they are set to flex-grow: 1;所以我为普通按钮创建了 div“行”,其中它们被设置为flex-grow: 1; so it stays responsive to the width.所以它对宽度保持响应。

I made a div container called ".bottom" for the left and right "columns".我为左右“列”制作了一个名为“.bottom”的div 容器。 The left contains the rows and the normal sized buttons, and the right contains the "=" button.左边包含行和正常大小的按钮,右边包含“=”按钮。

Problem:问题:
Both columns inside the .bottom part are overflowing from the #controller with their content. .bottom部分内的两列都从#controller中溢出了它们的内容。

I don't necessarily want to wrap my layout.我不一定要包装我的布局。 I figured out maybe I should create only columns, not rows, but before that I wanted to ask for advice.我想也许我应该只创建列,而不是行,但在此之前我想征求意见。 Thank you in advance!先感谢您!

HTML bottom part: HTML 底部:

<div id="calculator">
  <!-- screen of calculator -->
  <div id="controller">
    <!-- upper part of button rows -->
    <div class="bottom">
      <div class="columnLeft">
        <div class="row">
          <div class="button">1</div>
          <div class="button">2</div>
          <div class="button">3</div>
        </div>
        <div class="row">
          <div class="button">.</div>
          <div class="button">0</div>
          <div class="button">%</div>
        </div>
      </div>
      <div class="columnRight">
        <div class="longButton">=</div>
      </div>
    </div>
  </div>
</div>

CSS CSS

#calculator, #controller {
  display: flex;
  flex-direction: column;
}

#calculator {
  margin: auto;
  width: 50%;
}

#controller .row {
  display: flex;
}

#controller .bottom {
  display: flex;
  flex-direction: row;
}

#controller .bottom .columnLeft, #controller .bottom .columnRight {
  display: flex;
}

#controller .bottom .columnLeft {
  flex-grow: 3;
  flex-direction: column;
}

#controller .bottom .columnRight {
  flex-grow: 1;
}

.button, .longButton{
  flex-grow: 1;
}

Just place all the buttons in a CSS-Grid .只需将所有按钮放在CSS-Grid中。 To let an element occupy 2 rows you can use grid-row: span 2 on that element.要让一个元素占据 2 行,您可以在该元素上使用grid-row: span 2

 .calculator-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.5em; max-width: 200px; }.row-2 { grid-row: span 2; }
 <div class="calculator-grid"> <button>4</button> <button>5</button> <button>6</button> <button>/</button> <button>1</button> <button>2</button> <button>3</button> <button class="row-2">=</button> <button>.</button> <button>0</button> <button>%</button> <!-- empty for equal button --> </div>

CSS grid is the way to go here. CSS 格子是这里通往 go 的路。 You can use grid-template areas to span cells like the calculator example I've done below.您可以使用网格模板区域来跨越单元格,就像我在下面完成的计算器示例一样。 There's a great primer here and a good video by Kevin Powell here这里有一本很棒的入门书,还有凯文·鲍威尔 (Kevin Powell)精彩视频

I've knocked up an example to get you started.我已经举了一个例子来帮助你开始。

 .bottom { display: grid; width: 50%; grid-template-columns: repeat(4, 1fr); grid-template-areas: "b4 b5 b6 bdiv""b1 b2 b3 bequals""bdot b0 bpercent bequals"; gap: 0.5rem; }.bottom>div { /* aspect-ratio: 1; */ background-color: lightgray; display: grid; place-items: center; padding-block:1rem; transition: background-color 0.2s; cursor: pointer; }.bottom>div:hover { background-color:darkgray; }.button-1 { grid-area: b1; }.button-2 { grid-area: b2; }.button-3 { grid-area: b3; }.button-4 { grid-area: b4; }.button-5 { grid-area: b5; }.button-6 { grid-area: b6; }.button-div { grid-area: bdiv; }.button-percent { grid-area: bpercent; }.button-dot { grid-area: bdot; }.button-long { grid-area: bequals; }
 <div id="calculator"> <.-- screen of calculator --> <div id="controller"> <!-- upper part of button rows --> <div class="bottom"> <div class="button-4">4</div> <div class="button-5">5</div> <div class="button-6">6</div> <div class="button-div">/</div> <div class="button-1">1</div> <div class="button-2">2</div> <div class="button-3">3</div> <div class="button-dot">.</div> <div class="button-0">0</div> <div class="button-percent">%</div> <div class="button-long">=</div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM