div css里怎么让一个图片在任意位置,比如靠居中在偏下, css,使一个图片,在一个div块中,上下居中,靠左20px...

\u6587\u5b57\u548c\u56fe\u7247\u5728\u540c\u4e00\u4e2aDIV\u4e2d\uff0c\u8981\u8ba9\u56fe\u7247\u5c45\u4e2d\uff0c\u6587\u5b57\u9760\u5de6\u5bf9\u9f50\uff0c\u5e76\u4e14\uff0c\u53ea\u80fd\u8bbe\u7f6eDIV\u7684CSS\uff0c\u5982\u4f55\u505a\u5230

\u6587\u5b57\u9ed8\u8ba4\uff0c\u56fe\u7247\u7684display\u5c5e\u6027\u8bbe\u7f6e\u6210block \u7136\u540emargin:0 auto \u5373\u53ef


.tt{ border:1px solid #CCC; width:600px; height:600px; }
.tt img{ margin:0 auto; border:1px solid #F00; display:block; }




\u6587\u5b57\u5728\u5de6\u8fb9

\u7b80\u7b80\u5355\u5355





aaaa



*{ margin:0; padding:0; }
#abc{ height:200px; line-height:200p; border:2px solid}
#b{ width:96px; position:relative; top:50%; margin-top:-48px; margin-left:20px;}










让一个图片在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的值根据图片宽度变化,为负的二分之一图片宽度。


最终效果如下图:



使用定位属性,设置它的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>

1、设置margin值,这样可以在不同浏览器中位置自适应改变。
2、用定位,外层用相对的,内层用绝对的。

position:absolute 通过 left top right bottom 等来控制位置

用css样式进行定位布局,不懂的可以再问

扩展阅读:一个div中嵌套两个并排div ... css怎么让两个div在一排 ... div css页面布局 模板 ... div css布局模板代码 ... css div布局效果图 ... 怎么让两个div在一列 ... css两个div如何左右并排 ... html两个div怎么并排 ... css怎么让几个不同div共用 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网