CSS中如何实现让标签垂直居中呢?我们可以使用以下几种方法:
/* 方法一:使用display:flex */ .container { display: flex; justify-content: center; /* 水平居中 */ align-items: center; /* 垂直居中 */ } /* 方法二:使用position和transform */ .container { position: relative; } img { position: absolute; top: 50%; transform: translateY(-50%); } /* 方法三:使用line-height */ .container { line-height: height-of-container; text-align: center; /* 水平居中 */ } img { vertical-align: middle; display: inline-block; } /* 方法四:使用table和table-cell */ .container { display: table; height: height-of-container; width: 100%; text-align: center; /* 水平居中 */ } img { display: table-cell; vertical-align: middle; }
以上就是四种常用的方法,你可以根据实际需求选择适合自己的方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。