How to change checkbox size in react js?
September 18, 2024Hi Friends π,
Welcome To aHoisting!
To change checkbox size in react js, you can set height: '20px'
and width: '20px'
in checkboxStyle
. It will change checkbox size in react js.
Today, I am going to show you, how to change checkbox size in react js.
Table of contents
- Install and create a new React app.
- Import react component.
- Create a Component.
Letβs start with the first step.
Step 1: Install and create a new React app.
First you have to install the React project. You should use create-react-app
command to create a new React project.
npx create-react-app my-app
cd my-app
npm start
Step 2: Import react component.
After installing, you have to import your React component.
import React from 'react';
Step 3: Create a Component.
You can use onChange={(e) => { e.target.style = e.target.checked ? checkedStyle : checkboxStyle;}}
to change checkbox size in react js.
<div>
<input
type="checkbox"
style={checkboxStyle}
onChange={(e) => {
e.target.style = e.target.checked ? checkedStyle : checkboxStyle;
}}
/>
<label>Checkbox</label>
</div>
Change checkbox size example.
The below code is an example of a React. You have to import react
and set height: '20px'
and width: '20px'
in checkboxStyle
to change checkbox size in react js.
App.js
import React from 'react';
function App() {
const checkboxStyle = {
width: '20px',
height: '20px',
border: '2px solid #ccc',
borderRadius: '4px',
appearance: 'none',
cursor: 'pointer'
};
const checkedStyle = {
...checkboxStyle,
backgroundColor: 'green',
borderColor: 'green',
};
return (
<div>
<input
type="checkbox"
style={checkboxStyle}
onChange={(e) => {
e.target.style = e.target.checked ? checkedStyle : checkboxStyle;
}}
/>
<label>Checkbox</label>
</div>
);
}
export default App;
In the above code example, I have used the height: '20px'
to change checkbox size in react js.
Check the output of the above code.
All the best π