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. RuntimePermissions

    This sample shows runtime permissions available in Android M and above. Display the log on screen to ...

  2. Java基础知识强化之IO流笔记15:递归之删除带内容的目录案例

    1. 需求:递归删除带内容的目录 分析:   (1)封装目录   (2)获取该目录下的所有文件或者文件夹的File数组   (3)遍历该File数组,得到每一个File对象   (4)判断该File对 ...

  3. 异步tcp通信——APM.ConsoleDemo

    APM测试 俗话说麻雀虽小,五脏俱全.apm虽然简单,但是可以实现单机高性能消息推送(可以采用redis.kafka等改造成大型分布式消息推送服务器). 测试demo: using System; u ...

  4. Android 环境下编译FFmpeg

    Android 环境下编译FFmpeg 开发环境:Ubuntu 12.04.2 LTS , android-sdk-linux, android-ndk-r8e 一 .X264 编译 1.    X2 ...

  5. Java 数据类型转换(转换成字节型)

    package com.mystudypro.byteutil; import java.io.UnsupportedEncodingException; public class ConToByte ...

  6. 新闻web小酌

    首页如上 类图如下: 添加新闻的方法(dao): public boolean Add(News news) { boolean flag=false; Connection con =getConn ...

  7. mvc存储Cookie和读取Cookie方法

    mvc存储Cookie和读取Cookie方法: //存储 HttpCookie cookie = new HttpCookie("User"); System.Text.Encod ...

  8. jsp中的c标签

    核心标签库 引用: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> ...

  9. ArcGis(01)——地图切片以及发布底图服务

    ArcGis(01)——地图切片以及发布底图服务 环境 操作系统:win10_x64 Gis版本:Arcis server 10.2 准备 1.tif格式地图资源 2.Arcis server 10. ...

  10. 重新开始学习javase_内部类

    转(http://www.cnblogs.com/dolphin0520/p/3811445.html) 内部类: 在Java 1.1 中,可将一个类定义置入另一个类定义中.这就叫作“内部类”.创建内 ...