【问题标题】:Control flex-box overflow控制弹性盒溢出
【发布时间】:2018-08-23 07:03:16
【问题描述】:

我正在尝试在我的网站上创建一个包含 4 个个人资料框的个人资料部分:

它在大屏幕上看起来不错,但我试图控制盒子在较小的屏幕尺寸上的包装方式。目前,当盒子到达屏幕边缘时,它们会包裹到下一行,所以我最终在一行上有 3 个配置文件,在下一行有 1 个:

我想阻止这种情况发生,并根据屏幕大小设置 3 种不同的配置:

这可以通过弹性盒子实现吗?

.container {
    margin-bottom: 10px;
    margin-top: 10px;
    padding: 10px;
    align-items: flex-start;
    flex-basis: auto;
}

.flex-container {
    margin: 1px;
    list-style: none;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: row wrap;
    justify-content: space-between;
}

.flex-item {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
    transition: 0.1s;
    -webkit-filter: opacity(60%);
    /* Safari 6.0 - 9.0 */
    filter: opacity(60%);
    width: 231px;
    height: 350px;
    margin-top: 10px;
    line-height: 10px;
    color: white;
    font-weight: bold;
    text-align: center;
}

.flex-item h4 {
    font-size: 22pt;
}

.flex-item p {
    font-size: 14pt;
}

.flex-item:hover {
    box-shadow: 0 12px 24px 0 rgba(0, 0, 0, 0.4);
    -webkit-filter: opacity(100%);
    /* Safari 6.0 - 9.0 */
    filter: opacity(100%);
}


.person1 {
    background: #FB8B4F;
}

.person2 {
    background: #1FACE4;
}

.person3 {
    background: #EF626B;
}

.person4 {
    background: #8BE41F;
}

/*When screen is < 1000px : green */
@media screen and (max-width: 1000px) {
   /*  body {
        background-color: lightgreen;
    } */
    .flex-item {
        transform: scale(0.9);
    }
}

/*When screen is < 500px : blue */
@media screen and (max-width: 800px) {
    .flex-item {
      flex-direction: column;
      align-items:center;
    }
   /*  body {
        background-color: lightblue;
    } */
}
<!--Profiles-->
        <div class="container">
            <hr size="0.5">
            <h1 id="people">People</h1>
            <ul class="flex-container">
                <li class="flex-item person1">
                    <!-- <img src="images/profile_placeholder.gif" alt="p1"> -->

                    <h4><b>Person1</b></h4>
                    <p>Founder</p>
                </li>
                <li class="flex-item person2">
                    <!-- <img src="images/profile_placeholder.gif" alt="p2"> -->
                    <h4><b>Person2</b></h4>
                    <p>Treasurer</p>
                </li>
                <li class="flex-item person3">
                    <!-- <img src="images/profile_placeholder.gif" alt="p3"> -->
                    <h4><b>Person3</b></h4>
                    <p>Vice-president</p>
                </li>
                <li class="flex-item person4">
                    <!-- <img src="images/profile_placeholder.gif" alt="p4"> -->

                    <h4><b>Person4</b></h4>
                    <p>Secretary</p>
                </li>
            </ul>
        </div>

【问题讨论】:

  • 我想最简单的解决方案是使用 2/3 媒体查询并定义其中框的宽度
  • See this post。基本上,您要么必须不断完善您的媒体查询,要么使用 Javascript 来检测何时发生换行并执行操作。
  • 在你的媒体查询中定义不同的宽度

标签: html css flexbox


【解决方案1】:

尝试以下方法:

.container {
    margin-bottom: 10px;
    margin-top: 10px;
    padding: 10px;
    align-items: flex-start;
    flex-basis: auto;
}

.flex-container {
    margin: 1px;
    list-style: none;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: row wrap;
    justify-content: space-between;
}

.flex-item {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
    transition: 0.1s;
    -webkit-filter: opacity(60%);
    /* Safari 6.0 - 9.0 */
    filter: opacity(60%);
    width: 100%; /* changed */
    height: 350px;
    margin-top: 10px;
    line-height: 10px;
    color: white;
    font-weight: bold;
    text-align: center;
}

/* Added these line */
@media only screen and (min-width: 600px) {
  .flex-item {
    width: 50%;
  }
}

@media only screen and (min-width: 900px) {
  .flex-item {
    width: 25%;
  }
}/* To here */

.flex-item h4 {
    font-size: 22pt;
}

.flex-item p {
    font-size: 14pt;
}

.flex-item:hover {
    box-shadow: 0 12px 24px 0 rgba(0, 0, 0, 0.4);
    -webkit-filter: opacity(100%);
    /* Safari 6.0 - 9.0 */
    filter: opacity(100%);
}


