HTML div 宽度适合图像

标签 html css image

<!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/

相关文章:

javascript - 更改 SyntaxHighlighter 的背景颜色

html - 放大和缩小时页脚被剪切

javascript - 为特定的 div 重置 CSS 样式

javascript - JS给类添加一个数字(类名+数字)

android 在设备上加载和保存图像

javascript - 使用 AngularJS 将正确编码的(base64?)图像上传到 SharePoint

ruby-on-rails - 如何使用 Carrierwave 和 MiniMagick (Ruby on Rails) 将 PDF 转换为图像数组

javascript - 简单的 jquery 幻灯片元素

javascript - jQuery/JS 图表 - 根据条件绘制不同颜色的线条

asp.net - CSS 导致 xml 的位置越界