Avoiding the application of a border-right to the final child of a div in CSS

Check out this custom HTML code:

<div class="main">
<div class="parent">
  <div class="child">
    <span></span>
    <label></label>
  </div>
  <div class="child2">
   // some html content
 </div>
</div>
<div class="parent">
  <div class="child">
    <span></span>
    <label></label>
  </div>
  <div class="child2">
   // some html content
 </div>
</div>
<div class="parent">
  <div class="child">
    <span></span>
    <label></label>
  </div>
  <div class="child2">
   // some html content
 </div>
</div>
</div>

In order to add the border-right property to the child class, you can try the following CSS:

.parent.child:not(:last-child) {
  border-right: 1px solid tpBorderColor;
  padding-right: 20px;
}

If that doesn't work, you can attempt using this alternative approach:

.parent.child {
  border-right: 1px solid #e9e9e9;
}
.parent.child-child {
  border:none;
}

If these methods are not displaying the border as expected, do you have any other suggestions on how to achieve this?

Answer №1

Uncertain if this is what you were searching for, but if your .child element is adjacent to another .child element in the document object model, you can easily apply a style like this:

.child + .child {
  border-left: 1px solid currentColor;
  padding-left: 20px;
}

This style rule targets the second .child element that appears next to another .child.

.child + .child {
  border-left: 1px solid currentColor;
  padding-left: 20px;
}
<div class="main">
    <div class="parent">
        <div class="child">
            <span>343</span>
            <label>sfs</label>
        </div>
        <div class="child">
            // some html content
        </div>
    </div>
    <div class="parent">
        <div class="child">
            <span>sfa</span>
            <label>afa</label>
        </div>
        <div class="child">
            // some html content
        </div>
    </div>
    <div class="parent">
        <div class="child">
            <span>343</span>
            <label>asfdfaf</label>
        </div>
        <div class="child">
            // some html content
        </div>
    </div>
</div>

Answer №2

Great job on your code! Just a few tweaks were needed to get it perfect.

Here is the updated version for you to try:

.parent .child:not(:last-child) {
  border-right: 1px solid red;
  padding-right: 20px;
}

.child {
margin-bottom: 30px;
}
<div class="main">
    <div class="parent">
        <div class="child>
            <span>343</span>
            <label>sfs</label>
        </div>
        <div class="child">
            // some html content
        </div>
    </div>
    <div class="parent">
        <div class="child">
            <span>sfa</span>
            <label>afa</label>
        </div>
        <div class="child">
            // some html content
        </div>
    </div>
    <div class="parent">
        <div class="child">
            <span>343</span>
            <label>asfdfaf</label>
        </div>
        <div class="child">
            // some html content
        </div>
    </div>
</div>

Answer №3

After analyzing the code provided, it appears that any borders assigned to the <div class="child"> element will not be visible due to the absence of content within the HTML elements.

Furthermore, an adjustment is required in your CSS styling.

The reason for this modification is because you are currently targeting all elements with the class="parent child", but none of your elements possess both selectors within a single class. As the classes (parent > child) are nested, a space between them is necessary in your CSS file (refer to the snippet below).

.parent .child:not(:last-child) {
  border-right: 1px solid tpBorderColor;
  padding-right: 20px;
}

For instance,

<style>
    .parent .child:not(:last-child) {
        border-right: 1px solid #000;
        padding-right: 20px;
    }
</style>

<div class="main">
    <div class="parent">
        <div class="child">
            <span></span>
            <label></label>
        </div>
        <div class="child2">
            // some html content
        </div>
    </div>
    <div class="parent">
        <div class="child">
            <span>// some html content</span>
            <label></label>
        </div>
        <div class="child2">
            // some html content
        </div>
    </div>
    <div class="parent">
        <div class="child">
            <span>// some html content</span>
            <label></label>
        </div>
        <div class="child2">
            // some html content
        </div>
    </div>
</div>

Please note that the first occurrence of the styled element is not displayed in the example above due to the absence of content.

<div class="parent">
    <div class="child">
        <span></span>
        <label></label>
    </div>
    <div class="child2">
        // some html content
    </div>
</div>

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 can be achieved by combining Text Wrangler and terminal?

I'm curious about how to open an index.html file in terminal. For instance, I know that using sublime works like this: sublime index.html But what about textWrangler? And in general, how do I locate similar commands like "sublime" for any program of ...

Why does the Autofocus Attribute in HTML5 fail to work specifically in FireFox when <Form><input> elements are dynamically loaded using Ajax?

While testing the form that I loaded via ajax, I noticed an issue with autofocus on the "c_name" field in Firefox. When I access the form directly, the autofocus feature works as expected, but when loading it with ajax, it doesn't seem to work. Surpri ...

