LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-12-2023, 12:26 AM   #1
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,784
Blog Entries: 3

Rep: Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514
Selecting an element with specific descendants using CSS3


It's not quite programming but it is a puzzle. In the document below, I would like to have a CSS3 rule which puts A.0 and B.0 in bold type but leaves C.0 with a normal weight. The comments there show what I have tried but which does not work. Is there a way to do that in CSS3?

Code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Example</title>
<style type="text/css" media="screen">

 li { list-style: none; }
 li > ul > li.aok { font-weight: bold; }

/* li[ul>li.aok] { font-weight: bold; } */
/* li[li.aok] { font-weight: bold; } */
/* li[self::li ul li.aok] { font-weight: bold; } */
/* li[self::li li.aok] { font-weight: bold; } */
/* li[descendant::li.aok] { font-weight: bold; } */

/* CSS4 */
/* li:has(>ul>li.aok) { font-weight: bold; } */

</style>
</head>
<body>
 <ul>
  <li>A.0
   <ul>
    <li class="aok">AA.1</li>
    <li class="aok">AA.2</li>
   </ul>
  </li>
  <li>B.0
   <ul>
    <li class="aok">BB.1</li>
    <li class="notok">BB.2</li>
   </ul>
  </li>
  <li>C.0
   <ul>
    <li class="notok">CC.1</li>
    <li class="notok">CC.2</li>
   </ul>
  </li>
 </ul>
</body>
</html>
The proposed CSS4 may have the :has selector, but I'm looking for a way to do that in CSS3. Specifically I would like something which renders in both Firefox and Chromium.

PS. The above was blocked by LQ until javascript was activated. That is not a good move by LQ.
 
Old 02-12-2023, 01:03 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,974

Rep: Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179
Works as is for me???

I am using Chrome
 
Old 02-12-2023, 01:38 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,784

Original Poster
Blog Entries: 3

Rep: Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514
I wouldn't know about (or condone) Chrome, but :has() works in Chromium and not Firefox. It also works in the Chromium derivative Brave and thus presumably all the other Chromium derivatives.

However, as mentioned, a requirement is a solution that works in Firefox, too. I'd like to keep the logic for that in CSS3. I suppose it might be necessary to move it into the generator, but that would be more effort.
 
Old 02-12-2023, 11:48 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,784

Original Poster
Blog Entries: 3

Rep: Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514
I took a new look at the script which produces that HTML and adding two lines to it was sufficient to produce a work-around. The one line is just a for-loop iterating through an XPpath:

Code:
.//li[descendant::li[@class="ok"]]
That might be the more efficient method anyway.
 
Old 02-13-2023, 01:11 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,824

Rep: Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136
I'm confused as to what you actually want to do here. Is it to have different handling for the last immediate child, as I'm getting from your description? If so, what are the classes for, and why are all your attempts trying to use them?

Last edited by dugan; 02-13-2023 at 01:16 PM.
 
Old 02-13-2023, 01:14 PM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,784

Original Poster
Blog Entries: 3

Rep: Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514
Quote:
Originally Posted by dugan View Post
The sample at the top looks exactly the same in Chrome and Firefox for me.
Which versions? For me, Firefox 109 ignores :has()

Code:
$ apt-cache policy firefox | head -n 3
firefox:
  Installed: 109.0.1+linuxmint1+vera
  Candidate: 109.0.1+linuxmint1+vera
 
Old 02-13-2023, 01:17 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,824

Rep: Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136
I edited the post because I honestly wasn't sure if the code that worked (for you) in Chrome but not Firefox was active in the top post.

Is the requirement if it contains an unordered list with any list item that has a class of aok, then give it special styling?

Last edited by dugan; 02-13-2023 at 01:45 PM.
 
Old 02-13-2023, 01:46 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,784

Original Poster
Blog Entries: 3

Rep: Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514
The sample makes a nested list two levels deep. The initial levels of the list A.0, B.0, and C.0 should reflect the presence of any descendant list item with the class of "aok". So elements containing A.0 and B.0 should end up rendered as bold text while C.0 should not be.
 
Old 02-13-2023, 01:49 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,824

Rep: Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136
While you're waiting for :has support:

Add a class to either C0 or to A0 and B0.

And if you can't then do it with Javascript.

I'm saying this after looking at, well, what I'm sure you looked at:

https://stackoverflow.com/questions/...arent-selector

Last edited by dugan; 02-13-2023 at 01:50 PM.
 
Old 02-13-2023, 01:50 PM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,784

Original Poster
Blog Entries: 3

Rep: Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514Reputation: 3514
Quote:
Originally Posted by dugan View Post
While you're waiting for :has support:

Add a class to either C0 or to A0 and B0.
That's what I've ended up doing in #4 above. By luck it took only two lines.
 
Old 02-13-2023, 01:52 PM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,824

Rep: Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136Reputation: 5136
Good lord...

https://bugzilla.mozilla.org/show_bug.cgi?id=418039
 
1 members found this post helpful.
  


Reply

Tags
css3, descendant


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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: Comparing the descendants of Mandrake and Mandriva Linux LXer Syndicated Linux News 0 02-16-2022 12:30 AM
Visualize cpu, memory etc usage for Linux serve using Html5, Css3, Javascript bmxakias Programming 5 03-16-2018 06:41 PM
Google plus album photo animation with css3 and javascript johnsdk Programming 2 07-30-2011 05:38 PM
css3 capabilities sycamorex General 0 03-05-2010 06:32 PM

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

All times are GMT -5. The time now is 06:01 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