Is it possible to target the last child element in CSS without relying on nesting

Is there a way to select the very last child of an element and apply a specific class without using nesting?

For instance, how can I target only the final div in the structure below, nothing more and nothing less.


<body>
 <div class="very-last-child">
  <p>The first paragraph.</p>
  <p>The second paragraph.</p>
  <p>The third paragraph.</p>
  <p>The fourth paragraph.</p>
  <div class="select-me">
   <p>The first paragraph.</p>
   <p>The second paragraph.</p>
   <p>The third paragraph.</p>
   <p>The fourth paragraph.</p>
  </div>
 </div>
</body>

I have attempted a similar approach before but it did not yield the desired result.

div > :last-of-type {
  padding-bottom: 50px;
}

p, div{
border: 2px solid black;
}

SCENARIO: How can I add padding to the last element within my page wrapper, regardless of its type? Note that I am working with SCSS.

Answer №1

Your code is a bit confusing due to the class names used, but based on my understanding, here is what I believe you are aiming for.

.very-last-child > :last-child {
    padding-bottom: 50px;
}

With this code snippet, you will add 50px of padding at the bottom of the element with the class name select-me.

Answer №2

If you need to target just the final element within a container, simply employ the following syntax:

<selector> > :last-child {
    ...
}

For example,

body > :last-child {
    ...
}

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

After the page is reloaded, apply a class to the divs by selecting them based on the "data" attribute

I have a set of four cards labeled "card", each representing different body parts: eyes, torso, arms, and legs. <div class="card" data-lesson="eyes">eyes</div> <div class="card" data-lesson="torso">torso</div> <div class="card" ...

Tips for displaying a table with a button click

I am struggling to figure out how to embed a table inside a button in order to display the table when the button is clicked and hide it when clicked again. Below is the code I have been working with: function toggleTable(){ document.getElementById ...

Styling trick: Positioning a paragraph next to a span filled with SVG icons

I am facing an issue with the alignment of a set of SVGs contained within a <span> element appearing below a <p> element. I want the span to line up with the text 'Question 1'. Methods I have attempted: No conflicting margins or pa ...

Increase the height of the div element by a specified number of

Is there a way to dynamically expand a div's height using CSS without knowing its initial height? I want to increase the height by a specific amount, such as "x px taller" regardless of its starting size. For example, if the div starts at 100px tall, ...

Diagram with Cascading Style Sheets and Hypertext Markup Language

As I work on creating a flowchart, here is the code that I have developed: #flowchart { width: 580px; text-align: center; font-family: sans-serif; font-size: .8em; margin: auto; } #flowchart a { ...

How can I pass a variable to withStyles in Material UI?

Here is the code I am currently working with: class StyledInput extends React.Component{ styles = (color, theme) => ({ underline: { borderBottom: `2px solid ${color}`, '&:after': { borderBottom: `2px solid ${col ...

I'm puzzled as to why a scroll bar materializes

I recently encountered an interesting issue while working on my responsive web design project. Upon resizing the browser to a width of 615px or less, a horizontal scroll bar unexpectedly appeared. It's puzzling as to which element is causing this prob ...

AngularJS: Utilizing UI Bootstrap Popover with the Placement Set to 'bottom-right'

I am working with UI Bootstrap and AngularJS, attempting to customize a popover to have the 'bottom-right' placement instead of the default 'bottom' or 'right'. The desired functionality is illustrated in the image below. htt ...

Header that sticks to the top of a container as you scroll through it

Many questions arise regarding sticky elements in the DOM and the various libraries available to handle them, such as jquery.pin, sticky-kit, and more. However, the issue with most of these libraries is that they only function when the entire body is scro ...

Building an expandable vertical dropdown with Bootstrap that opens with a click

My goal is to design a vertical menu that expands when clicked, revealing 3 links for each item. I currently have the following setup: This is the initial layout: After clicking all items: The code I've implemented so far looks like this: <!DOC ...

What could be the reason behind the failure of this :after element?

I am facing an issue with the preloader on my webpage where the animation is not displaying as expected. The animation should appear on top of a dark black background before the page fully loads, but it seems to be missing. The CSS for the animation works ...

How to implement responsive CSS in Laravel 4

It has come to my attention that in Laravel 4, you can load CSS and scripts using the following method: {{ HTML::style('css/style.css'); }} Which will result in: <link media="all" type="text/css" rel="stylesheet" href="http://localhost/cupc ...

Make the HTML element expand to the remaining height of the viewport

Need help with creating a section that automatically fills the remaining viewport height below the navigation bar. The section includes a centered div and background image, along with a button at the bottom for scrolling down by one full viewport height. H ...

Establish starting dimensions and remember the previous sizes of allocations

I successfully implemented a draggable split panel using a code library from johnwalley on GitHub called https://github.com/johnwalley/allotment. My goal is to achieve the following functionalities: Clicking on Expand or collapse the allotment B should e ...

CSS problem: HTML element moving to the right upon clicking

Currently, I am utilizing a jQuery popup to present additional information to users. The setup involves having a link on the page that triggers the popup to appear from the top. The popup script being used is sourced from CodePen. However, an issue arises ...

Using PHPs nth-child in media queries

There are multiple images on MyPage, typically displayed in rows of 6. However, the number of images per row adjusts based on browser size using media queries. The issue arises with the margin set for the last image in each row, as the nth-child property r ...

Tips for activating scroll feature for certain section of an HTML page

For the styling of my page, I'm utilizing Scss and am facing an issue related to setting up scrolling for specific sections of an HTML page. You can check out the image reference here. During a scroll event, I want to ensure that the Categories (left ...

Corner of the Border Revealing a Clear Sky above

Looking to enhance my navigation bar (menu) by adding a cornered border when the user hovers over it. <nav> <a href="#home">Home</a> | <a href="#about">About</a> | <a href="#blog">Blog</a ...

Dynamic Bootstrap User Authentication Form

Currently, I am in the process of constructing a login form for my web application and have opted to use Bootstrap for its styling. To ensure that the screen is responsive, I've integrated the following "Meta Tag" into the structure: <meta name="v ...

Achieve a sliding effect between two divs using CSS only

I'm trying to create a design with 2 divs that are the same size. The first one is visible by default, while the second one is hidden initially. My goal is to achieve an effect where when you hover over the first div, the second one slides upward and ...