div垂直居中的方法_【CSS】8种常见的垂直水平居中方法

de91e901119fcb12173d4af6d7fcbae7.png

1.比较传统的方式 absolute+margin负值偏移

特点:父容器要设置宽高,需要知道子容器宽高,偏移量是子容器宽高的一半且是负值,兼容性好

  /* absolute+margin负值偏移布局 */.layout.absolute {position: relative;}.layout.absolute  article {position: absolute;top: 50%;left: 50%;margin-top: -50px;margin-left: -160px;}<section class="layout absolute"><article><p>1.absolute+margin负值偏移<br>(父容器要设置宽高,需要知道子容器宽高)</p></article></section>

2.absolute+transform布局

特点:父容器要设置宽高

  /* absolute+transform布局 */.layout.transform {position: relative;}.layout.transform article {position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);}<section class="layout transform"><article><p>2.absolute+transform<br>(父容器要设置宽高)</p></article></section> 

3.flex布局

特点:自适应

  /* flex布局 */.layout.flex {display: flex;justify-content: center;align-items: center;}<section class="layout flex"><article><p>3.flex<br>(自适应)</p></article></section>

4.table-cell布局

特点:比其他方法多1个容器,注意边距重叠

  .layout.table-cell {display: table;width: 100%;}.layout.table-cell .outer {display: table-cell;vertical-align: middle;text-align: center;}.layout.table-cell article {margin: 0 auto;overflow: auto;}<section class="layout table-cell"><div class="outer"><article><p>4.table-cell<br>(多1个容器,注意边距重叠)</p></article>                      </div></section> 

5.grid布局

特点:自适应,要考虑兼容性

   .layout.grid{display: grid;}.layout.grid article {justify-self: center;align-self: center;}<section class="layout grid"><article><p>5.grid<br>(自适应,要考虑兼容性)</p></article></section> 

6.inline-block布局

特点:自适应,兼容性好

   .layout.inline-block {text-align: center;}.layout.inline-block::after {content: '';height: 100%;display: inline-block;vertical-align: middle;}.layout.inline-block article {display: inline-block;vertical-align: middle;}<section class="layout inline-block"><article><p>6.inline-block<br>(自适应)</p></article></section> 

7.absolute + margin-auto 布局

特点:内外容器都要设置宽高,兼容性好

  .layout.margin-auto {position: relative;}.layout.margin-auto article{position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;}<section class="layout margin-auto"><article><p>7.absolute + margin-auto<br>(内外容器都要设置宽高) </p></article></section>

8.line-height 布局

特点:只适合单行inline,而且要知道父容器的高度,兼容性好

.layout.line-height {line-height: 150px;}.layout.line-height article {display: inline;margin:  auto;}<section class="layout line-height"><article><p>8.line-height(只适合单行inline,而且要知道父容器的高度)</p></article></section>

