zoukankan      html  css  js  c++  java
  • CSS实现不固定宽度和高度的自动居中

    有时候我们需要实现下面这种效果:

    嘎嘎,撑大高度不让你剧中
    嘎嘎,撑大高度不让你剧中
    嘎嘎,撑大高度不让你剧中
    嘎嘎,撑大高度不让你剧中
    嘎嘎,撑大高度不让你剧中
    嘎嘎,撑大高度不让你剧中
    嘎嘎,撑大高度不让你剧中
    我要居中

    直接上代码:

    html:

      <div class="main">
        <div class="left">
          嘎嘎,撑大高度不让你剧中
          <br/> 嘎嘎,撑大高度不让你剧中
          <br/> 嘎嘎,撑大高度不让你剧中
          <br/> 嘎嘎,撑大高度不让你剧中
          <br/> 嘎嘎,撑大高度不让你剧中
          <br/> 嘎嘎,撑大高度不让你剧中
          <br/> 嘎嘎,撑大高度不让你剧中
          <br/>
        </div>
        <div class="right">
          <span>我要居中</span>
        </div>
      </div> 
    

    css:

    * {
      padding: 0;
      margin: 0;
    }
    .main {
       100%;
      display: table;
    }
    .left {
      text-align: center;
      vertical-align: middle;
      display: table-cell;
       50%;
      box-sizing: border-box;
      border: 1px solid #ddd;
    }
    .right {
      display: table-cell;
      vertical-align: middle;
      text-align: center;
       50%;
      box-sizing: border-box;
      border: 1px solid #ddd;
    
      //针对ie6的hack
      _position: absolute;
      _top: 50%;
    }
    .right span {
      //针对ie6的hack
      _position: relative;
      _top: -50%;
    }
    

    由 ie6 hack 想到的 内容不确定高度宽度的居中:

    内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中, 内容居中,内容居中,内容居中,内容居中

    html:

    <div class="center-main">
      <div class="center-content">
        <span>内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,内容居中,      内容居中,内容居中,内容居中,内容居中</span>
      </div>
    </div>
    

    css:

    * {
      padding: 0;
      margin: 0;
    }
    html, body {
       100%;
      height: 100%;
    }
    .center-main {
       100%;
      height: 100%;
      position: absolute;
    }
    .center-content {
      position: relative;
      text-align: center;
      top: 50%;
    }
    .content span {
      top: -50%;
    }
    -----------------------------------------------------------------------
    Simple is Beautiful,Less is More.
    --FuGardenia
  • 相关阅读:
    centos7安装rlwrap
    Linux CentOS 7的图形界面安装(GNOME、KDE等)
    在oracle下我们如何正确的执行数据库恢复
    Viewer.js 图片预览插件使用
    深拷贝和浅拷贝
    ES6 export,import报错
    Yarn 命令详解
    npm命令 VS yarn命令
    Windows下nginx作为静态资源服务器使用
    关于Vue脚手架写法的问题
  • 原文地址:https://www.cnblogs.com/yunqianduan/p/4685163.html
Copyright © 2011-2022 走看看