Adjusting the text color of ul items with CSS

I currently have a menu setup like this:

<ul class="ipro_menu">

    <li><a href="/">Home</a></li>
    <li class="active-parent"><a href="/menu-item-1/">Menu Item 1</a> 

        <ul class="sub-menu">
            <li><a href="/subitem-1/">Subitem 1</a></li>
            <li class="active"><a href="/subitem-2/">Subitem 2</a></li>
        </ul>

    </li>
    <li><a href="/menu-item-2/">Menu Item 2</a></li>

</ul>   

When on a page, it gets the class active and if it is in a submenu under the main list (ul), then the main list element will also receive the class active-parent.

For example, if we are viewing the "Subitem 2" page in the above structure, then "Menu Item 1" should have the class active-parent.

The challenge I am facing is that I want to change the font color of the active-parent only, not all the elements in the submenu. Here's what I've tried so far:

ul.ipro_menu li.active-parent a {
color: #FF0000;
}

However, this code not only changes the font color of the active-parent element but affects all li elements in the submenu as well.

How can I modify this to only alter the font color of the specific element marked as active-parent?

Answer №1

When dealing with CSS, it is common to see expected behavior like this. To customize the style for children elements, you can create a more specific style just for those elements:

ul.ipro_menu li.active-parent ul.sub-menu li a {
    color:#000;
}

Answer №2

To enhance the styling, consider adding the active-parent class to the HREF attribute:

http://example.com/

<ul class="custom_menu">

<li><a href="/">Main Home</a></li>
<li><a class="active-parent" href="/menu-option-1/">Menu Option 1</a> 

<ul class="sub-menu">
<li><a href="/submenu-item-1/">Submenu Item 1</a></li>
<li class="active"><a href="/submenu-item-2/">Submenu Item 2</a></li>
</ul>

</li>
<li><a href="/menu-option-2/">Menu Option 2</a></li>

</ul> ​

ul.custom_menu a.active-parent  {
color: #00FF00;
}​

Answer №3

Implement the direct children selector:

ul.ipro_menu li.active-parent > a {
  color: #FF0000;
}

By applying this rule, only the immediate children of your li element will be impacted.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to show all cards in Angular when no filtering input is provided?

I have implemented a filter feature for multiple cards in Angular using Pipes. The filter works well, but I am facing an issue where no cards are displayed when there is no input value provided. I would like all the cards to be displayed when no input is g ...

Is it possible to transfer an array from PHP to JavaScript that has been retrieved from a MySQL database?

Looking for a way to directly move the PHP array $row['l_longitude'] and $row['l_latitude'] into your JavaScript code? Here is my PHP code in get_marker_connect2.php: <?php $servername = "localhost"; $username = "root"; $passcode = ...

Is it possible to position the Sprite image to the right of the anchor tag?

I have a CSS sprite image that is functioning correctly, but I am encountering an issue where the image is displaying on the left side of the anchor tag's text instead of the right. You can view the sprite image here. Expected result: [Mylinktext]&l ...

The MDL layout spacer is pushing the content to the following line

Here's an interesting approach to Material Design Lite. In this example from the MDL site, notice how the 'mdl-layout-spacer' class positions elements to the right of the containing div, giving a clean layout: Check it out <!-- Event ca ...

Having trouble positioning items to the right in bootstrap

<div id="unique-content-wrapper"> <nav class="navbar navbar-expand-lg navbar-light bg-light" id=""> <button class="navbar-toggler" id="menu-toggle"><span class="mater ...

Having trouble with CSS styling not applying after website is uploaded to server?

I have taken a basic page template that was originally designed for the rest of my website and customized it to appear more simplistic. The page now consists of an unordered list with a brief paragraph at the top and a logo aligned to the right. While ever ...

The first div should be hidden when the toggle is activated

I am currently working on a CRUD project and incorporating some Javascript/JQuery into it. I have a button labeled "Add", which, when clicked, displays a specific div element. However, I'm facing an issue where the div is already visible before the us ...

Ways to ensure "overflow: hidden" functions correctly across all web browsers

Issue: I am in the process of designing a webpage layout using divs and css instead of relying on an HTML table structure. It is essential for me to ensure that this design functions smoothly across all major browsers. The challenge I'm facing invol ...

Tips for submitting a form using javascript while preventing the default action

Looking for a way to submit a form in Javascript and prevent the default action? Let's explore how you can achieve this. Initially, my HTML form with the ID "contact_form" had an input element like so: <input id="contact_send_msg" type="submit" val ...

How can PHP be utilized to dynamically add classes to <li> elements and alternate them after every third item

I'm trying to find a way to add a class to an unordered list and alternate that class after every third item. Here's the code I have so far: <?php $i=1; foreach($this->items as $item) : ?> <li class="<?php if ($i % 3 == 0) : ...

How can I integrate data pulled from another website onto my HTML webpage with Vue.js?

I have successfully extracted data from an API website and can display it in the console. However, I am facing issues with displaying the data on the webpage itself. Can someone assist me in this matter? Specifically, I am trying to display the id element ...

What is the process for designing a navigation bar with both text and icons?

Does anyone know how to replicate the navigation bar similar to Stack Overflow's navbar with a search form and icon? I have tried using it but cannot figure out a way. <div class="form-group"> <input type="text& ...

How to align a search box to the right within a div using CSS in Bootstrap

Struggling to find the proper code to align my div element to the right of the page. Current alignment: https://i.sstatic.net/JoJu5.png Desired alignment: https://i.sstatic.net/xZWJ6.png Below is the code snippet: <h1 class="pb-2 mt-4 mb-2 border-bo ...

Creating personalized CSS for Sharepoint online site customization

While looking into supported and unsupported customization options and custom CSS for branding in SharePoint online, I found myself perplexed by the vague and confusing information provided by Microsoft's articles. Specifically, I was studying the ap ...

Wrapping a series of grids within a parent container to manage height effectively with grid960

Testing on the following browsers: Internet Explorer, Chrome, Firefox; Check out this example of an ideal layout in a PDF: Here is the link to the direct page: While Grid systems work well for width layout, I struggle with setting height constants. In ...

Is the flowplayer software free for use on a production server?

Here is the URL I am implementing to stream videos on my website: I would like to know if it is permissible and cost-free to use in a production environment. ...

Enable the entire button to be clickable without the need for JavaScript by utilizing a Bootstrap 5 button

Is there a way to make a button redirect when clicking anywhere on it, not just the text inside? Here is the button code utilizing Bootstrap 5: <button class="btn btn-rounded btn-primary" type="button">Placeholder Text</button& ...

Adjusting the style attributes for Material-UI Icons

I could use some assistance with changing the CSS property of MUI icons in order to assign a specific color when the icon is focused. I have implemented the icons in the header and am displaying different pages below them, so I would like to give each ac ...

How to remove excess whitespace from a Bootstrap 4 title

https://i.sstatic.net/aib65.png I need help removing the white space between Table Title and Sub Titles. Additionally, I want "Sub1" along with the icon to be left-aligned within the cell <div class="mdc-layout-grid__cell stretch-card mdc-l ...

Unable to get 'div' content to display using jQuery when using third party modules such as Angular

I am currently facing an issue where I am trying to display the content of a <div id="right"> when hovering the mouse over another div. The #right div contains a graph from Angular-charts (). Below is my jQuery code: <div> ...