Taille de la case à cocher en HTML / CSS

est-il possible de définir la taille d'une case à cocher en utilisant CSS ou HTML?

width et size travailler sous IE6+, mais pas avec Firefox, lorsque la case à cocher reste 16x16, même si j'ai mis une taille plus petite.

349
demandé sur Simon East 2008-11-21 00:34:14

12 réponses

c'est un peu moche (en raison de la mise à l'échelle), mais il fonctionne sur la plupart des nouveaux navigateurs:

input[type=checkbox]
{
  /* Double-sized Checkboxes */
  -ms-transform: scale(2); /* IE */
  -moz-transform: scale(2); /* FF */
  -webkit-transform: scale(2); /* Safari and Chrome */
  -o-transform: scale(2); /* Opera */
  padding: 10px;
}

/* Might want to wrap a span around your checkbox text */
.checkboxtext
{
  /* Checkbox text */
  font-size: 110%;
  display: inline;
}
<input  type="checkbox" name="optiona" id="opta" checked />
<span class="checkboxtext">
  Option A
</span>
<input type="checkbox" name="optionb" id="optb" />
<span class="checkboxtext">
  Option B
</span>
<input type="checkbox" name="optionc" id="optc" />
<span class="checkboxtext">
  Option C
</span>
362
répondu jdw 2016-11-22 11:00:35

solution de travail pour tous les navigateurs modernes.

input[type=checkbox] {
  transform: scale(1.5);
}

compatibilité:

  • IE: 10+
  • FF: 16+
  • Chrome: 36+
  • Safari: 9+
  • Opéra: 23+
  • iOS Safari: 9.2+
  • Chrome pour Android: 51+

apparence:

  • Scaled checkboxes on Chrome, Win 10 Chrome 58( Mai 2017), Windows 10
281
répondu Numbers 2017-05-18 21:19:38

une solution facile est d'utiliser la propriété zoom :

input[type=checkbox] {
    zoom: 1.5;
}
40
répondu Luiz 2016-08-18 13:01:54

Preview:

http://jsfiddle.net/h4qka9td /

*,*:after,*:before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

.switch {
  margin: 50px auto;
  position: relative;
}

.switch label {
  width: 100%;
  height: 100%;
  position: relative;
  display: block;
}

