I’ve added an image to a div and the height has been adjusted automatically.Then, I floated the image to the left and move the image to the top using relative positioning..However, the bg height remains the same. How can I decrease the height automatically?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSS(4)</title>
<style type="text/css">
#bg{width:1500px;background-color:#FCF;overflow:auto;}
#bg img{float:right;position:relative;top:-50px;}
</style>
</head>
<body>
<div id="bg">
<img src="../../Desktop/produitsnonalimentaires/images/Produitsalimentaire.png" />
</div>
</body>
</html>
Try using a negative top margin on the image instead of the
top:For example: http://jsfiddle.net/ambiguous/k2Rq7/1/
Setting
topon a relatively positioned element doesn’t move the element’s box, it just adjusts where the element is displayed with respect to that box:So the element’s box takes up its usual space (specifically the height in your case) and the outer
<div>doesn’t shrink to match the image’s displayed position. Using a negativemargin-topactually moves the image and that effectively reduces its height and the height of the containing<div>.