zoukankan      html  css  js  c++  java
  • 让图片在高度确定的块元素中垂直居中

    代码一:

    示例一
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb18030" />
    <title>Untitled Document</title>
    <style type="text/css">
    a
    {
        
    /* 以下行在标准浏览器必需,IE6可选 */
        display
    :table-cell;
        vertical-align
    :middle;
        width
    :300px;
        height
    :300px;
        
        
    /* 以下可选 */
        text-align
    :center;
        border
    :1px solid #CCC;
    }
    img
    {
        border
    :1px dotted #999;
    }
    </style>
    <!--[if lte IE 7]>
    <style type="text/css">
    a{
        line-height:300px; /* 与 a 的高度相同 */
        display:block; /* 不支持 table-cell,用 block 替代,否则不能水平居中 */
    }
    img{
        vertical-align:middle;
    }
    span{
        display:inline-block;
    }
    </style>
    <![endif]
    -->

    </head>

    <body>
        
    <href="#" /><img src="http://img.baidu.com/img/img-jg.gif" /><span></span></a>
    </body>
    </html>

    代码二:

    示例二
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb18030" />
    <title>Untitled Document</title>
    <style type="text/css">
    div.outer
    {
        width
    :300px;
        height
    :300px;
        
        
    /* 以下可选 */
        text-align
    :center;
        border
    :1px solid #CCC;
    }
    span
    {
        display
    :inline-block;
        line-height
    :300px;
        width
    :0px;
        visibility
    :hidden;
    }
    div.inner
    {
        display
    :inline-block;
        *display
    :inline;
        vertical-align
    :middle;
    }
    img
    { border:1px dotted #CCC; }
    </style>
    </head>

    <body>

    <div class="outer">
        
    <span>.</span><div class="inner"><img src="http://img.baidu.com/img/img-jg.gif" /></div>
    </div>

    </body>
    </html>

    代码三:

    代码
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb18030" />
    <title>Untitled Document</title>
    <style type="text/css">
    .box 
    {
            
    /*非IE的主流浏览器识别的垂直居中的方法*/
            display
    : table-cell;
            vertical-align
    :middle;

            
    /*设置水平居中*/
            text-align
    :center;

            
    /* 针对IE的Hack */
            *display
    : block;
            *font-size
    : 175px;/*约为高度的0.873,200*0.873 约为175*/
            *font-family
    :Arial;/*防止非utf-8引起的hack失效问题,如gbk编码*/

            width
    :200px;
            height
    :200px;
            border
    : 1px solid #eee;
    }
    .box img 
    {
            
    /*设置图片垂直居中*/
            vertical-align
    :middle;
    }
    </style>
    </head>

    <body>

    <div class="box">
        
    <img src="http://img.baidu.com/img/img-jg.gif" />
    </div>

    </body>
    </html>


  • 相关阅读:
    在你想要放弃的那一刻,想想为什么当初坚持走到了这里
    将来的你一定会感谢现在奋斗的自己
    学习改变命运,拼搏成就未来
    springmvc的DispatcherServlet源码——doDispatch方法解析
    SpringMVC02 AbstractController And MultiActionController
    SpringMVC简介01
    NIO
    数据库的锁机制
    Spring和MyBatis环境整合
    事务的隔离级别和mysql事务隔离级别修改
  • 原文地址:https://www.cnblogs.com/heiing/p/1717377.html
Copyright © 2011-2022 走看看