I want to make the easiest css for menus. There's an outer div and an inner anchor. If the outer's class is the same as the inner's id, that will be active styled.
Example code:
<div class='<?php echo $_GET['menu']; ?>'>
<a href="index.php?menu=menu1" id='menu1'>Menu 1</a>
<a href="index.php?menu=menu2" id='menu2'>Menu 2</a>
<a href="index.php?menu=menu3" id='menu3'>Menu 3</a>
<a href="index.php?menu=menu4" id='menu4'>Menu 4</a>
<a href="index.php?menu=menu5" id='menu5'>Menu 5</a>
</div>
and I don't want to write a lot of css, like:
.menu1 #menu1, .menu2 #menu2, .menu3 ....
{ /*active stlye*/}
so I want the following: if the classname is the same of the id under it would be active.
Thanks in advance.
You can't do that with CSS ; maybe you could instead use PHP to accomplish something close :
<?php for($i=1;$i<=5;$i++){ ?>
<a href="index.php?menu=menu<?php echo $i;?>" <?php if($_GET['menu'] == 'menu'.$i) echo 'class="selected"'; ?>>Menu <?php echo $i;?></a>
<?php } ?>
And the CSS :
.selected{
/*active style*/
}
Edit : a solution with PHP is better than one based on JS because : everyone will see the .selected class, even people who have JS deactivated + for the others, the menu will not blink. Honestly it is super annoying to see a menu CSS changed via JS.
That's not possible. CSS isn't a programming language, you have to fully specify every selector.
But since you already work with PHP it would be a lot easier if you apply the class to one of your anchor tags:
<div class='<?php echo $_GET['menu']; ?>'>
<a href="index.php?menu=menu1" <?php echo $a_class[1]; ?> id='menu1'>Menu 1</a>
<a href="index.php?menu=menu2" <?php echo $a_class[2]; ?> id='menu2'>Menu 2</a>
<a href="index.php?menu=menu3" <?php echo $a_class[3]; ?> id='menu3'>Menu 3</a>
<a href="index.php?menu=menu4" <?php echo $a_class[4]; ?> id='menu4'>Menu 4</a>
<a href="index.php?menu=menu5" <?php echo $a_class[5]; ?> id='menu5'>Menu 5</a>
</div>
You have to either specify a bunch of selectors or add code to your PHP script.
Let's suppose I got you well. Then an answer could be :
<div id="menu">
<?php
for($i = 0; $i < 5; $i++) {
if(isset($_GET['menu']) && $i == $_GET['menu'])
echo " Menu $i";
else
echo " Menu $i";
}
?>
</div>
And in your CSS
#menu a {
/* Whatever you want for your normal links */
}
#menu .menu_active {
/* Whatever you want for your active link */
}
And that's it !
Related
I am trying to make a sidebar button's background color change to let the visitor know which page they are currently on. I've tried searching the web for similar answers and applying them to my code but have yet been able to get it to work. I am using WordPress, CSS, & PHP/HTML if that helps any. Below is one of the buttons code:
.button-goldGrey {
color: #231f20;
border: 1px solid #231f20;
}
.button-goldGrey:hover {
background-color: #ccb086;
}
<a class="button-side button-goldGrey" href="Page1">LINK 1</a>
<a class="button-side button-goldGrey" href="Page1">LINK 1</a>
<a class="button-side button-goldGrey" href="Page1">LINK 1</a>
And I put this JSFiddle together if you prefer that: https://jsfiddle.net/zerojjc/huxznj7m/2/
As I mentioned on my comment to your question, CSS alone can't do this. You'll need some PHP to achieve this.
Assuming you can use PHP and that you can edit your theme's files, WordPress has a function called is_page() that you can use to add a new CSS class to your buttons.
For example:
CSS:
.button-goldGrey {
color: #231f20;
border: 1px solid #231f20;
}
.button-goldGrey:hover,
.button-goldGrey.current {
background-color: #ccb086;
}
PHP:
<a class="button-side button-goldGrey<?php echo (is_page('pwa-wealth-management')) ? ' current' : ''; ?>" href="Page1">PWA Wealth Management</a>
<a class="button-side button-goldGrey<?php echo (is_page('wilder-co')) ? ' current' : ''; ?>" href="Page1">Wilder & Company CPA's</a>
<a class="button-side button-goldGrey<?php echo (is_page('the-nichols-law-group')) ? ' current' : ''; ?>" href="Page1">The Nichols Law Group</a>
You can get the current page name using $_SERVER['PHP_SELF'] then you can just add proper classes in each <li> element and then later highlight the corresponding link with some CSS like the code below.
CSS code :
.button-goldGrey {
color: #231f20;
border: 1px solid #231f20;
}
.button-goldGrey:hover {
background-color: #ccb086;
}
.active {
color: red;
}
PHP + HTML code :
<?php
$activePage = $_SERVER['PHP_SELF'];
?>
<ul>
<li class="<?php ($activePage == 'Page1') ? 'active':''; ?>"><a class="button-side button-goldGrey" href="Page1">LINK 1</a></li>
<li class="<?php ($activePage == 'Page2') ? 'active':''; ?>"><a class="button-side button-goldGrey" href="Page2">LINK 1</a></li>
<li class="<?php ($activePage == 'Page3') ? 'active':''; ?>"><a class="button-side button-goldGrey" href="Page3">LINK 1</a></li>
</ul>
You can do this with just CSS3 ! Just put the CSS attribute directly into the concerned <a> element, such as:
<ul class="nav_links">
<li><a style="color: #ff6511" href="page.html">Accueil</a></li>
<li>Services</li>
<li>Tarif</li>
<li><a class="cta" href="#"><button>Contact</button></a></li>
</ul>
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 6 years ago.
I would change the CSS rule for the background to li.li-class if it contain an h3.
I have an HTML like this:
<div class="div-class">
<ul>
<li class="li-class">
<span itemprop="something">
<h3>Some title</h3>
</span>
</li>
</ul>
</div>
and the CSS for li background is:
.div-class ul li:nth-child(2n+1) { background: #f4f4f4; }
Where is the CSS selector for the li.li-class?
I have look this question Is there a CSS parent selector? and i try the code, but not work in this case.
Thanks all
As mentioned in comments, this is not possible using pure CSS.
It would however be very straight forward using the jQuery .has():
$("li").has("h3").addClass("yellow");
For the HTML:
<style>
.yellow{
background-color:yellow;
}
</style>
<div class="div-class">
<ul>
<li class="li-class">
<span itemprop="something">
<h3>Some title</h3>
</span>
</li>
<li class="li-class">
<span itemprop="something">
<p>
Not a header
</p>
</span>
</li>
</ul>
</div>
Will look at every li element and if it has a descendant h3 element it will add the yellow class to the li.
JSFiddle example
You can also make the selector more specific, so it does not select all lis but only the ones in a <div class="div-class"> with something like
$("div.div-class").find("li").has("h3").addClass("yellow");
If you are creating the html via php you have ultimate control:
<?php
$html = '';
foreach ($rows as &$row) {
// just figure out if you need the class
// before making the html - php is power
if (!empty($row['h3'])) {
$lih3_class = 'h3-in-li';
} else {
$lih3_class = '';
}
$html .= '<li class="li-class '.$lih3_class.'">';
$html .= ''; // put in your li contents as normal
$html .= '</li>';
}
echo $html;
?>
I have just built my first WordPress theme, the only thing I can't seem to work out is how can I tell WordPress to make menu links with the class active when it is on the page? This is the code I had for the static page:
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
<?php if(curPageName() == 'index.php'){ ?>
<li class="current">
Home
</li>
<?php } else{ ?>
<li>
Home
</li>
<?php } ?>
I have still got this code in the WordPress theme, but it is not working at the moment, I am thinking I could probably make it work as it must have something to do with the exact links, but I'd also like the built the menu in WordPress so that would require me to get rid of this code..
Anyone know what I can do or link me to something that will help me out?
The best way is to assign a page name to the body tag of your page identifying it. Then use php to assign an active class if the page matches.
CSS
a.active:hover {
color:#000;
border:1px solid #000;
background:#fff;
}
HTML
<body <?php $page = "one" ?>>
<div id="navbar">
<ul>
<li><a<?php if($page == 'one'): ?> class="active"<? endif ?> href="one.php">Page 1</a></li>
<li><a<?php if($page == 'two'): ?> class="active" <? endif ?> href="two.php">Page 2</a></li>
<li><a<?php if($page == 'three'): ?> class="active" <? endif ?> href="three.php">Page 3</a></li>
<li><a<?php if($page == 'four'): ?> class="active"<? endif ?> href="four.php">Page 4</a></li>
</ul>
</div>
In the above example page one will be active
I have a question about my menu. I have a menu with 6 items, which I want to give a different colour if they are ACTIVE (so in use by my users). I tried this already:
<nav id="sub-navigation" class="toggles-menu">
<ul id="quick-index-nav" role="navigation" class="pills slim muted">
<li>
<a id="qindex-day">abc</a>
</li>
<li>
<a id="qindex-day">random</a>
</li>
<li<? if(!$_GET['t']||$_GET['t']=='') echo ' class="active"';?>>
<a id="qindex-popular" target="right" href="right.php">all of them</a>
</li>
<li<? if($_GET['t']=='tod') echo ' class="active"';?>>
<a id="qindex-today" target="right" href="right.php?t=tod">today</a>
</li>
<li class="yesterday<? if($_GET['t']=='yes') echo ' active"';?>">
<a id="qindex-yesterday" target="right" href="right.php?t=yes">yesterday</a>
</li>
<li<? if($_GET['t']=='mix') echo ' class="active"';?>>
<a id="qindex-day" target="right" href="right.php?t=mix">samba</a>
</li>
</ul>
</nav>
CSS:
.pills>li.active>a{color:#fff}
.pills.slim>li>a{padding:3px 5px;}
.pills.muted>li>a{color:gray}
.pills.muted>li.active>a{color:#fff}
But this won't work. It makes nothing active if I click on them.
You need to add styles to your page, either via an external stylesheet or embedded in a style tag directly in the page.
CSS:
li.active a{
color: red; // for example
}
You can also use inline styles:
<li<? if(!$_GET['t']||$_GET['t']=='') echo ' style="color: red;"';?>>
It will work but is not recommended.
I have a portfolio page with a list of clients which when clicked on will display the corresponding gallery- all of this happens on the same page. I would like for the client name to be highlighted when the corresponding gallery is shown. I thought a:active would work, but so far isn't. Any ideas? (If it matters, I'm using Wordpress.)
live site
.active {
border: 1px solid #ff893b;
}
<div id="client-list">
<p>
<a href="?page_id=24" <?php if ($_get['page_id']=='24'){echo 'class="active"';} ?> >Häagen Dazs</a><br/>
<a href="?page_id=26" <?php if ($_get['page_id']=='26'){echo 'class="active"';} ?>>Hugo Boss Rodeo Drive</a><br/>
<a href="?page_id=28" <?php if ($_get['page_id']=='28'){echo 'class="active"';} ?>>Ford</a><br/>
<a href="?page_id=30" <?php if ($_get['page_id']=='30'){echo 'class="active"';} ?>>MOCA Contemporaries</a><br/>
<a href="?page_id=32" <?php if ($_get['page_id']=='32'){echo 'class="active"';} ?>>XBOX 360 Halo 3 Sneak Preview</a><br/>
<a href="?page_id=34" <?php if ($_get['page_id']=='34'){echo 'class="active"';} ?>>Saddlerock Smith & Basso Weddings</a><br/>
<a href="?page_id=36" <?php if ($_get['page_id']=='36'){echo 'class="active"';} ?>>Christie's</a><br/>
<a href="?page_id=42" <?php if ($_get['page_id']=='42'){echo 'class="active"';} ?>>Instyle Magazine + Ming by Mango</a></p>
<script type="text/javascript">
$(document).ready(function(){
$('a').click(function(){
$('a').removeClass('active');
$(this).addClass('active');
});
});
</script>
</div><!-- end client-list -->
<?php
if (!isset($_GET['page_id'])) {
$page_id='24';
}else{
$page_id=$_GET['page_id'];
}
?>
<html>
<head>
<title>Lab 1</title>
<style>
.active {
border: 1px solid #ff893b;
background:red;
color:white;
}
</style>
</head>
<body>
<div id="client-list">
<p>
<a href="?page_id=24" <?php if ($page_id=='24'){echo 'class="active"';} ?>> Häagen Dazs</a><br/>
<a href="?page_id=26" <?php if ($page_id=='26'){echo 'class="active"';} ?>> Hugo Boss Rodeo Drive</a><br/>
<a href="?page_id=28" <?php if ($page_id=='28'){echo 'class="active"';} ?>> Ford</a><br/>
<a href="?page_id=30" <?php if ($page_id=='30'){echo 'class="active"';} ?>> MOCA Contemporaries</a><br/>
<a href="?page_id=32" <?php if ($page_id=='32'){echo 'class="active"';} ?>> XBOX 360 Halo 3 Sneak Preview</a><br/>
<a href="?page_id=34" <?php if ($page_id=='34'){echo 'class="active"';} ?>> Saddlerock Smith & Basso Weddings</a><br/>
<a href="?page_id=36" <?php if ($page_id=='36'){echo 'class="active"';} ?>> Christie's</a><br/>
<a href="?page_id=42" <?php if ($page_id=='42'){echo 'class="active"';} ?>> Instyle Magazine + Ming by Mango</a>
</p>
</div>
</body>
</html>
i think what you need is not a:active but instead you should do a:visited.
#client-list p a:visited { border: 1px solid #ff893b; }
html
<div id="client-list">
<p>Häagen Dazs<br/>Hugo Boss Rodeo Drive<br/>Ford<br/>MOCA Contemporaries<br/>XBOX 360 Halo 3 Sneak Preview<br/>Saddlerock Smith & Basso Weddings<br/>Christie's<br/>Instyle Magazine + Ming by Mango</p>
</div>
You could use jQuery. When a link is clicked, add a class, e.g., "current" that highlights that link, and removes the class from all the other links (e.g., whichever one was previously highlighted).
I don't think you can do this purely with CSS. The :active class means you are in the process of actively clicking the link; it doesn't persist after you finish clicking it.
Hey why not use a jquery function like the following for adding and removing classes. It goes like this
Style
#client-list a.clicked
{
color : #ffffff;
/* or what ever style you want */
}
Jquery function
$(function(){.removeClass()
$('#client-list a').bind('click' , function(){
$('#client-list a').removeClass('clicked');
$(this).addClass('clicked');
} )
})
What query does it strip all the anchor tags from the class clicked and add it only to the current one.
Of course if you add specific click id to each anchor tag you can just save the previously clicked anchor.