.person1 {
    background: #FB8B4F;
}

.person2 {
    background: #1FACE4;
}

.person3 {
    background: #EF626B;
}

.person4 {
    background: #8BE41F;
}

/* Removed lines here */
<!--Profiles-->
        <div class="container">
            <hr size="0.5">
            <h1 id="people">People</h1>
            <ul class="flex-container">
                <li class="flex-item person1">
                    <!-- <img src="images/profile_placeholder.gif" alt="p1"> -->

                    <h4><b>Person1</b></h4>
                    <p>Founder</p>
                </li>
                <li class="flex-item person2">
                    <!-- <img src="images/profile_placeholder.gif" alt="p2"> -->
                    <h4><b>Person2</b></h4>
                    <p>Treasurer</p>
                </li>
                <li class="flex-item person3">
                    <!-- <img src="images/profile_placeholder.gif" alt="p3"> -->
                    <h4><b>Person3</b></h4>
                    <p>Vice-president</p>
                </li>
                <li class="flex-item person4">
                    <!-- <img src="images/profile_placeholder.gif" alt="p4"> -->

                    <h4><b>Person4</b></h4>
                    <p>Secretary</p>
                </li>
            </ul>
        </div>

【讨论】:

    【解决方案2】:

    这是一个有点 hacky 的解决方案...在 lis 中使用您的样式嵌套一个容器,然后根据媒体查询将 lis 宽度设置为 25%、50%、100%。

    .container {
        margin-bottom: 10px;
        margin-top: 10px;
        padding: 10px;
        align-items: flex-start;
        flex-basis: auto;
    }
    
    .flex-container {
        margin: 1px;
        list-style: none;
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        -webkit-flex-flow: row wrap;
        justify-content: space-between;
    }
    li {
      width: 25%;
      display: flex;
    }
    
    
    .flex-item {
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
        transition: 0.1s;
        -webkit-filter: opacity(60%);
        /* Safari 6.0 - 9.0 */
        filter: opacity(60%);
        width: 231px;
        height: 350px;
        /* margin-top: 10px; */
        line-height: 10px; 
        margin: 10px auto 0 auto;
        color: white;
        font-weight: bold;
        text-align: center;
    
    }
    
    .flex-item h4 {
        font-size: 22pt;
    }
    
    .flex-item p {
        font-size: 14pt;
    }
    
    .flex-item:hover {
        box-shadow: 0 12px 24px 0 rgba(0, 0, 0, 0.4);
        -webkit-filter: opacity(100%);
        /* Safari 6.0 - 9.0 */
        filter: opacity(100%);
    }
    
    
    .person1 {
        background: #FB8B4F;
    }
    
    .person2 {
        background: #1FACE4;
    }
    
    .person3 {
        background: #EF626B;
    }
    
    .person4 {
        background: #8BE41F;
    }
    
    /*When screen is < 1000px : green */
    @media screen and (max-width: 1000px) {
       /*  body {
            background-color: lightgreen;
        } */
        .flex-item {
            transform: scale(0.9);
        }
        li {
          width: 50%;
        }
    }
    
    /*When screen is < 500px : blue */
    @media screen and (max-width: 600px) {
        .flex-item {
          flex-direction: column;
          align-items:center;
        }
        li {
          width: 100%;
        }
       /*  body {
            background-color: lightblue;
        } */
    }
    <!--Profiles-->
            <div class="container">
                <hr size="0.5">
                <h1 id="people">People</h1>
                <ul class="flex-container">
                    <li>
                        <!-- <img src="images/profile_placeholder.gif" alt="p1"> -->
                        <div class="flex-item person1">
                          <h4><b>Person1</b></h4>
                          <p>Founder</p>
                        </div>
    
                    </li>
                    <li>
                        <!-- <img src="images/profile_placeholder.gif" alt="p2"> -->
                        <div class="flex-item person2">
                          <h4><b>Person2</b></h4>
                          <p>Treasurer</p>
                        </div>
    
                    </li>
                    <li>
                        <!-- <img src="images/profile_placeholder.gif" alt="p3"> -->
                        <div class="flex-item person3">
                          <h4><b>Person3</b></h4>
                          <p>Vice-president</p>
                        </div>
                    </li>
                    <li>
                        <!-- <img src="images/profile_placeholder.gif" alt="p4"> -->
                        <div class="flex-item person4">
                          <h4><b>Person4</b></h4>
                          <p>Secretary</p>
                        </div>
                    </li>
                </ul>
            </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-09
      • 2017-06-21
      • 2016-04-22
      • 2021-06-07
      • 2021-01-23
      • 2016-07-05
      • 2012-06-02
      • 2023-04-10
      相关资源
      最近更新 更多