flexbox:垂直居中两个div

时间:2017-07-12 12:38:23

标签: html css flexbox

我在屏幕中央有一个带有标题的模态。 该标题必须包含标题和关闭它的按钮。

--------------------------------
|                       (close)|
|  Text                        |
|                              |
--------------------------------

基本上我有两个要求:

  1. 标题内的每个文字都必须垂直居中。
  2. 能够将“(关闭)”靠近角落的标题。
  3. 现在我把所有东西都垂直居中,但是我放在标题中的元素是ma堆:

    --------------------------------
    |                              |
    |  Text                        |
    |  (close)                     |
    |                              |
    --------------------------------
    

    .modal-payment__wrapper {          // <- this is the whole box
        max-width: 50%;
        width: 500px;
        height: 200px;
        background: white;
        font-family: 'Lato', 'sans-serif';
    
    }
    
    .modal__header {                  // <- this is the header
        display: flex;
        justify-content: center;
        flex-direction: column;
        width: 100%;
        height: 50px;
        background-color: black;
        color: white;
        font-size: 16px;
        padding-left: 20px;
    }
    
    .modal__header__title {          // <- this is "text"
        text-transform: uppercase;
        letter-spacing: 2px;
        font-size: 1.7em;
        padding: 10px;
        flex-grow: 0;
        flex-srhink: 0;
        flex-basis: auto;
        align-self: auto;
    }
    <div class="modal-payment__background">
        <div class="modal-payment__wrapper">
            <div class="modal__header">
                <div>
                    Text
                </div>
                <div class="modal__header__x"
                    @click="closeModal">
                    x
                </div>
            </div>
            <div class="modal__body">

    我错过了什么?

2 个答案:

答案 0 :(得分:1)

提供的代码中存在相当多的错误,但我认为这正是您所需要的。我不得不删除@click,因为这里没有被识别出来。

.modal-payment__wrapper {
  max-width: 50%;
  width: 500px;
  height: 200px;
  background: white;
  font-family: 'Lato', 'sans-serif';
}

.modal__header {
  width: 100%;
  height: 50px;
  background-color: black;
  color: white;
  font-size: 16px;
  padding-left: 20px;
  position: relative;
  display: flex;
  align-items: center;
}

.modal__header__title {
  text-transform: uppercase;
  letter-spacing: 2px;
  font-size: 1.7em;
}

.modal__header__x {
  position: absolute;
  right: .5em;
  top: 0;
}
<div class="modal-payment__background">
  <div class="modal-payment__wrapper">
    <div class="modal__header">
      <div class="modal__header__title">
        Text
      </div>
      <div class="modal__header__x">
        x
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:0)

Yuo可以这样做。请参阅代码中的注释,以了解您所犯错误的位置。

.modal-payment__wrapper {        
    max-width: 50%;
    width: 500px;
    height: 200px;
    background: white;
    font-family: 'Lato', 'sans-serif';

}

.modal__header {        
 
    display: flex;   // You need this
    justify-content: center;
    flex-direction: row;   // You need this
    width: 100%;
    height: 50px;
    background-color: black;
    color: white;
    font-size: 16px;
    padding-left: 20px;
  
}

// You will need below css
.modal__header > div{
  justify-content: space-around;   
  width: 50%;  
  align-self: center;   
 
}

.modal__header__title {          // <- this is "text"
    text-transform: uppercase;
    letter-spacing: 2px;
    font-size: 1.7em;
    padding: 10px;
    flex-grow: 0;
    flex-shrink: 0;  // Spelling mistake
    flex-basis: auto;
    align-self: auto;
}

.modal__header__x{
  color: #fff;  // You need this
  
}
<div class="modal-payment__background">
    <div class="modal-payment__wrapper">
        <div class="modal__header">
            <div class="modal__header__title">
                Text
            </div>
            <div class="modal__header__x"
                @click="closeModal">
                x
            </div>
        </div>
        <div class="modal__body">
           BODY
         </div>   <!-- Missing /div in your code -->
  </div> <!-- Missing /div in your code -->
</div> <!-- Missing /div in your code -->