html中div内容居中的方法

html中div内容居中的方法方法 1 table celldiv 中的内容居中 不改变盒子尺寸 DOCTYPEhtmlh en headmetachar UTF 8 titleTitle titlestyledi background color red widt

一、div内容 居中的方法:

方法1:table-cell

div中的内容居中:不改变盒子尺寸。

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
      title> <style> div { 
        background-color: red; width: 300px; height: 300px; /*此元素会作为一个表格单元格显示 (类似 和 )*/ display: table-cell; /*垂直居中 */ vertical-align: middle; /*水平居中*/ text-align: center; }  
       style>  
        head> <body> <div>  
        <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
         div>  
          body>  
           html> 

方法2:display:flex

div中的内容居中:不改变盒子尺寸

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
      title> <style> div { 
        background-color: red; width: 300px; height: 300px; display: flex; // justify-content: center; /*水平居中*/ align-items: Center; /*垂直居中*/ }  
       style>  
        head> <body> <div> <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
         
         div>  
          body>  
           html> 

方法3:绝对定位和负边距

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
      title> <style> div { 
        background-color: red; width: 300px; height: 300px; position: relative; } img { 
        position: absolute; width: 150px; height: 100px; /*img左上角移动到html的中心*/ top: 50%; left: 50%; /*img左上角向左上角分别移动自身的一半距离*/ margin-left: -75px; margin-top: -50px; /*text-align: center;*/ }  
       style>  
        head> <body> <div> <img src="../share_icon_sina_weibo.png"/>  
         
         div>  
          body>  
           html> 

方法4:绝对定位和0

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
      title> <style> div { 
        background-color: red; width: 300px; height: 300px; position: relative; } img { 
        /*width: 50%;*/ /*height: 50%;*/ overflow: auto; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; }  
       style>  
        head> <body> <div> <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
         
         div>  
          body>  
           html> 

这种方法跟上面的有些类似,但是这里是通过margin:auto和top,left,right,bottom都设置为0实现居中,很神奇吧。不过这里得确定内部元素的高度,可以用百分比,比较适合移动端。

方法5:translate

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
      title> <style> div { 
        background-color: red; width: 300px; height: 300px; position: relative; } img { 
        position: absolute; width: 150px; height: 100px; /*img左上角移动到html的中心*/ top: 50%; left: 50%; /*img左上角向左上角分别移动自身的一半距离*/ transform: translate(-50%, -50%); /*text-align: center;*/ }  
       style>  
        head> <body> <div> <img src="../share_icon_sina_weibo.png"/>  
         
         div>  
          body>  
           html> 

这实际上是方法3的变形,移位是通过translate来实现的。

方法六:display:inline-block

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
      title> <style> div { 
        background-color: red; width: 300px; height: 300px; text-align: center; font-size: 0; } div:after { 
        content: ''; width: 0; height: 100%; display: inline-block; vertical-align: middle; } img { 
        vertical-align: middle; display: inline-block; font-size: 16px; }  
       style>  
        head> <body> <div> <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
         
         div>  
          body>  
           html> 

这种方法确实巧妙…通过:after来占位。

方法七:display:flex和margin:auto

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
      title> <style> div { 
        background-color: red; width: 300px; height: 300px; display: flex; /*text-align: center;*/ } img { 
        margin: auto; }  
       style>  
        head> <body> <div> <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
         
         div>  
          body>  
           html> 

方法八:display:-webkit-box

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title 
      title> <style> div { 
        background-color: red; width: 300px; height: 300px; display: -webkit-box; -webkit-box-pack: center; -webkit-box-align: center; -webkit-box-orient: vertical; text-align: center }  
       style>  
        head> <body> <div> <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
         
         div>  
          body>  
           html> 

效果图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZN7Lbw1N-73)(http://i4.buimg.com/1949/4f8cb3b41bed1792.png)]

参考:

纯CSS实现垂直居中的几种方法

如何让一个div居于页面正中间【实现方法】

注意:

以上不会改变div的尺寸:
以下使用padding会改变div的尺寸:

方法一

利用text-align属性将图片水平居中,然后设置padding-top的值使其垂直居中。

 
    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 name="author" type="Nancle from CAU CS101"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>如何让img标签在div里左右上下居中 
      title> <script type="text/javascript">  
       script> <style type="text/css"> #container1 { 
         /* 300*150*/ width: 300px; height: 150px; background-color: cyan; /*水平居中*/ text-align: center; } #container2 { 
         /* 300*150*/ width: 300px; height: 150px; background-color: cyan; /*水平居中*/ text-align: center; /*垂直居中*/ padding-top: 50px; /*div高度-img高度*/ }  
        style>  
         head> <body>  
         <div id="container1"> 
         <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
          div> <br> <br> <br>  
          <div id="container2"> 
          <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
           div>  
            body>  
             html> 

方法二:

只用padding属性,通过计算求得居中

 
    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 name="author" type="Nancle from CAU CS101"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>如何让img标签在div里左右上下居中 
      title> <script type="text/javascript">  
       script> <style type="text/css"> #container { 
         /* 300*150*/ width: 300px; height: 150px; background-color: cyan; } #container1 { 
         /* 300*150*/ width: 300px; height: 150px; background-color: cyan; /*水平居中*/ padding-left: 150px; /*div宽度-img宽度*/ } #container2 { 
         /* 300*150*/ width: 300px; height: 150px; background-color: cyan; /*水平居中*/ padding-left: 150px; /*div宽度-img宽度*/ /*垂直居中*/ padding-top: 50px; /*div高度-img高度*/ }  
        style>  
         head> <body> <div id="container"> 
         <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
          div> <br/> <br/> <br/> <div id="container1"> 
          <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
           div> <br/> <br/> <br/> <div id="container2"> 
           <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
            div> <p>  
             p>  
              body>  
               html> 

