How can I achieve float left at bottom and float right at top with css flexbox?

怎甘沉沦 提交于 2019-12-24 12:33:50

问题


How can I simulate float left bottom and float right at top with the CSS3 flexbox? I want to achieve following situation (Important: the text in the div boxes should be also vertical and horizontal centered):

EDIT: PLNKR EXAMPLE: http://plnkr.co/edit/oVfbAAIZxRQLRRML7rJR?p=preview But the text in the div boxes should be also vertical and horizontal centered.

Following Code should be augmented:

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

(I don't like margin-left: auto because there is no min-margin at very small screens.)

Solution:

The code above is augmented with the code below and children get following additional css:

display: flex;             // also for child box for its content
align-items: center;       // center vertically
justify-content: center;   // center text horizontally

回答1:


Try this 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;
}



回答2:


That's the solution:

Both parent and children have to use flex. (col-md-12 is from bootstrap and has nothing to do with the question)

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;
    flex-wrap: wrap;  /* 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;
 }


来源:https://stackoverflow.com/questions/33755676/how-can-i-achieve-float-left-at-bottom-and-float-right-at-top-with-css-flexbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!