一张图片在一个相应盒子中垂直居中

本文介绍了几种实现网页中图片垂直居中的方法,包括使用CSS的table-cell属性、background-position属性及表达式兼容IE等方案,并提供了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一。先上一个 不随相应 垂直居中的, 下面是 一个 宽度,是可以垂直居中的,,但是, 不相应。

<!doctype html>
<html style="font-size:15px;">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" />
<title>v</title>

<style type="text/css">
body,html{ height:100%; width:100%;}

.box{
display:table-cell; width:1em; height:1em; border:1px solid #beceeb; font-size:144px; text-align:center; vertical-align:middle;
 
}
.box img{
         vertical-align:middle; margin:0; padding:0; max-width:50%;
}

.al{ height:50%;}
</style>
</head>

<body>
<div class="box">
<img src="appLoad.png" >
</div>
<div class="al">

555
</div>

</body>
</html>

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>方法1 - 未知高度的图片垂直居中 - www.nowamagic.net</title>
<style type="text/css">

        * {
          margin: 0;
          padding: 0;
          font-family: "Microsoft YaHei";
          -webkit-text-size-adjust: none;
        }
        h3 {
          margin: 0;
          padding: 0;
        }
        a,
        button,
        input,
        textarea {
          -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
          -webkit-user-modify: read-write-plaintext-only;
        }
        html {
          -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
        }
        img {
          -webkit-touch-callout: none;
        }
        html,
        body {
          width: 100%;
          height: 100%;
          
        }
        
        

#box{
    width:100%;height:400px;
    display:table;
    text-align:center;
    border:1px solid #d3d3d3;
    background:#fff;
}
#box span{
    display:table-cell;
    vertical-align:middle;
}
#box img{
    border:1px solid #ccc;
}
</style>
<!--[if lte IE 7]>
<style type="text/css">?
#box{
    position:relative;
    overflow:hidden;
}
#box span{
    position:absolute;
    left:50%;top:50%;
}
#box img{
    position:relative;
    left:-50%;top:-50%;
}
</style>
<![endif]-->

</head>

<body>
<div style="position:absolute">
<div id="box">
    <span><img src="file:///D|/now-h5/jvWeb/source/static/download/images/appLoad.png" style="max-width:50%"></span>
</div>

</div>
</body>
</html>

 

最后我解决的办法。  

用 这个盒子的背景 用一个属性  background-position:center,  不管这个盒子 高度 和宽度,多少,都 可以 垂直居中。

 第二方法。 可以随 相应式,的, 可以

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>方法3 - 未知高度的图片垂直居中 - www.nowamagic.net</title>

<style type="text/css">
body {
    height:100%;
    width:100%;
}
#box{
    width:100%;height:400px;
    text-align:center;
    border:1px solid #d3d3d3;background:#fff;
}
#box p{
    width:100%;height:400px;
    line-height:400px;  /* 行高等于高度 */
}

/* 兼容标准浏览器 */
#box p:before{
    content:".";  /* 具体的值与垂直居中无关,尽可能的节省字符 */
    margin-left:-5px; font-size:10px;  /* 修复居中的小BUG */
    visibility:hidden;  /*设置成隐藏元素*/
}

#box p img{
    *margin-top:expression((400 - this.height )/2);  /* CSS表达式用来兼容IE6/IE7 */
    vertical-align:middle;
    border:1px solid #ccc;
    background-position:center
}
</style>

</head>

<body>
<div id="box">
    <p><img src="images/appLoad.png" style="max-width:50%"></p>
</div>

</body>
</html>

 

转载于:https://www.cnblogs.com/yjhua/p/5066858.html

