Một cách hữu ích khi bạn làm việc với hình ảnh, nhưng có thể được sử dụng như một giải pháp khác:
<html>
<head>
</head>
<style>
#someContainer {
width:50%;
position: relative;
}
#par {
width: 100%;
background-color:red;
}
#image {
width: 100%;
visibility: hidden;
}
#myContent {
position:absolute;
}
</style>
<div id="someContainer">
<div id="par">
<div id="myContent">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<img src="yourImage" id="image"/>
</div>
</div>
</html>
Để sử dụng, hãy thay thế yourImage bằng url hình ảnh của bạn. Bạn sử dụng hình ảnh với tỷ lệ chiều rộng / chiều cao mà bạn mong muốn.
div id = "myContent" ở đây là ví dụ về giải pháp thay thế trong đó myContent sẽ phủ lên hình ảnh.
Điều này hoạt động như: div gốc sẽ áp dụng cho chiều cao của hình ảnh, chiều cao hình ảnh sẽ áp dụng cho chiều rộng của cha mẹ. Tuy nhiên hình ảnh bị ẩn.