简体   繁体   English

图像粘在我的 React 组件中的 div 底部

[英]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-height34px ,它被粘在 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标签添加一个-10pxmargin-top ,以便它有点像这样垂直居中:

--------- Header Text --------- --------- Header 文字 ---------

But it doesn't work.但它不起作用。

What I have tried:我试过的:

  • Wrap all three children inside separate 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-textdisplay设置为block ,然后向包含imgdiv添加负边距。 (Has no effect) (没有效果)
  • Wrap the prop text inside a div with display:inline-block and add positive margin to it.使用display:inline-block将道具文本包裹在div中,并为其添加正边距。 (This just shifts all 3 children down by 10px) (这只是将所有 3 个孩子向下移动 10 像素)
  • Wrap both 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM