1.水平居中

.div{
margin:0 auto; (或者 margin:auto;

width:500px;
height:300px;
}

2.使用margin水平垂直居中

方式一:

.div {
text-align: center;
line-height: 200px;
border: 2px pink solid;
width: 300px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
margin: -100px 0 0 -150px;

}

方式二:

<!DOCTYPE html>
<html>
<head>
<title>块级元素水平,垂直居中</title>
<meta charset="utf-8">
<style>
.wrapper {
height: 600px;
border: 1px solid gray;
}
.box {
width: 100px;
height: 100px;
background: gold;
margin: 250px auto;

}
</style>
</head>
<body>
<div class="wrapper">
<div class="box"></div>
</div>
</body>
</html>

3.jquery实现DIV水平垂直居中

.div {
text-align: center;
line-height: 200px;
border: 2px pink solid;
width: 300px;
height: 200px;
}
< script >
$(window).resize(function(){
$(".div").css({
position: "absolute",
left: ($(window).width() - $(".div").outerWidth())/2,
top: ($(window).height() - $(".div").outerHeight())/2

});
}); $(function(){
$(window).resize();
});
< /script >

4.使用css3 tansform属性

<!DOCTYPE html>
<html>
<head>
<title>块级元素水平,垂直居中</title>
<meta charset="utf-8">
<style>
.wrapper {
height: 400px;
width:600px;
border: 2px solid pink;
border-radius:10px;
}
.box {
position:relative;
height:200px;
width:200px;
top:50%;
left:50%;
transform: translate(-50%,-50%);

background:#abcdef; }
</style>
</head>
<body>
<div class="wrapper">
<div class="box">adfagagafajkfhla</div>
</div>
</body>
</html>

效果如下:

单独设置垂直居中可使用:

top:50%;
tansfrom:translateY(-50%);

单独使用水平居中可使用:

left:50%;
tramsform:translateX(-50%);

5.table-cell

注意:可能会破坏页面整体布局

<!DOCTYPE html>
<html>
<head>
<title>块级元素水平,垂直居中</title>
<meta charset="utf-8">
<style>
.wrapper {
height: 400px;
width:600px;
border: 2px solid pink;
border-radius:10px;
display:table;

}
.box {
text-align:center;
position:relative;
display:table-cell;
vertical-align:middle;

background:#abcdef; }
</style>
</head>
<body>
<div class="wrapper">
<div class="box">adfagagafajkfhla</div>
</div>
</body>
</html>

效果如下:

6.使用示例:DIV创建水平垂直居中遮罩层

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" / >
<title></title>
<style>
#overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #000;
opacity: 0.5;
filter: alpha(opacity = 50);
} #win {
position: absolute;
top: 50%;
left: 50%;
width: 400px;
height: 200px;
background: #fff;
margin: -102px 0 0 -202px;
line-height: 200px;
text-align: center;
border: 4px solid #CCC; }
</style>
</head>
<body>
<div id="overlay" ></div >
<div id="win" >
Div层居中
</div >
</body>
</html>

效果:

元素水平垂直居中(transform,margin,table-cell,jQuery)的更多相关文章

  1. 手写面试编程题- 数组去重 深拷贝 获取文本节点 设置奇数偶数背景色 JS中检测变量为string类型的方法 第6题闭包 将两个数组合并为一个数组 怎样添加、移除、移动、复制、创建和查找节点? 继承 对一个数组实现随机排序 让元素水平 垂直居中的三种方式 通过jQuery的extend方法实现深拷贝

    第1题==>实现数组去重 通过 new Set(数组名) // var arr = [12, 12, 3, 4, 5, 4, 5, 6, 6]; // var newarr1 = new Set ...

  2. CSS元素水平垂直居中方法总结(主要对大漠以及张鑫旭博客所述方法进行了归纳)

    本文主要是对主流居中方法进行了归纳,有些地方甚至就是把别人的代码直接复制过来的,没有什么自己的东西,除了大漠以及张鑫旭的方法外,还有来自司徒正美.怿飞博客的几个方法 以下方法,由于测试环境的原因,IE ...

  3. CSS元素水平垂直居中的方法

    1.  元素水平居中 1.1  设置父元素的属性 text-align: center; 说明:此属性只针对父元素的子元素为内联元素时有效,比如:img,input,select,button等(行内 ...

  4. CSS布局:元素水平垂直居中

    CSS布局:元素水平垂直居中 本文将依次介绍在不同条件下实现水平垂直居中的多种方法 水平垂直居中是在写网页时经常会用到的需求,在上两篇博客中,分别介绍了水平居中和垂直居中的方法.本文的水平垂直居中就是 ...

  5. 【Web】CSS实现绝对定位元素水平垂直居中

    网页中常常需用让绝对定位元素水平垂直居中,下面介绍2种方法: 一 元素宽度未知 <!DOCTYPE html> <html lang="en"> <h ...

  6. CSS未知宽高元素水平垂直居中

    方法一 :table.cell-table 思路:显示设置父元素为:table,子元素为:cell-table,这样就可以使用vertical-align: center,实现水平居中优点:父元素(p ...

  7. 05. flex元素水平垂直居中(三种position水平垂直居中和两种新老版本水平垂直居中)

    flex元素水平垂直居中(三种position水平垂直居中和两种新老版本水平垂直居中) (1).position : <!DOCTYPE html> <html lang=" ...

  8. css 实现元素水平垂直居中总结5中方法

    个人总结,如有错误请指出,有好的建议请留言.o(^▽^)o 一.margin:0 auto:text-align:center:line-height方法 <div id="divAu ...

  9. css元素水平垂直居中

    温习一下元素水平垂直居中的几种方法 元素有具体宽度 1.absolute+负边距 .LV_center{ border: 1px solid red; position: absolute; widt ...

  10. css进阶 04-如何让一个元素水平垂直居中?

    04-如何让一个元素水平垂直居中? #前言 老板的手机收到一个红包,为什么红包没居中? 如何让一个子元素在父容器里水平垂直居中?这个问题必考,在实战开发中,也应用得非常多. 你也许能顺手写出好几种实现 ...

随机推荐

  1. [Android] Service和IntentService中显示Toast的区别

    1. 表象     Service中可以正常显示Toast,IntentService中不能正常显示Toast,在2.3系统上,不显示toast,在4.3系统上,toast显示,但是不会消失. 2. ...

  2. Java的容器小结

    1. 各个类与接口的关系:

  3. codevs1051单词接龙(栈)

    /* 看到n的范围就觉得这个不可能是DP啥的 因为这个接龙的规则十分的简单 只要前缀相同即可 所以先按字典序排一遍 这样保证符合规则的一定挨着 然后弄一个stack 每次拿栈顶元素看看待入栈的元素是否 ...

  4. 七.生成n位随机字符串

    --1.借助newid() go --创建视图(因为在函数中无法直接使用newid()) create view vnewid as select newid() N'MacoId'; go --创建 ...

  5. Css简介

  6. ios 中的UI控件学习总结(1)

    UIKit框架提供了非常多功能强大又易用的UI控件 下面列举一些在开发中可能用得上的UI控件 UIButton 按钮 UILabel 文本标签 UITextField 文本输入框 UIImageVie ...

  7. Sql Xtype

    SQL Server xtype的介绍   在数据库内创建的每个对象(约束.默认值.日志.规则.存储过程等)在表中占一行.只有在 tempdb 内,每个临时对象才在该表中占一行.  列名 数据类型 描 ...

  8. http请求的cookie

    Cookie的作用: Cookie是用于维持服务端会话状态的,通常由服务端写入,在后续请求中,供服务端读取. HTTP请求,Cookie的使用过程 1.server通过HTTP Response中的& ...

  9. SpringMVC简单搭建与入门

    SpringMVC框架是spring框架的一个模块.springmvc和spring无需要通过中间整合层进行整合. 学习的时候,先了解一下流程至关重要,下面,简单介绍一下流程. 源码下载:http:/ ...

  10. aborb()程序结束形式

    abort()与exit()的区别?         分类:             MFC              2011-01-04 14:13     2233人阅读     评论(0)   ...