继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

image图片怎么居中显示

陪伴而非守候
关注TA
已关注
手记 358
粉丝 62
获赞 285

I find three ways to center an image in a div:

<style type="text/css">
        .container1
        {
            border: solid 1px #666;
            width: 100px;
            height: 100px;
        }
        .container1 img
        {
            margin: 42px 0px 0px 42px; /* (100-16)/2 = 42 */
        }
        .container3
        {
            border: solid 1px #666;
            width: 100px;
            height: 100px;
            position: relative;
        }
        .container3 img
        {
            position: absolute;
            top: 42px;
            left: 42px; /* (100-16)/2 = 42 */
        }
        .container4
        {
            border: solid 1px #666;
            width: 100px;
            height: 100px;
            position: relative;
        }
        .container4 img
        {
            position: absolute;
            top: 50%;
            margin-top: -8px; /* 16/2 = 8 */
            left: 50%;
            margin-left: -8px;
        }
    </style>
    <div class="container1">
        <img src="mini/clock.gif" mce_src="mini/clock.gif" alt="" />
    </div>
    <br />
    <div class="container3">
        <img src="mini/clock.gif" mce_src="mini/clock.gif" alt="" />
    </div>
    <br />
    <div class="container4">
        <img src="mini/clock.gif" mce_src="mini/clock.gif" alt="" />
    </div>

In my opinion, the third method is graceful and accurate, which i borrow from my last example.


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP