首页 技术 正文
技术 2022年11月21日
0 收藏 891 点赞 2,633 浏览 900 个字

一:水平居中方案:
  1、行内元素
    设置 text-align:center

  2、定宽块状元素
    设置 左右 margin 值为 auto

  3、不定宽块状元素
    a:在元素外加入 table 标签(完整的,包括 table、tbody、tr、td),该元素写在 td 内,然后设置 margin 的值为 auto
    b:给该元素设置 displa:inine 方法
    c:父元素设置 position:relative 和 left:50%,子元素设置 position:relative 和 left:50%

  4、对于多个块级元素:
    对父元素设置 text-align: center;
    对子元素设置 display: inline-block;

  5、使用 flex 布局

二:垂直居中设置

  1、行内元素
    单行:
      设置上下 pandding 相等;
      或者设置 line-height 和 height 相等

    多行:
      设置上下 pandding 相等;
      父元素设置 display: table-cell; 和 vertical-align: middle;
      或者使用 flex 布局;

  2、块级元素:下面前两种方案,父元素需使用相对布局
    父元素已知高度:子元素使用绝对布局 top: 50%,再用负的 margin-top 把子元素往上拉一半的高度;
    父元素未知高度:子元素使用绝对布局 position: absolute; top: 50%; transform: translateY(-50%);
    使用 Flexbox:选择方向,justify-content: center;

三:水平垂直居中

  定高定宽:
    先用绝对布局 top: 50%; left: 50%;,再用和宽高的一半相等的负 margin 把子元素回拉;

  高度和宽度未知:
    先用绝对布局 top: 50%; left: 50%;,再设置 transform: translate(-50%, -50%);

  使用 Flexbox:
    justify-content: center; align-items: center;

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903