html - flexbox:垂直居中两个div

标签 html css flexbox

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

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

所以基本上我有两个要求:

  1. 标题中的每个文本都必须垂直居中。
  2. 能够将“(关闭)”放置在 Angular 落的标题附近。

现在我的所有内容都垂直居中,但我将元素放在 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">

我错过了什么?

最佳答案

提供的代码中有不少错误,但我认为这就是您所需要的。我不得不删除 @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>

关于html - flexbox:垂直居中两个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45057923/

相关文章:

html - 如何使用 flexbox 在 Firefox 中显示响应式图像

html - 如何在 Flex 元素中获取 float 行为?

html - Flexbox:当屏幕太小时折叠 - 如何?

JavaScript 如何让用户在文本框中键入变量来求解方程?

javascript - 悬停/鼠标悬停时背景图像消失,菜单在 IE 中不起作用

Javascript 超链接不适用于 jQuery 追加

css - 如何使我的页面内容停止使用页面主体进行填充

css - 访问特定的表格单元格

html - 多个超链接样式

layout - React native flex layout with VictoryChart, Chart适合父容器