[英]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.