What is the reason behind the image not displaying before the AJAX request is initiated and then disappearing once the request is completed?

I am attempting to display a "loading" gif while my AJAX request is being processed. Below are the functions I am using: beforeSend: function () { $('.form-sending-gif').show(); console.log("The beforeSend function was called"); } and ...

Change SVG path data into a range of 0 to 1 in order to utilize it as a clip path with objectBoundingBox

My SVG shape, exported from Illustrator as a clipping path, is quite complex. The issue I'm facing is that objectBoundingBox restricts path data to the 0-1 range, but my path data exceeds this range. Here is the current code I'm using: <svg& ...

Incorporating a custom background image for Google Maps

Can a live Google map achieve the same overlay effect as a static image? Discover more here Many thanks, Greg Update: Check out this Fiddle that was created based on another Fiddle by @SinistraD ...

Implementing a clickable search icon within a form input field using CSS: a guide

I have added a search icon to the form input field. Check it out here: https://i.sstatic.net/pnv6k.jpg #searchform { position: relative; } #searchform:before { content: "\f118"; font-family: "pharma"; right: 0px; position: absolute; ...

Modifying the TITLE dynamically post header insertion: A guide

Is there a way to dynamically change the title of an HTML page based on the content included below the header section using PHP? For example: <html> <header><title>I would like to change</title></header> <!--CONTENT--> ...

Struggles with organizing tables in asp.net

My apologies for my lack of knowledge in this area, but I am struggling to make my table look correct on my webform. I begin by creating a table: <table> </table> And I want to add a border. The traditional way is to use the 'border&apo ...

Is there an issue with the JavaScript functionality of the max/min button?

Help needed with maximize/minimize button functionality as my JavaScript code is not working. Any suggestions for a solution would be greatly appreciated. Thank you in advance. Below is the code snippet: <html> <head> <script> $(functio ...

Calculating totals in real-time with JavaScript for a dynamic PHP and HTML form that includes quantity,

I have created a dynamic form with fields and a table that adjusts based on the number of results. Each column in the form represents name, quantity, price, and total price. The PHP and SQL code runs in a loop to populate the list depending on the number ...

How to prevent jQuery from continually recalculating width on window resize?

Here is the code I've been working on: http://jsfiddle.net/7cXZj/ var callback = function () { $('.progress-bar').width($('.progress-bar').parent().width() - 190); $(".mainpart-background").animate({ width: "80%" }, 80 ...

Enhancing Time Precision in CakePHP: Introducing Quarter-Accurate Time Input

I'm currently implementing time inputs using the following code snippet: $form->input("time") However, the select boxes generated have minute-accuracy, which is unnecessary for my project. Is there a way to restrict the options in the select lis ...

The background image on Nuxt.js is not adapting to different screen sizes

My journey with developing a Nuxt app hit a snag in the CSS department early on. The issue plaguing me, as is often the case, is responsive background images. Everything looked great during testing on desktop browsers, but when I opened it up on my mobile ...

Unlocking fashion with $refs: A step-by-step guide

After using console.log(this.$refs['block' + row + column]);, I confirmed that I can successfully access the HTML element it refers to. https://i.stack.imgur.com/7KYqB.png However, when attempting to access the element's style using variou ...

Display array elements in a PDF document using pdfmake

Upon reaching the final page of my Angular project, I have an array filled with data retrieved from a database. How can I utilize pdfmake to import this data into a PDF file? My goal is to display a table where the first column shows interv.code and the ...

CSS styling for a background image in the header

Why isn't my background image showing up even though I've used a direct link to the image? What could be causing this issue? .header { background-image: url("https://www.africa.com/wp-content/uploads/2015/07/Kenya.jpg"); background-attac ...

Positioning of header, main content, and footer elements

My webpage is structured into three sections: the header, the main, and the footer. The header at the top has a fixed height of 109px with a 6px border. The main section should extend across the entire page below the header and reach the bottom where the ...

Stop the text from wrapping to the full width of its parent when it overflows onto the following line

When I shrink the width in responsive design, the text overflows to the next line and the text wrapper expands to fit the parent container. Is there a way to prevent this behavior so that it looks like the red container? I could add padding to the contain ...

HTML files do not inherit from other HTML files

I am venturing into the world of VSCode for the first time to create a web application. Starting with the basics, I have an index.html page with a simple "Hello, world!" message. Additionally, I have developed a layout.html file that contains code for a na ...

I'm looking for a way to add an onclick event to every element created by Vue.js through a "v-for" loop in order to toggle the visibility of its inner content

My Vue data consists of: data: function() { return { results: { "items": [ { "id": "770c257b-7ded-437c-99b5-3b8953b5be3a", "name": "Gsbssbb", "price": 9559, "colour": { ...