css - 将图像放在 div 的中心

标签 css

我有一个 ID 为 leftheader 的 div,我想在 div 的中心放置一张图片。我正在尝试这个 css

#leftheader {
    float: left;
    width: 28%;
    height: 140px;
    margin: auto;
}

#leftheader img {
   /*padding-left: 80px;*/
}

当我使用 padding-left: 80px; 时,我得到了图像的中心对齐。但是由于我在 leftheader 的宽度中使用了 %,所以我不想使用 像素值。谁能给我推荐一个更通用的 css 布局。

最佳答案

如果你使用text-align :center;您的图像应在 div 中居中。

#leftheader {
    float: left;
    width: 28%;
    height: 140px;
    margin: auto;
    text-align: center;
}

关于css - 将图像放在 div 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13802379/

相关文章:

javascript - 如何计算连续 flexbox 元素的数量?

css - 根据目标对比度缩放颜色

css - 如何在 IE8 中使用带有 RowExpander 插件的 ExtJS stripeRows

html - padding-top 不影响 Chrome

twitter-bootstrap - 为什么 flex-fill 不在 Bootstrap 中创建相等的宽度?

javascript - JS/JQuery 隐藏元素、更改文本、显示元素

css - IE8 和 Primefaces p :selectOneMenu misbehave when having a lots of p:selectOneMenu in a form

html - Css: :nth-child: 我错过了什么?

html - CSS Resize/Zoom-In 对图像的影响,同时保持尺寸

css - 是否有 CSS 技巧可以在所有设备上获得默认字体大小