1、transform实现居中(未设宽高

<div id="wrap">内容</div>
<style>
#wrap{
padding:50px;
background:red;
position:absolute;
top:50%;
left:50%;
-webkit-transform:translate(-50%,-50%);
-moz-transform:translate(-50%,-50%);
transform:translate(-50%,-50%);
}
</style>

2、margin:auto(固定宽高

<div class="div1">
<div class="div2"></div>
</div> <style>
*{
padding:0px;
margin:0px;
}
.div1{
width:800px;
height:600px;
position: relative;
border:1px solid #f00;
}
.div2{
width:300px;
height:200px;
position: absolute;
margin:auto;
top:0px;
bottom: 0px;
left:0px;
right:0px;
background: pink
}
</style>

3、绝对定位(固定宽高

    <div class="div1">
<div class="div2"></div>
</div> <style>
*{
padding:0px;
margin:0px;
}
.div1{
width:800px;
height:600px;
position: relative;
border:1px solid #f00;
}
.div2{
width:300px;
height:200px;
position: absolute;
top:50%;
left:50%;
margin-left: -150px;
margin-top: -100px;
background: pink
}
</style>

4、table-cell和vertical-align(固定宽高),但是这种方法会使父元素无法居中

<div class="div1">
<div class="div2"></div>
</div> <style>
.div1{
width:500px;
height:500px;
border:1px solid black;
display:table-cell;
vertical-align: middle; }
.div2{
background: yellow;
width:300px;
margin:auto;
}
</style>

css3实现水平垂直居中的更多相关文章

  1. css3实现水平垂直居中------(特别注意,里边的固定还是不固定)

    a,----定位方式(父元素宽高固定,子元素宽高固定) <div class="Father"> <div class="children"& ...

  2. CSS3中flexbox如何实现水平垂直居中和三列等高布局

    最近这些天都在弥补css以及css3的基础知识,在打开网页的时候,发现了火狐默认首页上有这样一个东西.

  3. 用CSS/CSS3 实现水平居中和垂直居中,水平垂直居中的方式

    一.水平居中 (1)行内元素解决方案:父为块元素+text-align: center 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: 使用te ...

  4. css3种不知道宽高的情况下水平垂直居中的方法

    第一种:display:table-cell 组合使用display:table-cell和vertical-align.text-align,使父元素内的所有行内元素水平垂直居中(内部div设置di ...

  5. 水平垂直居中div(css3)

    一.在需要居中的元素加上如下C3属性即可: <!doctype html><html lang="en"><head> <meta cha ...

  6. css3 flex 详解,可以实现div内容水平垂直居中

    先说一下flex一系列属性: 一.flex-direction: (元素排列方向) ※ flex-direction:row (横向从左到右排列==左对齐) ※ flex-direction:row- ...

  7. css3不定宽高水平垂直居中

    1 justify-content:center;//子元素水平居中 2 align-items:center;//子元素垂直居中 3 display:-webkit-flex; 在父级元素上面加上上 ...

  8. CSS3/CSS之居中解析(水平+垂直居中、水平居中,垂直居中)

    首先,我们来看下垂直居中: (1).如果是单行文本,则可以设置的line-height的数值,让其等于父级元素的高度! <!DOCTYPE html> <html lang=&quo ...

  9. css3水平垂直居中(不知道宽高同样适用)

    css水平垂直居中 第一种方法: 在父div里加: display: table-cell; vertical-align: middle; text-align: center; 内部div设置: ...

随机推荐

  1. NetSarang软件中nssock2.dll模块被植入恶意代码技术分析与防护方案

    原文地址:http://blog.nsfocus.net/nssock2-dll-module-malicious-code-analysis-report/ NetSarang是一家提供安全连接解决 ...

  2. HDU - 1078 FatMouse and Cheese (记忆化搜索)

    FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension ...

  3. centos7项目部署

    1. 安装nginx   添加CentOS 7 Nginx yum资源库 sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/ng ...

  4. 第九节:JWT简介和以JS+WebApi为例基于JWT的安全校验

    一. 简介 1. 背景 传统的基于Session的校验存在诸多问题,比如:Session过期.服务器开销过大.不能分布式部署.不适合前后端分离的项目. 传统的基于Token的校验需要存储Key-Val ...

  5. js中的简单数据类型和复杂数据类型的存储

    基本类型存储的是值而复杂数据类型也叫引用类型存储的是对象的地址如0x00001而在栈中存的是变量数值和函数参数 堆中存的是对象和数组 堆栈空间分配 栈(操作系统):由操作系统自动分配释放 ,存放函数的 ...

  6. pip 安装问题

    同时安装了Python2 和Python3的情况下,由于我的电脑默认的是使用Python3,pip的时候直接就安装在3上了,为了让2也安装,办法之一就是在安装python2的路径下比如,D:\Anac ...

  7. tomcat7的安装与maven安装

    tomcat7的安装与配置: 下载tomcat7 :wget 地址 解压:tar -zxvf 文件名 编辑tomcat目录下的conf下的server.xml文件: <Connector por ...

  8. 阿里的fastJson.jar jsonArray 和 list 互转

    阿里的fastJson.jar: //list转换为json List<CustPhone> list = new ArrayList<CustPhone>(); String ...

  9. word2vec概述

    既然是概述,那么我也只会在文中谈一点关于 Word2Vec 的思想和大概的方法.对于这个算法,如果一开始学习就深入到算法细节中,反而会陷入局部极值点,最后甚至不知道这个算法是干嘛的.在了解算法大概的思 ...

  10. underscore用法大全

    1._.find函数 var one = _.find(all, function (item) { return item.C_ID == selected; }); $('#C_NAME').va ...