EN
React - stretch elements in flexbox
3 points
In this article, we would like to show you how to stretch elements inside flexbox in React.
Quick solution:
xxxxxxxxxx
1
<div style={{display: 'flex'}}>
2
<div style={{flex: 1}}>1</div>
3
<div style={{flex: 2}}>2</div>
4
</div>
Preview:

In this example, we use display: 'flex'
style for container element to enable flexbox (default mode is: horizontall). To stretch the elemets we use flex: 1
(flex: 2
, flex: 3
, etc.) property depending on the width we want to get (the higher the value, the wider the element).
xxxxxxxxxx
1
// Note: Uncomment import lines during working with JSX Compiler.
2
// import React from "react";
3
// import ReactDOM from "react-dom";
4
5
const App = () => {
6
return (
7
<div style={{display: 'flex', padding: '5px', border: '1px solid black'}}>
8
<button style={{flex: 1}}>1</button>
9
<button style={{flex: 1}}>1</button>
10
<button style={{flex: 2}}>2</button>
11
<button style={{flex: 3}}>3</button>
12
</div>
13
);
14
};
15
16
const root = document.querySelector('#root');
17
ReactDOM.render(<App />, root);