Is there a way to apply specific CSS to the last paragraph within a particular div class?

Is there a way to style the last p within a specific class without affecting others?

For instance:

<div class="my-container">
  <div class="banner-message1">
    <p>1</p>
  </div>
  <div class="banner-message2">
    <p>2</p>
  </div>
</div>

How can I apply unique styling to just the <p>2</p> and not the <p>1</p>

The typical method of using div.my-container p:last-of-type won't work in this case since both <p>1</p> and <p>2</p> are the last paragraphs within their respective parent divs (banner-message1 and banner-message2)

https://jsfiddle.net/hygzq3ab/

In a JSFiddle example, you can see how applying last-of-type affects all last p tags, even when nested within different classes.

An attempt to use

div.my-container p:last-of-type { margin-bottom:0; }
doesn't achieve the desired result.

Answer №1

To style the last paragraph within a <div>, you can use the following CSS:

.my-container > div:last-of-type p {
    margin-bottom: 0;
}

If the last <p> is not always inside a <div>, you can target it like this:

.my-container > div:last-child p,
.my-container > p:last-child {
    margin-bottom: 0;
}

It's important to note that using :last-child is necessary in this case to ensure you only style the last paragraph and not any other elements.

Answer №2

Is this what you were looking for?

Here is the HTML code snippet:

<div class="my-container">
<div class="banner-message">
  <p>
  1
  </p>
</div>
<div class="banner-message">
  <p>
  2a
  </p><p>
  2b
  </p>
</div>
<div class="banner-message">
<p>
aaa
</p>
</p>
  <p>
  3
  </p>
</div>
</div>

Also, here is the relevant CSS code:

.banner-message:last-child p:last-child {
  color: red
}

This CSS rule should address your query.

Answer №3

Another way to achieve this is by utilizing: last:child

 .my-container > div:last-child p {
        margin-bottom: 0;
    }

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

Is it possible to use the Stop Button on the HTML5 Audio Tag to halt a live MP3 stream

Is there a way to add a stop button as well? Currently, I have play and pause buttons, but the stop function doesn't truly clear the music buffer in the browser; it just stops playback without resetting to the beginning. This is fine for MP3 files but ...

What could be the reason for the text being displayed beyond its designated container?

Web Development <section id="section1"></section> <section id="section2">Content outside section</section> CSS section{ width : 100px; height : 100px; } #section1{ background-color : lightgreen; float ...

Expand the Layer if it overflows

Currently, I have a div element containing various content with Java dropdown links causing the length of the content to vary greatly vertically. By applying "overflow-y: scroll," the content remains in the layer and a scrollbar appears, functioning as in ...

Ways to configure an Express.js server file with the router path set as the root directory

I have a directory structure set up like this root private js css index.html app.js privateRouter.js ... Within my index.html, I am referencing the js and css files using relative paths: <link rel="stylesheet" href="css/index.css" ...

Adjusting the template layout of Django Rest Framework

Modifying the login function in the code is my current task - {% block userlinks %} {% if user.is_authenticated %} {% optional_logout request user %} {% else %} {% optional_login request %} {% endif %} {% endblock %} I am loo ...

Is there a way to ensure a table created with CSS display: table adheres to a specified minimum height?

Using display: table, display: table-row and display: table-cell, I am dealing with a large number of rows in a table and utilizing AngularJS ng-repeat to populate them. Although the example shows just two rows, in reality there are many more that all need ...

Nav bar width remains stagnant despite varying content lengths

My navigation bar looks great with 5 objects, but as soon as I add 3 more, it suddenly drops below the main header. Why is this happening? 5 Objects: 7 Objects: HTML: <div id="header"> <div class="w960"> <div id="logo"> ...

UIBootstrap Tooltip Triggering on Various Events

Can the triggers for Tooltip in UIBootstrap be extended to accept multiple conditions? For instance, I would like my tooltip to close on both 'blur' and 'click' actions. I attempted to pass in an array but it did not work: $tooltipPr ...

Select two out of the three links from a repeating HTML structure using JQuery

Having difficulty creating an efficient jQuery selector to choose 2 out of 3 existing a elements within a repeating HTML structure. Below is the code snippet. The goal is to select Link 1, Link 2, excluding Link 3. Keep in mind that the entire HTML struct ...

Tips for making Angular automatically generate the property in HTML when employing the bind feature

When using property binding in Angular, I noticed that the property is not created in the HTML unless I directly write the value in the code. For example, in a StackBlitz demonstration with a 'button' component that changes color based on the typ ...

The NPM Install process seems to be skipping certain files that were successfully installed in the past

When I first installed NPM Install in a folder, it created multiple folders and files: node_modules public src .DS_Store package.json package-lock.json webpack.config.js After that, npm start functioned perfectly. Now, as I embark on a new project for th ...

Retrieving video tag source dimensions using Angular 2

Currently, I am in the process of constructing a video component and finding a way to obtain the dimensions of the source video (either the source file or the container) so that I can pass it to another component. I have incorporated the following code: / ...

Troubleshooting issue with CSS in Django_tables2

Working with the django_tables2 plugin for the first time has been an interesting experience. I followed the installation and quick start guide provided in their documentation. After pip-installing django-tables2, adding 'django_tables2' to my ...

Variations in CSS Stylesheets for Various Parts of the Body

Currently, I am in the process of creating a single page website. One issue that I have encountered is needing a Bootstrap "modal" class popup to appear when a button is clicked. However, the template I am utilizing was designed using Bootstrap Version 1, ...

What is the best way to design a polygon-shaped responsive div?

I've been attempting to design a unique layout with a pentagon-shaped div, however, I'm encountering some challenges. Despite using SVG for the shape, it does not display correctly in Firefox. Additionally, I need the div to be responsive and sca ...

Retrieve data from a JSON gist by parsing it as a query string

I have a JavaScript-based application with three key files: index.html app.js input.json The app.js file references input.json multiple times to populate content in div elements within index.html. My goal is to enhance the functionality so that when acc ...

Verify the presence of a mouse before attempting to hover

Is there a way to determine if the user has a cursor to hover over elements using JavaScript, while also utilizing Bootstrap? if (hasCursor()) { doCode(); } I initially attempted to check if it was a touch device, but faced limitations testing on my 2- ...

Adjust the background hue of the Row in the Table

Is there a way to update the background color of a Table Row? I attempted using "BackgroundColor" but didn't see any changes Any suggestions on how to fix this issue? <TableRow style={{ backgroundColor: "#FF0000" }}> <TableCell& ...

Display a quote within an input field on a webpage using PHP

Here is a simplified version of the code I am trying to work with: <?php $var = 'Read "The Book"'; ?> <input type="text" value="<?php echo $var; ?>" /> The issue I am facing is that when displayed in the input field, it o ...

Using custom fonts with @font-face doesn't work in WordPress

I'm currently in the process of trying to incorporate custom fonts into a WordPress website, but I'm facing challenges getting the desired font to appear correctly. Interestingly enough, I've managed to successfully display the custom font l ...