1.css让浮动的盒子水平居中
解决方法:
- 给浮动的盒子加一个父盒子,设置宽度跟浮动盒子一样大小,并且overflow:hidden; 设置该盒子为margin: 0 auto;
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title></title><style>*{padding: 0;margin: 0;}.box{width: 400px;height: 300px;background-color: red;}.main{width: 100px;overflow: hidden;margin: 0 auto;}.child{width: 100px;height: 100px;background-color: green;margin: 0 auto;float: left;}</style>
</head>
<body><div class="box"><div class="main"><div class="child"></div></div></div></body>
</html>
2.CSS背景颜色 背景图片 居中 重复 固定样式background经验篇
background:url(../images/index/indexbackground.png) no-repeat center top;
3.CSS垂直居中的8种方法
1、通过verticle-align:middle实现CSS垂直居中
通过vertical-align:middle实现CSS垂直居中是最常使用的方法,但是有一点需要格外注意,vertical生效的前提是元素的display:inline-block。
2通过display:flex实现CSS垂直居中
随着越来越多浏览器兼容CSS中的flexbox特性,所以现在通过“display:flex”实现CSS水平居中的方案也越来越受青睐。
通过display:flex实现CSS垂直居中的方法是给父元素display:flex;而子元素align-self:center;
这个跟CSS水平居中的原理是一样的,只是在flex-direction上有所差别,一个是row(默认值),另外一个是column。
3、通过伪元素:before实现CSS垂直居中。
具体方式是为父元素添加伪元素:before,使得子元素实现垂直居中
4.、通过display:table-cell实现CSS垂直居中
给父元素display:table,子元素display:table-cell的方式实现CSS垂直居中
5、通过隐藏节点实现CSS垂直居中
创建一个隐藏节点#hide,使得隐藏节点的height值为剩余高度的一半即可。
这种方法也适用于CSS水平居中,原理一样。
6、已知父元素高度通过transform实现CSS垂直居中
给子元素的position:relative,再通过translateY即可定位到垂直居中的位置
7、通过line-height实现CSS垂直居中
设置子元素的line-height值等于父元素的height,这种方法适用于子元素为单行文本的情况
8.position定位的方法
1 元素已知宽和高css
.center {background: red;width: 100px;height: 100px;position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;} /* or */.center {background: red;width: 100px;height: 100px;position: absolute;top: 50%;left: 50%;margin: -50px 0 0 -50px;}
2 元素宽高未知css.
.center {color: red;position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);}
9 flex布局
.wrap {width: 100%;height: 100vh;display: flex;align-items: center;justify-content: center;}.item {color: red;}