Selecting Multiple General Siblings of Elements Within Separate Div's - html

I know can control the display of a single class by using the following CSS:
.some-random-class{
display: none;
}
input[type=checkbox]:checked ~ .some-random-class{
display: flex;
}
I understand that this code controls the display of any object with the class of "some-random-class" that is a sibling of whatever checkbox is checked. If I have 3 classes I want to control the display of, I can do so with 3 separate input "functions" addressing each class individually. What I can't quite seem to figure out is how to control multiple classes in with the same "function" and control classes that are not in the same generation as in the following HTML:
<div>
<input type="checkbox">
<div class="first-class">
stuff to hide
</div>
<div class="second-class">
stuff to hide
<input type="checkbox">
<div class="third-class">
stuff to hide
</div>
</div>
</div>
When I try to control all three using what I think logically should work, it breaks the whole thing.
The following CSS is what I thought SHOULD work, however doesn't:
input[type=checkbox]:checked ~ .first-class ~ .second-class ~ .third-class {
display: flex;
}
Is this even possible, or am I asking too much of CSS?
If what I want to accomplish is possible, what am I not understanding about the above function?

You need to separate each rule with , , not just concatenate them with ~, because that only can select a single element.
input[type=checkbox]:checked ~ .first-class,
input[type=checkbox]:checked ~ .second-class,
input[type=checkbox]:checked ~ .third-class {
color: red;
}
input[type=checkbox]:checked ~ .third-class {
color: blue;
}
<div>
<input type="checkbox">
<div class="first-class">
stuff to hide
</div>
<div class="second-class">
stuff to hide
<input type="checkbox">
<div class="third-class">
stuff to hide
</div>
</div>
</div>

Related

Styling Only Radio Inputs Within a Two-Level Deep Div

I have a containing div that has three divs inside. I want to style only the two divs that contain the radio input. Without using class names, is it possible to select those two divs?
If not, how do I select just the radio inputs and style those? Here's my attempt, with non-working CSS:
.container > div > input[type="radio"] {
border:1px solid green;
}
<div class="container">
<div>
<input type="radio" id="22" name="SetFour">
<label for="22"><span>Selection One</span></label>
</div>
<div>Some Random Div</div>
<div>
<input type="radio" id="23" name="SetFour">
<label for="23"><span>Selection Two</span></label>
</div>
</div>
CodePen for reference
You can use nth-of-type. But do this only if you have no alternatives and are sure that this block will not change in the future.
.container > div:nth-of-type(1),
.container > div:nth-of-type(3) {
border:1px solid green;
}
The selector selects the radio buttons, but the radio inputs don’t support the border property.
In case you want to select the divs, not the inputs, use classes; although there is a :has() pseudo‐class in the specifications, no major browser currently supports it.
https://caniuse.com/css-has
https://www.w3.org/TR/selectors-4/#relational
you have to set them a class.
write the similar class and styling.
or their id.

CSS can hide span-element and show it on hover, does not work for p-element [duplicate]

This question already has an answer here:
List of HTML5 elements that can be nested inside P element?
(1 answer)
Closed 2 years ago.
I want to offer a selection by radio buttons. Upon hovering over the label for the button, I want there to appear an explanation for that option.
I can achieve this by putting the explanation text into a span-element, but not in a p-element...and I fail to understand the difference:
<fieldset>
<h2>Select player</h2>
<div clas="radios">
<legend>Select character class:</legend>
<p class="row">
<input type="radio" id="character-ninja" name="character" value="ninja">
<label for="character-ninja" class="show">Ninja</label>
<p class="hidden">The ninja class....</p>
</p>
</div>
</fieldset>
with the style sheet
.hidden{
display: none;
}
.show:hover + .hidden{
display: block;
}
The text in the p-element is hidden by "display:none", but it does not appear upon hovering over the text in the label-element.
If I change the p-element into a span-element, the text is also hidden by "display:none", but it does appear upon hovering over the text in the label-element.
I think that the different behavior might be the result of nesting a p-element within a p-element...but even so I don't quite understand why it is "partially working", as I would call it.
You should use div as a row element:
<div class="row">
The reason why nested p tags does not work in your code is that it is not valid HTML. It is corrected by the browser. So the sibling of label is no longer p then.
.hidden{
display: none;
}
.show:hover + .hidden{
display: block;
}
<fieldset>
<h2>Select player</h2>
<div clas="radios">
<legend>Select character class:</legend>
<div class="row">
<input type="radio" id="character-ninja" name="character" value="ninja">
<label for="character-ninja" class="show">Ninja</label>
<p class="hidden">The ninja class....</p>
</div>
</div>
</fieldset>

How to hide / display different page sections with radio input buttons

