【问题标题】:Layout with 3 columns of different sizes using Flex使用 Flex 进行 3 列不同大小的布局
【发布时间】:2018-12-03 21:30:50
【问题描述】:

我有一个网站部分必须包含三个并排的元素。每个元素都有一个最小宽度。如果屏幕的宽度不足以包含所有三个元素,那些不适合的,进入一个新行。

为了构建这个布局,我使用了FlexCode.

html:

<main id='main'>
    <div id='firstRow' class='row'>
        <div id='col1C' class='col'>col1C title
            <div id='col1Ccon'>col1Ccon content</div>
        </div>
        <div id='col2C' class='col'>col2C title
            <div id='col2Ccon'>col2Ccon content</div>
        </div>
        <div id='col3C' class='col'>col3C title
            <div id='col3Ccon'>col3Ccon content</div>
        </div>
    </div>
</main>

css:

:root {
    --w1Col: 478px;
    --w2Col: 370px;
    --w3Col: 350px;
    --wSum3: calc(var(--w1Col) + var(--w2Col) + var(--w3Col));
}

html {
    /*height: 100%;*/
}

body {
    margin: 0;
    font-size: 9pt;
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    background-color: whitesmoke;
    height: 100%;
    display: flex;
    flex-direction: column;
}

/***************************************************************
 * Layout first row
 */
#main {
    background-color: white;
    /*border: 4px solid red;*/
    color: black;
    height: 100%;
    flex-grow: 1; /* ability for a flex item to grow if necessary */
    flex-shrink: 0; /* ability for a flex item to shrink if necessary */
    flex-basis: auto; /* defines the default size of an element before the remaining space is distributed */
}

#firstRow {
    background-color: white;
    /*border: 1px solid black;*/
    margin: 10px;
    padding: 10px;
    display: flex;
    flex-direction: row; /* colums layout */
    flex-wrap: wrap; /* wrap in multiple lines if it's necessary */
    /*min-width: var(--wSum3);*/
    /*justify-content: flex-end;  defines the alignment along the main axis */
}

#firstRow .col {
    /*border: 1px solid black;*/
    flex: 1;
    padding: 5px;
    text-align: center;
}

#col1C {
    background-color: green;
    width: var(--w1Col);
    min-width: var(--w1Col);
    order: 1; /* column order */
    flex-basis: 40%;/*var(--w1Col);  column width */
    justify-content: flex-end;

}
#col2C {
    background-color: blue;
    width: var(--w2Col);
    min-width: var(--w2Col);
    order: 2; /* column order */
    flex-basis: 35%; /* var(--w2Col);  column width */
    justify-content: flex-end;
}
#col3C {
    background-color:  red;
    width: var(--w3Col);
    min-width: var(--w3Col);
    order: 3; /* column order */
    flex-basis: 25%; /*var(--w3Col);  column width */
    justify-content: flex-end;
}

#col1Ccon, #col2Ccon, #col3Ccon {
    /*border: 1px solid black;*/
    margin: 0 auto; /* center content */
}

#col1Ccon {
    width: var(--w1Col);
    background-color: lightgreen;
    height: 200px;
}
#col2Ccon {
    width: var(--w2Col);
    background-color: lightblue;
    height: 150px;
}
#col3Ccon {
    width: var(--w3Col);
    background-color: salmon;
    height: 200px;
}

代码有效,但我想修复一些技巧。

  1. 这 3 列都有相同的宽度,我希望能够选择这个宽度。这是因为第一个元素的最小宽度大于其他两个元素,因此边缘更小且美观。见the same example,我只改了颜色

  2. 剩余空间现在在col*Ccon 容器的两侧增加。相反,我只想在col1Ccon 的左侧和col3Ccon 的右侧增长。因此,我希望网站 (col1Ccon + col2Ccon + col3Ccon) 的内容始终保持在页面的中心,而“边框”会发生什么变化,会增加和减少。

我被困住了,非常感谢任何建议。谢谢:)

【问题讨论】:

  • 我不确定我是否理解您想要实现的目标,但我在这里做了一个简化的示例:codepen.io/wiiiiilllllll/pen/vrQzWX 这有帮助吗?
  • 请张贴想要的结果的图片,因为我也不知道你想要什么。
  • 您尝试过媒体查询吗?我找到了一个 codepen,但它可能会消失 -->codepen.io/estelle/pen/brDpB

标签: css layout flexbox


【解决方案1】:

