[英]Image is sticking to bottom of the div in my React Component
我有一個名為 SectionHeader 的反應組件,它只返回帶有道具文本的自定義 header。 這是從渲染 function 內部返回的內容:
<div
id='header-text'
style={{
width:'100%',
textAlign:'center',
fontSize:'28px',
lineHeight:'34px'
}}
>
<img src={require('./stroke.svg')} style={{marginRight:'50px'}} />
{this.props.text}
<img src={require('./stroke.svg')} style={{marginLeft:'50px'}} />
</div>
現在stroke.svg
的高度僅為6px
,但寬度更長。 由於line-height
是34px
,它被粘在 div header-text
的底部,有點像這樣:
_________ Header 文字 __________
我需要為兩個img
標簽添加一個-10px
的margin-top
,以便它有點像這樣垂直居中:
--------- Header 文字 ---------
但它不起作用。
我試過的:
div
標記中,並將它們的display
設置為inline-blocks
並將header-text
的display
設置為block
,然后向包含img
的div
添加負邊距。 (沒有效果)display:inline-block
將道具文本包裹在div
中,並為其添加正邊距。 (這只是將所有 3 個孩子向下移動 10 像素)display:inline-block
將兩個img
包裹在div
中,並為其添加負邊距。 (沒有效果)實現這一目標的正確方法是什么?
最簡單的方法是 go 與 flex-box。 這是 CSS: display: flex; align-items: center
display: flex; align-items: center
。 例如,您還必須將文本包裝在像span
這樣的元素中
<div
id='header-text'
style={{
width:'100%',
textAlign:center,
display: flex,
alignItems: center,
fontSize:'28px',
lineHeight:'34px'
}}
>
<img src={require('./stroke.svg')} style={{marginRight:'50px'}} />
<span>{this.props.text}</span>
<img src={require('./stroke.svg')} style={{marginLeft:'50px'}} />
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.