盒模型之margin

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>margin</title>
<style type="text/css">
/*前提:sup显示区域宽全屏,高200px, sub显示区域宽高个100px*/
/*需求1:sup左上右顶格显示*/
html, body {
/*body默认具有margin: 8px*/
margin: 0;
}
.sup {
width: auto;
height: 200px;
background-color: red;
}
.sub {
width: 100px;
height: 100px;
background-color: orange;
}
/*需求2:sub在sup的水平中央显示*/
/*.sup { 错误
text-align: center;
}*/
.sub {
/*上下0,左右auto*/
/*auto:左右auto,自适应(平方)剩余可布局空间*/
margin: 0 auto;
}
/*需求3:sub在sup的垂直中央显示*/
/*垂直会遇到margin父子坑*/
/*.sub { 不行
margin: auto;
}*/
.sup {
height: 100px;
padding: 50px 0;
}
/*.sup {
height: 100px;
border-top: 50px solid red;
border-bottom: 50px solid red;
}*/
/*需求4:sub在sup的水平居右显示*/
.sub {
margin-left: auto;
/*margin-right: 0;*/
margin-right: 10px;
}
</style>
</head>
<body>
<div class="sup">
<div class="sub"></div>
</div>
</body>
</html>

posted @ 2018-09-25 17:09  不沉之月  阅读(175)  评论(0编辑  收藏  举报