html - 添加文本时防止包含图像的 div 调整大小

标签 html css

我有一个 div 以网站为中心并包含一张图片。 div 扩展了它的 height 以按预期适合图片。然后我添加了一些文本,我希望它位于 div 的底部。但除此之外,div 也调整了一点点,所以它的 heightheight higher图片。这是例子:

HTML:

<div class="siteContent">
    <img class="debug" src="../../Content/themes/base/images/pageTitle.png" /> 
    <span>
        aaa
    </span>
</div>

CSS:

body
{
    margin: 0;
}
.siteContent
{   
    width: 960px;
    margin-left: auto;
    margin-right: auto;
    border: thin solid;
}
.debug
{
    border: thin solid;
}

结果: enter image description here

不需要的效果标记为红色。

我能够通过像这样修改 CSS 来为 IE 8 和 Firefox 修复此问题:

body
{
    margin: 0;
}
.siteContent
{   
    width: 960px;
    margin-left: auto;
    margin-right: auto;
    border: thin solid;
    position: relative;
}
.siteContent span{
    position: absolute;
    bottom: 0px;
}
.debug
{
    border: thin solid;
}

运行后,我在 Mozilla 中得到了正确的结果:

enter image description here

但这不适用于 Chrome 和 Safary。 如何让它适用于所有主流浏览器?

最佳答案

发生这种情况是因为图像与文本的基线对齐。尝试为您的图片设置 vertical-align: bottomvertical-align: text-bottom 来解决这个问题。

关于html - 添加文本时防止包含图像的 div 调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5245878/

相关文章:

javascript - 将 DIV 折叠到零高度

javascript - 突出显示 html 元素

javascript - BrowserSync CSS 注入(inject) + runSequence

Css 线性渐变在 mozilla firefox 中无法正常工作

javascript - 在边界线的位置插入空格

javascript - 将div当前内容替换为另一个页面响应

html - 将自动高度与 float 一起使用

javascript - 了解 scrollr.js 立方贝塞尔曲线

html - 在一个 div 中居中放置三个图像

javascript - 避免 navbar-toggler 在选择时改变颜色