html - 带标题的垂直居中列 flexbox

标签 html css flexbox

我设法使用 flexbox 使列内容垂直对齐。 但是,我还需要为列添加标题。

我怎样才能有标题,其中文本以列宽为中心并且位于列的顶部(内容必须保持垂直居中)

看我的codepen试试: http://codepen.io/anon/pen/QNmrNx

.tothetop{
      position: absolute;
      top:0;
      text-align:center;
      background: yellow;  
    }

将标题放在顶部,但宽度与列宽不匹配,所以我无法将文本居中

最佳答案

如果我没理解错的话,您希望标题文本相对于列水平居中。这可能对你有帮助 -

header {
    display: flex;
    justify-content: center;
    width: 500px;
    margin: auto;
}     
.myView {
            margin: auto;
            display: flex;
            flex-direction: row;
            justify-content: space-around;

            height: 500px;
            width: 500px;
        }

        .wpType {
           display: -webkit-flex;
           display: flex;
           -webkit-flex-direction: column;
           flex-direction: column;
           -webkit-align-items: center;
           align-items: center;
           -webkit-justify-content: center;
           justify-content: center;
            padding: 0;
            margin: 0;
            list-style: none;
            border: 1px solid silver;
        }

        .wpType:nth-child(even){
            background: blue;
        }
        .wpType:nth-child(odd){
            background: red;
        }

        .wp{
            flex: 0 1 auto;
            padding: 5px;
            width: 100px;
            height: 100px;
            margin: 10px;
            background: white;
            line-height: 100px;
            color: white;
            font-weight: bold;
            font-size: 2em;
            text-align: center;
        }
<header>header</header>
<div class="myView">
    <div class="wpType">
        <div class="wp"></div>

    </div>
    <div class="wpType">
        <div class="wp"></div>
        <div class="wp"></div>
        <div class="wp"></div>
    </div>
    <div class="wpType">
        <div class="wp"></div>
        <div class="wp"></div>
        <div class="wp"></div>
    </div>
    <div class="wpType"><div class="wp"></div>
        <div class="wp"></div></div>
    <div class="wpType"><div class="wp"></div></div>

</div>

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

相关文章:

html - svg 类型图片标签的使用

css - div 内的两行文本居中

html - 如何在居中的 flexbox div 容器中换行?

html - 具有可调整大小图像的 flexbox 列

html - 如何同时垂直对齐底部和顶部?

javascript - 如何在不同选项卡上定期更新网站标题?

html - 从屏幕中心附近的元素发出的全屏背景

javascript - polymer 纸页眉面板在纸盒面板中不显示任何内容

html - CSS 属性不适用于嵌套的 div

html - 高度 100% flex