怎样用CSS3把图片居中?

Goraidh
浏览 5706回答 5
5回答

阳光暖暖的时光慢慢的

<html> <head>     <title>div居中</title> </head> <body>    <img src="1.jpg" style="width:100px;height:100px;position:absolute; top:0;bottom:0;left:0;right:0;margin:auto;"> </body> </html>

qq_哪嬡芣侢秇着_0

直接把IMG转成块元素就行了,比如img{    display:block;    width:300px;    height:300px;    margin:auto;}

谢凌

margin:0 auto;

weibo_正在输入

昨天刚刚给朋友写的如何居中的代码,你可以看一下效果<html> <head> <title>div居中</title> </head> <body style="background:blue; width:100%; height:100%"> <div style="background:red; width:400px; height:200px;margin:auto; "> 方式一 1. 设置宽高 width:400px; height:400px;<br> 2. 设置外边距 margin:0 auto; </div> <div style="background:yellow;width:400px; height:200px; position:absolute; left:45%; top:45%"> 方式二 1. 设置定位方式为绝对定位  position:absolute; 2. 设置该div距离父元素的左边距和上边距为 40%,40%  left:45%; top:45% ; </div> </body> </html>

李晓健

居中的方式有很多,主要看你页面的布局了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

CSS3