html - Flexbox 计算器没有正确对齐

标签 html css flexbox

<分区>

我确信计算器是一个常见的元素,但我真的很难在这里使用 flexbox。 我为每个计算器按钮行和不同的按钮大小设置了一个行类。但是最后一个按钮将向下一行而不是留在该行的原始行中,即使它们有足够的宽度也是如此。

* {
        box-sizing: border-box;
    }
    
    body {
        margin: 0;
        padding: 0;
    }
    
    .wrapper {
        display: flex;
        flex-direction: column;
        background-color: #0f0f0f;
        color: #fff;
        width: 400px;
        align-content: stretch;
        justify-content: space-between;
        font-family: Helvetica, Arial, sans-serif;
    }
    
        /* ROWS */
    
    .result-row {
        width: 100%;
        font-family: monospace;
        background-color: #0f0f0f;
        text-align: right;
    }
    
    .row {
        width: 100%;
    }
    
        /* BUTTONS */
    
    .small {
        width: 24.5%;
    }
    
    .medium {
        width: 49.5%;
    }
    
    .large {
        width: 74.5%;
    }
    
    .button {
        border: 0;
        border-radius: 0;
    }
<div class="wrapper">
        <div class="result-row">
            0
        </div>
        <div class="row">
            <button class="medium button">C</button>
            <button class="small button">&#8592;</button>
            <button class="small button">&divide;</button>
        </div>
        <div class="row">
            <button class="small button">7</button>
            <button class="small button">8</button>
            <button class="small button">9</button>
            <button class="small button">X</button>
        </div>
        <div class="row">
            <button class="small button">4</button>
            <button class="small button">5</button>
            <button class="small button">6</button>
            <button class="small button">-</button>
        </div>
        <div class="row">
            <button class="small button">1</button>
            <button class="small button">2</button>
            <button class="small button">3</button>
            <button class="small button">+</button>
        </div>
        <div class="row">
            <button class="large button">0</button>
            <button class="small button">=</button>
        </div>
</div>

我真的不明白这里发生了什么,我认为只要容器中有足够的空间,div 中的元素就不能在带有 flexbox 的新“行”中占用空间。

最佳答案

由于 flex children 只继承了 parent 的一层,所以你还需要在 .row 上设置 flex display。

.row {
  width: 100%;
  display: flex;
  flex-wrap: nowrap;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}

.wrapper {
  display: flex;
  flex-direction: column;
  background-color: #0f0f0f;
  color: #fff;
  width: 400px;
  align-content: stretch;
  justify-content: space-between;
  font-family: Helvetica, Arial, sans-serif;
}

/* ROWS */

.result-row {
  width: 100%;
  font-family: monospace;
  background-color: #0f0f0f;
  text-align: right;
}

.row {
  width: 100%;
  display: flex;
  flex-wrap: nowrap;
}

/* BUTTONS */

.small {
  width: 24.5%;
}

.medium {
  width: 49.5%;
}

.large {
  width: 74.5%;
}

.button {
  border: 0;
  border-radius: 0;
}
<div class="wrapper">
  <div class="result-row">
    0
  </div>
  <div class="row">
    <button class="medium button">C</button>
    <button class="small button">&#8592;</button>
    <button class="small button">&divide;</button>
  </div>
  <div class="row">
    <button class="small button">7</button>
    <button class="small button">8</button>
    <button class="small button">9</button>
    <button class="small button">X</button>
  </div>
  <div class="row">
    <button class="small button">4</button>
    <button class="small button">5</button>
    <button class="small button">6</button>
    <button class="small button">-</button>
  </div>
  <div class="row">
    <button class="small button">1</button>
    <button class="small button">2</button>
    <button class="small button">3</button>
    <button class="small button">+</button>
  </div>
  <div class="row">
    <button class="large button">0</button>
    <button class="small button">=</button>
  </div>
</div>

enter image description here

jsFiddle

关于html - Flexbox 计算器没有正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60537992/

相关文章:

javascript - 如何在html5视频中添加背景音乐?

javascript - Chrome扩展注入(inject)js

html - 围绕位置 :static in setting z-index 工作

css - NavBar 中的 nav-item 下拉列表,有没有办法让它在使用 btn-outline 时显示边框?

css - Firefox 错误或错误的 flex 样式?

ios - "display: flex"和 "align-items: center"怎么可能在我的 iphone 上不再工作了?

html - 两个独立的 div,为什么一个渲染在另一个里面?

javascript - 有没有办法在 MS Internet Explorer 中实现 HTML5 <keygen> 功能?

css - 为什么这个固定的标题在滚动时会移动?

html - 用 flex 连接对 Angular 线 div