LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2023, 09:04 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Rep: Reputation: 52
How do I get CSS code [class^="sz"] to work


I have scattered in a large text web page a large number of small sections of text that need to be shown or hidden individually and I only have a very rudimentary knowledge of CSS.
These sections are given ID 'sz1' to 'sz99' and I understand I should use CSS code like:
Code:
[class^="sz"] { 
}
[class$="n"] (n being a number)
to show or hide each.

Almost every combination I have tried results in all of the individual bits of text showing/hiding or those that follows from the one that is currently 'clicked'.
(I have tried the code below using checkboxes instead of radio buttons.)

Can someone tell me what is a correct code for this? I simply can not figure how [class^="sz"] works despite hours of searching on the internet.
All info I have found is about applying the same effect on all.
Code:
css
===
input#show, input#hide {
    display:none;
}

span#sz1 {
    display:none;
}
input#show:checked ~ span#sz1 {
  display:block;
}

input#hide:checked ~ span#sz1 {
    display:none;
}
</style>

html
====
<label for="show">
    <span>[Show]</span>
</label>
<input type=radio id="show" name="group">
<label for="hide">
    <span>[Hide]</span> 
</label>    
<input type=radio id="hide" name="group">
<span id="sz1">
Some content here.
</span>
The HTML part is repeated as many times as there is a text segment starting with ID "sz".

Thank you for your help.

Last edited by rblampain; 12-08-2023 at 09:09 AM.
 
Old 12-08-2023, 08:53 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897
My CSS knowledge isn't very good either.

The different wildcards are ^ i.e. starts with, * contains and $ ends with. Regardless of using wildcards if you select one you select all so I don't know of anyway except for having a css definition for each radiobox.

Code:
<html>
<head>

<style>
[id^="show"], [id^="hide"] {
    display:none;
}

[id^="sz"] {
    display:none;
}

[id="show"]:checked ~ [id="sz1"] {
  display:block;
}

[id="hide"]:checked ~ [id="sz1"]  {
    display:none;
}

[id="show1"]:checked ~ [id="sz2"] {
  display:block;
}

[id="hide1"]:checked ~ [id="sz2"]  {
    display:none;
}
</style>
</head>
<body>
<label for="show">
    <span>[Show]</span>
</label>
<input type=radio id="show" name="group">
<label for="hide">
    <span>[Hide]</span> 
</label>    
<input type=radio id="hide" name="group">
<span id="sz1">
Some content here.
</span>
<br> <br>
<label for="show1">
    <span>[Show]</span>
</label>
<input type=radio id="show1" name="group1">
<label for="hide1">
    <span>[Hide]</span> 
</label>    
<input type=radio id="hide1" name="group1">
<span id="sz2">
Some content here 2.
</span>
</body>
</html>

Last edited by michaelk; 12-08-2023 at 08:57 PM.
 
1 members found this post helpful.
Old 12-09-2023, 12:04 AM   #3
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Original Poster
Rep: Reputation: 52
Thank you for the answer.
My question is misleading because while the wildcards ^ and $ are both used in the code, only the wildcard $ is used to toggle visibility.

Quote:
Regardless of using wildcards if you select one you select all so I don't know of anyway except for having a css definition for each radiobox.
According to my understanding, that is not correct. Using
Code:
[class^="sz"]
Will select every ID starting with 'sz' of which there may be 99 while using

Code:
[class$="z1"]
will select any ID ending with 'z1' of which there will only be 1.


I know I have to use
Code:
[class$="n"]
when n is the ID number previously mentioned which may, in my example, be 1 to 99.
What I fail to figure out is how to link the code together so that any ID starting with "sz" does something and only the one ending with a number between 1 and 99 has visibility toggled.

Last edited by rblampain; 12-09-2023 at 12:12 AM.
 
Old 12-09-2023, 02:44 AM   #4
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Original Poster
Rep: Reputation: 52
It looks like michaelk is correct, I was already using the solution proposed which works.
What I tried to do is not possible with CSS and JS is not an option, so my CSS has very long lines of IDs.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Y'know CSS was to kill off HTML table layout? Well, second time's a charm: Meet CSS Grid LXer Syndicated Linux News 0 04-01-2017 07:03 PM
LXer: GTK+ 3.20 to Feature Massive CSS Changes, Port Most Widgets to CSS Nodes LXer Syndicated Linux News 0 11-22-2015 06:30 PM
LXer: Making GTK3 themes – Part 2: The gtk.css and gtk-widgets.css file LXer Syndicated Linux News 0 07-24-2012 01:50 PM
CSS - Is there a way to put several lines of HTML into one line using CSS? Chronothread Programming 12 01-05-2011 06:06 AM
HTML/CSS/JS: Problem with <p> not having an absolute position with CSS. RHLinuxGUY Programming 7 03-03-2007 12:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration