css - 高度样式属性在 div 元素中不起作用

标签 css html

我在 <div> 上设置了 20px 的高度,虽然当它在浏览器中呈现时,它只有 14 像素高。

有什么想法吗?

<div style="display:inline; height:20px width: 70px">My Text Here</div>

最佳答案

您不能为具有display:inline; 的元素设置heightwidth。请改用 display:inline-block;

来自 the CSS2 spec :

10.6.1 Inline, non-replaced elements

The height property does not apply. The height of the content area should be based on the font, but this specification does not specify how. A UA may, e.g., use the em-box or the maximum ascender and descender of the font. (The latter would ensure that glyphs with parts above or below the em-box still fall within the content area, but leads to differently sized boxes for different fonts; the former would ensure authors can control background styling relative to the 'line-height', but leads to glyphs painting outside their content area.)

编辑 — 您还缺少 ; height 属性的终止符:

<div style="display:inline; height:20px width: 70px">My Text Here</div>
<!--                                  ^^ here                       -->

工作示例:http://jsfiddle.net/FpqtJ/

关于css - 高度样式属性在 div 元素中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4967613/

相关文章:

javascript - 围绕轴旋转 css 元素

python - 按日期升序和降序排列的列

html - IE 问题 - 阴影通过容器边界切割图像

css - 跨多个 tbody 元素实现交替行颜色

html - 在表格单元格中放置三个 block

javascript - 与周围空单元格重叠的 css 网格对象上的鼠标悬停事件

javascript - 下拉值在满足条件时重置为第一个值

javascript - 如何将图像分成几 block

javascript - 使拖放功能遍及所有网站,而不仅仅是特定的 div 部分

javascript - 当使用新值循环 2000 个元素的数组时,浏览器更新元素需要很长时间