首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将div定位在另一个div的中心。

无法将div定位在另一个div的中心。
EN

Stack Overflow用户
提问于 2014-12-15 09:38:56
回答 2查看 45关注 0票数 2

我不知道为什么id 'text‘的div自动获得100%的高度和宽度。我希望它的中心像id‘徽标’的div,并给它一个填充在顶部和底部的77 of。

请帮帮忙。

代码语言:javascript
复制
<div id="wrapper-1">
<div class="divider"></div>
<div id="logo">
    Logo
</div>
</div>
<div id="wrapper-2">
<div class="divider"></div>
<div id="text">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam  nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</div>
</div>

CSS

代码语言:javascript
复制
body, html{
padding: 0;
margin: 0;
width: 100%;
height: 100%;
background-color: #131316;
}

.divider{
width: 1px;
height: 100%;
background-color: #f9f9f9;
margin: auto;
z-index: 1;
}

#wrapper-1{
width: 100%;
height: 100%;
position: relative;
}

#logo{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
width: 177px;
height: 177px;
background-color: #f9f9f9;
color: #131316;
z-index: 2;
}

#wrapper-2{
width: 100%;
height: 100%;
position: relative;
}

#text{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
text-align: center;
background-color: blue;
}

这是小提琴- 链接

EN

回答 2

Stack Overflow用户

发布于 2014-12-15 09:44:22

演示

css

代码语言:javascript
复制
#text{                        //its a div so display block by default, thus width 100% by default.
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    text-align: center;
    background-color: blue;
    width:177px;
    height:177px;            //you need to give width and height here, as to explicitly absolute positioned it with top, left, right, bottom 0
}
票数 0
EN

Stack Overflow用户

发布于 2014-12-15 09:45:50

您只需按以下所示更新#text类:

代码语言:javascript
复制
#text {
position: absolute;
width: 177px;
height: 177px;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
text-align: center;
background-color: rgb(0, 0, 255)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27481064

复制
相关文章

相似问题

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