i'm trying to style a radio button using just css, but i can't figure out why it does not work:
HTML
<input type="radio">
CSS
input[type="radio"]{
background: green;
}
http://jsfiddle.net/vNmLe/
Radio buttons, checkboxes and selects can't be styled very well in a cross-browser fashion using only CSS. You'll need some extra markup and a little javascript.
One technique is to wrap the input inside a div and set the input's opacity to 0. You position the input inside of the wrapping div so that it fills the entire space.
HTML:
<div class="faux-radio" data-group="radio-test">
<input type="radio" id="radio-1" name="radio-test">
</div>
CSS:
input[type="radio"] {
left: 0;
margin: 0;
padding: 0;
position: absolute;
top: 0;
z-index: 5;
/* IE 8 */
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
-moz-opacity: 0;
opacity: 0;
}
.faux-radio {
width: 12px;
height: 12px;
background: #f2f2f2;
border: 1px solid #a6a6a6;
border-radius: 12px;
display: inline-block;
margin-right: 2px;
position: relative;
}
.faux-radio.selected:after {
content: '';
width: 6px;
height: 6px;
background: #666;
border-radius: 6px;
position: absolute;
top: 2px;
left: 2px;
}
label {
margin-right: 20px;
}
With this technique you get your own style and still get all of the standard input behavior. But you have extra markup for every form element. And you'll need to add some javascript to provide the visual feedback users expect when clicking form elemens.
Here's a quick radio button example with js included: http://jsfiddle.net/xevw3/17/
Related
I used a radio button in my simple project. The radio button looks good with a blue color background when selected. But while running the same code using Edge and Firefox, the background color is black.
I heard that the radio button style is Browser specific. Is this true?
And is there any possibility to change the style without custom radio buttons?
If a custom radio button is the only option, then how do you render it the same as in Google Chrome?
Radio Button visible in Chrome
Radio Button visible in Firefox and Edge
I recreated the chrome radio button with custom css and added a blue background instead of gray. It shows up the same on chrome, firefox, and edge. You might want to do some tweaking of the css to fit your size needs though.
Here is the code
HTML
div class="radio-item">
<input type="radio" id="ritema" name="ritem" value="ropt1">
<label for="ritema">Option 1</label>
</div>
<div class="radio-item">
<input type="radio" id="ritemb" name="ritem" value="ropt2">
<label for="ritemb">Option 2</label>
</div>
CSS
.radio-item {
display: inline-block;
position: relative;
padding: 0 6px;
margin: 10px 0 0;
}
.radio-item input[type='radio'] {
display: none;
}
.radio-item label {
color: black;
font-weight: normal;
}
.radio-item label:before {
content: " ";
display: inline-block;
position: relative;
top: 5px;
margin: 0 5px 0 0;
width: 20px;
height: 20px;
border-radius: 11px;
border: 2px solid gray;
background-color: transparent;
}
.radio-item input[type=radio]:checked + label:after {
border-radius: 11px;
width: 12px;
height: 12px;
position: absolute;
top: 11px;
left: 12px;
content: " ";
display: block;
background: blue;
background-color: blue;
}
I have a styled checkbox that is the child of a container along with some text. The reason the checkbox and text are children of a parent is so that they can sit next to each other and be centered vertically on the UI. This has worked fine for me; however, I've noticed that the checkbox starts to change from a perfect circle into more of an oval as text starts to wrap into multiple lines (on mobile the text is two lines long and on desktop it is only one line). How could I fix this so that the checkbox does not stretch as the text wraps into multiple lines? Below is my html and styling, thank you.
.opt-in {
display: flex;
align-items: center;
color: #f4a11e;
}
input[type='checkbox'] {
position: relative;
margin: 17px 15px 0 0;
cursor: pointer;
width: 20px;
height: 20px;
-webkit-appearance: none;
background: transparent;
border: 1px solid #f4a11e;
outline: none;
border-radius: 50%;
transition: 0.5s;
}
input[type='checkbox']:before {
content: '';
position: absolute;
top: 45%;
left: 50%;
width: 4px;
height: 10px;
opacity: 0;
border-right: 1px solid #f4a11e;
border-bottom: 1px solid #f4a11e;
transform: translate(-50%, -50%) rotateZ(40deg);
transition: 0.2s;
}
input:checked[type='checkbox']:before {
opacity: 1;
}
<div class="opt-in">
<input type="checkbox" v-model="optIn" id="checkbox" />
<label for="checkbox">Opt-in to receive the latest cloud insights and industry deep dives.</label>
</div>
In the CSS code in the input[type='checkbox'] {} section, try using min-width: 20px; rather than width: 20px;.
Worked for me in a test implementation with copy/pasted code although I had to set max-width in the properties of the opt-in div to test it.
I don't have a technical explanation but I believe it has something to do with the relative position or the display: flex overriding the specified width/height in pixels.
I've a tile with an input of type radio, a label and a span text below this two elements. The input and label are connected with an id. They should be next to each other. The span should be directly below the label. This works so far and my layout matches. If I click on the input or on the label, the radio gets selected. Now what I'd like to do is, to select the radio by clicking on the tile, no mather where. So every click on the tile should select my radio. I would like to solve this with pure HTML/CSS without using JS, if possible. The only idea I had, is to give position: relative; to the radio and position: absolute; to the radio__wrap and the label and make them have width/height of 100%, so I can pull over the label over the whole tile. This idea crashed my layout (hard do position the span correctly). Is there a way to solve this by using pure HTML/CSS. Below is my snippet:
.tile {
border: 1px solid transparent;
border-radius: 10px;
box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.2);
cursor: pointer;
margin-bottom: 30px;
}
.tile__wrap {
position: relative;
}
.radio {
padding: 24px 16px 16px;
display: flex;
flex-direction: column;
position: relative;
}
.radio__wrap {
margin-bottom: 10px;
}
.radio__label {
background-color: lightblue;
position: relative;
}
.radio__text {
padding-left: 24px;
display: block;
}
.tile--clickable .radio__wrap {
position: relative;
width: 100%;
height: 100%;
z-index: 5;
}
.tile--clickable .radio__label {
width: 100%;
height: 100%;
}
<div class="tile">
<div class="tile__wrap">
<div class="radio">
<div class="radio__wrap">
<input class="radio__input" id="radio01" type="radio">
<label class="radio__label" for="radio01">I'm the label</label>
</div>
<span class="radio__text">I'm the text</span>
</div>
</div>
</div>
I'm trying to style a radio button using css3 pseudo elements. In works great in chrome but it jumps back to the default styling in firefox. Any idea?
Here's a fiddle: JSFIDDLE
The HTML:
<input class="fancy" name="cc1" type="radio" checked/>
<input class="fancy" name="cc1" type="radio"/>
The CSS:
input[type="radio"].fancy:before {
content:"";
width: 24px;
height: 24px;
background-color: #1f1f1f;
display: block;
border-radius: 50%;
position: relative;
top: -6px;
left: -6px;
box-shadow: 0 1px 2px rgba(255,255,255,.1),
inset 0 2px 2px rgba(0,0,0,.8);
}
input[type="radio"].fancy:checked:before {
visibility: visible;
content:"";
width: 24px;
height: 24px;
background-color: #1f1f1f;
display: block;
border-radius: 50%;
position: relative;
top: -6px;
left: -6px;
box-shadow: 0 1px 2px rgba(255,255,255,.2),
inset 0 2px 2px rgba(0,0,0,.8);
}
input[type="radio"].fancy:checked:after {
visibility: visible;
content:"";
width: 14px;
height: 14px;
display: block;
border-radius: 50%;
position: relative;
top: -25px;
left: -1px;
background: yellowgreen;
}
I'm trying to avoid background-images
Unfortunately, what you're trying to do is not valid -- ::before and ::after do not work on input elements (any input; it's not just restricted to radio buttons).
Proof available here: http://jsfiddle.net/LvaE2/1/ -- even in the most simple case, it doesn't work.
It also doesn't work in IE or Opera. The fact that it does work in Chrome is because Chrome is going beyond the spec in allowing it.
Your best bet is to do your styling on a label element that is linked to the actual radio button using the for attribute, and then set the radio button itself to display:none;. This is how everyone else does it.
Related question here: Which elements support the ::before and ::after pseudo-elements?
Hope that helps.
CSS3 Input radio will working in Firefox: version > 80 if added style appearance
input[type=checkbox] { appearance:initial; }
Please see the Snippet:
input[type=radio] { appearance:initial; }
input[type=radio]:after{
content: "after content";
width: 100px; height: 100px; display:block;
background: red;
}
input[type=radio]:checked:after{
content: "after content";
width: 100px; height: 100px; display:block;
background: blue;
}
<input type="radio">
<input type="radio">
<input type="radio">
In the following code, I want a tool-tip to come up when the user hovers the span, how do I do that? I don't want to use any links.
<span> text </span>
Here's the simple, built-in way:
<span title="My tip">text</span>
That gives you plain text tooltips. If you want rich tooltips, with formatted HTML in them, you'll need to use a library to do that. Fortunately there are loads of those.
Custom Tooltips with pure CSS - no JavaScript needed:
Example here (with code) / Full screen example
As an alternative to the default title attribute tooltips, you can make your own custom CSS tooltips using :before/:after pseudo elements and HTML5 data-* attributes.
Using the provided CSS, you can add a tooltip to an element using the data-tooltip attribute.
You can also control the position of the custom tooltip using the data-tooltip-position attribute (accepted values: top/right/bottom/left).
For instance, the following will add a tooltop positioned at the bottom of the span element.
<span data-tooltip="Custom tooltip text." data-tooltip-position="bottom">Custom bottom tooltip.</span>
How does this work?
You can display the custom tooltips with pseudo elements by retrieving the custom attribute values using the attr() function.
[data-tooltip]:before {
content: attr(data-tooltip);
}
In terms of positioning the tooltip, just use the attribute selector and change the placement based on the attribute's value.
Example here (with code) / Full screen example
Full CSS used in the example - customize this to your needs.
[data-tooltip] {
display: inline-block;
position: relative;
cursor: help;
padding: 4px;
}
/* Tooltip styling */
[data-tooltip]:before {
content: attr(data-tooltip);
display: none;
position: absolute;
background: #000;
color: #fff;
padding: 4px 8px;
font-size: 14px;
line-height: 1.4;
min-width: 100px;
text-align: center;
border-radius: 4px;
}
/* Dynamic horizontal centering */
[data-tooltip-position="top"]:before,
[data-tooltip-position="bottom"]:before {
left: 50%;
-ms-transform: translateX(-50%);
-moz-transform: translateX(-50%);
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
}
/* Dynamic vertical centering */
[data-tooltip-position="right"]:before,
[data-tooltip-position="left"]:before {
top: 50%;
-ms-transform: translateY(-50%);
-moz-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
}
[data-tooltip-position="top"]:before {
bottom: 100%;
margin-bottom: 6px;
}
[data-tooltip-position="right"]:before {
left: 100%;
margin-left: 6px;
}
[data-tooltip-position="bottom"]:before {
top: 100%;
margin-top: 6px;
}
[data-tooltip-position="left"]:before {
right: 100%;
margin-right: 6px;
}
/* Tooltip arrow styling/placement */
[data-tooltip]:after {
content: '';
display: none;
position: absolute;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
}
/* Dynamic horizontal centering for the tooltip */
[data-tooltip-position="top"]:after,
[data-tooltip-position="bottom"]:after {
left: 50%;
margin-left: -6px;
}
/* Dynamic vertical centering for the tooltip */
[data-tooltip-position="right"]:after,
[data-tooltip-position="left"]:after {
top: 50%;
margin-top: -6px;
}
[data-tooltip-position="top"]:after {
bottom: 100%;
border-width: 6px 6px 0;
border-top-color: #000;
}
[data-tooltip-position="right"]:after {
left: 100%;
border-width: 6px 6px 6px 0;
border-right-color: #000;
}
[data-tooltip-position="bottom"]:after {
top: 100%;
border-width: 0 6px 6px;
border-bottom-color: #000;
}
[data-tooltip-position="left"]:after {
right: 100%;
border-width: 6px 0 6px 6px;
border-left-color: #000;
}
/* Show the tooltip when hovering */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
display: block;
z-index: 50;
}
In most browsers, the title attribute will render as a tooltip, and is generally flexible as to what sorts of elements it'll work with.
<span title="This will show as a tooltip">Mouse over for a tooltip!</span>
stackoverflow.com
<img src="something.png" alt="Something" title="Something">
All of those will render tooltips in most every browser.
For the basic tooltip, you want:
<span title="This is my tooltip"> Hover on me to see tooltip! </span>
The title attribute will be used as the text for tooltip by the browser. If you want to apply style to it, consider using some libraries, e.g. jQuery UI.