5服务支持:由以下部分构成 5.1顶部有一张高 384px宽 100%的图片,下边距为 23px 5.2常用功能部分:宽 1400px,下外边距 40px,标题下外边距为 20px,字体大小为 28px,文本水平垂直居中字体颜色为#000。内容是 5个子项组成(flex水平对齐,平均分配)每个子项宽 200px高 200px背景色为#cedbf5,子项内容水平垂直对齐,字体大小为 20px,子项内容的图片盒子背景图大小为 60px不重复。鼠标移入子项时字体颜色变成#fff,背景色为#f08300,放大 1.1倍,内容图片盒子的背景图切换成白色,子项有过渡效果。 5.3投诉建议部分:宽 1200px。标题下外边距为 30px,字体大小为 30px,文本水平垂直居中字体颜色为#000。下面有个小标题,水平垂直居中,内边距为 10px,左右外边距为 20px,字体颜色为#000。小标题高亮:字体颜色为#f08300下边框 1px实线颜色#f08300。内容部分标题的*号是字体颜色红色,其他字体颜色是#767676,上下内边距为 7px,字体大小是 16px。姓名,手机,邮箱,运货单,问题内容的包裹输入框的盒子宽 450px,左右内边距 12px上下内边距 6px,(flex 一行两个水平对齐。背景色为#767676,输入框宽 100%,高100%。输入框下面的提示内边距为上下 15px,左 10px,下外边距 8px,字体大小为 12px。问题内容部分:输入框数文本域,默认 3行文字高度,只能上下拉动不能左右拉动。宽度 100%。最后有两个按钮宽 180px,高 40px,字体颜色为#fff,背景色为#f08300,圆角 20px文本水平垂直对齐两端对齐。
03-22
### CSS 实现子盒子在父盒子中水平垂直居中的布局方法 #### 方法一:Flexbox 布局 通过将父盒子设置为 Flex 容器,并应用 `justify-content` 和 `align-items` 属性,可以让子盒子轻松实现水平和垂直方向上的居中[^3]。 ```css .parent { display: flex; justify-content: center; /* 水平居中 */ align-items: center; /* 垂直居中 */ } .child { width: 100px; height: 100px; background-color: lightblue; } ``` 此方法简单易用,兼容现代浏览器,无需额外设定子盒子的具体尺寸。 --- #### 方法二:绝对定位与自动外边距 当子盒子具有固定宽度和高度时,可以通过绝对定位配合负值的 `margin` 来实现居中效果[^2]。 ```css .parent { position: relative; width: 500px; height: 500px; border: 1px solid black; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); /* 调整到中心位置 */ width: 100px; height: 100px; background-color: lightcoral; } ``` 该方法适用于已知子盒子大小的情况,但需要手动调整偏移量以适配不同屏幕分辨率。 --- #### 方法三:Table Cell 显示模式 通过将父盒子定义为表格单元格 (`display: table-cell`) 并结合 `vertical-align: middle` 及 `text-align: center`,能够完成子盒子的双轴向居中操作[^4]。 ```css .parent { display: table-cell; vertical-align: middle; text-align: center; width: 500px; height: 500px; border: 1px solid green; } .child { display: inline-block; width: 100px; height: 100px; background-color: lightskyblue; } ``` 这种方式适合静态页面设计,但在响应式场景下可能会遇到一些限制。 --- #### 方法四:Grid 布局 借助 CSS Grid 的强大功能,只需几行代码即可达成理想的效果: ```css .parent { display: grid; place-items: center; /* 同时处理水平和垂直方向 */ width: 500px; height: 500px; border: 1px solid purple; } .child { width: 100px; height: 100px; background-color: palegreen; } ``` 这种方法不仅简洁明了,还支持更复杂的多列或多行排列需求。 --- #### 方法五:传统 Margin 自动分配法 如果子盒子有固定的宽高属性,那么可以直接依赖上下左右四个方向均为 `auto` 的外边距来达到目标。 ```css .parent { display: block; width: 500px; height: 500px; border: 1px solid teal; text-align: center; /* 配合内联块状元素使用 */ } .child { display: inline-block; margin: auto; width: 100px; height: 100px; background-color: tomato; } ``` 注意这里的前提是 `.child` 必须是块级或者内联块级元素才能生效。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值