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

css图片居中分css图片水平居中和垂直居中两种情况,有时候还需要图片同时水平垂直居中,下面分几种居中情况分别介绍。

css图片水平居中

利用margin: 0 auto实现图片水平居中

利用margin: 0 auto实现图片居中就是在图片上加上css样式margin: 0 auto 如下:

<div style="text-align: center; width: 500px; border: green solid 1px;">
<img alt="" src="https://www.baidu.com/img/baidu_jgylogo3.gif" style="margin: 0 auto;" />
</div>

利用文本的水平居中属性text-align: center

代码如下:

<div style="text-align: center; width: 500px; border: green solid 1px;">
<img alt="" src="https://www.baidu.com/img/baidu_jgylogo3.gif" style="display: inline-block;" />
</div>

css图片垂直居中

利用高==行高实现图片垂直居中

这种方法是要知道高度才可以使用,代码如下:

<div style="text-align: center; width: 500px;height:200px; line-height:200px; border: green solid 1px;">
<img alt="" src="https://www.baidu.com/img/baidu_jgylogo3.gif" style="display: inline-block; vertical-align: middle;" />
</div>

利用table实现图片垂直居中

利用table的方法是利用了table的垂直居中属性,代码如下:

这里使用display: table;和display: table-cell;来模拟table,这种方法并不兼容IE6/IE7,IE67不支持display: table,如果你不需要支持IE67那就可以用

缺点:当你设置了display: table;可能会改变你的原有布局

<div style="text-align: center; width: 500px;height:200px; display: table;border: green solid 1px;">
<span style="display: table-cell; vertical-align: middle; "><img alt="" src="https://www.baidu.com/img/baidu_jgylogo3.gif" style="display: inline-block;" /></span>
</div>

利用绝对定位实现图片垂直居中

如果已知图片的宽度和高度可以这样,代码如下:

<div style="width: 500px;height:200px; position: relative; border: green solid 1px;">
<img alt="" src="https://www.baidu.com/img/baidu_jgylogo3.gif" style="width: 120px; height: 40px;position: absolute; left:50%; top: 50%; margin-left: -60px;margin-top: -20px;" />
</div>

移动端可以利用flex布局实现css图片垂直居中

移动端一般浏览器版本都比较高,所以可以大胆的使用flex布局,(flex布局参考css3的flex布局用法)演示代码如下:

css代码:
<style type="text/css">
/*web前端开发http://www.51xuediannao.com/*/
.ui-flex {
display: -webkit-box !important;
display: -webkit-flex !important;
display: -ms-flexbox !important;
display: flex !important;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap
} .ui-flex, .ui-flex *, .ui-flex :after, .ui-flex :before {
box-sizing: border-box
} .ui-flex.justify-center {
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center
}
.ui-flex.center {
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center
}
</style>
html代码:
<div class="ui-flex justify-center center" style="border: green solid 1px; width: 500px; height: 200px;">
<div class="cell">
<img alt="" src="https://www.baidu.com/img/baidu_jgylogo3.gif" style="" />
</div>
</div>

