Html 主分区内的中心分区
我创建了一个迷宫,我想将一个内部div居中 虽然我用边距将其居中:0自动;这行不通 (当用户进入墙并失败时,此div显示悲伤的笑脸) 这里是小提琴链接:Html 主分区内的中心分区,html,alignment,center,css,Html,Alignment,Center,Css,我创建了一个迷宫,我想将一个内部div居中 虽然我用边距将其居中:0自动;这行不通 (当用户进入墙并失败时,此div显示悲伤的笑脸) 这里是小提琴链接: 如果要使用绝对定位,您需要这样做: #highlight_lose { width: 550px; height:550px; position: absolute; top: 50%; left: 50%; margin: -225px 0 0 -225px; display: none
如果要使用绝对定位,您需要这样做:
#highlight_lose {
width: 550px;
height:550px;
position: absolute;
top: 50%;
left: 50%;
margin: -225px 0 0 -225px;
display: none;
}
#main {
position: relative;
width: 550px;
height: 550px;
float: left;
margin-left: 220px;
margin-top: 100px;
background: grey;
overflow: hidden;
}
#highlight_win {
width: 550px;
height: 550px;
position: absolute;
top: 50%;
left: 50%;
display: none;
margin: -180px 0 0 -180px;
}
#highlight_lose {
width: 550px;
height:550px;
position: absolute;
top: 50%;
left: 50%;
margin: -180px 0 0 -180px;
display: none;
}
编辑:您还需要添加位置:相对代码>到主分区。这里是一个更新的小提琴
因为有其他元素没有完全居中,所以它看起来不合适
编辑:如前所述,笑脸看起来没有居中,因为您的代码已关闭。迷宫真的应该在一个div里面。然而,我能够通过简单地玩边距来将它集中在眼球中心
要实现这一点,您需要如下设置css:
#highlight_lose {
width: 550px;
height:550px;
position: absolute;
top: 50%;
left: 50%;
margin: -225px 0 0 -225px;
display: none;
}
#main {
position: relative;
width: 550px;
height: 550px;
float: left;
margin-left: 220px;
margin-top: 100px;
background: grey;
overflow: hidden;
}
#highlight_win {
width: 550px;
height: 550px;
position: absolute;
top: 50%;
left: 50%;
display: none;
margin: -180px 0 0 -180px;
}
#highlight_lose {
width: 550px;
height:550px;
position: absolute;
top: 50%;
left: 50%;
margin: -180px 0 0 -180px;
display: none;
}
加价
<div class="outer">
<div class="inner">
</div>
</div>
固定水平定心
对于垂直居中对齐,孩子的顶部=父母身高的一半-孩子身高的一半
你是指垂直居中吗?当我有一个绝对位置时,我有时会使用此技巧将其居中。设置为左侧:50%;您可以设置左边距:-275px;来抵消你笑脸宽度的一半。这将始终保持您的微笑中心。:)它看起来像是在#main
内居中。这是你想要的吗?像这样@ExplosionPills i nned smily(#highlight_lose)将位于迷宫(#main)的中心,您还需要将父div设置为position:relative@乔希:谢谢你的时间,但你给我的小提琴例子并没有把它放在中心
margin:auto;