<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 1px solid black;
width: auto;
}
</style>
</head>
<body>
<div>
<img src = "http://www.flowerpicturegallery.com/d/1977-3/yellow+daisy+flower+picture.jpg">
</div>
</body>
</html>
我使用上面的代码来显示图像。除了两个事实,代码运行良好:1) 如何使 div 的宽度适合图像(在图像停止的地方停止)?我尝试了 width: auto;
或 width: 100%;
但它们都不起作用......并且 2) 为什么底部有一个非常小的空白区域以及如何我可以解决这个问题吗?
最佳答案
HTML
<div class="child-width">
<img src = "http://www.flowerpicturegallery.com/d/1977-3/yellow+daisy+flower+picture.jpg">
</div>
CSS
.child-width {
border: 1px solid black;
display: inline-block; // add this
}
检查这个example fiddle
关于HTML div 宽度适合图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44325691/