本文链接:css图片居中(水平居中和垂直居中

css图片居中(水平居中和垂直居中)的更多相关文章

  1. HTML/CSS:图片居中(水平居中和垂直居中)

    css图片居中(水平居中和垂直居中) css图片居中分css图片水平居中和垂直居中两种情况,有时候还需要图片同时水平垂直居中, 下面分几种居中情况分别介绍: css图片水平居中 1.利用margin: ...

  2. css图片垂直水平居中及放大(实现水平垂直居中的效果有哪些方法?)

    实现水平垂直居中方法有很多种: 一.万能法: 1.已知高度宽度元素的水平垂直居中,利用绝对定位和负边距实现. <style type="text/css"> .wrap ...

  3. css确定元素水平居中和垂直居中

    ---恢复内容开始--- 首先,我们在了解如何通过css了解元素水平和垂直居中之前,先要了解下html都有哪些元素,这些元素与偶有哪些分类,因为不同类别的元素的水平垂直居中方法是完全不同的,究其根本当 ...

  4. CSS中元素水平居中和垂直居中的方法

    #CSS中水平居中和垂直居中的方法 一. 水平居中 1.行内元素(文本,图片等) 如果被设置元素为文本.图片等行内元素时,可以通过给父元素设置` text-align:center;` 来实现 2.定 ...

  5. css图片居中,通过纯css实现图片居中的多种实现方法

    在网页布局中,图文排版是我们常用的,那么经常会遇到如何让图片居中显示呢,这篇文章将总结常用css实现图片居中的方法总结: html结构: <div class="demo" ...

  6. CSS 实现盒子水平居中、垂直居中和水平垂直居中的方法

     CSS 实现盒子模型水平居中 水平居中效果图如下: HTML: CSS 全局样式: 方法一:使用margin: 0 auto;(只适用于子盒子有宽的时候) 方法二:text-align + disp ...

  7. CSS中有关水平居中和垂直居中的解决办法

    CCS中让div等块级元素在父级元素中居中的方法: (1)div{  margin:0 auto   } 该方法只能实现水平的居中,无法实现元素的垂直居中 (2)当div元素的宽高是固定的,然后设置位 ...

  8. CSS实例:水平居中和垂直居中的多种解决方案

    1.单行垂直居中 文字在层中垂直居中vertical-align 属性是做不到的.我们这里有个比较巧妙的方法就是:设置height的高度与line-height的高度相同! Example Sourc ...

  9. 用CSS/CSS3 实现水平居中和垂直居中,水平垂直居中的方式

    一.水平居中 (1)行内元素解决方案:父为块元素+text-align: center 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: 使用te ...

随机推荐

  1. 配置远程连接mysql数据库 Connect to remote mysql database

    设有本地机器(local machine), ip地址为localip 远程机器(remote machine), ip地址remoteip 要通过在local machine的终端连接remote ...

  2. 我的Android进阶之旅------&gt;Android关于ImageSpan和SpannableString的初步了解

    近期要实现一个类似QQ聊天输入框.在输入框中能够同一时候输入文字和表情图像的功能.例如以下图所看到的的效果: 为了实现这个效果.先去了解了一下ImageSpan和SpannableString的使用方 ...

  3. MODULE_DEVICE_TABLE的理解【转】

    本文转载自:http://blog.csdn.net/sidely/article/details/39666471 在Linux IIC驱动中看到一段代码: static struct platfo ...

  4. Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar 报错

    报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...

  5. E20171028-hm

    stick   n. 棍棒,棍枝; 枝条; 操纵杆; 球棍; stopwatch  n. (赛跑等记时用的) 秒表,跑表; agency   n. 代理; 机构; 力量; various   adj. ...

  6. thinkphp的model的where条件的两种形式

    thinkphp的model的where查询时有两种形式. $model->field('id')->where('customer_num is null or customer_num ...

  7. vue-video-player视频播放插件

    安装依赖 npm install vue-video-player -S 引入配置 //main.js import 'video.js/dist/video-js.css' import 'vue- ...

  8. C#中Random

    说明:C#中的随机数是一个伪随机数,随机数字从一组有限的数字选择以相同的概率,所选的数字不是完全随机的,因为使用数学算法来选择它们.在大多数Windows系统中,Random的15毫秒内创建的对象很可 ...

  9. 汇编程序45:检测点13.2 (loop指令的中断例程)

    安装程序: assume cs:code //loop指令的替代实现 code segment start: mov ax,cs mov ds,ax mov si,offset sub1 mov ax ...

  10. java环境搭建(及安装问题“No repository found containing”解决) 并创立第一个java程序

    环境: java8 及 Eclipse java8 配置:http://jingyan.baidu.com/article/e2284b2b5967e7e2e7118d74.html Eclipse ...