您的列都具有相同宽度的原因是因为您为 col 类提供了flex: 1。在这种情况下,这意味着每一列(即所有列)都有三分之一的屏幕可供它们使用。您可以做的是从 col 类中删除 flex 属性,而是在列的 id 选择器范围内使用它。因此,在下面的代码中,我将 flex 属性放在 id 选择器中,稍有不同的是,第二列的 flex-grow 属性为 0(flex 属性中的第一个值)。如您所说,第一列略大,因此需要更多空间。

然后,保持内容居中:第一部分是将第二列的flex-grow 设为 0,因此它位于中间。其他两列确实会增长,并且在内容列的一侧有一个自动margin,它将在一侧增长,并且内容列将向左或向右对齐,以提供居中内容的想法。这仅适用于更大的屏幕,对于较小的屏幕,您可以使用媒体查询来正确对齐内容。

所以最后,您的代码可能看起来像这样。我不知道这是否正是您的想法,但是您可以尝试一下,看看它是否有效。

:root {
    --w1Col: 478px;
    --w2Col: 370px;
    --w3Col: 350px;
    --wSum3: calc(var(--w1Col) + var(--w2Col) + var(--w3Col));
}

body {
    margin: 0;
    font-size: 9pt;
    font-family: 'Roboto', sans-serif;
    font-weight: 300;
    background-color: whitesmoke;
    height: 100%;
    display: flex;
    flex-direction: column;
}

/***************************************************************
 * Layout first row
 */
#main {
    background-color: white;
    color: black;
    height: 100%;
    flex: 1 0 auto; 
    width: 100%;
}

#firstRow {
    background-color: white;
    margin: 10px;
    padding: 10px;
    display: flex;
    flex-direction: row; /* colums layout */
    flex-wrap: wrap; /* wrap in multiple lines if it's necessary */
}

#firstRow .col {
    padding: 5px;
    text-align: center;
}

#col1C {
    background-color: green;
    order: 1; /* column order */
    flex: 1 1 var(--w1Col);
}
#col2C {
    background-color: blue;
    order: 2; /* column order */
    flex: 0 1 var(--w2Col);
}
#col3C {
    background-color:  red;
    order: 3; /* column order */
    flex: 1 1 var(--w3Col);
}

#col1Ccon {
    max-width: var(--w1Col);
    background-color: lightgreen;
    height: 200px;
    margin: 0 0 0 auto;
}

#col2Ccon {
    max-width: var(--w2Col);
    background-color: lightblue;
    height: 150px;
    margin: 0 auto;
}
#col3Ccon {
    max-width: var(--w3Col);
    background-color: salmon;
    height: 200px;
    margin: 0 auto 0 0;
}

@media only screen and (max-width: 1268px) {
  #col2C {
    flex-grow: 1;
  }

  #col2Ccon {
    margin: 0 auto 0 0;
  }

  #col3Ccon {
    margin: 0 auto;
  }
}

@media only screen and (max-width: 908px) {
  #col1Ccon {
    margin: 0 auto;
  }

  #col2Ccon {
    margin: 0 0 0 auto;
  }

  #col3Ccon {
    margin: 0 auto 0 0;
  }
}

@media only screen and (max-width: 780px) {
  #col2Ccon, #col3Ccon {
    margin: 0 auto;
  }
}

关于第二个问题

【讨论】:

    【解决方案2】:

    替换您的样式表来解决您的问题

    body {
        margin: 0;
        font-size: 9pt;
        font-family: 'Roboto', sans-serif;
        font-weight: 300;
        background-color: whitesmoke;
    }
    #firstRow {
        background-color: white;
        margin: 10px;
        padding: 10px;
        display: flex;
        flex-wrap: wrap;
    }
    
    #firstRow .col {
        flex: 1;
        padding: 5px;
        text-align: center;
    }
    
    #col1C {
        background-color: green;
    }
    #col2C {
        background-color: blue;
    }
    #col3C {
        background-color: red;
    }
    
    #col1Ccon, #col2Ccon, #col3Ccon {
        margin: 0 auto; 
    }
    
    #col1Ccon {
        background-color: lightgreen;
        height: 200px;
    }
    #col2Ccon {
        background-color: lightblue;
        height: 200px;
    }
    #col3Ccon {
        background-color: salmon;
        height: 200px;
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-09
      • 2019-05-16
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 2022-11-21
      • 1970-01-01
      相关资源
      最近更新 更多