css - 如何将图像置于正中间的 div 中

标签 css

我试图将图像置于 div 中的正中间 - 水平和垂直,但这对我不起作用:

#col_as_table
{
    float:left;
    position:relative;
    width:14%;
    min-height:100px;
    height:auto;
    display: table;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}

HTML

<div id="col_as_table"><img src="test.png"></div>

最佳答案

试一试。你需要一个额外的 div。

Fiddle!

HTML:

<div class="divTable">
    <div class="innerDivTable">
        <img src="test.png"/>
    </div>
</div>

CSS:

.divTable {
    width: 200px; /* purely for demonstration */
    height: 200px; /* purely for demonstration */
    display: table;
    border: 1px solid black;
}

.innerDivTable {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

关于css - 如何将图像置于正中间的 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24686030/

相关文章:

css - 选择箭头样式更改

html - Flexbox 样式化一些标注文本

html - 有没有办法在 float 元素上设置 CSS 最小宽度?

html - 如何使用 HTML/CSS 中的标题制作菜单背景

jquery - 如何切换:hover effect?

css - #menu div 中的垂直居中 UL 菜单和 IMG

html - 如何在 <tr> 标签上获取 css 背景颜色以跨越整行

html - 使用 flexbox 的响应式表格

css - Div 不调整以适合图像的高度和宽度

javascript - HTML & CSS : triggering active class for a section in index. html