HTML网页编程的CSS中关于margin-top和margin-bottom的问题

<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.main{
background:#000;
width:300px;
height:400px;
margin:auto;
}
.up{
border:2px solid #C0F;
padding:5px;
background:#FFF;
margin-left:auto;
margin-right:auto;
margin-top:336px;
height:50px;
width:100px;
}</style>
</head>
<body><div class="main">
<div class="up">向下对齐</div>
</div>
</body>

如图,在DW中显示已经向下对齐,但在浏览器显示中却是

请高手帮忙看下是什么问题?求解答。

第1个回答  推荐于2017-10-13
<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>无标题文档</title>

<style type="text/css">

.main{

background:#000;
width:300px;
height:400px;
margin:auto;
text-align:center;
position:relative;

}

.main .up{

border:2px solid #C0F;

padding:5px 5px;
background:#FFF;

position:absolute;
top:336px;
left:100px;

height:50px;
width:100px;

}

</style>
</head>

<body>

<div class="main">

<div class="up">向下对齐</div>

</div>
</body>追问

我知道父类采用了position后,子类中就可以利用left,right,top,这些来进行定位。
但是为什么不能用margin-top和margin-bottom来进行垂直方向的定位呢?

本回答被提问者和网友采纳
第2个回答  2012-09-24
如果父元素中没有定上外边距margin-top的话,子元素定了上外边距,那么父元素和子元素的上外边距会重合。
第3个回答  2012-09-20
我觉得你可以用绝对定位来做比较好控制一点,用margin-top它也有一定的值,而且这个值是相对于父元素的上边距。可以考虑用绝对定位的方式去实现。
第4个回答  2012-09-20
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>无标题文档</title>

<style type="text/css">

.main{
background:#000;
width:300px;
height:400px;
margin:0 auto;
text-align:center;
vertical-align:bottom;
position:relative;
}

.up{
border:2px solid #C0F;
text-align:center;
background:#FFF;

height:100px;
width:100px;
position:absolute;
bottom:0px;

}

</style>
</head>

<body>

<div class="main">

<div class="up">向下对齐</div>

</div>
</body>
你看看我这个的
第5个回答  2012-09-20
浏览器的问题吧 ,用IE的话应该可以的,我试过了。但是用谷歌的就是你说的那样了..其他浏览器不晓得怎么样..
相似回答