I've implemented antd in my react project and there's one thing that I can't figure out for the life of me. I have a row with 3 columns and when the screen size shrinks, I want to collapse the 3 columns into a single column.
Whenever I resize the screen they just collapse one at a time instead of all at once. I was able to get this going in vanilla html/css using flex, but for some reason I can't get the same effect with antd. I've tried adding media query, but no luck.
Here's what it looks like when it starts to collapse, and I've also attached my codepens for reference. Could it be that my flex settings on the columns are set incorrectly?
React Example: Codepen
CSS
@import 'antd/dist/antd.css';
.Red {background: red; color: white; width: 200px;}
.Green {background: green; color: white; width: 200px;}
.Blue {background: blue; color: white; width: 200px;}
@media screen and (max-width: 992px) {
.column {
flex: 10%;
}
}
JS
const { Row, Col, Divider } = antd;
ReactDOM.render(
<>
<Row className="row">
<Col flex="1 0 auto" className="column Red">Red</Col>
<Col flex="1 0 auto" className="column Green">Green</Col>
<Col flex="1 0 auto" className="column Blue">Blue</Col>
</Row>
</>,
mountNode,
);
Vanilla Example: (Expected output): Codepen
HTML
<div class="row">
<div class="column red">Red</div>
<div class="column green">Green</div>
<div class="column blue">Blue</div>
</div>
CSS
.red {background: red; color: white; width: 300px; float: left}
.green {background: green; color: white; width: 300px; float: left}
.blue {background: blue; color: white; width: 300px; float: left}
/* Container for flexboxes */
.row {
display: flex;
flex-wrap: wrap;
}
.column {
flex: 25%;
padding: 20px;
}
@media screen and (max-width: 800px) {
.column {
flex: 50%;
}
}
Ant Design, referred to as Antd in this context, is a pretty great library. It contains all the components I could need in nice wrapper components that makes developing a UI very simple. One major omission, however, is a responsive mobile header.
Design concept In most business situations, Ant Design needs to solve a lot of information storage problems within the design area, so based on 12 Grids System, we divided the design area into 24 sections.
There are 12 template columns available per row, allowing you to create different combinations of elements that span any number of columns. Column classes indicate the number of template columns to span (e.g., col-4 spans four).
The flex-basis that you set to auto makes the wrapping behavior to split into two columns before splitting it into three. Because it is the minimal initial value set like width for a row and height for a column.
You can set the flex-basis to 25% so that it does not split according to default wrapping behaviour.
Changes made -
HTML
const { Row, Col, Divider } = antd;
ReactDOM.render(
<>
<Row className="row">
<Col flex="1 0 25%" className="column Red">Red</Col> <!-- From auto to 25%, same as below -->
<Col flex="1 0 25%" className="column Green">Green</Col>
<Col flex="1 0 25%" className="column Blue">Blue</Col>
</Row>
</>,
mountNode,
);
CSS
@media screen and (max-width: 992px) {
.column {
flex: 100% !important; /* !important so that it overrides the inline style because by default it has higher priority */
}
}
Codepen Demo: https://codepen.io/m4n0/pen/OJNrLqz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With