【发布时间】:2018-12-03 21:30:50
【问题描述】:
我有一个网站部分必须包含三个并排的元素。每个元素都有一个最小宽度。如果屏幕的宽度不足以包含所有三个元素,那些不适合的,进入一个新行。
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;
}
代码有效,但我想修复一些技巧。
这 3 列都有相同的宽度,我希望能够选择这个宽度。这是因为第一个元素的最小宽度大于其他两个元素,因此边缘更小且美观。见the same example,我只改了颜色
剩余空间现在在
col*Ccon容器的两侧增加。相反,我只想在col1Ccon的左侧和col3Ccon的右侧增长。因此,我希望网站 (col1Ccon + col2Ccon + col3Ccon) 的内容始终保持在页面的中心,而“边框”会发生什么变化,会增加和减少。
我被困住了,非常感谢任何建议。谢谢:)
【问题讨论】:
-
我不确定我是否理解您想要实现的目标,但我在这里做了一个简化的示例:codepen.io/wiiiiilllllll/pen/vrQzWX 这有帮助吗?
-
请张贴想要的结果的图片,因为我也不知道你想要什么。
-
您尝试过媒体查询吗?我找到了一个 codepen,但它可能会消失 -->codepen.io/estelle/pen/brDpB