1、盒子在父元素中垂直居中的3中方式
- 方法1:已知宽高,水平、垂直各移动自身宽、高度的一半
-
<style> .box{ width: 500px; height: 400px; border: 3px dashed red; margin: 100px auto; position: relative; } .son{ width: 150px; height: 150px; background-color:tomato; position: absolute; left:50%; top:50%; /* 方法1 :已知宽高,水平、垂直各移动自身宽、高度的一半*/ argin-left: -75px; margin-top:-75px; } </style> </head> <body> <div class="box"> <div class="son"></div> </div> </body>
-
- 方法2:未知宽高,移动自身高度、宽度一半
-
<style> .box{ width: 500px; height: 400px; border: 3px dashed red; margin: 100px auto; position: relative; } .son{ width: 150px; height: 150px; background-color:tomato; position: absolute; left:50%; top:50%; /* 方法2:假设未知宽高,移动自身高度、宽度一半*/ transform: translate(-50%,-50%); } </style> </head> <body> <div class="box"> <div class="son"></div> </div> </body>
-
- 方法3:弹性布局flex,写给父元素
-
<style> .box{ width: 500px; height: 400px; border: 3px dashed red; margin: 100px auto; position: relative; /****方法3:弹性布局flex,写给父元素****/ display:flex; align-items:center; justify-content: center; } .son{ width: 150px; height: 150px; background-color:tomato; position: absolute; } </style> </head> <body> <div class="box"> <div class="son"></div> </div> </body>
-
2、清除浮动方法4种
①给父元素设置高度
②BFC保护(块级格式化上下文):给父元素设置overflow:hidden;
③在父元素末尾添加一个虚拟子元素,转为块元素,设置clear:both;
④伪元素
<style>
.box{
/*方法1:给父元素设置高度 */
/*height:200px;*/
width: 700px;
background-color:grey;
/*方法2:块级格式化上下文BFC */
/* overflow: hidden; */
}
.box>div{
width:200px;
height: 200px;
float: left;
}
.one{
background-color: orangered;
}
.two{
background-color:aquamarine
}
.three{
background-color:greenyellow;
}
/* 方法3: */
.clear{
clear:both;
/* 必须是块状标签 */
display: block;
}
/* 方法4:添加伪元素 */
/* .box::after{
content:"";
display:flex;
clear:both;
} */
</style>
</head>
<body>
<div class="box">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
<!-- 方法3:给父元素最后面添加一个假的子元素 -->
<!-- <span class="clear"></span> -->
</div>
3、渐进增强和优雅降级
①渐进增强 progressive enhancement:
先保证基本的功能,再改进和追加功能。
针对低版本浏览器进行构建页面,保证基本的功能,然后 再针对高级浏览器进行效果、交互等改进和追加功能达到更好的用户体验。
②优雅降级 graceful degradation:
先构建完整的功能,再针对低版本浏览器进行兼容 -webkit- -ms- -o- ...
4、盒子模型
1、边框border
边框样式属性 |