html - 如何将图像包裹在div中,然后将div居中

标签 html css

我有这个 CSS:

   <style type="text/css">

    #heading{
        margin:auto; 
        background-color: brown;

    }
</style>

和这个 html:

<div id="heading">
        <image src="image_files/header-background.jpg" alt="speakom"   />
    </div>

我不知道图像的确切大小..但我希望 div 以准确的宽度包裹图像,然后我想使该 div 居中..我该如何实现?

最佳答案

使用 inline-block 将使 div 成为其内容的宽度,并允许您使用自动边距将其居中:

#heading {
    margin: 0px auto;
    display: inline-block;
}​

关于html - 如何将图像包裹在div中,然后将div居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9956667/

相关文章:

javascript - 一个盒子一次变色而不是两个?

javascript - 子 div 单击事件的意外行为

html - li 元素符号点需要背景颜色

javascript - 禁用复选框的功能无法正常工作

javascript - jQuery 和行高

html - 在 HTML 元素中 float 的 CSS3 动画

html - 我无法在右侧对齐登录和创建新帐户选项卡

javascript - 如何在 css 网格中显示一些元素,直到按下更多按钮?

html - 标题 + 粘性侧边栏

css - 如何使用具有百分比和未知数量列的 CSS 网格?