简体   繁体   English

无法在同一行 React Bootstrap 上垂直居中对齐图像和文本

[英]Cannot vertical align center an image and text on the same line React Bootstrap

I am trying to align an image and text vertically on the same line in React Bootstrap:我试图在 React Bootstrap 的同一行上垂直对齐图像和文本:

<CardBody 
    className="d-flex justify-content-between align-items-center" 
    style={{borderTop: "1px solid #6c757d", paddingTop: "1rem"}}
>
    <div>
        <p className="text-muted p-0" style={{backgroundColor: 'red'}}>{subtitle}</p>
    </div>
    <img
        
        src={infoIcon}
        alt="Info"
        style={{width: 20, backgroundColor: 'red', verticalAlign: 'center'}}
    />
</CardBody>

However the output is like follows:然而 output 如下所示:

在此处输入图像描述

What am I doing wrong here?我在这里做错了什么?

It's the Bootstrap CSS attached to <p> that you are using inside the first div that's doing this.这是您在执行此操作的第一个div中使用的附加到<p>的 Bootstrap CSS。

Visit the p element in devtools and notice the margin it has from Bootstrap:访问 devtools 中的p元素并注意它与 Bootstrap 的margin

p {
    margin-top: 0;
    margin-bottom: 1rem;
}

Override that and it should work.覆盖它,它应该工作。

Example sandbox 示例沙箱

you can put them in an element and use flex like:您可以将它们放在一个元素中并像这样使用 flex:

 <div style={{display: 'flex', justifyContent: 'center', alignItem: 'center'}}> <div> <p className="text-muted p-0" style={{backgroundColor: 'red'}}>{subtitle}</p> </div> <img src={infoIcon} alt="Info" style={{width: 20, backgroundColor: 'red', verticalAlign: 'center'}} /> </div>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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