如何使用 css flexbox 实现底部左侧浮动和顶部右侧浮动?
How can I achieve float left at bottom and float right at top with css flexbox?
如何使用 CSS3 flexbox 模拟左下浮动和右上浮动?我想实现以下情况(重要: div 框中的文本也应垂直和水平居中):
编辑:PLNKR 示例:http://plnkr.co/edit/oVfbAAIZxRQLRRML7rJR?p=preview
但 div 框中的文本也应垂直和水平居中。
应增加以下代码:
display: flex; // for parent container
align-self: flex-end; // for child div-1 to place it at bottom
margin-left: auto; // for child div-2 to place it right top
(我不喜欢margin-left: auto
,因为在非常小的屏幕上没有min-margin。)
解法:
上面的代码用下面的代码进行了扩充,children 得到以下额外的 css:
display: flex; // also for child box for its content
align-items: center; // center vertically
justify-content: center; // center text horizontally
试试这个https://jsfiddle.net/2Lzo9vfc/108/
HTML
<div class="content">
<div class="div div-1">Div 1</div>
<div class="div div-2">Div 2</div>
</div>
CSS
.content {
display: flex;
padding: 20px;
border: 1px solid black;
width: 100%;
height: 200px;
flex-wrap: wrap; /* is not necessary */
}
.div {
padding: 25px;
height: 20px;
color: white;
background: black;
}
.div-1 {
align-self: flex-end;
}
.div-2 {
margin-left: auto;
}
这就是解决方案:
parent和children都必须使用flex。 (col-md-12来自bootstrap,与问题无关)
HTML
<div class="content col-md-12">
<div class="div div-1">Div 1</div>
<div class="div div-2">Div 2</div>
</div>
CSS
.content {
display: flex;
padding: 20px;
border: 1px solid black;
height: 200px;
<s>flex-wrap: wrap;</s> /* is not necessary */
}
.div {
padding: 10px;
color: white;
background: black;
width: 100px;
display: flex;
align-items: center;
justify-content: center;
}
.div-1 {
height: 90px;
align-self: flex-end;
}
.div-2 {
height: 60px;
margin-left: auto;
}
如何使用 CSS3 flexbox 模拟左下浮动和右上浮动?我想实现以下情况(重要: div 框中的文本也应垂直和水平居中):
编辑:PLNKR 示例:http://plnkr.co/edit/oVfbAAIZxRQLRRML7rJR?p=preview 但 div 框中的文本也应垂直和水平居中。
应增加以下代码:
display: flex; // for parent container
align-self: flex-end; // for child div-1 to place it at bottom
margin-left: auto; // for child div-2 to place it right top
(我不喜欢margin-left: auto
,因为在非常小的屏幕上没有min-margin。)
解法:
上面的代码用下面的代码进行了扩充,children 得到以下额外的 css:
display: flex; // also for child box for its content
align-items: center; // center vertically
justify-content: center; // center text horizontally
试试这个https://jsfiddle.net/2Lzo9vfc/108/
HTML
<div class="content">
<div class="div div-1">Div 1</div>
<div class="div div-2">Div 2</div>
</div>
CSS
.content {
display: flex;
padding: 20px;
border: 1px solid black;
width: 100%;
height: 200px;
flex-wrap: wrap; /* is not necessary */
}
.div {
padding: 25px;
height: 20px;
color: white;
background: black;
}
.div-1 {
align-self: flex-end;
}
.div-2 {
margin-left: auto;
}
这就是解决方案:
parent和children都必须使用flex。 (col-md-12来自bootstrap,与问题无关)
HTML
<div class="content col-md-12">
<div class="div div-1">Div 1</div>
<div class="div div-2">Div 2</div>
</div>
CSS
.content {
display: flex;
padding: 20px;
border: 1px solid black;
height: 200px;
<s>flex-wrap: wrap;</s> /* is not necessary */
}
.div {
padding: 10px;
color: white;
background: black;
width: 100px;
display: flex;
align-items: center;
justify-content: center;
}
.div-1 {
height: 90px;
align-self: flex-end;
}
.div-2 {
height: 60px;
margin-left: auto;
}