Pure CSS Segmented Buttons
Sunday, 18th September, 2011
So, I was playing around with some CSS the other day, and was challenged to make some buttons “look like a mac”, specifically, the segmented ones shown on Wikimedia (I don’t have a mac!) What I managed to get working are some buttons, which are actually radio buttons, so standard HTML form elements, which look like this:
It should work on Firefox, Chromium, and other css3 browsers. The HTML is pretty simple, and nothing special. Notice the labels attached to the input elements. The edge labels have a class defined to allow rounded corners.
1 2 3 4 5 6 7 | <form> <input id="male" type="radio" name="sex" value="male" checked="checked" /> <label for="male">Male</label> <input id="female" type="radio" name="sex" value="female" /> <label for="female">Female</label> <input id="ostrich" type="radio" name="sex" value="ostrich" /> <label for="ostrich">Ostrich</label></form> |
<form> <input id="male" type="radio" name="sex" value="male" checked="checked" /> <label for="male">Male</label> <input id="female" type="radio" name="sex" value="female" /> <label for="female">Female</label> <input id="ostrich" type="radio" name="sex" value="ostrich" /> <label for="ostrich">Ostrich</label></form>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | /* Hide the Radio button itself */ input[type="radio"] { display: none; } label { font-family: 'Bentham', arial, serif; font-size: 1.2em; /* force the elements to line up alongside one another */ display: inline-block; /* A little border */ border: solid 1px black; /* give it some space */ padding: 0.2em; /* Nice gradient background */ /* Both default and selected are taken from http://ux5.co/2011/useful-css3-gradients-apple-osx-colors/ */ background: #bdc6cd; /* Old browsers */ background: -moz-linear-gradient(top, #eef6f9 0%, #bdc6cd 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#eef6f9), color-stop(100%,#bdc6cd)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top, #eef6f9 0%,#bdc6cd 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top, #eef6f9 0%,#bdc6cd 100%); /* Opera11.10+ */ background: -ms-linear-gradient(top, #eef6f9 0%,#bdc6cd 100%); /* IE10+ */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#eef6f9', endColorstr='#bdc6cd',GradientType=0 ); /* IE6-9 */ background: linear-gradient(top, #eef6f9 0%,#bdc6cd 100%); /* W3C */ /* Small bit of shadow */ -webkit-box-shadow: 2px 2px 4px #000; -moz-box-shadow: 2px 2px 4px #000; box-shadow: 2px 2px 4px #000; } /* Apply this style to a label which is linked to an input box, which is checked or slected */ input:checked + label { color: white; /* change the background to a nice blue gradient */ background: -moz-linear-gradient(top, #5792d1 0%, #2f5e9e 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#5792d1),color-stop(100%,#2f5e9e)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top, #5792d1 0%,#2f5e9e 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top, #5792d1 0%,#2f5e9e 100%); /* Opera11.10+ */ background: -ms-linear-gradient(top, #5792d1 0%,#2f5e9e 100%); /* IE10+ */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#5792d1',endColorstr='#2f5e9e',GradientType=0 ); /* IE6-9 */ background: linear-gradient(top, #5792d1 0%,#2f5e9e 100%); /* W3C */ } /* round the corners of each end */ label.leftend { border-top-left-radius: 5px; -moz-border-radius-topleft: 5px; border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px; border-right: none; } label.rightend { border-top-right-radius: 5px; -moz-border-radius-topright: 5px; border-bottom-right-radius: 5px; -moz-border-radius-bottomright: 5px; border-left: none; } |
/* Hide the Radio button itself */
input[type="radio"] {
display: none;
}
label {
font-family: 'Bentham', arial, serif;
font-size: 1.2em;
/* force the elements to line up alongside one another */
display: inline-block;
/* A little border */
border: solid 1px black;
/* give it some space */
padding: 0.2em;
/* Nice gradient background */
/* Both default and selected are taken from http://ux5.co/2011/useful-css3-gradients-apple-osx-colors/ */
background: #bdc6cd; /* Old browsers */
background: -moz-linear-gradient(top, #eef6f9 0%, #bdc6cd 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#eef6f9),
color-stop(100%,#bdc6cd)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #eef6f9 0%,#bdc6cd 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #eef6f9 0%,#bdc6cd 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, #eef6f9 0%,#bdc6cd 100%); /* IE10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#eef6f9',
endColorstr='#bdc6cd',GradientType=0 ); /* IE6-9 */
background: linear-gradient(top, #eef6f9 0%,#bdc6cd 100%); /* W3C */
/* Small bit of shadow */
-webkit-box-shadow: 2px 2px 4px #000;
-moz-box-shadow: 2px 2px 4px #000;
box-shadow: 2px 2px 4px #000;
}
/* Apply this style to a label which is linked to an input box, which is checked or slected */
input:checked + label {
color: white;
/* change the background to a nice blue gradient */
background: -moz-linear-gradient(top, #5792d1 0%, #2f5e9e 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#5792d1),color-stop(100%,#2f5e9e)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #5792d1 0%,#2f5e9e 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #5792d1 0%,#2f5e9e 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, #5792d1 0%,#2f5e9e 100%); /* IE10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#5792d1',endColorstr='#2f5e9e',GradientType=0 ); /* IE6-9 */
background: linear-gradient(top, #5792d1 0%,#2f5e9e 100%); /* W3C */
}
/* round the corners of each end */
label.leftend {
border-top-left-radius: 5px;
-moz-border-radius-topleft: 5px;
border-bottom-left-radius: 5px;
-moz-border-radius-bottomleft: 5px;
border-right: none;
}
label.rightend {
border-top-right-radius: 5px;
-moz-border-radius-topright: 5px;
border-bottom-right-radius: 5px;
-moz-border-radius-bottomright: 5px;
border-left: none;
}