html - 使各个部分位于彼此下方

标签 html css

我正在尝试创建多个视口(viewport)大小的部分,并且我需要在部分的中心和中间对齐测试。我尝试使用 display: table-cell 来垂直对齐文本,但随后各部分彼此相邻。

我创建了FIDDLE

html:

   <div id="main">
        <section id="home">
            gf
        </section>
        <section id="about">
            gh
        </section>
        <section id="contactMe">
            gf
        </section>
  </div>

CSS:

#main{
    vertical-align: middle;
    width:100%;
    text-align: center;
}
#home{
    background-color: green;
    background-size: cover;
    height:100vh;
    width: 100vw;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
#about{
    background-color: blue;
    background-size: cover;
    height:100vh;
    width: 10vw;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
#contactMe{
    background-color: red;
    background-size: cover;
    height:100vh;
    width: 100vw;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

感谢任何帮助。

如果需要更多信息,请在评论中提及。谢谢。

编辑:各部分应位于彼此下方

最佳答案

这是一种使其发挥作用的快速方法。将文本包裹在 div 中,然后将 display: table 应用于 section 元素,然后将 display: table-cell code> 到 div 的。

#main{
    vertical-align: middle;
    width:100%;
    text-align: center;
}
#main section {
    background-size: cover;
    height:100vh;
    width: 100vw;
    display: table;
}
#main section div {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
#home{
    background-color: green;
}
#about{
    background-color: blue;
}
#contactMe{
    background-color: red;
}
<div id="main">
  <section id="home">
    <div>gf</div>
  </section>
  <section id="about">
    <div>gh</div>
  </section>
  <section id="contactMe">
    <div>gf</div>
  </section>
</div>

关于html - 使各个部分位于彼此下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27962614/

相关文章:

html - 使用 flexbox 时,<div> 和 <span> 之间是否存在功能差异

html - 在不应该的情况下使用 50% 父级宽度换行的 Flex div

html - 在调整网站窗口大小时调整 html 中的元素

css - 如何使用 fullpage.js 获取背景(封面)图像以调整大小并适合浏览器宽度?

html - 为什么视口(viewport)元标记中的 initial-scale=1 什么都不做?

javascript - HTML 中的 Angular 6 如果字段未定义则显示不适用

javascript - jquery隐藏一个包含flash的div而不重置它

php - 在 joomla 网站中更改图像路径时出错

javascript - IE7 Javascript 悬停无法正常工作

html - 绝对定位输入的宽度不符合 CSS 规则