Having trouble with CSS last-child selector not functioning properly with nested divs?

I was in the process of designing my personal website with Bootstrap, focusing specifically on the resume section where I am implementing a vertical timeline to showcase my experiences.

For the final experience in each section, I wanted to adjust the line length to indicate the conclusion of that particular section using the :last-child selector. However, I encountered difficulties making it work with nested divs.

Unfortunately, my CSS seems to be selecting only the very last instance of section-item instead of the last one for each section-col.

Below is a snippet of my HTML:

<div class = "container h-100 contact-form">
  <h2>Resume</h2>          
  <div class = "row justify-content-center">
    <div class = "col-md-6">
      <div class = "section-col"></div>
      <h4>Education</h4>
      <br>
      <div class = "section-item">
         <h5>School Name Here</h5>
         <h6>Degree Here</h6>
         <p>Location | Class of</p>
      </div>
      <div class = "section-item">
         <h5>Second School Name</h5>
         <h6>2nd School Name Location</h6>
         <p>Graduation</p>
      </div>
      <br>
    </div>
    <div class = "section-col">
      <h4 class = "section-name">Professional Experience</h4>
      <br>
      <div class = "section-item">
        <h5>Company</h5>
        <h6>Position</h6>
        <p>June 2020 - August 2020</p>
      </div>
      <div class = "section-item">
        <h5>Other company</h5>
        <h6>Position</h6>
        <p>June 2020 - July 2020</p>
      </div><br>
    </div>
  </div>

The CSS code snippet shows my attempt (The first block generates the line and the second affects the circle):

.section-item {
    border-left: 2px solid grey;
    padding-left: 15px;
    margin-left: 5px;
    height: 120px;
    
}

.section-item > h5{
    line-height: 80%;
}

.section-col .section-item:last-child {
    height: 70px; 
}

.section-item:before{
    content: " ";
    display: inline-block;
    position: absolute;
    border-radius: 50%;
    background: #fff;
    border: 3px solid black;
    width: 15px;
    height: 15px;
    left: 14px;
    right: 0;
   
}

Displayed below is an image illustrating the issue:

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

If anyone has suggestions or solutions to this problem, I would greatly appreciate any feedback. Thank you!

Answer №1

The :last-child selector inside each .section-col is specifically targeting a <br> element. As a result, the selector

.section-col .section-item:last-child
does not apply to any element and has no impact.

There are three options available:

  1. Use :last-of-type in place of :last-child
  2. Remove the <br> entirely
  3. Enclose the .section-items within a new <div> and handle the styling with :last-child there

Answer №2

Give this a shot:

.section-col .section-item:last-of-type {
    background: #f2f2f2;
}

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 extend this dropdown menu to span the entire width of the page?

My Bootstrap 5 megamenu needs all the dropdown menus to span the entire page width and be in the same position, similar to this page. How can I make the dropdown menus larger? Which Bootstrap class should I modify? I've attempted to make changes, bu ...

Modifying the appearance of Bootstrap 4 Nav-tabs using CSS

I have implemented Bootstrap 4 Nav-tabs as shown below: <ul class="nav nav-tabs" id="myTab" role="tablist"> <li class="nav-item"> <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" ...

Positioning the bottom of a two-column layout using CSS

I am working on a 2 column layout that should have a height of 100% of the window size. The left side will contain an image taking up 90% of the window size positioned at the bottom of the wrapper. On the right side, there will be text occupying the top 50 ...

Save a PNG file using the HTML5 Canvas when the Submit button is clicked, with the help of PHP

I am looking for assistance with a signature capture form. I came across a standard template on Github that works perfectly, but I wanted to customize it further. However, my Javascript skills are still in the early stages. The current code allows you to c ...

Steps for positioning the navigation bar beneath header 1

In my HTML code, here is a link to an image: https://i.sstatic.net/zhj7o.jpg I have successfully completed the sections associated with this image: https://i.sstatic.net/wIE6N.png However, I'm facing an issue now. I am unable t ...

