mardi 17 juillet 2018

W3School's Custom checkbox alignment issue when there's no text

I used W3School's custom checkbox snippet found here: https://www.w3schools.com/howto/howto_css_custom_checkbox.asp

I adjusted things slightly to the following: (Most importantly for this post I wanted the checkbox and text to be vertically aligned middle):

/*END******************** checkbox styling *********************/

.chkboxContainer {
  display: block;
  position: relative;
  padding-left: 35px;
  padding-top: 6px;
  margin-bottom: 4px;
  cursor: pointer;
  font-size: 14px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}


/* Hide the browser's default checkbox */
.chkboxContainer input[type=checkbox] {
  position: absolute;
  opacity: 0;
  cursor: pointer;
}


/* Create a custom checkbox */
.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #f9fafb;
  border: 1px solid #d4d8de;
  border-radius: 4px;
  -webkit-transition: background 0.5s;
  -moz-transition: background 0.5s;
  -ms-transition: background 0.5s;
  -o-transition: background 0.5s;
  transition: background 0.5s;
}


/* When the checkbox is checked, add a green background */
.chkboxContainer input:checked~.checkmark {
  background-color: #b5dc39;
}


/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

/* Show the checkmark when checked */
.chkboxContainer input:checked~.checkmark:after {
  display: block;
}


/* Style the checkmark/indicator */
.chkboxContainer .checkmark:after {
  left: 9px;
  top: 5px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}


/*END******************** checkbox styling *********************/

I found that when the styling was applied to an asp:CheckBox that had no checkbox caption the alignment would go awry - for me, it became bottom aligned and I could not fix it using vertical-alignment style and I did not want to stray too far from the W3School's snippet.

<!--How the style was applied to the asp:CheckBox -->

<asp:Label ID="chkB_SMOKER_FLAG_Container" class="chkboxContainer" runat="server" AssociatedControlID="chkB_SMOKER_FLAG">
  <asp:CheckBox ID="chkB_SMOKER_FLAG" runat="server" Text="" AutoPostBack="True" OnCheckedChanged="OnSmokerFlagChanged"></asp:CheckBox>
  <span class="checkmark"></span>
</asp:Label>



Aucun commentaire:

Enregistrer un commentaire