Sizing
React Bootstrap 5 Sizing component
Easily make an element as wide or as tall with our width and height utilities.
Relative to the parent
Width and height utilities are generated from the utility API in
_utilities.scss
. Includes support for 25%
, 50%
,
75%
, 100%
, and auto
by default. Modify those values as
you need to generate different utilities here.
Width 25%
Width 50%
Width 75%
Width 100%
Width auto
import React from 'react';
export default function App() {
return (
<>
<div className="w-25 p-3" style="background-color: #eee">Width 25%</div>
<div className="w-50 p-3" style="background-color: #eee">Width 50%</div>
<div className="w-75 p-3" style="background-color: #eee">Width 75%</div>
<div className="w-100 p-3" style="background-color: #eee">Width 100%</div>
<div className="w-auto p-3" style="background-color: #eee">Width auto</div>
</>
);
}
Height 25%
Height 50%
Height 75%
Height 100%
Height auto
import React from 'react';
export default function App() {
return (
<div style={{height: 100, backgroundColor: "rgba(255, 0, 0, 0.1)"}}>
<div
className="h-25 d-inline-block"
style={{width: 120, backgroundColor: "rgba(0, 0, 255, 0.1)"}}
>
Height 25%
</div>
<div
className="h-50 d-inline-block"
style={{width: 120, backgroundColor: "rgba(0, 0, 255, 0.1)"}}
>
Height 50%
</div>
<div
className="h-75 d-inline-block"
style={{width: 120, backgroundColor: "rgba(0, 0, 255, 0.1)"}}
>
Height 75%
</div>
<div
className="h-100 d-inline-block"
style={{width: 120, backgroundColor: "rgba(0, 0, 255, 0.1)"}}
>
Height 100%
</div>
<div
className="h-auto d-inline-block"
style={{width: 120, backgroundColor: "rgba(0, 0, 255, 0.1)"}}
>
Height auto
</div>
</div>
);
}
You can also use max-width: 100%;
and max-height: 100%;
utilities as
needed.
import React from 'react';
export default function App() {
return (
<img src="https://mdbootstrap.com/img/new/slides/041.jpg" className="mw-100" alt="..." />
);
}
Max-height 100%
import React from 'react';
export default function App() {
return (
<div style={{height: 100, backgroundColor: "rgba(255, 0, 0, 0.1)"}}>
<div
className="mh-100"
style={{width: 100, height: 200, backgroundColor: "rgba(0, 0, 255, 0.1)"}}
>
Max-height 100%
</div>
</div>
);
}
Relative to the viewport
You can also use utilities to set the width and height relative to the viewport.
import React from 'react';
export default function App() {
return (
<>
<div className="min-vw-100">Min-width 100vw</div>
<div className="min-vh-100">Min-height 100vh</div>
<div className="vw-100">Width 100vw</div>
<div className="vh-100">Height 100vh</div>
</>
);
}