Applicable exclusively to the parent list item

I am creating a menu with a submenu that has a varying line-height.

Unfortunately, I am struggling to prevent this line-height from impacting the list items in my child (submenu).

I attempted using ul.nav > li but it continues to affect the lower menu bar.

$(window).on("scroll resize load", function () {
    if($(window).width() > 863) {
        wide();

        if ($(window).scrollTop() > 75) headerLow();
        else headerHigh();
    } else {
        headerLow();
        thin();
    }
});

var headerHigh = function () {
    $('#head').removeClass('mini').css('height', '75' );
};

var headerLow = function () {
    $('#head').addClass('mini').css('height', '60' );
};

var thin = function() {
    $('#slider').css({ 'padding-top' : '60px'});
};

var wide = function() {
    $('#slider').css( 'padding-top' , '78px');
};

<div id="head">
    <div class="container">
        <ul class="nav pull-right">
            <li><a href="#"></a>
                <ul>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                    <li><a href="#"></a></li>
                </ul>
            </li>
            <li><a href="#"></a></li>
            <li><a href="#"></a></li>
        </ul>
    </div>
</div>

.nav li { line-height: 80px;transition: line-height 0.5s ease; }  
.nav li > ul li { line-height: 10px; }
.mini ul.nav > li, .mini .logo { line-height: 60px; }
.mini h1 { font-size: 2em; }

The issue arises when I scroll down, and the second menu is affected by .mini. See live demo.

Answer №1

Utilize the code

li:nth-child(3) { line-height: 60px; }

NOTE: This modification will impact the inside li elements, not the sub li elements.

Check out this resource to grasp How the nth-child Selector Works.

or

Assign a class to your li elements and apply CSS using that class.

<ul>
    <li class="menuLI">Menu</li>
    <li class="menuLI">About Us</li>
    <li class="menuLI">Contact Us
        <ul>
            <li class="subMenuLI">Starters</li>
            <li class="subMenuLI">Breakfast and Brunch</li>
        </ul>
    </li>
</ul>

View the revised JSFiddle Link here.

Note: These are the updates I have made.

.nav li > ul li{ line-height: 40px; } // Updated
.nav li ul { line-height: 0px; } // Added

Answer №2

It is accurate to use the selector ul.nav > li. This selector targets the elements you would anticipate, as shown in this JSFiddle example.

The issue may be related to inheritance. Regardless of which elements you choose, the child li elements will inherit the line-height property. To solve this, you can simply specify a more suitable line-height value that won't impact your design.

I trust this addresses your query!

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

Equal-height rows and columns using Flexbox

I've been working on a row layout with two columns, each set to 50% width. The left column contains an image, while the right column displays text. Here is my HTML: .row{ display:flex; ...

Tips for transferring PHP variables through jQuery

