CSS如何水平垂直居中?

1、CSS如何实现水平居中?    

margin: 0 auto

2、CSS如何实现水平垂直居中?

首先设置一个div元素,设置背景颜色以便看出变化。代码如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>CSS水平垂直居中</title><style>.content {width: 300px;height: 300px;background: blue;}</style>
</head>
<body><div class="content"></div>
</body>
</html>

页面效果如下:

通过设置margin: 0 auto实现水平居中。代码如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>CSS水平垂直居中</title><style>.content {width: 300px;height: 300px;background: blue;margin: 0 auto}</style>
</head>
<body><div class="content"></div>
</body>
</html>

页面效果如下:

如何实现垂直居中呢?

方法一:通过display:relative设置top、bottom、right、left等属性实现位置偏移

在这之前,我们先要设置div元素的祖先元素html和body的高度为100%(因为他们默认是为0的),并且清除默认样式,即把margin和padding设置为0(如果不清除默认样式的话,浏览器就会出现滚动条)

通过设置position可以实现元素向不同方向的移动。在默认情况下(position:static; //静止的、不可以移动的)我们不可以直接设置top、bottom、right、left等属性使它在文档中发生位置偏移,需要设置position的值为relative,absolute,fixed等(relative是不会使元素脱离文档流的,元素在文档流里是从上往下、从左到右紧密的布局的。)实现位置偏移。

代码实现如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>CSS水平垂直居中</title><style>html,body {width: 100%;height: 100%;margin: 0;padding: 0;}.content {width: 300px;height: 300px;background: blue;margin: 0 auto; /*水平居中*/position: relative; /*脱离文档流*/top: 50%; /*偏移*/}</style>
</head>
<body><div class="content"></div>
</body>
</html>

页面效果如下:

这时,并没有居中,图片显示效果偏下,因为这里我们设置top:50%,所以我们设置的块状元素在页面中心位置开始向下分布,需要设置margin-top: -150px;150px是块状元素一半的高度。代码如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>CSS水平垂直居中</title><style>html,body {width: 100%;height: 100%;margin: 0;padding: 0;}.content {width: 300px;height: 300px;background: blue;margin: 0 auto; /*水平居中*/position: relative; /*脱离文档流*/top: 50%; /*偏移*/margin-top: -150px; }</style>
</head>
<body><div class="content"></div>
</body>
</html>

页面效果如下:

方法二:通过CSS3的transform属性

CSS3的transform属性也可以实现这个功能,通过设置div的transform: translateY(-50%),意思是使得div向上平移(translate)自身高度的一半(50%)。代码如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>CSS水平垂直居中</title><style>html,body {width: 100%;height: 100%;margin: 0;padding: 0;}.content {width: 300px;height: 300px;background: blue;margin: 0 auto; /*水平居中*/position: relative; /*脱离文档流*/top: 50%; /*偏移*/transform: translateY(-50%);}</style>
</head>
<body><div class="content"></div>
</body>
</html>

页面效果如下:

方法三:使用CSS3的弹性布局(flex)

使用CSS3的弹性布局很简单,只要设置父元素(这里是指body)的display的值为flex即可,代码如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>CSS水平垂直居中</title><style>html,body {width: 100%;height: 100%;margin: 0;padding: 0;}body {display: flex;align-items: center; /*定义body的元素垂直居中*/justify-content: center; /*定义body的里的元素水平居中*/}.content {width: 300px;height: 300px;background: blue;}</style>
</head>
<body><div class="content"></div>
</body>
</html>

页面效果如下:

原文地址:CSS实现垂直居中的常用方法

转载于:https://www.cnblogs.com/songsongblue/p/10935289.html

CSS如何水平垂直居中?相关推荐

  1. 你知道CSS实现水平垂直居中的第10种方式吗?

    你知道CSS实现水平垂直居中的第10种方式吗? 仅居中元素定宽高适用: absolute + 负 margin absolute + margin auto absolute + calc 居中元素不 ...

  2. 清除浮动的方法总结CSS实现水平垂直居中方法总结

    1.清除浮动的方法总结 当容器的高度为auto,且容器的内容中有浮动(float为left或right)的元素,在这种情况下,容器的高度不能自动伸长以适应内容的高度,使得内容溢出到容器外面而影响(甚至 ...

  3. html字居右垂直设置,css文字水平垂直居中怎么设置?

    css文字水平垂直居中怎么设置?下面本篇文章就来给大家介绍使用CSS设置文字水平居中和垂直居中的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 1.文字水平居中 在CSS中想要 ...

  4. CSS实现水平垂直居中的方式有哪些?

    目录 CSS实现水平垂直居中的方式有哪些? 1.利用flex布局 2.利用flex+margin 3.利用定位,子绝父相 3.1.利用margin: auto(偏移量都为0) 3.2.利用平移tran ...

  5. css实现水平垂直居中的七种方式

    css实现水平垂直居中的七种方式 一.使用grid布局 二.使用flex布局 三.使用定位+外边距 四.使用定位+平移 五.使用外边距 + 平移 六.使用文本对齐 + 行高 七.使用表格单元 一.使用 ...

  6. html字体如何设置垂直居中显示,css文字水平垂直居中怎么设置?

    css文字水平垂直居中怎么设置?下面本篇文章就来给大家介绍使用CSS设置文字水平居中和垂直居中的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 1.文字水平居中 在CSS中想要 ...

  7. CSS图片水平垂直居中的三种方法

    CSS图片水平垂直居中的三种方法 设置行高 使用定位和translate 使用弹性盒子 设置行高 我们知道img元素为行内快元素,所以首先设置图片元素的父元素里文字水平居中,行高与整体高度一致. 其次 ...

  8. css 垂直居中_html中div使用CSS实现水平/垂直居中的多种方式

    CSS中的居中,在工作中,会经常遇到.它可以分为水平居中和垂直居中,以下是几种实现居中的方式. 以下例子中,涉及到的CSS属性值. .parent-frame { width: 200px; heig ...

  9. CSS实现水平垂直居中的方法总结

    在CSS-trick 上面有一篇很棒的博文介绍CSS中的水平垂直居中 戳这里. 在这里我强烈推荐这篇文章的原因是:整篇文章的逻辑结构非常清晰.我之前也看过不少CSS实现垂直居中相关的博客,很多博客的总 ...

最新文章

  1. markdown 使用
  2. Objective-C学习—UIWebView的使用
  3. weblogic常见漏洞
  4. centos7安装zabbix时的一些注意事项
  5. 56. Attribute value 属性
  6. tree 树 搜索关键字
  7. 车牌正则oracle,中国车牌号正则表达式
  8. Windows server 2012 R2 无法安装vc2015
  9. Hello I am whiter. Nice to meet you!!!
  10. 局域网内两台电脑设置共享文件夹并访问
  11. win10开热点后,手机连接显示IP配置失败(或显示正在连接中)
  12. win10安装php8.0
  13. 计算机打印机副机无法打印,打印机共享无法打印怎么办,教您解决电脑打印机共享无法打印...
  14. 数据资产运营 = 数据资产盘点 + 数据治理 + 数据价值实现
  15. es 一个字段设置多个分词器
  16. html如何给header添加token,将Token添加到请求头Header中
  17. 利用vs将cs文件编译成dll文件
  18. 江西省中小学生学籍管理-小学新生注册(4)
  19. 05. 微信公众号消息加解密
  20. Spring Data Neo4j

热门文章

  1. fpga运算服务器_SparseArray替代HashMap来提高性能
  2. 6个座位办公室最佳位置_办公室座位最佳位置(讲解)
  3. python关键词提取_如何从Python格式字符串中提取关键字? - python
  4. Android tv开发px,【Android】TV端项目开发挖坑总结
  5. Maven Install报错:Perhaps you are running on a JRE rather than a JDK
  6. Spring-cloud Config Server 3种配置方式
  7. SpringBoot使用@Scheduled创建定时任务
  8. rabbitmq如何清空queue队列数据
  9. Ubuntu关闭防火墙
  10. 在 ML2 中配置 OVS vlan network - 每天5分钟玩转 OpenStack(136)