首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Div元素垂直居中

问将Div元素垂直居中
EN

Stack Overflow用户
提问于 2012-05-16 01:18:02
回答 1查看 244关注 0票数 0

我在一个非常基本的网站模板上工作,左上角有固定的标题,左下角和右下角有固定的底线。

中间的内容应该是居中的,我只是不能完全弄清楚我做错了什么。

问题1。)如果移除height: 100% css属性,为什么垂直居中不起作用?

问题2。)当在html标记上声明height: 100%时,为什么站点大于100%并延伸到浏览器窗口之外?

问题3。)为什么bottom-left显示正确,但bottom-right显示不正确

问题4。)如果with设置为100%,文本右对齐,文本不应该从浏览器右框架开始并向左延伸吗?为什么要扩展浏览器窗口?

非常感谢你的帮助。代码在下面可见

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<style type="text/css">

html {
    height:100%;
}

body {
    height: 100%;
    margin-left: 20px;‚
}

.title {
    position: absolute;
    top: 20px;
    font-family: serif;
    font-size: 20px;
    text-align: left;
}

/* Center Text Section */

.area {
  width: 100%;
  height: 100%;
  position: relative;
}

.middlespace {
  position: absolute;
  left: 50%;
  top: 50%;
  display: table;
}

.middlespace p {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

.exhibitionindex {
    text-align: center;
    width: 500px;
    margin-right: auto;
    margin-left: auto;
}

.bottom-left {

    position: absolute;
    bottom: 15px;
    text-align: left;
}

.bottom-right {
    position: absolute;
    bottom: 15px;
    text-align: right;
}

.exhibitiontitle {
    color: #d4d4d4;
    font-style: italic;
    font-family: sans-serif;
    font-size: 10px;
    text-align: center;
}

.bold {
    font-family: serif;
}


.about {
    font-size: 11px;
}

.contact {
    font-size: 11px;
}

.openinghours {
    font-style: italic;
    color: #8e8e8e;
    font-size: 11px;
}

</style>

<title>Website Title</title>

</head>
<body>
    <div class="title">Logo / Title Text</div>

    <div class="area">
        <div class="middlespace">
            <p>27. April 2012</p>
        </div>
    </div>
    <div class="bottom-left">
        <span class="about"><span class="bold">XYZ</span> is a project by Person One, Person Two, Person Three&nbsp;&nbsp;&#124;&nbsp;</span>
        <span class="contact">Website Information &mdash; <a href="mailto:info@info.com">info@info.com</a></span>
    </div>
    <div class="bottom-right"><span class="openinghours">Opening Hours Information</span></div>
</body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2012-05-16 01:47:43

问题1。)如果删除height: 100% css属性,为什么垂直居中不起作用?

默认情况下,html和body元素扩展高度以适合其内容。因此,在不设置高度的情况下,您的html的高度仅与包含样式的内容所需的高度相同。此外,绝对定位的元素不会影响其父元素的大小。如果不将高度设置为100%,area也不会是整个窗口的高度,因此您将在页面顶部的一个小条带中垂直居中。

问题2。)当html标签声明height: 100%时,为什么站点大于100%并延伸到浏览器窗口之外?

仅此一项并不会超出页面边界。但是,如果您将边距、边框或类似的属性添加到100%的元素中,则除了100%之外,还会添加这些属性,从而使您的元素超出窗口的边缘。

问题3。)为什么左下角显示正确,但右下角显示不正确?

你绝对是在定位这些元素。Div元素通常是父元素宽度的100%。但是,当您绝对定位它们时,它们的宽度会缩小以适合内容。在本例中,您尝试在默认情况下左对齐的div中执行text-align。因为div只和文本一样大,所以左对齐、右对齐和居中对齐看起来都是一样的。因为您已经对div进行了绝对定位,所以只需使用绝对定位来对齐它们:

代码语言:javascript
复制
.bottom-left {
    position: absolute;
    bottom: 15px;
    left: 15px;
}
.bottom-right {
    position: absolute;
    bottom: 15px;
    right: 15px;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10605737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档