Tips for displaying multiple results from a MySQL query in a single .ejs file using Node.js and Express

Currently diving into Node.js and working on a web application, I am faced with the challenge of rendering twice in the same .ejs file. Consider the scenario presented in the following .ejs file: <table> <% rows.forEach(function(ind){ %> /* m ...

Is there a way to customize the background color of a dropdown menu?

Is there a way to change the background color of my dropdown menu when it is open? I have tried using the following CSS code: .dropdown.open { background-color: black; } Here is how the HTML looks like: <ul class="nav navbar-nav navbar-right"> ...

Achieve vertical centering of items without relying on absolute positioning or flexbox

I am struggling to align three divs vertically without using position:absolute or flexbox, as they will affect other elements on the page that I cannot control. Here is the current code snippet: <div class="search-wrapper text-center " ...

Can you help me identify the issue with my current Jade for loop implementation?

Here is my full loop code written in Jade: block content div(class='row') div(class='col-lg-12') h1(class='subject') 로또라이 div(class='row') div(class='col-lg-8') - for (var ...

Aligning content vertically in Bootstrap 4 so it is centered

I've been struggling to center my Container in the middle of the page using Bootstrap 4. I haven't had much luck so far. Any suggestions would be greatly appreciated. You can check out what I have so far on Codepen.io and experiment with it to s ...

Storing a form in a database using SQL

I have developed a PHP script that is meant to save form input into a database called XXXX_comments in a table called Comments, with columns for Name and Comment. When a user clicks the Save button, the form data should be inserted into the database. The d ...

Continuously make Ajax requests to populate numerous div elements

There are two div elements present on my webpage. <div id="1"></div> <div id="2"></div> I am attempting to dynamically populate these divs using the following php call. <script> var queries = ["SELECT * from table1", "S ...

Clicking on the modal button causes the code to run multiple times in JQuery and JavaScript

Hello, I'm experiencing an issue where the code inside a modal is being executed multiple times when the modal button is clicked. For example, if I click the modal button once, the code runs once; if I click it twice, the code runs twice, and so on. ...

Error encountered while compiling SASS in _root.scss file for Boostrap 4 beta2

After getting the latest version of boostrap 4 beta 2, I ran into an issue when trying to compile the boostrap.scss file using my Koala compiler. An error popped up saying: "Invalid CSS after "...lor}: #{$value}": expected "{", was ";" on line 4 of scss ...

Determine the overall sum of all items

Utilizing data fetched from my SQLite database, I'm utilizing *ngFor to display a comprehensive list of items with their respective names, prices, amounts, and totals. How can I calculate the grand total and display it at the bottom of the list? chec ...

Exploring the Benefits of jQuery History Plugin, Optimizing with Google Sitemap and

Despite searching online, I have yet to find a precise solution to my query. On my index.php page, there are two specific DIV's: #post-title and #post-content, whose content is dynamically loaded through a jQuery function triggered by clicking a link ...

Transition one background image into another using background positioning

Snippet of code: https://jsfiddle.net/foy4m43j/ HTML: <div id="background"></div> CSS: #background { background-image: url("http://i.imgur.com/hH9IWA0.png"); background-position: 0 0; background-repeat: repea ...

Divs gracefully appear one after the other in a sequential manner

I am attempting to showcase a sequence of 4 divs in a row using the ease-in CSS transition feature. I came across this example that demonstrates what I am aiming for: http://jsfiddle.net/57uGQ/enter code here. However, despite my best efforts, I am unable ...

Shifting Icon to the Right within the Drawer Navigator Toolbar

While modifying the example code for Material UI's drawer navigator, I decided to enhance it by adding a notification icon and a checkout icon with the Admin Panel typography in the toolbar. However, I encountered an issue where the checkout icon app ...

The issue of a centered image not displaying properly and the inability to adjust the font

I'm trying to get the image to display bigger and in the center, but no matter what I try it's not working. HTML: <div class="content-grid"> <img src="test.jpg" /> </div> CSS: .content-grid img{ display: block; margin: ...