Tips for adding padding to a parent div without affecting a specific child element

This is the desired outcome I am aiming for

https://i.sstatic.net/nKiz3.png

The parent div has a white background, and I want to add padding to the left, right, and top, without applying it to the last child div. How can this be achieved using CSS? When adding padding directly, it affects all children, so is there a way to prevent this for a specific child?

JSX Structure:

<div className="parent">
  <div className="child-1"></div>
  <div className="child-2"></div>
  <div className="child-3"></div>
</div>

I have organized the children divs, but I'm struggling to apply padding based on the required condition.
Is individually applying margin to each child the only solution? (I prefer not to do this as the number of children could change dynamically, making it hard to maintain)

Answer №1

Learn how to target the last child using the :last-child selector.

.parent {
  background: magenta;
  display: flex;
  flex-direction: column;
}

.parent > * {
  height: 100px;
  background: green;
  margin: 10px;
}

.parent :last-child {
  margin: 0;
  background: blue;
}
<div class="parent">
  <div class="child-1"></div>
  <div class="child-2"></div>
  <div class="child-3"></div>
</div>

Answer №2

Instead of using padding, consider applying margin to the initial 2 elements within the container.

Leave the parent element without any 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

What is the best way to display an image retrieved from MongoDB as binary data on React.js?

I have developed an API that can receive post requests and save asset cards for rent (similar to Airbnb) along with images. Additionally, I am using React for the frontend development. The data stored in MongoDB has the following structure: _id:objec ...

In a React class, where is the best place to add a conditional statement?

I need to incorporate a conditional statement that will display a certain percentage value in green if it is positive, and red if it is negative. However, I am uncertain about the placement of this conditional statement. <div> {this.state.tableData.m ...

What is the best way for me to insert a paragraph between two lists?

Is it possible to insert a paragraph in between lists even though it's not allowed? Here is an example of the code: <ol> <li>One</li> <p>Paragraph 1</p> <li>Two</li> <p>Paragraph 2</p ...

PHP/HTML failing to upload extra large files

Allowing users to upload large files is causing an issue for me. I tested uploading a 22 MB file and it was successful. However, when I tried uploading a 200 MB file, it seems that the file does not enter the if block and instead returns the output from th ...

generate radio buttons and corresponding labels in real-time

I am trying to automate the creation of elements for each record retrieved from my webservice. <label for="ZAALID_1">Zaal 1</label> <input id="ZAALID_1" type="radio" name="RESERVATIE.ZAALID" value="1" MSGCHECKED="~IF(CHK ...

Having trouble with CSS text not wrapping correctly?

While working on a web application, I encountered an issue where the text within a <div> element on my page was not wrapping properly and instead overflowing outside the box. The <div> in question goes through two stages: one where it is not e ...

What is the best way to substitute spaces in a URL with dashes?

Just starting out with reactjs and tackling a small project. My goal is to replace white spaces with dashes (-) in the URL parameters. Can anyone lend a hand with this? Appreciate any help in advance! ...

Use jQuery to generate and add several elements to the DOM

Hey there! Currently, I'm working on a real-time chat application using socket io. My goal is to display the user's username and message in a unique way by encapsulating the username in a strong tag. I've made some attempts: $('<div ...

Utilizing Material-UI checkboxes in combination with ReactJS and Redux for enhanced functionality

I am currently working on a project where I need to display selected checkbox items using material-ui's checkbox component. Although I have successfully displayed the items with checkboxes, I am facing difficulties in displaying the selected items. ...

What is the process to launch Docusaurus as a service on a Linux system, specifically Ubuntu?

Hey there! I'm currently trying to set up Docusaurus V2 as a service on Ubuntu headless. Right now, I have a temporary script in my Docusaurus folder that works when I start it with ./start.sh: /var/www/citro-docs-2/start.sh #!/bin/bash npm run serv ...

Reducing the amount of products in a MySQL online shopping cart

I have been working on a PHP shopping cart project, and I have successfully implemented functionalities to add, remove, and update items in the shopping cart. However, I am facing a challenge with adjusting the quantity of items in the database when a user ...

Creating an uncomplicated selector bar for a basic javascript slideshow

I've spent the last couple of hours teaching myself some basic JavaScript, so please bear with me. Currently, I have a simple code in place for a straightforward slideshow on a website. Here is the JavaScript I'm using (where the 'slide&apo ...

How to prevent a nested element from inheriting the parent's styling

<ul id="login"> <li><a href="#">User></a></li> <li><a href="#">Logout></a></li> </ul> My menu and sub-menu are functioning correctly, however, the last item that came from a Partial ...

What is the reason for next-redux-wrapper double hydrating in the logger?

I am encountering difficulties while using next-redux-wrapper with nextjs and I need some clarification on the concept of Hydrate between server and client. Recently, I have started learning nextjs with next-redux-wrapper and redux-toolkit packages. After ...

Ensure every individual input field in a Bootstrap form is validated using JavaScript before submitting the form

As a newcomer to Javascript, I am seeking assistance with validating each field in the form below without having to click on the submit button. Your help is greatly appreciated! <form action="" id = "form1" class = "form-group row"> & ...

Is your JQuery Gallery experiencing issues with the next button function?

I'm working on developing a simple gallery using JQuery. The main concept is to have all image files named x.png (where x is a number), and the program will then add a number to the current one, creating x+1.png and so forth. Here's the code I ...

Phaser 3 shows images as vibrant green squares

In my project, I have two scenes: Loading and Menu. In the loading scene, I load images with the intention of displaying them in the menu. Here is the code for the Loading scene: import { CTS } from './../CTS.js'; import { MenuScene } from &apo ...

I cannot locate the dropdown list anywhere

As a beginner in html and css, I decided to follow a tutorial on youtube. The tutorial focuses on creating a navigation bar with dropdown menus using html and css. I wanted the names Ria, Kezia, and Gelia to be displayed when hovering my mouse over the Su ...

Currently, I am working on integrating the functionality of uploading multiple files in a React application

I am currently experimenting with uploading multiple images in React as an array. The issue I am facing is that when I select 2 images to upload, only one gets uploaded while the other is marked as undefined. Take a look at the code snippet below: const ...

I am experiencing difficulty retrieving checked row data using the onRowSelected function in Material UI DataGrid

My goal is to retrieve the data from a Checked row when it's clicked within a DataGrid component. Initially, I achieved this by using the onRowSelected attribute of the DataGrid. However, I am currently unable to access this attribute. I am uncerta ...