zoukankan      html  css  js  c++  java
  • 元素居中布局

    水平居中和垂直居中

    水平居中方法1(margin:0 auto):

     垂直居中方法1(table-cell + vertical-align:middle):

    (以下因为在编辑器上截图显示不全,故只上代码) 

    水平居中方法2(绝对定位):

    <div style=" 300px ; height:300px; background-color: red; position: relative">
      <div style=" 100px;height:100px;background-color: yellow ;position: absolute; left:50%;margin-left: -50px"></div>
    </div>

    同理对应垂直居中方法2:

    <div style=" 300px ; height:300px; background-color: red; position: relative">
      <div style=" 100px;height:100px;background-color: yellow ;position: absolute; top:50%;margin-top: -50px"></div>
    </div>

    水平居中方法3(子div浮动+相对定位):

    <div style=" 300px ; height:300px; background-color: red ;">
      <div style="float:left;margin-left:50%; position:relative; left:-50px; 100px;height:100px;background-color: yellow ; "></div>
    </div>

    同理对应垂直居中方法3:

    <div style=" 300px ; height:300px; background-color: red ;">
      <div style="float:left;margin-top:50%; position:relative; top:-50px; 100px;height:100px;background-color: yellow ; "></div>
    </div>

    水平居中方法4(flex):

    <div style="display:flex; justify-content:center ; 300px ; height:300px; background-color: red ;">
      <div style=" 100px;height:100px;background-color: yellow ; "> </div>
    </div>

    同理垂直居中方法4:

    <div style="display:flex; align-items:center; 300px ; height:300px; background-color: red ;">
      <div style=" 100px;height:100px;background-color: yellow ; "> </div>
    </div>

     

  • 相关阅读:
    第2章 面试流程
    py面试指导系列-第1章 py服务端工程师面试指导
    十月的第一天
    I shall return
    k8S
    Eclipse编辑器基本设置
    Windows 下有什么软件能够极大地提高工作效率
    Bootstrap 轻量级后台管理系统模板--ACE使用介绍
    2014年将会受欢迎的IT技能--你有多少哪?
    面向程序员的数据库访问性能优化法则
  • 原文地址:https://www.cnblogs.com/btsn/p/11610436.html
Copyright © 2011-2022 走看看