[英]Image is sticking to bottom of the div in my React Component
I have a react component called SectionHeader which just returns the custom header with props text.我有一个名为 SectionHeader 的反应组件,它只返回带有道具文本的自定义 header。 This is what is returned from inside the render function:
这是从渲染 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>
Now the stroke.svg
is just 6px
in height but longer in width.现在
stroke.svg
的高度仅为6px
,但宽度更长。 Since line-height
is 34px
it is stuck to the bottom of the div header-text
somewhat like this:由于
line-height
是34px
,它被粘在 div header-text
的底部,有点像这样:
_________ Header Text __________ _________ Header 文字 __________
I need to add a margin-top
of -10px
to both img
tags so that it is somewhat vertically centered like this:我需要为两个
img
标签添加一个-10px
的margin-top
,以便它有点像这样垂直居中:
--------- Header Text --------- --------- Header 文字 ---------
But it doesn't work.但它不起作用。
What I have tried:我试过的:
div
tags and set display
for them as inline-blocks
and set display
of header-text
to block
, then add negative margin to the div
s containing the img
.div
标记中,并将它们的display
设置为inline-blocks
并将header-text
的display
设置为block
,然后向包含img
的div
添加负边距。 (Has no effect) div
with display:inline-block
and add positive margin to it.display:inline-block
将道具文本包裹在div
中,并为其添加正边距。 (This just shifts all 3 children down by 10px) img
s inside div
s with display:inline-block
and add negative margin to them.display:inline-block
将两个img
包裹在div
中,并为其添加负边距。 (Has no effect) What is the right way to achieve this?实现这一目标的正确方法是什么?
The easiest way would be to go with flex-box.最简单的方法是 go 与 flex-box。 Here is the CSS:
display: flex; align-items: center
这是 CSS:
display: flex; align-items: center
display: flex; align-items: center
. display: flex; align-items: center
。 You would also have to wrap your text inside an element like a span
for instance例如,您还必须将文本包装在像
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.