元素水平垂直居中(transform,margin,table-cell,jQuery)
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)的更多相关文章
- 手写面试编程题- 数组去重 深拷贝 获取文本节点 设置奇数偶数背景色 JS中检测变量为string类型的方法 第6题闭包 将两个数组合并为一个数组 怎样添加、移除、移动、复制、创建和查找节点? 继承 对一个数组实现随机排序 让元素水平 垂直居中的三种方式 通过jQuery的extend方法实现深拷贝
第1题==>实现数组去重 通过 new Set(数组名) // var arr = [12, 12, 3, 4, 5, 4, 5, 6, 6]; // var newarr1 = new Set ...
- CSS元素水平垂直居中方法总结(主要对大漠以及张鑫旭博客所述方法进行了归纳)
本文主要是对主流居中方法进行了归纳,有些地方甚至就是把别人的代码直接复制过来的,没有什么自己的东西,除了大漠以及张鑫旭的方法外,还有来自司徒正美.怿飞博客的几个方法 以下方法,由于测试环境的原因,IE ...
- CSS元素水平垂直居中的方法
1. 元素水平居中 1.1 设置父元素的属性 text-align: center; 说明:此属性只针对父元素的子元素为内联元素时有效,比如:img,input,select,button等(行内 ...
- CSS布局:元素水平垂直居中
CSS布局:元素水平垂直居中 本文将依次介绍在不同条件下实现水平垂直居中的多种方法 水平垂直居中是在写网页时经常会用到的需求,在上两篇博客中,分别介绍了水平居中和垂直居中的方法.本文的水平垂直居中就是 ...
- 【Web】CSS实现绝对定位元素水平垂直居中
网页中常常需用让绝对定位元素水平垂直居中,下面介绍2种方法: 一 元素宽度未知 <!DOCTYPE html> <html lang="en"> <h ...
- CSS未知宽高元素水平垂直居中
方法一 :table.cell-table 思路:显示设置父元素为:table,子元素为:cell-table,这样就可以使用vertical-align: center,实现水平居中优点:父元素(p ...
- 05. flex元素水平垂直居中(三种position水平垂直居中和两种新老版本水平垂直居中)
flex元素水平垂直居中(三种position水平垂直居中和两种新老版本水平垂直居中) (1).position : <!DOCTYPE html> <html lang=" ...
- css 实现元素水平垂直居中总结5中方法
个人总结,如有错误请指出,有好的建议请留言.o(^▽^)o 一.margin:0 auto:text-align:center:line-height方法 <div id="divAu ...
- css元素水平垂直居中
温习一下元素水平垂直居中的几种方法 元素有具体宽度 1.absolute+负边距 .LV_center{ border: 1px solid red; position: absolute; widt ...
- css进阶 04-如何让一个元素水平垂直居中?
04-如何让一个元素水平垂直居中? #前言 老板的手机收到一个红包,为什么红包没居中? 如何让一个子元素在父容器里水平垂直居中?这个问题必考,在实战开发中,也应用得非常多. 你也许能顺手写出好几种实现 ...
随机推荐
- Zlib文件压缩和解压
开源代码:http://www.zlib.net/zlib使用手册:http://www.zlib.net/manual.htmlzlib wince版:http://www.tenik.co.jp/ ...
- vs2012+qt5.2.0环境搭建/vs2013 + qt5.3.2 环境搭建
分类: Windows Qt2014-01-17 00:50 15434人阅读 评论(18) 收藏 举报 此文章已作废,请参考我的新文章: vs2013 + qt5.3.2 环境搭建 ( http:/ ...
- Meth | elementary OS常用配置
一,搜狗输入法 sudo apt-get remove ibussudo add-apt-repository ppa:fcitx-team/nightlysudo apt-get updatesud ...
- css之选择器
我们都用过jquery,使用jquery选择器,非常的简单,最近刚好有项目上手,拿起书本看了一下,发现好多的东西都忘掉了,好记性不如烂笔头,就将这章内容记录下来,现在我们看下css原生的选择器. 选择 ...
- Nginx报错:Sorry, the page you are looking for is currently unavailable. Please try again later.
查看了进程, nginx, php-fpm都在运行, 排除程序错误, 那么就是配置的问题了. 一个可能的错误, 是由于配置中的 fastcgi_pass 配置错了 错误的配置如下 server { l ...
- Windows与Linux文件共享
Windows与Linux文件共享 Samba服务器 安装Samba服务器 rpm –ivh /mnt/Packages/Samba-3.5.10-125.el6.i686.rpm 添加用户并修改密码 ...
- HttpClient4.0
****************************HttpClient4.0用法***************************** 1.初始化HttpParams,设置组件参数 //Ht ...
- 5JS树形结构菜单和jQuery版
第一版JS版HTML: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- Linq101-Miscellaneous
using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Miscell ...
- asp.net C# 导出EXCEL数据
if (dt == null) { return ""; } Microsoft.Office.Interop.Excel.Application xlApp = new Micr ...