Perform the following task for each initial list item within an unordered list:

Here is the code I'm working with:

ul {
  list-style-type: none;
}

ul:not(.sub-menu) {
  padding: 0px;
}

ul li {
  padding-top: 10px;
}

ul li:first-child:not(.sub-menu) {
  margin-bottom: 30px;
}

.submenu {
  margin-left: 20px;
}
<ul>
  <li>test</li>
  <li>test</li>
  <li>test</li>
  <li>has submenu
    <ul class="submenu">
      <li>child</li>
      <li>child</li>
    </ul>
  </li>
</ul>

The challenge I am facing is ensuring that only li elements without a nested ul have a margin-bottom of 30px. Currently, this style affects the nested ul li elements as well.

I have tried using

ul li:first-child:not(.sub-menu li)
, but it doesn't work. Is there an alternative solution?

Answer №1

If you're unable to modify the HTML directly and require a static solution, this code snippet should suffice.

ul > li:not(:nth-of-type(4)){
    margin-bottom: 30px;
}

The above CSS rule will apply a bottom margin of 30px to all direct child li elements inside the parent ul, except for the fourth one.

Alternatively,

If a dynamic approach is needed, jQuery can be utilized as shown below:

$('ul li').each(function(){ // Iterate through each li element

    if(!$(this).parent().hasClass('submenu')){ // Check if the parent does not have the 'submenu' class

        $(this).css('padding-bottom','30px'); // If not, add padding
    }

})

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

Tips for organizing ng-repeat information according to a condition of true or false

Apologies if this question has already been addressed, but I have not been able to find a solution. I am attempting to organize an ng-repeat based on the selection of radio buttons, but I am struggling with hiding and showing items within the ng-repeat. ...

"Ascend beyond the constraints of fixed measurements with dynamic

My JQuery image fader has a width of 100% and height set to auto. I'm thrilled that it works perfectly on the iPhone as well. Now, I am facing a challenge while trying to make content "float" after my JQuery slider. The problem arises because I use ...

Creating a single div to overlay two divs solely for mobile devices can be achieved by using media queries and absolute

Creating a mobile-friendly page seems to be a challenge for me. I want to have header text, top and bottom text, and an image of a phone that hovers over both texts. I attempted this using Twitter Bootstrap, but I struggled with the overflow property ...

Trouble with reading from a newly generated file in a node.js program

Whenever I launch my results.html page, I generate a new JSON file and use express.static to allow access to the public folder files in the browser. Although my application is functioning properly, I find myself having to click the button multiple times f ...

Ways to extract a word from the source code of a webpage

<a class="thumb" href="/pe/Productos/Marca/Nike/Nike-Air-VaporMax-2020-FK/p/bt_NE_10677654" title="Nike Air VaporMax 2020 FK"> <img data-src="//media.marathon.store/products/h75/h1b/h00/9012525662238.jpg" alt="Nike Air VaporMax 2020 FK" class="laz ...

What steps can be taken to safeguard the title of the document in the top window from being altered by any iframe or script?

My typical approach is to delete and redefine the title property like in the code snippet below: if (delete document.title) { Object.defineProperty(document, 'title', { get: getter, set: setter, enumerable: true, ...

Fixed-positioned elements

I'm facing a small issue with HTML5 that I can't seem to figure out. Currently, I have a header image followed by a menu div containing a nav element directly below it. My goal is to make the menu div stay fixed when scrolling down while keeping ...

Add a pair of assorted div elements to a shared container

I have two different sections with classes named "red" and "blue". By default, these sections are hidden. I want to duplicate them and display them in a single container named "cont". The red button appends the red section and the blue button appends the b ...

Creating a horizontal line to the left of centered text using CSS styling

Is there a way to display a horizontal line to the left of text while keeping the text center aligned? Refer to the image below for clarification. Below is the markup I am currently using, which includes Bootstrap 4: https://i.sstatic.net/KF8WJ.png ...

Is it possible to make the mat-menu-item the same size as the mat-menu button in Angular Material v.12?

How can I ensure that the mat-menu-item button is the same size as the mat-menu itself? For example, click here <div class="container d-block d-md-none"> <div class="row"> <div class="col d-flex justify-content-cent ...

Ensure that all divs have the same height and padding when resizing

I have 3 divs nested within a parent div. My goal is to dynamically set the height of all the child divs to match the height of the div with the maximum height, even when the screen resizes due to responsiveness. Below is my HTML structure: <div class ...

How to dynamically set the image source in Vue.js using elements from a single object in a list

I currently have a collection of objects that only consists of an id and a path: export let carList = [ { id : ++id, path : "img/RedCar.png", }, { id : ++id, path : "img/BlueCar.png", }, ...

How to correctly position a modal in a React application using CSS

Currently, I am working on integrating the react-modal NPM library into a React Typescript project. The issue I am facing is that the modal occupies the entire width of the screen by default, with padding included. My goal is to have it display as a small ...

Converting Data from MySQL and PHP to Excel

Is there a way to export human resource applications stored in my MySQL table as an Excel file? function exportExcel($filename='ExportExcel',$columns=array(),$data=array(),$replaceDotCol=array()){ global $connect; header('Content-Enc ...

Use jQuery to parse the JSON below and then showcase the information in an HTML table

Can anyone assist me with parsing the following JSON using jQuery and presenting it in an HTML table? I want to showcase the values of "key" and "doc_count" in an HTML table. Your help would be greatly appreciated. Please find the JSON data below: { ...

Text area capacity

Is there a limit to the maximum capacity of a textarea for accepting text? The HTML page functions correctly when the text is limited to around 130-140 words. However, if the text exceeds this limit, it causes the page to hang without any response. The tex ...

Using selenium in conjunction with python to click a unique button

Having trouble clicking the connect button on the "people you may know" page of LinkedIn () The HTML code for these buttons is: <a class="vcard-button bt-connect bt-primary" href="#"><span>&nbsp;</span>Connect</a> I attempted ...

The image data is not displaying in the $_FILES variable

I am having an issue updating an image in my database. I have a modal that loads using jQuery. When I click on the save modification button, all the form data appears except for the image file, which does not show up in the $_FILES array in PHP. I have tri ...

Prevent bouncing effect in Cordova/Phonegap specifically for the header and footer sections

I've utilized the property in the config.xml file to prevent bouncing in the webview (iOS): <preference name="DisallowOverscroll" value="true" /> It's working as intended. However, I'm wondering if there's a way to disable bounc ...

Tips for vertically centering elements in the header using flexbox

Within the header of my website, I have structured 3 separate divs - one for a logo, one for a search input, and one for a cart link. My goal is to align these 3 .col-md-4 divs in a vertically centered position. While these divs currently have an align-it ...