方法三:

利用图片的margin属性将图片水平居中,利用DIV的padding属性将图片垂直居中。

Img是内联元素,要设置其margin属性使其居中,就要将其转换为块元素display:block;然后利用margin:0 auto;实现图片的水平居中。

 
    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 name="author" type="Nancle from CAU CS101"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>如何让img标签在div里左右上下居中 
      title> <script type="text/javascript">  
       script> <style type="text/css"> .div1 { 
         /* 300*150*/ width: 300px; height: 150px; background-color: cyan; /*垂直居中*/ padding-top: 50px; } /* Img是内联元素,要设置其margin属性使其居中, 就要将其转换为块元素display:block;然后利用margin:0 auto;实现图片的水平居中*/ img { 
         /*background-color: red;*/ display: block; /*水平居中*/ margin: 0 auto; }  
        style>  
         head> <body> <div class="div1"> <img src="../share_icon_sina_weibo.png" width="150" height="100"/>  
          div>  
           body>  
            html> 

参考:

如何让图片在div中居中显示?

二、div盒子 居中的方法:

方法一:

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>div居于页面正中间 
      title> <style type="text/css"> * { 
        margin: 0; padding: 0; background-color: #EAEAEA; } div { 
        width: 200px; height: 200px; background-color: #1E90FF; /*--------------------------文字居中方法--------------------------*/ text-align: center; /*水平居中*/ line-height: 200px; /*垂直居中:div的全高度*/ /*--------------------------div居中方法--------------------------*/ /*使div脱离文档流*/ position: absolute; /*左上角位置*/ top: 50%; /*div下移父容器的一半高度*/ left: 50%; /*div右移父容器的一半宽度*/ /*中心矫正:上移div自身一半高度,左移div自身一半宽度*/ -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); transform: translate(-50%, -50%); }  
       style>  
        head> <body> <div> 我在中间  
         
         div>  
          
          body>  
           html> 

方法二:

 
    DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>div居中方法 
      title> <style> /*先要设置div元素的祖先元素html和body的高度为100%(因为他们默认是为0的), 并且清除默认样式,即把margin和padding设置为0(如果不清除默认样式的话,浏览器就会出现滚动条*/ html, body { 
        width: 100%; height: 100%; margin: 0; padding: 0; } div { 
        width: 300px; height: 300px; background: orange; position: relative; /*相对父容器*/ /*-----------------------div居中-----------------------*/ /*水平居中*/ margin: 0 auto; /*水平居中*/ /*垂直居中*/ top: 50%; /*垂直偏移:父元素的一半高度*/ margin-top: -150px; /*垂直矫正:上移自身一半高度*/ /*---------------------文字居div中间----------------------*/ text-align: center; line-height: 300px; }  
       style>  
        head> <body> <div>我在中间 
         div>  
          body>  
           html> 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/200437.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月20日 上午10:50
下一篇 2026年3月20日 上午10:51


相关推荐

  • iocomp入门教程-以MFC中iplotx为例

    iocomp入门教程-以MFC中iplotx为例最近要做一个项目需要绘制曲线,为了节省时间,就选用了iocomp控件,可网上相关的教程极少,官方给的文档还是比较详尽,但缺少具体的前期准备步骤,在初次接触这个控件很容易蒙,所以我写下这篇,给入门者以便利。用到的材料:              iocomp激活成功教程版(目前常见的为V3和V4,两个版本按喜好选择吧~            

    2022年7月17日
    19
  • linux smartctl 命令,使用smartctl命令检查磁盘

    linux smartctl 命令,使用smartctl命令检查磁盘使用smartctl命令检查磁盘在TS过程中会碰到很多磁盘异常出现影响系统数据或者生产数据的情况,但是有时候无法判断磁盘出现问题是由于磁盘物理损坏还是磁盘SATA口接触不良导致,这个时候smartctl命令就可以很容易判断出磁盘是否存在物理损坏问题,并且及时作出预警。使用smartctl进行SMART测试所有现代硬盘都可通过SMART属性监视其当前状态。这些值提供有关硬盘各种参数的信息,并可提供有…

    2022年6月16日
    43
  • kafka安装完整步骤_kafka集群搭建详细步骤

    kafka安装完整步骤_kafka集群搭建详细步骤本文记录在linux环境下,安装kafka,并做简单测试,如果zookeeper没有安装,可参考zookeeper安装:1.下载安装包地址:http://kafka.apache.org/downloads,注意不要下载成source了。2.上传至服务器rz命令上传至服务器解压[root@localhostlocal]#tar-zxvfkafka_2.11-2.1.1.t…

    2022年8月31日
    4
  • Tomcat 到底干了啥

    Tomcat 到底干了啥Tomcat到底干了啥

    2022年7月15日
    16
  • ima(腾讯AI智能工作台) v2.4.0.1758 安卓手机版

    ima(腾讯AI智能工作台) v2.4.0.1758 安卓手机版

    2026年3月13日
    2
  • web 应用程序_web应用程序是什么意思

    web 应用程序_web应用程序是什么意思1、HTTP通讯协议根据联机方式与所使用的网络服务不同,会有不同的通信协议。例如,发送信件时会使用SMTP(SimpleMailTransferProtocol,简单邮件传输协议),传输文件会

    2022年8月3日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号