I want to both style a label and reveal a div when a checkbox is checked. I have found ways to do one or the other, but not both, and I don't understand why. Note the CSS is the exact same in both examples and is:
input {
display: none
}
.layerpic {
height: 100px;
width: 100px;
background: red;
opacity: 1;
float: left;
}
label {
clear: none;
float: left;
}
.xyz input:checked + .layerpic{
opacity: 0.5;
}
.xyz input:checked + label {
font-weight: bold;
}
Now the HTML. This example allows the div to be affected, but not the label. Note the only difference is in the html where the label comes before the input:
<div class ="xyz">
<label class="ribs" for="hider2">Hide layer</label>
<input type="checkbox" id="hider2">
<div class="layerpic"> hi
</div>
</div>
This example allows the label to be affected, but not the div:
<div class ="xyz">
<input type="checkbox" id="hider2">
<label class="ribs" for="hider2">Hide layer</label>
<div class="layerpic"> hi
</div>
</div>
Can anyone explain why different elements are affected just by flipping which HTML comes first, and is it possible to have both elements affected when the checkbox is checked?
You need to rearrange your HTML, then use + and ~ selectors
input {
display: none
}
.layerpic {
height: 100px;
width: 100px;
background: red;
opacity: 1;
float: left;
}
label {
clear: none;
float: left;
}
.xyz input:checked+.layerpic {
opacity: 0.5;
}
.xyz input:checked~label {
font-weight: bold;
}
<div class="xyz">
<input type="checkbox" id="hider2">
<div class="layerpic"> hi</div>
<label class="ribs" for="hider2">Hide layer</label>
</div>
Related
I want to align two inputs in the same line.
I used the solutions available here:
http://jsfiddle.net/XAkXg/
http://www.java2s.com/Code/HTMLCSS/Form/inputclassidselectorandpropertyselector.htm
Aligning html inputs on same line
html form - make inputs appear on the same line
Everything would work, but in my case, the input[type=text] has been defined earlier
The CSS code looks pretty much as this:
input {
margin-left: 50px;
}
input[type=text] {
width: 75%;
min-width: 450px;
padding: 12px 20px;
box-sizing: border-box;
outline: none;
border: 1px solid #f7fbff;
background-color: #f7fbff;
}
input[type=text]:focus {
background-color: #c6e2f2;
}
input[type=number]:focus {
background-color: #c6e2f2;
}
and now I have implemented the location feature into my HTML code:
<figure class="fig" id="location">
<label>
<div class="order">1</div>
<p>Location<span class="asterisk">*</span></p>
</label>
<button class="locbtn" id="btn-geolocation">Find my location</button>
<br>
<label for="lat" class="location">Latitude</label>
<input type="text" name="latitude" class="location">
<label for="lon" class="location">Longitude</label>
<input type="text" name="longitude" class="location">
<div style="clear:both;"></div>
</figure>
and accordingly CSS:
.location {
width: 50x;
float: left;
vertical-align: top;
margin: auto;
}
.location input[type=text]{
width: 25px;
display: inline-block;
}
and the effect is, as you can see below:
the input texts don't work, since they have been defined earlier in the code (for entire form).
How can I make the input[type=text] definition also for this section considered?
Moreover, instead of display: inline-block; I tried: display: inline; and float: left; It didn't work either.
I am trying to make a collapsible button with pure HTML and CSS. Here is what I have:
#hidden {
display: none;
height: 100px;
background: red;
}
:checked+#hidden {
display: block;
}
<input type="checkbox" id="my_checkbox" style="display:none;">
<div id="hidden"></div>
<label for="my_checkbox">Show/hide</label>
This works. However, I want the hidden div to come after the button instead of before. When I move the div to after the checkbox label, it does not work.
How can I fix this ?
Thanks!
You want to use a different CSS selector. The below uses the General sibling combinator to target the div no matter its order with respect to the input element (so long as it follows it).
#hidden {
display: none;
height: 100px;
background: red;
}
:checked ~ #hidden {
display: block;
}
<input type="checkbox" id="my_checkbox" style="display:none;">
<label for="my_checkbox">Show/hide</label>
<div id="hidden"></div>
use negation instead of +, so that it will select all divs related to that class name
#hidden {
display: none;
height: 100px;
background: red;
}
:checked~#hidden {
display: block;
}
<input type="checkbox" id="my_checkbox" style="display:none;">
<label for="my_checkbox">Show/hide</label>
<div id="hidden"></div>
Everybody knows the trick with toggling by click between 2 styles of <label> via <input type="checkbox">.
input {
display: none;
}
input:checked+label {
background: green;
}
label {
background: yellow;
cursor: pointer;
display: block;
height: 100px;
width: 100px;
}
<input type="checkbox" id="toggle">
<label for="toggle"></label>
But what if you want to toggle through a chain of styles?
For example: click 1 => CSS 1, click 2 => CSS 2, click 3 => CSS 3, click 4 => CSS 1... etc.
Is it possible with HTML + CSS only?
UPD: Yes, it is. My approach is below. But is it possible to make the solution simpler?
input, label {
display: none;
}
div {
height: 50px;
width: 50px;
}
#toggle1:checked~div {
background: red;
}
#toggle2:checked~div {
background: yellow;
}
#toggle3:checked~div {
background: blue;
}
#toggle1:checked~div [for=toggle2],
#toggle2:checked~div [for=toggle3],
#toggle3:checked~div [for=toggle1] {
cursor: pointer;
display: block;
height: 100%;
}
<input type="radio" name="toggle" id="toggle1" checked>
<input type="radio" name="toggle" id="toggle2">
<input type="radio" name="toggle" id="toggle3">
<div>
<label for="toggle1"></label>
<label for="toggle2"></label>
<label for="toggle3"></label>
</div>
Could someone explain how the last part of the code works? Specifically:
[type=radio]:checked {
}
[type=radio]:checked ~ .content {
z-index: 1;
}
I'm just starting with CSS as a newb and wanted to try to create some interactive CSS tabs; which lead me to look at some existing code out there. Needless to say it has left me quite confused.
Why is [type=radio]:checked needed? It had z-index: 2; inside the brackets but I took that out and the code still works just fine; although when I try and delete [type=radio]:checked all together the code breaks. Why? It has no properties currently.
[type=radio]:checked ~ .content used to be [type=radio]:checked ~ label ~ .content but I took out label and it still works fine. Why was it ever needed?
HTML:
<div class="tab">
<input type="radio" id="tab-1" name="tab-group-1" checked>
<label for="tab-1">Tab One</label>
<div class="content">
tab#1
</div>
</div>
<div class="tab">
<input type="radio" id="tab-2" name="tab-group-1">
<label for="tab-2">Tab Two</label>
<div class="content">
tab#2
</div>
</div>
<div class="tab">
<input type="radio" id="tab-3" name="tab-group-1">
<label for="tab-3">Tab Three</label>
<div class="content">
tab#3
</div>
</div>
</div>
</html>
CSS:
.tabs {
position: relative;
height: 200px; /* This part sucks */
clear: both;
margin: 25px 0;
}
.tab {
float: left;
}
.tab label {
background: #eee;
padding: 10px;
border: 1px solid #ccc;
margin-left: -1px;
position: relative;
left: 1px;
}
.tab [type=radio] {
display: none;
}
.content {
position: absolute;
top: 28px;
left: 0;
background: white;
right: 0;
bottom: 0;
padding: 20px;
border: 1px solid #ccc;
}
[type=radio]:checked {
}
[type=radio]:checked ~ .content {
z-index: 1;
}
The last part of your CSS:
[type=radio]:checked {
}
[type=radio]:checked ~ .content {
z-index: 1;
}
This is giving a z-index to the class content. Since only one tab is clicked it is giving a z-index to only one content class and that makes it display. (Since no others have a z-index)
If you want to see how it works then add a z-index to the content class, lets say 10, in your CSS and watch how it gets all screwy. Now since that code is only giving a z-index: 1; it doesn't display correctly since they all have 10 in this example. Now go to the above snidbit of code and put a z-index: 11; and watch how it works correctly. Since only one gets a high z-index: 11; it becomes the displaying one.
If you don't know what the [type=radio]:checked means, it is pertaining to an active state or clicked state for that radio input.
This part of code: [type=radio]:checked ~ label ~ .content is allowing a more distinguished and precise selection of a DOM element. It will work without it because .content is below the radio tag. It will only apply to an element that is 1. input radio > 2. label > 3. .content.
If you also don't know what z-index does then let me know and I'll brake that down.
Is there any way to make a radio button bigger using CSS?
If not, how else can I do it?
Try this code:
input[type='radio'] {
transform: scale(2);
}
You can easily able to set it's height and width as with any element.
Here is the fiddle with code
JSFIDDLE BIG RADIO BUTTON
HTML
<input id="r1" type="radio" name="group1" class="radio1" />
<label for="r1">label 1 text</label>
<input id="r2" type="radio" name="group1" class="radio2" />
<label for="r2">label 2 text</label>
<input id="r3" type="radio" name="group1" class="radio3" />
<label for="r3">label 3 text</label>
<input id="r4" type="radio" name="group1" class="radio4" />
<label for="r4">label 4 text</label>
CSS
input[type=radio] {
display: none;
}
input[type=radio] + label::before {
content: '';
display: inline-block;
border: 1px solid #000;
border-radius: 50%;
margin: 0 0.5em;
}
input[type=radio]:checked + label::before {
background-color: #ffa;
}
.radio1 + label::before {
width: 0.5em;
height: 0.5em;
}
.radio2 + label::before {
width: 0.75em;
height: 0.75em;
}
.radio3 + label::before {
width: 1em;
height: 1em;
}
.radio4 + label::before {
width: 1.5em;
height: 1.5em;
}
Styling radio button is not easy.
Form elements in general are either problematic or impossible to style using CSS alone.
Just go through this link for your own style with bigger size for radio buttons..
Also look at this link...
Bigger radio buttons
Don't use transform: scale(1.3), it really looks horrible. Just use this:
input[type='radio'] {
height: 15px;
width: 15px;
vertical-align: middle;
}
<input type="radio">Select this item
You can do it using CSS but browser and OS also impact on this. Look at following article.
Styling radio buttons with CSS
Try this:
HTML
<label>
<input type="radio" value="1">
<div></div>
</label>
CSS
input[type="radio"] {
display: none;
}
input[type="radio"] + div {
height: 20px;
width: 20px;
display: inline-block;
cursor: pointer;
vertical-align: middle;
background: #FFF;
border: 1px solid #d2d2d2;
border-radius: 100%;
}
input[type="radio"] + div:hover {
border-color: #c2c2c2;
}
input[type="radio"]:checked + div {
background:gray;
}
DEMO: http://jsfiddle.net/nuzhysgg/
There might be some quirky <span> tricks inside radio elements but I imagine using them across different browsers would be annoying to debug.
I've used this script in the past but not recently.
CSS3 transform scale is blurry. Setting height & width does not work with FF (even the newest 66 does not support, 2020). The only cross-browser solution is custom HTML markup + CSS, which unfortunatelly is not the easiest way. See helpful tutorial custom radios & checkboxes.