【问题标题】:Use flexbox to create a responsive wrappable row of buttons使用 flexbox 创建响应式可包装的按钮行
【发布时间】:2020-12-11 09:02:08
【问题描述】:

我正在尝试使一排按钮响应。一行中可以有 3 个或 2 个按钮。我认为 flexbox(或者可能是网格)是解决方案的原因是因为按钮中的文本是国际化的,并且语言会有所不同,这意味着它们将根据按钮中的文本进行换行,而不仅仅是屏幕宽度,所以我可以'不要只编写与屏幕宽度相对应的媒体查询。

这些是没有包装的初始状​​态: 起始状态(无换行)是以下任一图像(取决于 2 或 3 个按钮):

当容器变得太小时,这些是所需的包装状态:

我创建了一个小提琴,允许您调整框的宽度并在此处切换可选按钮:

See Fiddle here to test each variant

这是开始的 html,但也许将前两个按钮包装在一起会有所帮助


    <div class="container">
      <div class="button button1">
        This is a longer button
      </div>
      <div class="button button2">
        Optional Button
      </div>
      <div class="button button3">
        Confirmation button
      </div>
    </div>
    <button class="width-button">Toggle width</button>
    <button class="toggle-button-button">Toggle optional button</button

>

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    如果您的布局是固定的,并且您确实在使用 .narrow 类,您可以这样做,

    document.querySelector('.width-button').addEventListener('click', () => {
      document.querySelector('.container').classList.toggle('narrow');
    });
    
    document.querySelector('.toggle-button-button').addEventListener('click', () => {
      document.querySelector('.button1').classList.toggle('hide');
    });
    .container {
      width: 580px;
      background-color: lightgray;
      margin: 40px 0;
      padding: 12px;
      transition: width 200ms ease-in-out;
      display: flex;
      flex-wrap: wrap;
    }
    
    .container.narrow {
      width: 320px;
    }
    
    .button {
      padding: 12px;
      background-color: green;
      display: inline-block;
      border-radius: 8px;
      margin: 5px
        /* demo */
    }
    
    .button1.hide+.button2 {
      display: none;
    }
    
    .button3 {
      margin-left: auto;
    }
    
    
    .container.narrow .button1.hide,
    .container.narrow .button3 {
      flex: 0 100%;
    }
    <div class="container">
      <div class="button button1">
        This is a longer button
      </div>
      <div class="button button2">
        Optional Button
      </div>
      <div class="button button3">
        Confirmation button
      </div>
    </div>
    <button class="width-button">Toggle width</button>
    <button class="toggle-button-button">Toggle optional button</button>

    否则,您可以使用媒体查询更改 .narrow 类,如下所示:

    document.querySelector('.toggle-button-button').addEventListener('click', () => {
      document.querySelector('.button1').classList.toggle('toggle');
    });
    body {
      margin: 0
    }
    
    .container {
      width: 100%;
      background-color: lightgray;
      margin: 40px 0;
      padding: 12px;
      box-sizing: border-box;
      transition: width 200ms ease-in-out;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
    }
    
    @media (min-width: 600px) {
      .container {
        justify-content: flex-start
      }
    }
    
    .button {
      padding: 12px;
      background-color: green;
      display: inline-block;
      border-radius: 8px;
      /* demo */
      margin: 5px;
    }
    
    .toggle+.button2 {
      display: none;
    }
    
    .button1.toggle {
      flex: 1;
    }
    
    @media (min-width: 600px) {
      .button1.toggle{
        flex: 0 auto;
      }
    }
    
    .button3 {
      flex: 0 100%;
    }
    
    @media (min-width: 600px) {
      .button3 {
        margin-left: auto;
        flex: 0 auto;
      }
    }
    <div class="container">
      <div class="button button1">
        This is a longer button
      </div>
      <div class="button button2">
        Optional Button
      </div>
      <div class="button button3">
        Confirmation button
      </div>
    </div>
    <button class="toggle-button-button">Toggle optional button</button>

    【讨论】:

    • 抱歉,.narrow 类只是我应用到容器中的东西,以显示会折叠按钮的宽度,这不是我实际可以使用的东西。我在问题中提到按钮将根据查看应用程序的语言具有不同的宽度,因此文本换行以不同的宽度发生,因此无法进行媒体查询。我将更新问题以澄清 .narrow 类只是为了显示较小的状态。
    • 我刚刚更新了 fiddle 以获得关于 .narrow 类的 cmets。很抱歉造成混乱。
    • @jmancherje 我已经更新了每个按钮的 auto 宽度的答案。我仍然认为您需要为此使用媒体查询
    • @jmancherje 我已经用媒体查询解决方案更新了答案,看看,记住使用的断点是随机的,所以更适合你使用
    【解决方案2】:

    以及使用 flexbox 的想法。调整两个容器的大小以查看结果

    .container {
      width: 480px;
      background-color: lightgray;
      margin: 40px 0;
      padding: 12px;
      overflow:hidden;
      resize:horizontal;
      display:flex;
      flex-wrap:wrap;
      z-index:0;
      position:relative;
    }
    
    
    .button {
      padding: 12px;
      background-color: green;
      display: inline-block;
      border-radius: 8px;
      margin:2px;
      position:relative;
    }
    
    .button3 {
      flex-grow:1; /* grow on wrap  */
      order:2; /* put after the pseudo element*/
    }
    
    /* this will push the confirmation button 
    and will complete the first button on wrap */
    .container::after {
      content:"";
      flex-grow:999; /* push the last button */
      flex-basis:1px;
      background:green;
      border-radius:8px;
      position:relative;
      margin:2px 2px 2px -20px;
      z-index:-3;
    }
    
    /* hide the pseudo element when confirmation button on the first row */
    .button3::before {
      content:"";
      position:absolute;
      z-index:-1;
      top:0;
      bottom:0;
      right:100%;
      width:100vw;
      background:lightgray;
    }
    
    /* make sure the button area is big enough for the events*/
    .button::after {
       content:"";
       position:absolute;
       z-index:-2;
       top:0;
       bottom:0;
       left:100%;
       width:100vw;
    }
    <div class="container">
      <div class="button button1">
        This is a longer button
      </div>
      <div class="button button2">
        Optional Button
      </div>
      <div class="button button3">
        Confirmation button
      </div>
    </div>
    
    <div class="container">
      <div class="button button1">
        This is a longer button
      </div>
      <div class="button button2" style="display:none;">
        Optional Button
      </div>
      <div class="button button3">
        Confirmation button
      </div>
    </div>

    【讨论】:

    • 在调整大小时不起作用,没有任何变化。仅在更改 width 时才有效,并且第二个示例无法按 OP 的方式工作。不是第一个示例,因为 OP 想要具有 auto 宽度且宽度不同的按钮
    猜你喜欢
    • 2019-10-15
    • 2022-01-12
    • 2019-08-24
    • 1970-01-01
    • 2018-06-22
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多