您现在的位置:首页 >> 前端 >> 内容

CSS的几种垂直居中样式总结

时间:2018/5/9 9:39:02 点击: 42

  核心提示:一.水平居中:.parent {1.一个行内元素;多个块状元素,还需将元素的display属性设置为inline-block[html] view plain copytext-align:cente...

一.水平居中:

.parent {  

    1.一个行内元素;多个块状元素,还需将元素的display属性设置为inline-block

[html] view plain copy

text-align:center;     

    2.块状元素

[html] view plain copy

margin: 0 auto;  

width:300px;//一定要有宽度   

    3.CSS3的属性,兼容性不太好,在苹果手机端适应性不好

[html] view plain copy

display:flex;  

justify-content:center;  

}

二.垂直居中:

1.

[html] view plain copy

.parent {  

    height: 200px;  

}  

/* 以下代码中,将a元素的height和line-height设置的和父元素一样高度即可实现垂直居中 */  

a {  

    height: 200px;  

    line-height:200px;  

}  

2.多行的行内元素

组合使用display:table-cell和vertical-align:middle属性来定义需要居中的元素的父容器元素生成效果,如下:

[html] view plain copy

.parent {  

    width: 300px;  

    height: 300px;  

    /* 以下属性垂直居中 */  

    display: table-cell;  

    vertical-align:middle;  

}  

3.已知高度的块状元素解决方案

[html] view plain copy

.parent{  

    position:relative;  

}  

.item{  

    top: 50%;  

    margin-top: -50px;  /* margin-top值为自身高度的一半 */  

    position: absolute;  

    padding:0;  

}  

三.水平垂直居中:

1.已知高度和宽度的元素,这是一种不常见的居中方法,可自适应,比方案2更智能,如下:

[html] view plain copy

.item{  

    position: absolute;  

    margin:auto;  

    left:0;  

    top:0;  

    right:0;  

    bottom:0;  

}  

2.已知高度和宽度的元素解决方案

[html] view plain copy

  

[html] view plain copy

.parent{  

    position:relative;  

}  

.item{  

    position: absolute;  

    top: 50%;  

    left: 50%;  

    margin-top: -75px;  /* 设置margin-left / margin-top 为自身高度的一半 */  

    margin-left: -75px;  

}  

3.未知高度和宽度元素:

[html] view plain copy

.item{  

    position: absolute;  

    top: 50%;  

    left: 50%;  

    transform: translate(-50%, -50%);  /* 使用css3的transform来实现 */  

}  

4.使用绝对定位实现

[html] view plain copy

position:absolute;  

margin:auto;  

display:block;  

top:0;  

left:0;  

right:0;  

bottom:0;  

//该元素的父元素设置为static以外的值  

5.使用flex布局实现

[html] view plain copy

.parent{  

    display: flex;  

    justify-content:center;  

    align-items: center;  

    /* 注意这里需要设置高度来查看垂直居中效果 */  

    height: 300px;  

}  

四.其它

1.图片居中对齐

[html] view plain copy

img {  

    display: block;  

    margin: auto;  

    width: 40%;  

}  

2.利用span图片在p中水平垂直居中:

[html] view plain copy

.parent{  

    text-align: center;  

    span{  

        height: 100%;  

        vertical-align: middle;  

        display: inline-block;  

    }  

    img{  

        vertical-align: middle;  

    }  

}  

3.文字水平垂直居中

[html] view plain copy

/*单行文字居中*/  

.center {  

    line-height: 200px;  

    height: 200px;  

    border: 3px solid green;  

    text-align: center;  

}  

/* 如果文本有多行,添加以下代码: */  

.center p {  

    line-height: 1.5;  

    display: inline-block;  

    vertical-align: middle;  

}  

4.logo在a链接垂直居中:

[html] view plain copy

.logo{  

    height:20px;  

    a{  

        line-height: 20px;  

        font-size: 0;  

        img{  

            vertical-align: middle;  

        }  

    }  

 }  

Tags:CS SS S的 的几 
作者:网络 来源:A_white_go
请选择您看到这篇文章时的心情: 已有0人表态:
0
0
0
0
0
0
0
0
惊讶 欠揍 支持 很棒 愤怒 搞笑 恶心 不解