图片垂直对齐的方法

本文介绍了一种使用HTML和CSS定位技巧的方法,用于在固定尺寸的容器内使图片垂直居中显示,包括不同宽度下效果的实现。
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>利用定位来显示垂直局中的图片</title>
<style type="text/css">
div{width:400px;overflow:hidden;border:solid 1px black;position:relative;}
ul{height:110px;width:20000px;}
.miao{width:170px;height:100px;display:table;text-align:center;border:solid 1px red;*position:relative;*overflow:hidden;float:left;}
.miao a{display:table-cell;vertical-align:middle;border:solid 1px blue;*position:absolute;*left:50%;*top:50%;}
.miao a img{border:dashed 1px green;*position:relative;*left:-50%;*top:-50%;}
</style>

</head>
<body>
<h1>固定高宽的容器中,图片垂直局中。</h1>
<div>
<ul>
<li class="miao">
<a><img src="http://bbs.blueidea.com/images/defalit/logo.gif" alt="Logo" /></a>
</li>
<hr />
<li class="miao" style="width:300px;height:80px;">
<a><img src="ttp://bbs.blueidea.com/images/defalit/logo.gif" alt="Logo" /></a>
</li>
<hr />
<li class="miao" style="width:500px;height:220px;">
<a><img src="http://www.google.cn/intl/zh-CN/images/logo_cn.gif" alt="Google" /></a>
</li>
</ul>
</div>
</body>
</html>
在 Vue 框架中实现图片垂直居中对齐,可以借助 CSS 的多种方法。以下是一些常见的实现方式: ### 使用 Flexbox 布局 Flexbox 是一种非常方便的布局方式,可以轻松实现垂直和水平居中。 ```html <template> <div class="container"> <img class="center-image" src="../assets/image/iot/lean.png" alt="居中图片"> </div> </template> <style scoped> .container { display: flex; align-items: center; /* 垂直居中 */ justify-content: center; /* 水平居中 */ height: 100vh; /* 设置容器高度为视口高度,可以根据需要调整 */ } </style> ``` ### 使用绝对定位和 transform 如果希望使用绝对定位来实现垂直居中,可以使用 `transform` 属性来实现。 ```html <template> <div class="container"> <img class="center-image" src="../assets/image/iot/lean.png" alt="居中图片"> </div> </template> <style scoped> .container { position: relative; height: 100vh; /* 设置容器高度为视口高度,可以根据需要调整 */ } .center-image { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } </style> ``` ### 使用 Grid 布局 CSS Grid 布局也可以用来实现垂直居中。 ```html <template> <div class="container"> <img class="center-image" src="../assets/image/iot/lean.png" alt="居中图片"> </div> </template> <style scoped> .container { display: grid; place-items: center; /* 同时设置垂直和水平居中 */ height: 100vh; /* 设置容器高度为视口高度,可以根据需要调整 */ } </style> ``` ### 注意事项 - 确保父容器有明确的高度,否则垂直居中可能无法生效。 - 在 Vue 中使用 `scoped` 样式时,确保样式只影响当前组件。 通过以上方法,可以在 Vue 框架中实现图片垂直居中对齐[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值