Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Control Checkbox not working without label #26221

Closed
dpetratsch opened this issue Apr 9, 2018 · 22 comments
Closed

Custom Control Checkbox not working without label #26221

dpetratsch opened this issue Apr 9, 2018 · 22 comments
Labels

Comments

@dpetratsch
Copy link

When ommitting the label tag in a custom-control-input, the checkbox is not showing up

<div class="custom-control custom-radio">
  <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
</div>

Sometimes one would need a styled checkbox without a label, especially when working with selection lists or tables.

@FrancisStat
Copy link

FrancisStat commented Apr 9, 2018

Yup i got the same problem. I have a table of radio buttons and the label is in the table column headers. The custom radio buttons don't show up if there's no label.

<div class="custom-control custom-radio" role="group">
    <input 
        type="radio" 
        name="quest-27" 
        id="quest-27-10" 
        class="custom-control-input position-static" 
        aria-label="Never" 
        value="1">
</div>

It doesn't work with or without the position-static class.

I did more tests and it absolutely needs a label with the class custom-control-label next to it or else it doesn't show up. If i add a label without this class it still doesn't show up. If i add a label with the classes custom-control-label sr-only it doesn't show up either.

I guess the custom control is tied to label.custom-control-label::before?

@jipexu
Copy link
Contributor

jipexu commented Apr 9, 2018

why omitted the tag ? you can let the label text empty or use sr-only ?

@FrancisStat
Copy link

FrancisStat commented Apr 9, 2018

@jipexu it doesn't work either if you add the sr-only class to the label. An empty label element with the class custom-control-label works but it's kind of a hack and it's not what the boostrap documentation recommend to do : https://getbootstrap.com/docs/4.0/components/forms/#without-labels.

@jipexu
Copy link
Contributor

jipexu commented Apr 9, 2018

you noticed this for the 4.0 and 4.1 ?

@FrancisStat
Copy link

I'm on 4.0.0 right now which is the last version published on npm.

@jipexu
Copy link
Contributor

jipexu commented Apr 9, 2018

i use this method empty label in several context ... probably not the best way but found only this one

@FrancisStat
Copy link

FrancisStat commented Apr 9, 2018

I just updated to 4.1.0 and still not showing up. I guess i'll have to use an empty label for now. Here's what i did i think it's probably cleaner than an empty label :

<div class="custom-control custom-radio" role="group">
    <input 
        type="radio" 
        name="quest-27" 
        id="quest-27-10" 
        class="custom-control-input"
        value="1">
    <label class="custom-control-label">
        <span class="sr-only">Never</span>
    </label>
</div>

@dpetratsch
Copy link
Author

My version is 4.0.0 published on npm.

Working with empty labels is working, but thats not a good practice as it results in unneeded code overhead.

@FrancisStat
Copy link

FrancisStat commented Apr 10, 2018

I looked at the css code and i don't think it's an issue. It's actually by design. The custom-control-label class basically attaches a fake control to the label and when you click that fake control you in fact click the label since it's part of it and when you click a label in a browser it clicks the checkbox or radio button. You can do fake on/off slider with the same technic. Of course can't work if there's no label. So to make it works they would need to use a new hack. The doc should be updated though to make it clear that removing the label and using the position-static class wont work with custom controls.

@ghost
Copy link

ghost commented Apr 10, 2018

The custom checkbox is also off-center when used with a blank label.

https://jsfiddle.net/wkewpegp/8/

@FrancisStat
Copy link

@AndyCJ It's because the div.custom-control element has a left padding of 1.5em (to give space for the fake control) while the fake control is 1em width. The div.custom-control left padding is slightly larger than the custom control (to separate the custom control from the label) so when the label element is empty it looks off center (while it's not if you put a border to the div.custom-control you can see it's centered). To counter that you can reduce the left padding of the div.custom-control to 1em when the label is empty and it will be centered. Should probably be something handled by bootstrap but the use case if kind of niche. Usually you want a label tied to the checkbox.

@ghost
Copy link

ghost commented Apr 11, 2018

@FrancisStat

Thank you for the suggestion, that worked a treat. I'll pop a helper class in to our SASS to cover this usage.

I agree you want a label when making a "classic" form layout for sure.
Two fairly mainstream "alternative" uses would be a checkbox in a Table cell where the column header is the label, or on a panel where you're indicating your selecting what the entire panel represents.

@mdo
Copy link
Member

mdo commented May 2, 2018

Docs explain how these are built—the <label> element with .custom-control-label is used to generate the custom "checkbox". The only way around this, which isn't ideal given there's no label to explain the element, is another element with that class. Example: https://jsbin.com/hoxuvuteza.

@mdo mdo closed this as completed May 2, 2018
@AlessandroVecchi
Copy link

I have the same problem (v 4.1.1)
Using "text-hide" instead of "sr-only" it works!

@cichy380
Copy link

cichy380 commented Nov 27, 2018

This problem starts from Bootstrap v4.1.1 (#25944)

Fix empty custom-control-label alignment issue
Before: https://codepen.io/anon/pen/yRdegO
After: https://codepen.io/anon/pen/bmPEgX

.custom-control-label {
  vertical-align: top;
}

source: #27566 (comment)

@ThomasObale
Copy link

ThomasObale commented Apr 1, 2019

I know this is a little late hah. Just for future reference

.custom-control-label::after { display: none; }

@Omi0
Copy link

Omi0 commented Aug 23, 2019

I've checked and checked and checked all over again and realized that I've forgotten to put for attribute to the label...

@DiegoAndresCortes
Copy link

DiegoAndresCortes commented Oct 3, 2019

Checkboxes aren't working properly for 'mass' checking.
E.g. If I have a table/list with checkboxes it can't get the "id" of the main checkbox because there's none for the label

@anthonyadamski
Copy link

Suggesting we reopen this. When hiding label using the "text-hide" method, checkboxes are still off centered due to the padding that exists to offset the label from the checkbox.

@futhr
Copy link

futhr commented Nov 28, 2019

.custom-control {
   padding-left: 0;

   .custom-control-label {
     // set same width/height as before and after
     width: 1em; 
     height: 1em;

     &::before,
     &::after {
       left: 0;
     }
   }

   .custom-control-input {
     display: none; // optional
   }
 }

@IMalaniak
Copy link

The problem is, that You have to use label "for" attribute, and id for input, because it triggers the script

@fernandoweiler
Copy link

This worked 100% for me. Checkbox is there without label:

<div class="custom-control custom-checkbox mr-sm-2">
    <input id="FieldName" type="checkbox" class="custom-control-input" />
    <label for="FieldName" class="custom-control-label">
        <span class="sr-only"></span>
    </label>
</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests