html - 为什么 span with height: auto 和 display: inline-block parent of svg 的高度比它的 svg child 大?
问题描述
简单的问题:
父母:
span
和display: inline-block
和设置width
为。height
auto
孩子:
svg
使用height
并width
设置为35px
。
问题:
为什么span
得到高度= 39px
(见下面的图片和片段)?
const svg = document.getElementById('svg1');
const svgStyle = window.getComputedStyle(svg);
const span = document.getElementById('span1');
const spanStyle = window.getComputedStyle(span);
const p = document.getElementById('p1');
p.innerHTML = `
svg width = ${svgStyle.width}
svg height = ${svgStyle.height}
span width = ${spanStyle.width}
<b>span height = ${spanStyle.height} <---- Why the height of the span is greater than the svg?</b>
`
#span1 {
display: inline-block;
width: auto;
height: auto;
}
#p1 {
white-space: pre-wrap;
}
<span id="span1">
<svg id="svg1"
viewBox='0 0 24 24',
height='35px'
width='35px'
>
<path d='M16.021,15.96l-2.374-2.375c-0.048-0.047-0.105-0.079-0.169-0.099c0.403-0.566,0.643-1.26,0.643-2.009 C14.12,9.557,12.563,8,10.644,8c-1.921,0-3.478,1.557-3.478,3.478c0,1.92,1.557,3.477,3.478,3.477c0.749,0,1.442-0.239,2.01-0.643 c0.019,0.063,0.051,0.121,0.098,0.169l2.375,2.374c0.19,0.189,0.543,0.143,0.79-0.104S16.21,16.15,16.021,15.96z M10.644,13.69 c-1.221,0-2.213-0.991-2.213-2.213c0-1.221,0.992-2.213,2.213-2.213c1.222,0,2.213,0.992,2.213,2.213 C12.856,12.699,11.865,13.69,10.644,13.69z'/>
</svg>
</span>
<p id="p1"></p>
解决方案
如果您使用元素的vertical-align
属性,则将是所需的高度。我很幸运地使用了svg 元素。<svg>
<span>
bottom
top
检查vertical-align
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align上的 MDN
推荐阅读
- c# - 使用 CefSharp 从 Web 浏览器/网页截屏时异步等待同步事件/操作
- r - 类别内的条件计数
- java - 如何使用浏览器访问安全的 Spring API?
- c# - C#:运行N个任务的正确方法,等待第一次成功(检查它),停止其他任务并尽快将结果返回给用户
- excel - 365 office运行时1004调试
- c# - 每两次调用一次事件点击
- reactjs - React - 使用新方法时如何更改以旧方式编写的代码?
- pdf - Mendeley 以前的 pdf 视图功能?
- linux - 期望 ssh reg_ex 用于远程提示
- apache-kafka - 将 Kafka 拆分为单独的主题或单个主题/多个分区