完整例子:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>垂直居中</title><style>html,body {padding: 0;margin: 0;}.layout {margin-bottom: 15px;height: 150px;background-color: blueviolet;background-clip: content-box;}.layout article {background-color: lightcoral;color: #fff;width: 320px;height: 100px;  text-align: center;}/* absolute+margin负值偏移布局 */.layout.absolute {position: relative;}.layout.absolute  article {position: absolute;top: 50%;left: 50%;margin-top: -50px;margin-left: -160px;}/* absolute+transform布局 */.layout.transform {position: relative;}.layout.transform article {position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);}/* flex布局 */.layout.flex {display: flex;justify-content: center;align-items: center;}/* table-cell布局 */.layout.table-cell {display: table;width: 100%;}.layout.table-cell .outer {display: table-cell;vertical-align: middle;text-align: center;}.layout.table-cell article {margin: 0 auto;overflow: auto;}/* grid布局 */.layout.grid{display: grid;}.layout.grid article {justify-self: center;align-self: center;}/* inline-block布局 */.layout.inline-block {text-align: center;}.layout.inline-block::after {content: '';height: 100%;display: inline-block;vertical-align: middle;}.layout.inline-block article {display: inline-block;vertical-align: middle;}/* absolute + margin-auto */.layout.margin-auto {position: relative;}.layout.margin-auto article{position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;}/* line-height 布局 */.layout.line-height {line-height: 150px;}.layout.line-height article {display: inline;margin:  auto;}</style>
</head>
<body><section class="layout absolute"><article><p>1.absolute+margin负值偏移<br>(父容器要设置宽高,需要知道子容器宽高)</p></article></section><section class="layout transform"><article><p>2.absolute+transform<br>(父容器要设置宽高)</p></article></section><section class="layout flex"><article><p>3.flex<br>(自适应)</p></article></section><section class="layout table-cell"><div class="outer"><article><p>4.table-cell<br>(多1个容器,注意边距重叠)</p></article></div></section><section class="layout grid"><article><p>5.grid<br>(自适应,要考虑兼容性)</p></article></section><section class="layout inline-block"><article><p>6.inline-block<br>(自适应)</p></article></section><section class="layout margin-auto"><article><p>7.absolute + margin-auto<br>(内外容器都要设置宽高) </p></article></section><section class="layout line-height"><article><p>8.line-height(只适合单行inline,而且要知道父容器的高度)</p></article></section></body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/498978.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

websocket 导致大量apache进程_Swoole 服务端主动向websocket推送消息

在之前的博文中&#xff0c;我们已经学完了如果使用swoole搭建websocket长连接&#xff0c;也学会了swoole的多进程数据共享操作。但在一个完整的websocket长连接日常操作链中&#xff0c;服务端往往会主动给在线的用户单独推送消息&#xff0c;会群发一些消息。在Swoole-webso…

python因子分析法_python——因子分析

因子分析用Python做的一个典型例子 一、实验目的 采用合适的数据分析方法对下面的题进行解答二、实验要求 采用因子分析方法&#xff0c;根据48位应聘者的15项指标得分&#xff0c;选出6名最优秀的应聘者。 三、代码 importpandas aspd importnumpy asnp importmath asmath imp…

polycom安卓手机客户端_三款免费「游戏串流」APP,在手机/电视上玩PC游戏

以前想把电脑游戏输出到电视机上玩&#xff0c;得用HDMI线输出&#xff0c;如果电脑主机和电视机离太远&#xff0c;HDMI线不够长&#xff0c;还得抱电脑主机到电视机旁边&#xff0c;非常不方便。随着技术的发展&#xff0c;现在已经有「串流」功能&#xff0c;只要电视机和电…

python获取返回值_python如何获取函数的返回值

函数需要先定义后调用&#xff0c;函数体中 return 语句的结果就是返回值。如果一个函数没有 reutrn 语句&#xff0c;其实它有一个隐含的 return 语句&#xff0c;返回值是 None&#xff0c;类型也是 NoneType。return 语句的作用&#xff1a;结束函数调用、返回值 指定返回值…

java模拟器百度_Java模拟实现百度文档在线浏览

这个思路是我参考网上而来&#xff0c;代码是我实现。采用Apache下面的OpenOffice将资源文件转化为pdf文件&#xff0c;然后将pdf文件转化为swf文件&#xff0c;用FlexPaper浏览。ok&#xff0c;A、下载OpenOffice (转换资源文件)B、下载JodConverter(调用OpenOffice)C、下载Sw…

body click js 委托_JS 事件循环

进程 线程CPU 分配资源的最小单位是进程&#xff0c;同一个时间内单个 CPU 只能运行一个进程&#xff0c;单个 CPU 一次只能运行一个任务CPU 调度的最小单位是线程&#xff0c;一个进程里面包含多个线程。可以看看阮老师的这篇文章&#xff0c;进程与线程的一个简单解释浏览器的…

django jsonresponse_利用 Django 动态展示 Pyecharts 图表数据的几种方法

本文将介绍如何在 web 框架 Django 中使用可视化工具 Pyecharts, 看完本教程你将掌握几种动态展示可视化数据的方法!Django 模板渲染1. 新建一个 Django 项目命令行中输入以下命令django-admin startproject pyecharts_django_demo创建一个应用程序python manage.py startapp d…

python网页登录验证码不显示_进网页需要验证码?不好意思,Python从来不惧各种验证码!...

今天要来说说滑动验证码了大家应该都很熟悉点击滑块然后移动到图片缺口进行验证现在越来越多的网站使用这样的验证方式为的是增加验证码识别的难度那么&#xff0c;对于这种验证码应该怎么破呢接下来就是见证神奇的时刻打开 b 站的登录页面可以看到登录的时候需要进行滑块验证按…

怎么调用新建模型里文章的内容_优雅地进行Tensorflow Lite模型转换

初涉知乎江湖&#xff0c;知道大佬很多&#xff0c;请温柔以待&#xff01;&#xff01;&#xff01;七日凌晨&#xff0c;谷歌连夜发布了有关于Tensorflow最新成果和技术&#xff0c;这应该是贾扬清离开脸书后另一个深度学习界令人惊呼的事件了吧&#xff01;&#xff08;旁白…

git add后取消_Git常用命令-总结

创建git用户$ git config --global user.name "Your Name"$ git config --global user.email "emailexample.com"初始化一个Git仓库&#xff0c;使用git init命令。添加文件到Git仓库&#xff0c;分两步&#xff1a;使用命令git add &#xff0c;注意&…

查看csv编码_[小O地图-数据] - 坐标转地址文字(逆地理编码)

小O地图是一款基于互联网地图进行地理数据处理、分析、图表的软件。致力为广大科研人员提供专业地图数据&#xff0c;用于科研及学习。小&#xff2f;地图提供将【经纬度坐标转换为地址】的功能&#xff0c;例如&#xff1a;将“116.359861, 39.917225” 转换为 “北京市西城区…

python自动化办公源码_python自动化办公:文件篇(自动整理文件,一键完成)

import os list_all[]#初始化一个空列表 for root ,dirs,files in os.walk(rC:UsersShineionDesktop新建文件夹): for name in files: file_pathos.path.join(root,name)#包含路径的文件 file_nameos.path.split(file_path)[-1] list_all.append(file_name) print(list_all)如果…

ocr中文数据集_CNOCR:测试集准确率最高98%,自带识别模型的中文OCR包

今天 Gitee 为大家介绍的是一款中文 OCR 包。大家都知道&#xff0c;训练模型是一件非常费时费力的事情&#xff0c;但今天这款项目已经自带训练好的识别模型&#xff0c;我们只需要下载下来使用即可&#xff0c;可以说是非常方便了&#xff0c;那么下面我们就去看看这个项目的…

sha算法源代码java_SHA算法Java实现

一 简介安全散列算法固定长度摘要信息二 SHA算法SHA-1、SHA-2(SHA-224、SHA-256、SHA384、SHA-512)三 SHA算法实现package com.imooc.security.sha; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.Security; impo…

arraylist remove() java_执行ArrayList的remove(object)方法抛异常?

简介或许有很多小伙伴都尝试过如下的代码&#xff1a;然后会发现抛出java.util.ConcurrentModificationException异常&#xff0c;这是一个并发异常。那么这个到底是什么情况&#xff1f;首先需要介绍一下增强for循环增强for循环增强for循环是Java1.5后&#xff0c;Collection实…

centos gdb调试_gdb-miss-debuginfo

使用gdb调试core时候&#xff0c;提示Missing separate debuginfos的解决办法。错误提示错误提示信息如下&#xff1a;Missing separate debuginfos, use: debuginfo-install glibc-2.17-106.el7_2.8.x86_64 libaio-0.3.109-13.el7.x86_64 libgcc-4.8.5-4.el7.x86_64 libstdc-4…

怎么判断自己启动的线程是否执行完成 java_多线程?怎么用?

Java线程&#xff1a;概念与原理操作系统中线程和进程的概念现在的操作系统是多任务操作系统。多线程是实现多任务的一种方式。进程是指一个内存中运行的应用程序&#xff0c;每个进程都有自己独立的一块内存空间&#xff0c;一个进程中可以启动多个线程。比如在Windows系统中&…

mysql设置中文_Flask中MySQL预热

Flask和MySQL导读现在Flask已经学习到了类视图,它其实是这样一个关系FLask高级视图视图函数app.route\add_url_rule类视图蓝图blueprint(这个接下来要讲)结束蓝图后,接着就是Flask数据库的结合SQLAlchemy.在学习SQLAlchemy之前,希望和大家一起了解学习一下mysql这篇文章不涉及理…

html首行缩进2字符怎么设置_Word如何设置首行缩进2字符

在使用Word编写书籍或者文稿时&#xff0c;我们都会有个习惯将每个段落前面空两个字符&#xff0c;这样可以很好的方便读者阅读时分辨段落。有的人习惯用几个空格来代替&#xff0c;有的人习惯用空白字符V1来代替(像Word联盟网站上的每篇教程就是用V1来标记段落的)。其实在Word…

java 面试什么是类_Java 面试题代码类收集

long temp(int)3.9;System.out.println(temp);temp%2;System.out.println(temp);31View CodeJava里面类的方法名可以与类名一样&#xff0c;与构造函数的区别是方法有返回值&#xff0c;构造函数没有。public classUserInfo {publicUserInfo(){}publicString UserInfo(){return…