2016-05-15 52 views
3

我想要我的文本居中水平和垂直柔性项目。我环顾四周,并尝试了很多东西,但迄今为止似乎没有任何工作...垂直和水平弯曲项目中心p

我可以嵌套另一个flexbox-容器内的现有?

html, body { 
 
    height: 100%; 
 
    margin: 0; 
 
} 
 
.flex-container { 
 
    height: 100%; 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 
.page1 { 
 
    flex-grow: 1; 
 
    min-height: 100%; 
 
    background: lightblue; 
 
} 
 
#text1 { 
 
    font-size: 160%; 
 
    color: white; 
 
    font-family: roboto, sans-serif; 
 
} 
 
.page2 { 
 
    flex-grow: 1; 
 
    min-height: 100%; 
 
    background: cyan; 
 
} 
 
.page3 { 
 
    flex-grow: 1; 
 
    min-height: 100%; 
 
    background: lightgreen; 
 
} 
 
.page3 { 
 
    flex-grow: 1; 
 
    min-height: 100%; 
 
    background: lightgreen; 
 
}
<div class="flex-container"> 
 
    <div class="page1"> 
 
    <p id="text1">blabla bla blablabla.</p> 
 
    </div> 
 
    <div class="page2"></div> 
 
    <div class="page3"></div> 
 
</div>

+1

是的,你可以嵌套Flex容器。 http://stackoverflow.com/a/33049198/3597276 –

回答

3

是,使柔性物品也柔性容器,如下图所示。我还添加了margin: auto#text1以居中。

html, body { 
 
    height: 100%; 
 
    margin: 0; 
 
} 
 

 
.flex-container { 
 
    height: 100%; 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
.page1 { 
 
    display: flex; 
 
    flex-direction: column; 
 
    flex-grow: 1; 
 
    min-height: 100%; 
 
    background: lightblue; 
 
} 
 

 
#text1 { 
 
    margin: auto; 
 
    font-size: 160%; 
 
    color: white; 
 
    font-family: roboto, sans-serif; 
 
} 
 

 
.page2 { 
 
    flex-grow: 1; 
 
    min-height: 100%; 
 
    background: cyan; 
 
} 
 

 
.page3 { 
 
    flex-grow: 1; 
 
    min-height: 100%; 
 
    background: lightgreen; 
 
} 
 

 
.page3 { 
 
    flex-grow: 1; 
 
    min-height: 100%; 
 
    background: lightgreen; 
 
}
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
    <title> Portfolio </title> 
 
    <link rel="stylesheet" type="text/css" href="style.css"> 
 
    </head> 
 

 
    <body> 
 

 
    <div class="flex-container"> 
 

 
     <div class="page1"> 
 
     <p id="text1"> blabla bla blablabla. </p> 
 
     </div> 
 

 

 
     <div class="page2"> 
 

 
     </div> 
 

 
     <div class="page3"> 
 

 
     </div> 
 

 
    </div> 
 

 

 

 
    </body> 
 
</html>

+0

感谢您的帮助M8。解决方案非常简单,但我无法弄清楚......#Ineedabreak –