1 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
要了解問題,請?jiān)谄渲刑砑右恍┪谋静⑹褂貌煌淖煮w大?。?/p>
.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 與文本的基線對齊,如果字體大小增大,則會出現(xiàn)溢出。如果我們保持相同的字體大小并減小高度,則邏輯相同:
.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>
如果刪除文本,行為將保持不變,因?yàn)榛€不會改變:
.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>
為了避免這種情況,只需調(diào)整對齊方式并使其與基線不同:
.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 關(guān)注
- 112 瀏覽
添加回答
舉報(bào)