Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Yesterday 23:10:19

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 2,185
Website Mastodon

Adding CSS style to nav list

I have tried for 2 days and can’t solve this issue.

If I hand code the class="is-active" on each page it works.

 <nav class="site-nav js-site-nav">
	<ul class="nav" role="navigation">
	<li class="site-nav__item"><a href="https://newsite.ca/portfolio">Portfolio</a></li>
	<li class="site-nav__item"><a href="https://newsite.ca/about">About</a></li>
	<li class="site-nav__item"><a href="https://newsite.ca/notes" class="is-active">Notes </a></li>
		  </ul>
	</nav>

BUT when using this Form Menu_Nav on my pages <txp:output_form form="Menu_Nav" /> I cannot get Active class to work. I have tried moving the class="is-active" string everywhere without success.

<nav class="site-nav js-site-nav">
	<ul class="nav" role="navigation">
	<li class="site-nav__item"<txp:if_section name="portfolio"> class="is-active"</txp:if_section>> 
	 <txp:section link="1" title="1"  name="portfolio" />    
	  </li>
	<li class="site-nav__item"<txp:if_section name="about"> </txp:if_section>> 
		<txp:section link="1" title="1" name="about" />   
	  </li>
	<li class="site-nav__item"<txp:if_section name="notes"> </txp:if_section>> 
		<txp:section link="1" title="1" name="notes"  />  
	  </li>
			  </ul>
		   </nav>

I either get no active class to happen or every link is rendered active.

what am I missing? the Docs say to use this string:

<ul id="subnav">
<li><a href="<txp:site_url />articles" <txp:if_section name="articles">class="active" </txp:if_section>>Articles</a></li>
</ul>

…. texted postive

Offline

#2 Yesterday 23:23:08

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,109
Website GitHub

Re: Adding CSS style to nav list

Your is_active check needs to go inside the last double quote:

<li class="site-nav__item<txp:if_section name='portfolio'> is-active</txp:if_section>">
...

But you can simplify the code a lot using txp:section_list:

<txp:variable name="current_section"><txp:section /></txp:variable>
<nav class="site-nav js-site-nav">
<txp:section_list sections="portfolio, about, notes" wraptag="ul" class="nav" break="">
<li class="site-nav__item<txp:if_section name='<txp:variable name="current_section" />'> is-active</txp:if_section>">
   <txp:section link title />
</li>
</txp:section_list>
</nav>

Last edited by Bloke (Yesterday 23:25:24)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 Yesterday 23:44:15

phiw13
Plugin Author
From: South-Western Japan
Registered: 2004-02-27
Posts: 3,544
Website

Re: Adding CSS style to nav list

One pattern used for a long time:

<nav aria-label="Site Navigation" class="a-box" itemtype="https://schema.org/SiteNavigationElement" itemscope="itemscope">
<h3 id="menu">Around here:</h3>
<ul><txp:section_list default_title="Home" include_default wraptag="" break="" sections=",section_1,section_2,section_3">
<li<txp:if_section not name=''><txp:if_section name='<txp:section />'> class="is_active"</txp:if_section></txp:if_section>><a itemprop="url" href="<txp:section url />"><txp:section title /></a></li></txp:section_list></ul>
</nav>

Edit – oops, I see Stef already answered with similar code…

Last edited by phiw13 (Yesterday 23:46:08)


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
phiw13 on Codeberg

Offline

#4 Yesterday 23:51:28

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 2,185
Website Mastodon

Re: Adding CSS style to nav list

thanks!. I tried this example from Stef but no active class takes affect:

<li class="site-nav__item<txp:if_section name='portfolio'> is-active</txp:if_section>">

page source: but link is not active.

<li class="site-nav__item is-active">
	 <a href="https://mysite.ca/portfolio/">Portfolio</a>    
	  </li>

…. texted postive

Offline

#5 Today 00:54:04

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,109
Website GitHub

Re: Adding CSS style to nav list

Mine was untested so it might not be exact.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#6 Today 04:33:50

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 2,185
Website Mastodon

Re: Adding CSS style to nav list

well I tried and tried but without any luck. I had to revert to brute force, but good enough as i only have 4 sections.

this works a charm:

<li class="site-nav__item"><txp:if_section name="portfolio">
     <a href="https://mysite.ca/portfolio/" class="is-active"> Portfolio </a> 
<txp:else />
    <a href="https://mysite.ca/portfolio/"> Portfolio </a>
 </txp:if_section>

  etcetera etcetera

if anyone can make the more elegant option work I would be happy to try it out

PS how does everyone make the code posted look the way Bloke posted? coloured coded and all.


…. texted postive

Offline

#7 Today 04:34:41

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 2,185
Website Mastodon

Re: Adding CSS style to nav list

phiw13 wrote #341442:

One pattern used for a long time:

<nav aria-label="Site Navigation" class="a-box" itemtype="https://schema.org/SiteNavigationElement" itemscope="itemscope">...

Edit – oops, I see Stef already answered with similar code…

never got this to work either


…. texted postive

Offline

#8 Today 04:50:39

phiw13
Plugin Author
From: South-Western Japan
Registered: 2004-02-27
Posts: 3,544
Website

Re: Adding CSS style to nav list

bici wrote #341450:

never got this to work either

Weird. What is going wrong? Have you checked with the browser page inspector to check that a/ the correct markup is generated and b/ your styles are applied correctly – not overridden by some other styles…


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
phiw13 on Codeberg

Offline

#9 Today 06:58:58

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 2,185
Website Mastodon

Re: Adding CSS style to nav list

phiw13 wrote #341451:

Weird. What is going wrong? Have you checked with the browser page inspector to check that a/ the correct markup is generated and b/ your styles are applied correctly – not overridden by some other styles…

yes it is weird. there is only one css file.
the issue is that active class is never added to the a href area , but is grouped with first <li class=“site-nav__item”> so it renders as <li class=“site-nav__item” class=is-active> …. and is not being applied within a href </a> link.

this is my working output from view source:

<li class="site-nav__item">
     <a href="https://mysite.ca/portfolio/" class="is-active"> Portfolio </a>

all the other options ends up outputting:

<li class="site-nav__item" class="is-active" >
     <a href="https://piovesan.ca/portfolio/"> Portfolio </a> 

with no active link being rendered.


…. texted postive

Offline

#10 Today 07:18:32

phiw13
Plugin Author
From: South-Western Japan
Registered: 2004-02-27
Posts: 3,544
Website

Re: Adding CSS style to nav list

bici wrote #341452:

yes it is weird. there is only one css file.
the issue is that active class is never added to the a href area , but is grouped with first <li class=“site-nav__item”> so it renders as <li class=“site-nav__item” class=is-active> …. and is not being applied within a href </a> link.

So not really weird :-)

Adjust your stylesheet…

.is-active a[href] { /* your styling */ }

Or keep your stylesheet as is and change your markup a little, taking my sample above

<nav aria-label="Site Navigation" class="a-box" itemtype="https://schema.org/SiteNavigationElement" itemscope="itemscope">
<h3 id="menu">Around here:</h3>
<ul><txp:section_list default_title="Home" include_default wraptag="" break="" sections=",section_1,section_2,section_3">
<li<txp:if_section not name=''></txp:if_section>><a <txp:if_section name='<txp:section />'> class="is_active"</txp:if_section> itemprop="url" href="<txp:section url />"><txp:section title /></a></li></txp:section_list></ul>
</nav>

Basically, the snippet <txp:if_section name='<txp:section />'> class="is_active"</txp:if_section> moves form the <li to inside the <a………>


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
phiw13 on Codeberg

Offline

Board footer

Powered by FluxBB