1 回答
TA贡献1829条经验 获得超13个赞
要了解问题,请在其中添加一些文本并使用不同的字体大小:
.box {
height: 30px;
display:inline-block;
vertical-align:top;
background-color: red;
margin:5px;
}
svg {
width:20px;
}
<div class="box">
A
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="font-size:30px;">
A
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="font-size:40px;">
A
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
您可以清楚地看到 SVG 与文本的基线对齐,如果字体大小增大,则会出现溢出。如果我们保持相同的字体大小并减小高度,则逻辑相同:
.box {
height: 40px;
display:inline-block;
vertical-align:top;
background-color: red;
margin:5px;
}
svg {
width:40px;
}
<div class="box">
A
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="height:20px;">
A
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50" style="width:20px;">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="height:10px;">
A
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50" style="width:10px;">
<rect width="50" height="50"/>
</svg>
</div>
如果删除文本,行为将保持不变,因为基线不会改变:
.box {
height: 40px;
width:40px;
display:inline-block;
background-color: red;
margin:5px;
padding:2px;
}
<div class="box">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="height:20px;width:20px;">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="height:10px;width:10px;">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="height:5px;width:5px;">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
为了避免这种情况,只需调整对齐方式并使其与基线不同:
.box {
height: 20px;
width:20px;
display:inline-block;
background-color: red;
margin:5px;
padding:2px;
}
svg {
vertical-align:top;
}
<div class="box">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="height:10px;width:10px;">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="height:5px;width:5px;">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
或者将字体大小重置为 0:
.box {
height: 20px;
width:20px;
display:inline-block;
background-color: red;
margin:5px;
padding:2px;
font-size:0;
}
<div class="box">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="height:10px;width:10px;">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
<div class="box" style="height:5px;width:5px;">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 50 50">
<rect width="50" height="50"/>
</svg>
</div>
- 1 回答
- 0 关注
- 77 浏览
添加回答
举报