Having an image upload script, I am looking to pass PHP variables using jQuery. Here is the jQuery code: <script type="text/javascript" > $(document).ready(function() { $('#form_data').live('change', function() { ...

Accessing an element using jQuery within a knockout event

I've recently started experimenting with knockout.js in combination with jQuery, and my experience with knockout.js is still quite limited. If you want to take a look at my code example, you can find it here: http://jsfiddle.net/Ninjanoel/ADYN6/ Cur ...

The JQUERY Uncaught Error: Syntax error message popped up when trying to access an unrecognized expression on the javascript: void(0)

I've encountered an issue after upgrading to jQuery 3.4.1 while using bootstrap 3. The console keeps showing this error: Uncaught Error: Syntax error, unrecognized expression: # Here is the section of code causing the error: <div class="btn-grou ...

Creating a custom HTML table layout with both variable and fixed column widths, along with grouping headers

Is there a way to format an HTML table so that it appears like this: <- full page width -> <-20px->< dynamic ><-20px->< dynamic > +------------------+-------------------+ ¦ A ¦ B ...

Adjusting the size of all elements on a webpage

Upon completing my project, I noticed that my localhost:3000 is zoomed in at 125%, causing it to appear less than ideal at 100% zoom. Is there a way to adjust the zoom/scale of my website to match how it appeared on my localhost environment? I came across ...

Support for these CSS properties of left: 0; and right: 0; are compatible with

Just wondering if it's okay to utilize the CSS code below to ensure the element adjusts to its parent's width. .element { position: absolute; left: 0; right: 0; background-color: #ccc; border-top: 1px solid #ddd; } We don&ap ...

Issue with Bootstrap 5 Carousel not transitioning between slides

I am trying to add a carousel to my webpage, but it seems like it's not sliding and only displaying the first image. I've double-checked my code and everything else from Bootstrap works fine. Here's the snippet of code I'm using: < ...

Customize the Underline Color in MaterialUI Select Component

I'm experimenting with MaterialUI and trying to figure out how to customize the underline and text color of the Select component when an item is selected. Currently, the underline and label appear blue after selection with a gray background. Is there ...

Having trouble retrieving the desired data from the JSON file

My current code is not giving me the expected results while trying to access JSON values with jQuery. Any suggestions on how I can resolve this issue? // JSON response: [{ "private": "8044553.0" }, { "governmentdocs": "98952.0" }, { "officiald ...

Ways to display or conceal a moving svg based on the current tab selection

Below is the fiddle and snippet: https://jsfiddle.net/e130kr2x/ I've successfully achieved animating the SVG when a tab is active, but I'm facing difficulty in making it not display or fade out when opening a new tab. The new tab should activate ...

Favicon in browser blinks when hash changes

Whenever I adjust the hash location using either document.location.hash or window.location.hash, there seems to be a slight 'blinking' effect in most browsers. It's quite unattractive and I need to find a way to prevent it, especially since ...

Deleting content from cells in table for all even td items

This problem has been causing me frustration; I've attempted various methods using jquery and CSS, but to no avail. Essentially, I have the following table structure: <table id="mainTable"> <thead> <tr> <th>Arrival ...

Determining if a path is within the same directory: a step-by-step guide

I need to check if the input path is located in the same folder or not Question: Is the input path in the same folder? For example, if my current path is f://learning/java and I am currently in a folder named java, then any path that directly belongs to ...

What is the process for converting inline SVG images on the server?

Looking to use inline SVG as a background-image for another element? Check out this example that currently only works in Chrome. If you need an alternative method, take a look at the simpler inline SVG from the RaphaelJS demo here. <svg height="480" ve ...

Exploring the Features of Bottom and Right in jQuery

Here is a snippet of jQuery code that I included in a sample program: $('#left').click(function(){ $('.box').animate({ left: "-=40px", }, function(){/* End of Animation*/}); }); $('#right ...

Using AJAX to pass the ID name when clicking in PHP

Currently, I am in the process of implementing an Ajax-driven filtered search system that consists of three distinct tabs. These tabs allow users to search by names, category, and location. I have successfully enabled searching when a user types a name int ...

Easily iterate through the <li> elements using jQuery and append them to the <datalist> dynamically

My jQuery loop seems to be malfunctioning as it's not showing the values of my li elements. Instead, I'm seeing [object HTMLElement] in my input search bar. <div id="sidebar-wrapper"> <input type="text" list="searchList" class="searc ...

Arrange the parallel table columns within their own individual divs to be perfectly aligned

I have a layout with two divs stacked on top of each other, each containing a table with the same number of columns. I need to ensure that the columns in both tables are the same width and aligned properly. If a column in one table expands, I want the corr ...

Utilizing a loop for setting variable values in JavaScript

Using both JavaScript and JQuery. Let's imagine there is an array called ListArray with various sentences inside. Sounds easy enough. Is there a way to achieve this? var List = for (var i = 0; i < 10; i++) { //iterate over an array here to c ...