zoukankan      html  css  js  c++  java
  • 浮动div中的图片垂直居中

     

     table-cell方法垂直水平居中

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="description" content="table-cell方法居中">
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <style>
    .upload_img {
      position: relative;
      width: 150px;
      height: 150px;
      border: 1px solid #ccc;
      display: table-cell;
      background: #eee;
      vertical-align: middle;
    }
    .upload_img img {
      border: none;
      max-width: 100%;
      max-height: 100%;
      display: block;
      margin: auto;
    }
    </style>
    
    <body>
        <div id="imgdiv3" class="upload_img">
               <img src="http://images2015.cnblogs.com/blog/653009/201603/653009-20160310103258132-1841991840.jpg"  id="imgShow3"  >
          </div>
    </body>
    </html>

     

    当div浮动的时候就无法使用上面的方法进行垂直居中了,接下来就用到line-height进行居中了

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="description" content="[add your bin description]">
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
        <style>
                .upload-img {
                float:right;
          width: 200px;
          height: 200px;
          background-color: #ccc;
          line-height: 200px;
          vertical-align: middle;
          text-align: center
        }
    img{
      vertical-align: middle;max-width:100%;max-height:100%;
    }
        </style>    
    <body>
    <div>
      <div class="upload-img">
        <img  src="http://images2015.cnblogs.com/blog/653009/201603/653009-20160310103258132-1841991840.jpg">
       </div>
    </div>
    
    </body>
    </html>

    有个问题值得注意,编写代码的时候没有添加

    <!doctype html>

    造成了没有办法垂直居中!

    代码演示:https://jsfiddle.net/silence19/djmznmpa/

  • 相关阅读:
    安装vmware tools 使用hgfs共享文件一波三折
    RedHat停止sendmail加快启动
    [宏]_IO, _IOR, _IOW, _IOWR 宏的用法与解析
    __VA_ARGS__
    超级终端串口发送命令,uboot接收不到
    statfs函数获取大容量磁盘信息速度慢的解决过程
    ctags使用简介
    做技术多久才能入门
    linux目录和文件介绍
    OpenScalesLayer
  • 原文地址:https://www.cnblogs.com/silences/p/6178034.html
Copyright © 2011-2022 走看看