.switch input {
  top: 0; 
  right: 0; 
  bottom: 0; 
  left: 0;
  opacity: 0;
  z-index: 100;
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

/* DEMO 3 */

.switch.demo3 {
  width: 180px;
  height: 50px;
}

.switch.demo3 label {
  display: block;
  width: 100%;
  height: 100%;
  background: #a5a39d;
  border-radius: 40px;
  box-shadow:
      inset 0 3px 8px 1px rgba(0,0,0,0.2),
      0 1px 0 rgba(255,255,255,0.5);
}

.switch.demo3 label:after {
  content: "";
  position: absolute;
  z-index: -1;
  top: -8px; right: -8px; bottom: -8px; left: -8px;
  border-radius: inherit;
  background: #ababab;
  background: -moz-linear-gradient(#f2f2f2, #ababab);
  background: -ms-linear-gradient(#f2f2f2, #ababab);
  background: -o-linear-gradient(#f2f2f2, #ababab);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#f2f2f2), to(#ababab));
  background: -webkit-linear-gradient(#f2f2f2, #ababab);
  background: linear-gradient(#f2f2f2, #ababab);
  box-shadow: 0 0 10px rgba(0,0,0,0.3),
        0 1px 1px rgba(0,0,0,0.25);
}

.switch.demo3 label:before {
  content: "";
  position: absolute;
  z-index: -1;
  top: -18px; right: -18px; bottom: -18px; left: -18px;
  border-radius: inherit;
  background: #eee;
  background: -moz-linear-gradient(#e5e7e6, #eee);
  background: -ms-linear-gradient(#e5e7e6, #eee);
  background: -o-linear-gradient(#e5e7e6, #eee);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#e5e7e6), to(#eee));
  background: -webkit-linear-gradient(#e5e7e6, #eee);
  background: linear-gradient(#e5e7e6, #eee);
  box-shadow:
      0 1px 0 rgba(255,255,255,0.5);
  -webkit-filter: blur(1px);
  -moz-filter: blur(1px);
  -ms-filter: blur(1px);
  -o-filter: blur(1px);
  filter: blur(1px);
}

.switch.demo3 label i {
  display: block;
  height: 100%;
  width: 60%;
  border-radius: inherit;
  background: silver;
  position: absolute;
  z-index: 2;
  right: 40%;
  top: 0;
  background: #b2ac9e;
  background: -moz-linear-gradient(#f7f2f6, #b2ac9e);
  background: -ms-linear-gradient(#f7f2f6, #b2ac9e);
  background: -o-linear-gradient(#f7f2f6, #b2ac9e);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#f7f2f6), to(#b2ac9e));
  background: -webkit-linear-gradient(#f7f2f6, #b2ac9e);
  background: linear-gradient(#f7f2f6, #b2ac9e);
  box-shadow:
      inset 0 1px 0 white,
      0 0 8px rgba(0,0,0,0.3),
      0 5px 5px rgba(0,0,0,0.2);
}

.switch.demo3 label i:after {
  content: "";
  position: absolute;
  left: 15%;
  top: 25%;
  width: 70%;
  height: 50%;
  background: #d2cbc3;
  background: -moz-linear-gradient(#cbc7bc, #d2cbc3);
  background: -ms-linear-gradient(#cbc7bc, #d2cbc3);
  background: -o-linear-gradient(#cbc7bc, #d2cbc3);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#cbc7bc), to(#d2cbc3));
  background: -webkit-linear-gradient(#cbc7bc, #d2cbc3);
  background: linear-gradient(#cbc7bc, #d2cbc3);
  border-radius: inherit;
}

.switch.demo3 label i:before {
  content: "off";
  text-transform: uppercase;
  font-style: normal;
  font-weight: bold;
  color: rgba(0,0,0,0.4);
  text-shadow: 0 1px 0 #bcb8ae, 0 -1px 0 #97958e;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 24px;
  position: absolute;
  top: 50%;
  margin-top: -12px;
  right: -50%;
}

.switch.demo3 input:checked ~ label {
  background: #9abb82;
}

.switch.demo3 input:checked ~ label i {
  right: -1%;
}

.switch.demo3 input:checked ~ label i:before {
  content: "on";
  right: 115%;
  color: #82a06a;
  text-shadow: 
    0 1px 0 #afcb9b,
    0 -1px 0 #6b8659;
}
<div class="switch demo3">
  <input type="checkbox">
  <label><i></i>
  </label>
</div>

<div class="switch demo3">
  <input type="checkbox" checked>
  <label><i></i>
  </label>
</div>
25
répondu UniMe 2017-10-12 09:15:39

version 2017 : https://jsfiddle.net/ksvx2txb/11 /

à propos de : le navigateur utilisera la fonction zoom css si elle est prise en charge, sinon, utilisera transform: scale.

pourquoi ne pas utiliser transform? Il semble laid sur certains navigateurs, y compris chrome sur mac.

Chrome - œuvres

Firefox (mac) - travaux

Opéra - œuvres

Bord - œuvres

Firefox (win) - grand, flou !

Safari-grand, flou !

@supports (zoom:2) {
	input[type="radio"],  input[type=checkbox]{
	zoom: 2;
	}
}
@supports not (zoom:2) {
	input[type="radio"],  input[type=checkbox]{
		transform: scale(2);
		margin: 15px;
	}
}
label{
  /* fix vertical align issues */
	display: inline-block;
	vertical-align: top;
	margin-top: 10px;
}
<input type="radio" name="aa" value="1" id="aa" checked /> 
<label for="aa">Radio 1</label>
<br />
<input type="radio" name="aa" value="2" id="bb" /> 
<label for="bb">Radio 2</label>

<br /><br />

<input  type="checkbox" name="optiona" id="cc" checked /> 
<label for="cc">Checkbox 1</label>
<br />
<input  type="checkbox" name="optiona" id="dd" /> 
<label for="dd">Checkbox 1</label>
23
répondu ViliusL 2018-04-10 09:09:59

je viens de sortir avec ceci:

input[type="checkbox"] {display:none;}
input[type="checkbox"] + label:before {content:"☐";}
input:checked + label:before {content:"☑";}
label:hover {color:blue;}
<input id="check" type="checkbox" /><label for="check">Checkbox</label>

bien sûr, grâce à cela, vous pouvez changer la valeur de content à vos besoins et utiliser une image si vous souhaitez ou utiliser une autre police...

L'intérêt principal ici est que:

  1. la taille de la case à cocher reste proportionnelle à la taille du texte

  2. vous pouvez contrôler l'aspect, la couleur, la taille de la case à cocher

  3. Pas de HTML supplémentaire nécessaire !

  4. seulement 3 lignes de CSS nécessaires (la dernière est juste pour vous donner des idées)

13
répondu Sharcoux 2017-10-24 13:06:33

je cherchais une case à cocher un peu plus grande et j'ai regardé le code source de 37Signals Basecamp pour trouver la solution suivante -

vous pouvez changer la taille de la police pour faire la case à cocher légèrement plus grand:

font-size: x-large;

ensuite, vous pouvez aligner la case à cocher correctement en faisant:

vertical-align: top;
margin-top: 3px; /* change to center it */
7
répondu Justin 2014-01-27 17:14:07

trouvé ce conseil utile aujourd'hui: vous pouvez faire des cases à cocher plus grand en Safari-qui est généralement résistant aux approches habituelles - avec cet attribut: -webkit-transform: scale(1.3, 1.3);

5
répondu Avi Flax 2010-06-28 12:55:08

D'après ce que j'ai compris, ce n'est pas facile du tout de faire un cross-browser. Au lieu d'essayer de manipuler le contrôle de case à cocher, vous pouvez toujours construire votre propre implémentation en utilisant des images, javascript, et des champs d'entrée cachés. Je suppose que cela est similaire à ce que niceforms est (de la réponse de Staicu lonut ci-dessus), mais ne serait pas particulièrement difficile à mettre en œuvre. Je crois que jQuery est un plugin pour permettre ce comportement personnalisé (va chercher le lien et poster ici si je peux trouver il.)

4
répondu localshred 2008-11-20 21:48:40

le problème est que Firefox n'écoute ni la largeur ni la hauteur. Désactivez ça et vous pouvez y aller.

input[type=checkbox] {
    width: 25px;
    height: 25px;
    -moz-appearance: none;
}
1
répondu Worthy7 2017-04-27 01:20:34

les autres réponses ont montré une case à cocher pixelisée, alors que je voulais quelque chose de beau. Le résultat ressemble à ceci: checkbox preview

même si cette version est plus compliquée, je pense qu'il vaut la peine de l'essayer.

HTML

<label class="checkbox-list__item">
  <input class="checkbox_buttons" type="checkbox" checked="checked" style="display: none;">
  <div class="checkbox-list__check"></div>
</label>

SCSS

.checkbox-list__item {
  position: relative;
  padding: 10px 0;
  display: block;
  cursor: pointer;
  margin: 0 0 0 34px;
  border-bottom: 1px solid #b4bcc2;

  &:last-of-type {
    border-bottom: 0;
  }
}

.checkbox-list__check {
  width: 18px;
  height: 18px;
  border: 3px solid #b4bcc2;
  position: absolute;
  left: -34px;
  top: 50%;
  margin-top: -12px;
  transition: border .3s ease;
  border-radius: 5px;

    &:before {
    position: absolute;
    display: block;
    width: 18px;
    height: 22px;
    top: -2px;
    left: 0px;
    padding-left: 2px;
    background-color: transparent;
    transition: background-color .3s ease;
    content: '13';
    font-family: initial;
    font-size: 19px;
    color: white;
  }
}

// Checked state
input[type="checkbox"]:checked ~ .checkbox-list__check {
  border-color: #5bc0de;

  &:before {
    background-color: #5bc0de;
  }
}

Jsfiddle: https://jsfiddle.net/asbd4hpr /

1
répondu hcarreras 2017-07-04 16:43:18

j'ai trouvé cette bibliothèque CSS-only très utile: https://lokesh-coder.github.io/pretty-checkbox /

ou, vous pourriez rouler votre propre avec ce même concept de base, similaire à ce que @Sharcoux a posté. C'est en gros:

  • Cacher la case à cocher normale (opacité 0 et placé où elle irait)
  • ajouter une fausse case à cocher basée sur css
  • utiliser input:checked~div label pour le checked style
  • assurez-vous que votre <label> est cliquable en utilisant for=yourinputID

.pretty {
  position: relative;
  margin: 1em;
}
.pretty input {
  position: absolute;
  left: 0;
  top: 0;
  min-width: 1em;
  width: 100%;
  height: 100%;
  z-index: 2;
  opacity: 0;
  margin: 0;
  padding: 0;
  cursor: pointer;
}
.pretty-inner {
  box-sizing: border-box;
  position: relative;
}
.pretty-inner label {
  position: initial;
  display: inline-block;
  font-weight: 400;
  margin: 0;
  text-indent: 1.5em;
  min-width: calc(1em + 2px);
}
.pretty-inner label:after,
.pretty-inner label:before {
  content: '';
  width: calc(1em + 2px);
  height: calc(1em + 2px);
  display: block;
  box-sizing: border-box;
  border-radius: 0;
  border: 1px solid transparent;
  z-index: 0;
  position: absolute;
  left: 0;
  top: 0;
  background-color: transparent;
}
.pretty-inner label:before {
  border-color: #bdc3c7;
}
.pretty input:checked~.pretty-inner label:after {
  background-color: #00bb82;
  width: calc(1em - 6px);
  height: calc(1em - 6px);
  top: 4px;
  left: 4px;
}


/* Add checkmark character style */
.pretty input:checked~.pretty-inner.checkmark:after {
  content: '13';
  color: #fff;
  position: absolute;
  font-size: 0.65em;
  left: 6px;
  top: 3px;
}



body {
  font-size: 20px;
  font-family: sans-serif;
}
<div class="pretty">
	<input type="checkbox" id="demo" name="demo">
	<div class="pretty-inner"><label for="demo">I agree.</label></div>
</div>

<div class="pretty">
	<input type="checkbox" id="demo" name="demo">
	<div class="pretty-inner checkmark"><label for="demo">Please check the box.</label></div>
</div>
1
répondu squarecandy 2017-12-31 00:12:41