I'm an amature web developer, just getting my feet wet with my first proper html project.
I'm trying to build a webpage where you have a 'tab' selection in the top row and the currently selected tab shows a different section on the main page (hiding the other sections when not selected). I've done this using a radio input inside a label inside a table made of divs, and defined all of what I think is the correct CSS. (see below)
But the tabs do not work: depending on what I change, either every section shows up at once, or none of them do.
Here's my HTML:
<div class="sheet-table">
<div class="sheet-table-row sheet-candara">
<div class="sheet-col">
<label class="container" title="Adventure tab">
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab1" value="1">
<span class="sheet-tabs sheet-tab1 sheet-center">SECTION 1</span>
</label>
</div>
<div class="sheet-col">
<label class="container" title="Lifestyle tab">
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab2" value="2">
<span class="sheet-tabs sheet-tab2 sheet-center">SECTION 2</span>
</label>
</div>
<div class="sheet-col">
<label class="container" title="Options tab">
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab3" value="3">
<span class="sheet-tabs sheet-tab3 sheet-center">SECTION 3</span>
</label>
</div>
</div>
</div>
Then later on I have the section content (which I've commented out here):
<div class="sheet-section-tab1">
<!-- section 1 'adventure' content -->
</div>
<div class="sheet-section-tab2">
<!-- section 2 'lifestyle' content -->
</div>
<div class="sheet-section-tab3">
<!-- section 3 'options' content -->
</div>
And the CSS (a portion of which was copied and edited from W3Schools.com):
.container {
display: inline-block;
position: relative;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
.sheet-tabs {
position: inherit;
padding: 0.2em 0.9em;
z-index: 9999;
}
.container:hover input ~ .sheet-tabs {
background-color: rgba(30, 20, 20, 0.3);
border-radius: 0.5em;
content: attr(title);
}
.container input:checked ~ .sheet-tabs {
background-color: rgba(30, 20, 20, 0.7);
color: white;
border-radius: 0.5em;
}
div[class^="sheet-section"] {
display: none;
}
.sheet-section-tab1,
.sheet-section-tab2,
.sheet-section-tab3 {
display: none;
}
input.sheet-tab1:checked ~ div.sheet-section-tab1,
input.sheet-tab2:checked ~ div.sheet-section-tab2,
input.sheet-tab3:checked ~ div.sheet-section-tab3{
display: block;
}
Now, the strange thing that I discovered while trying to get this to work was that it does work when I take the inputs and spans out of their respective labels and div tables, like so:
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab1" value="1">
<span class="sheet-tabs sheet-tab1 sheet-center">SECTION 1</span>
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab2" value="2">
<span class="sheet-tabs sheet-tab2 sheet-center">SECTION 2</span>
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab3" value="3">
<span class="sheet-tabs sheet-tab3 sheet-center">SECTION 3</span>
<div class="sheet-section-tab1">
<!-- section 1 'adventure' content -->
</div>
<div class="sheet-section-tab2">
<!-- section 2 'lifestyle' content -->
</div>
<div class="sheet-section-tab3">
<!-- section 3 'options' content -->
</div>
But then I lose all the CSS styling that I worked so hard on...
Is there any way to get the functionality that I desire without compromising on the style, or am I doing something fundamentally wrong here? Or is there just a simple mistake I'm not seeing? Thanks!
(Also, I don't know if this will be relevant to any answers, but for reasons specific to my hosting platform I can't use the id attribute).
Using just HTML and CSS to create a tab system like you propose is possible, but is going to be messy and overall inefficient, and styling of the page will be difficult due to how the radio buttons will need to be aligned in order to achieve this. I highly recommend using java script to accomplish what you are doing. Here is a link to a HowTo on JavaScript tabs. However, if it is a must for this project to be accomplished through the strict use of HTML and CSS, let me know and I'll take a deeper look. From now what I can tell you is a possible way to accomplish this is to use the "+" selector. Structure the page in a way so that it alternate between radio button and the div/tab to be displayed, as so:
radio button
div
radio button
div
radio button
div
This way, the radio button is assigned to the div below it. Now you can target a checked radio button through the use of ":checked" and then using "+" which will select the closest div to the radio button. This can be accomplished in css using the following code:
.tabs {
display: none;
}
[name="radiobutton"]:checked + .tab {
display: block;
}
Cheers!
Have you tried adding in your Css overflow:Hidden;

CSS Checkbox styling unique classes

Is there a good way to select an input that has unique classes.
At the moment this will do...
input[type="checkbox"]:checked + label {....}
But what if I want to grab the label by the class name usethis? Code below -
<div class="checkbox">
<input id="c1" class="rounded-input" type="checkbox" rel="performance" onchange="change()" />
<label class="usethis" for="c1">Performance</label>
</div>
See if this works
input[type="checkbox"]:checked + .usethis {
color: red;
font-size: 5rem; // i have given some random styles here
}
This style will only come when the checkbox is checked, and if you have another checkbox in the page without the label having the class .usethis, this style wont be applied to those checkboxes

How to target an outer Div when checkbox is checked

I want to target a div when a checkbox is checked. Can anybody tell me how can I target an outer div when checkbox is checked?
if($('.checkboxClassName').checked) {
$(this).parent();
}
It will target the parent div that the checkbox is inside, you can use more .parent() if tour target is not inside the same parent.
example: if your code is like this:
<div class="container">
<div class="target"></div>
<div class="parent">
<div class="checkboxDiv">
<input type="checkbox">
</div>
</div>
</div>
and you want to target the div.target you'll need the code like this:
if($('.checkboxClassName').checked) {
$(this).parent().parent().parent().find(".target").css('background','magenta');
} else {
$(this).parent().parent().parent().find(".target").css('background','cyan');
}
those parents will work like this: $(this).parent() = targeting div.checkboxDiv
$(this).parent().parent() = targeting div.parent
...
Note how this jsFiddle highlights the usage in a very simple way:
A check box is focused upon (checked).
The CSS style :checked catches this occurrence and applies a CSS style to the div contents.
The div can be another element you want, just make sure you play around with the code and adapt it to your needs. Let us know if you need any more help!
Source: :checked
HTML
<input type="checkbox" id="ossm" name="ossm">
<label for="ossm">CSS is Awesome</label>
CSS
input[type=checkbox] + label {
color: #ccc;
font-style: italic;
}
input[type=checkbox]:checked + label {
color: #f00;
font-style: normal;
}
Edit: I thought you would like a reference to 'how' CSS works here:
Attribute Selector by value
Element plus Element
...and in general CSS selectors