美文网首页
CSS图片居中(水平居中和垂直居中)

CSS图片居中(水平居中和垂直居中)

作者: Cherry丶小丸子 | 来源:发表于2019-07-08 16:01 被阅读0次

css图片水平居中

1、利用margin: 0 auto实现图片水平居中
    <div style="width: 500px; height: 500px; border: green solid 1px;">
        <img src="images/top.png" style="display:block; margin: 0 auto;" />
    </div>
2、利用文本的水平居中属性text-align: center
   <div style="text-align: center; width: 500px; height: 500px; border: green solid 1px;">
       <img src="images/top.png" style="display: inline-block;" />
   </div>
3、利用父元素display:flex; flex-direction:column;子元素align-self: center实现CSS水平居中。  
   <div style="width: 500px ; height:500px; display: flex; flex-direction:column; border: green solid 1px;">
       <img src="images/top.png" style="align-self: center" />
   </div>

css图片垂直居中

1、利用高==行高实现图片垂直居中
   <div style="width: 500px ;height:500px; line-height:500px; border: green solid 1px;">
       <img src="images/top.png" style="display: inline-block; vertical-align: middle;" />
   </div>
2、利用父元素display:table,子元素display:table-cell的方式实现CSS垂直居中
   <div style="width: 500px; height:500px; display: table; border: green solid 1px;">
       <span style="display: table-cell; vertical-align: middle; ">
           <img src="images/top.png"/>
       </span>
   </div>
3、利用父元素display:flex;子元素align-self: center实现CSS垂直居中。  
   <div style="width: 500px ; height:500px; display: flex; border: green solid 1px;">
       <img src="images/top.png" style="align-self: center" />
   </div>
4、利用隐藏节点实现CSS垂直居中。
   <div style="width: 500px ; height:500px; border: green solid 1px;">
       <div style="width:50%; height:25%;"></div>
       <img src="images/top.png" style="height:50%;" />
   </div>

css图片水平垂直居中

1、利用文本水平,行高垂直实现水平垂直居中
   <div style="width: 500px ; height:500px; line-height:500px; text-align:center; border: green solid 1px;">
       <img src="images/top.png" style="display: inline-block; vertical-align: middle;" />
   </div>
2、利用绝对定位实现图片水平垂直居中
   <div style="width: 500px; height:500px; position: relative; border: green solid 1px;">
       <img src="images/top.png" style="width: 120px; height: 40px; position: absolute; left:50%; top: 50%; margin-left: -60px; margin-top: -20px;" />
   </div>
3、移动端可以利用flex布局实现css图片水平垂直居中
   <div style="width: 500px; height:500px; display: flex; justify-content: center; align-items: center; border: green solid 1px;">
       <img src="images/top.png"/>
   </div>

4、相对绝对,利用上下左右为0,margin:auto;实现水平垂直居中
   <div style="width: 500px; height:500px; position:relative;  border: green solid 1px;">
       <img src="images/top.png"/ style="width:300px; height:300px; position: absolute; left:0; right:0; top:0; bottom:0; margin:auto;">
   </div>
5、一个未知宽高的div,利用 transform: translate(-50%,-50%)实现水平垂直居中;
   <div style="width: 500px; height:500px; position:relative;  border: green solid 1px;">
       <img src="images/top.png"/ style="width:300px; height:300px; position: absolute; top:50%; left:50%; transform: translate(-50%,-50%);">
   </div>

相关文章

  • css 图片居中

    css图片居中(水平居中和垂直居中) css图片水平居中 块状元素直接用text-align:center, di...

  • CSS图片居中(水平居中和垂直居中)

    css图片水平居中 css图片垂直居中 css图片水平垂直居中

  • css 居中

    居中有水平居中和垂直居中。 水平居中+垂直居中 flex法 position法 就是计算呗~ 参考 CSS各种居中...

  • CSS - 垂直水平居中方法

    前言 总括:整理 css 垂直水平居中方法,区分内联元素与块级元素 CSS垂直居中和水平居中 用css让一个容器水...

  • CSS居中完全指南——构建CSS居中决策树

    CSS居中完全指南——构建CSS居中决策树 本文总结CSS居中,包括水平居中和垂直居中.本文相当于CSS决策树,下...

  • CSS居中小结

    CSS居中总结 最近在学习CSS居中,居中里面又包含水平居中和垂直居中,分不太清内联元素(inline or in...

  • 常用的居中方法

    本文将介绍的居中方法有 ,水平居中,垂直居中和水平垂直居中。 一水平居中 二水平垂直居中

  • css水平居中和水平垂直居中

    水平居中和水平垂直居中demo

  • css各种居中方法总结

    CSS中的居中可分为水平居中和垂直居中。水平居中分为行内元素居中和块状元素居中两种情况,而块状元素又分为定宽块状元...

  • 定位和居中问题

    CSS的居中分水平居中和垂直居中,如果要让元素水平、垂直同时据中,不同情况下有不同的方法 分类: 居中元素大小是固...

网友评论

      本文标题:CSS图片居中(水平居中和垂直居中)

      本文链接:https://www.haomeiwen.com/subject/krqahctx.html