In Ant Design, how can we center <Icon> vertically in <Row>?

enzeberg picture enzeberg · Feb 16, 2018 · Viewed 26k times · Source

Please see my React code example:

import React from 'react';
import { Row, Col, Icon } from 'antd';
const MyRow = () => (
  <Row type="flex" align="middle">
    <Col md={10}>Trouble is a friend</Col>
    <Col md={10}><Icon type="play-circle-o" /></Col>
    <Col md={4}><div>Lenka</div></Col>
  </Row>
);

...

When I rendered <MyRow />, I found that the text in <Row> was centered vertically well, but the <Icon> component didn't do so. So that my <MyRow> didn't look good. I expected all the content, not only the text but also the SVG in <Row> could be centered vertically.

I also tried other icon library, e.g. react-icons-kit, which did not work.

Does anyone have an idea?

Answer

enzeberg picture enzeberg · Feb 17, 2018

Thanks to Jesper We's help! Luckily I found the solution for this little problem:

For Icon in antd:

<Row type="flex" align="middle">
  <Col>
    <div style={{ display: 'inline-flex', justifyContent: 'center', alignItems: 'center'}}>
      <Icon type="play-circle-o" style={{ display: 'inline-block', verticalAlign: 'middle' }} />
    </div>
  </Col>
</Row>

For Icon in react-icons-kit:

import Icon from 'react-icons-kit';
import { ic_play_circle_outline } from 'react-icons-kit/md/ic_play_circle_outline';
...
<Row type="flex" align="middle">
  <Col>
    <Icon icon={ic_play_circle_outline} style={{ verticalAlign: 'middle' }} />
  </Col>
</Row>