Checkbox with label is rendered wrong
See original GitHub issueIssue
According to the readme it’s possible to just add a standard <label>
element after the checkbox.
<Checkbox
nativeControlId='my-checkbox'
checked={this.state.checked}
indeterminate={this.state.indeterminate}
onChange={(e) => this.setState({
checked: e.target.checked,
indeterminate: e.target.indeterminate})
}
/>
<label htmlFor='my-checkbox'>My Checkbox</label>
Will render a checkbox looking like this, with the label wrongly positioned and wrong font:
Example code here: https://stackblitz.com/edit/react-wpvkxv
Possible solution
It’s missing the Form Field CSS. There is no Form Field React component so this is the best solution I found but it seems wrong to have to do it like this:
- add form field CSS from package @material/form-field
- use a wrapping div around the
<Checkbox>
and<label>
with the class “mdc-form-field”
Then it looks as expected:
Example code here: https://stackblitz.com/edit/react-stbwsz?file=Checkbox.js
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (4 by maintainers)
Top Results From Across the Web
label not working with checkbox - Stack Overflow
I believe the label element links to the id attribute, not the name attribute. Try this: <form> <input type="checkbox" name="showRatings" id="showRatings" ...
Read more >HTML is not rendered in checkbox labels [#2920854] - Drupal
Checkbox labels with HTML code are not rendered, because the template outputs {{ input_title }}, but {{ label }} is the correct output....
Read more >Element: <oj-checkboxset> - Oracle
If user-assistance-density is 'compact', it will show on the label as an icon. In the Alta theme the input's label will render a...
Read more ><input>: The Input (Form Input) element - HTML
<label for="name">Name (4 to 8 characters):</label> ... checkbox, A check box allowing single values to be selected/deselected.
Read more >Checkbox | Primer React
<FormControl.Label>Default checkbox</FormControl.Label> ... checkbox state should be used if the input value is neither true nor false.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@seriema yes, adding the form field component is the correct solution. We never created the form-field component since it is just styles. But if you’d like to create a component for it please feel free 😃
import "@material/form-field/dist/mdc.form-field.css";
for css – apparently.
(Found this all woefully confusing.)