用CSS 实现元素垂直居中,有哪些好的方案?

yumo6662个月前 (04-26)技术文章23

水平居中方案:

水平居中设置

1、行内元素

设置 text-align:center

2、定宽块状元素

设置 左右 margin 值为 auto

3、不定宽块状元素

a:在元素外加入 table 标签(完整的,包括 table、tbody、tr、td),该元素写在 td 内,然后设置 margin 的值为 auto

b:给该元素设置 displa:inine 方法

c:父元素设置 position:relative 和 left:50%,子元素设置 position:relative 和 left:50%

垂直居中设置

1、父元素高度确定的单行文本

设置 height = line-height

2、父元素高度确定的多行文本

a:插入 table (插入方法和水平居中一样),然后设置 vertical-align:middle

b:先设置 display:table-cell 再设置 vertical-align:middle

在前端面试中,大都会问你div居中的方法:

文笔不好,就随便寥寥几句话概括了,希望大家能够轻拍

不过以后文笔肯定会变得更好一些的。

开始这些东西之前也可以测试一下你对html了解多少,让我们测试一下吧,小测验:你对HTML5了解有多少?

今天我们就细数一下几种方法:

1,使用position:absolute,设置left、top、margin-left、margin-top的属性

.one{

position:absolute;

width:200px;

height:200px;

top:50%;

left:50%;

margin-top:-100px;

margin-left:-100px;

background:red;

}

这种方法基本浏览器都能够兼容,不足之处就是需要固定宽高。

2,使用position:fixed,同样设置left、top、margin-left、margin-top的属性

.two{

position:fixed;

width:180px;

height:180px;

margin-top:-90px;

margin-left:-90px;

background:orange;

}

大家都知道的position:fixed,IE是不支持这个属性的

3,利用position:fixed属性,margin:auto这个必须不要忘记了。

.three{

position:fixed;

width:160px;

height:160px;

top:0;

right:0;

bottom:0;

left:0;

margin:auto;

background:pink;

}

4,利用position:absolute属性,设置top/bottom/right/left

.four{

position:absolute;

width:140px;

height:140px;

background:black;

}

5,利用display:table-cell属性使内容垂直居中

.five{

display:table-cell;

vertical-align:middle;

text-align:center;

width:120px;

height:120px;

background:purple;

}

6,最简单的一种使行内元素居中的方法,使用line-height属性

.six{

width:100px;

height:100px;

line-height:100px;

text-align:center;

background:gray;

}

这种方法也很实用,比如使文字垂直居中对齐

7,使用css3的display:-webkit-box属性,再设置
-webkit-box-pack:center/-webkit-box-align:center

.seven{

width:90px;

height:90px;

display:-webkit-box;

-webkit-box-pack:center;

-webkit-box-align:center;

background:yellow;

color:black;

}

8,使用css3的新属性transform:translate(x,y)属性

.eight{

position:absolute;

width:80px;

height:80px;

transform:translate(-50%,-50%);

-webkit-transform:translate(-50%,-50%);

-moz-transform:translate(-50%,-50%);

-ms-transform:translate(-50%,-50%);

background:green;

}

这个方法可以不需要设定固定的宽高,在移动端用的会比较多,在移动端css3兼容的比较好

9、最高大上的一种,使用:before元素

.nine{

position:fixed;

display:block;

background:rgba(0,0,0,.5);

}

.nine:before{

content:'';

display:inline-block;

vertical-align:middle;

height:100%;

}

.nine .content{

width:60px;

height:60px;

line-height:60px;

color:red;

总而言之所有的居中的方法就是你必须要掌握css属性的这个概念HTML DIV+CSS,你掌握了就可以好好的运用这些居中的东西了

限时!!免费送Dreamweaver、js等前端教程

↓↓↓

相关文章

用CSS实现居中的七种方法

微信ID:WEB_wysj(点击关注) ◎ ◎ ◎ ◎ ◎◎◎◎◎一┳═┻︻▄(页底留言开放,欢迎来吐槽)● ● ●在网页上使 HTML 元素居中看似一件很简单的事情. 至少在某些情况下是这样的,但是...

CSS 元素分类与水平居中

元素分类在讲解CSS布局之前,我们需要提前知道一些知识,在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素、内联元素(又叫行内元素)和内联块状元素。常用的内联元素有:<a>...

实战经验分享:怎么在自己的网站里面调用第三方网站的页面内容

今日,站长在线(olzz.com)的首页右侧边栏里面,有一个是【mip验证】的工具入口,其实我在其他网站也放了这个入口,但是不同的是,以前的页面入口是直接链接到百度mip官方的页面检测地址的,而今天,...

videojs

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&#...

网页设计HTML零基础入门

一、Html概述Html是Hyper Text Mark-up Language 的首字母简写,意思是超文本标记语言,超文本指的是超链接,标记指的是标签,是一种用来制作网页的语言,这种语言由一个个的标...

前端兼容性问题总结

1.如何在IE6及更早浏览器中定义小高度的容器? IE6及更早浏览器之所以无法直接定义较小高度的容器是因为默认会有行高。 解决:#test{overflow:hidden;height:1px;fon...