div css里怎么让一个图片在任意位置,比如靠居中在偏下,

如题所述

让一个图片在div任意位置,可以使用绝对定位的方式。如下示例代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>测试页面</title>
</head>
<body>
<style type="text/css">
.test-box{width:300px;height:260px;margin:0px auto;position:relative;border:1px solid #333;}
.test-box img{position:absolute;bottom:10px;left:50%;margin-left:-60px;border:0;}
</style>

<div class="test-box">
<img src="https://www.baidu.com/img/baidu_jgylogo3.gif?v=31680756.gif">
</div>
</body>
</html>

示例中做了一个宽为300px,高为260px像素的容器,其中图片通过绝对定位,控制其位于容器的水平居中,容器底部的位置。bottom:10px;控制图片距离底部10px,left:50%、margin-left:-60px;控制图片水平居中。其中的margin-left的值根据图片宽度变化,为负的二分之一图片宽度。


最终效果如下图:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-09-19
使用定位属性,设置它的left和top属性值来控制具体位置,例如
<div style="width:500px;height:400px;border:1px solid red;position:relative;">
<img src="852869179433967774.jpg" style="width:100px;height:100px;position:absolute;left:200px;top:200px;"/>
</div>本回答被网友采纳
第2个回答  2012-09-19
1、设置margin值,这样可以在不同浏览器中位置自适应改变。
2、用定位,外层用相对的,内层用绝对的。
第3个回答  2012-09-19
position:absolute 通过 left top right bottom 等来控制位置
第4个回答  2012-09-19
用css样式进行定位布局,不懂的可以再问
相似回答