Ways to implement a style to the initial child of nested recursive classes

I am attempting to create a style that only affects the first grandchild of the main container. The structure is as follows:

.container {
  display: flex;
}

.container.main {
  border: 1px solid gray;
}

.container.main .item>* {
  border-color: red;
}

.item {
  width: 30px;
  height: 30px;
  border: 1px solid blue;
  margin: 1px 1px 2px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>
  <div class="container main">
    <div class="container">
      <div class="item"></div>
      <div class="container">
        <div class="item">

        </div>
        <div class="container">
          <div class="container">
            <div class="container">
              <div class="item"></div>
              <div class="item"></div>
              <div class="item"></div>
              <div class="container">
                <div class="item"></div>
                <div class="item"></div>

              </div>
            </div>
          </div>
          <div class="item"></div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

I would like to make the first child yellow and the last child dotted. How can I achieve this? Each child could be nested at different levels in various containers.

Answer №1

To achieve this effect, a combination of CSS and JavaScript is required as the levels vary. By utilizing the following code snippet:

.element.start { /* apply style 1 */ }
.element.end { /* apply style 2 */ }
const container = document.querySelector('.wrapper.main');
const elements = container.querySelectorAll('.element');
elements[0].classList.add('start');
elements[elements.length-1].classList.add('end');

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

Tips for modifying the color of a specific section of the border within a table

.table { background: #fff; position: relative; bottom: 210px; width: 90%; border: 2px solid #fff; border-width: 30px; border-collapse: collapse; } .table-year-1 { font-size: 22px; width: 2%; padding-top: 3px; ...

How to show table cell value in Angular 4 using condition-based logic

I am a beginner in Angular development. Here is the HTML code I am working with: <tr *ngFor="let item of calendarTableSelected; let idx = index"> <span *ngIf="idx === 0"> <td style="width:15%;" *ngFor="let name of item.results" ...

Which CSS properties can I implement to ensure that the text remains legible regardless of the background behind it?

Looking ahead, I am displaying an issue where the colors are almost perfect, but when different percentages are applied, some or all of the text becomes obscured. My goal is to assign the font color based on the background difference. I vaguely remember s ...

Label the Kendo Switch device

While using the Switch Component from Kendo UI for Angular, I ran into an issue. The default labels "On" and "Off" were missing on my switch component even though they appeared in the documentation default switch. Upon further investigation, I found the "o ...

Creating a HTML element that functions as a text input using a div

I am encountering an issue with using a div as text input where the cursor flashes at the beginning of the string on a second attempt to edit the field. However, during the initial attempt, it does allow me to type from left to right. Another problem I am ...

Trying to display and conceal images based on the screen size of the device

Just starting out with responsive web design, I'm working on switching between a large image and a small one based on screen width. It's all good when I resize my desktop browser, but when I check it on my mobile phone, the large image is still ...

Ways to extract HTML code as a preview within text using SQL and PHP

I added a message to an SQL database in the following format: "Feel free to take a look at my photo below (img src=..myimg.jpg..) and leave your comments as well." Now, I am trying to fetch it using PHP and include a preview of the image. The image code ...

Incorporating a transition effect to simultaneously toggle between two classes

I'm currently troubleshooting the demo below. For some reason, I am unable to apply a transition to either .fa-chevron-right or .fa-chevron-left when toggling between two classes. Can anyone help me figure out why? $("button").on("click", function( ...

Experiencing issues with uploading a basic .html website using FileZilla

Currently attempting to launch a very basic website using the free webspace provided by my college. The webspace includes a www and a data folder - www for the website and data for regular data purposes. I have created a simple .html file with JS & CSS tha ...

JQuery Mobile Listview Filter Not Working: Troubleshooting Tips

Recently diving into the world of jquery mobile, I've managed to successfully create a homepage and a sub-page labeled "bible_studies," complete with a dynamic list generated through AJAX. Everything seems to be functioning perfectly except for one i ...

Larger icon graphics

Is it possible to increase the size of Glyphicons in Twitter Bootstrap 3.0 without using the btn-lg class? I found this code that makes glyphicons big: <button type="button" class="btn btn-default btn-lg"> <span class="glyphicon glyphicon-th- ...

Jumping Iframe Anchor Link in Src

I'm facing a challenge with an iframe positioned in the center of a webpage. I want to show content from another page within the iframe, but not starting at the very top. To achieve this, I inserted an anchor into the src of my iframe, linked to an a ...

Divide the chosen option using the underscore symbol

I have implemented Material CSS in my web page design which includes a single select country list dropdown with specific values. <select id="select_id" name="select_id" required> <option value="8">select1_1</o ...

How to successfully implement ng-show with Html5's <dialog> tags

It's incredible how simple Html5 dialogs are. It feels like they should have been here 15 years ago! I'm having trouble getting ng-show to work with dialogs. Any tips on how to make it happen? <!DOCTYPE html> <html ng-app="project"> ...

Fading in elements using JQuery every other click

Currently, I am in the process of developing a Burgermenu that features a simple dropdown menu with no animations. Recently, I attempted to implement a script that would cause this dropdown menu to fade in upon clicking: <script> $('span.nav-b ...

Can you explain the distinction between the onclick(function(){}) and on('click',function(){}) functions in jQuery?

My goal is to dynamically load pages into a specific div using ajax. Here's my HTML code: <ul id="nav" class="nav" style="font-size:12px;"> <li><a href="#" id="m_blink">Tab1</a></li> <li><a href="#" id= ...

Adding External HTML Content to a JQuery Mobile Website

As I work on dynamically integrating an external HTML source into a jQuery mobile page, I encounter the issue of it not appearing as expected - lacking the jQuery mobile styling. Can anyone offer some guidance on what might be going wrong? Main HTML: < ...

Div controls the margins of all other elements

I am encountering an issue when trying to position #slider, where the navigation and elements above it also move. It seems as though they are contained within #slider, even though they are not. HTML: <div id="slider" class="clearfix"> <d ...

Having trouble with my intricate-box and grid layout - the images are all overlapping!

Could you please take a look at the image below and tell me what I might be doing incorrectly? When the page initially loads, pictures appear like this. However, if I refresh the page, the issue is resolved. https://i.sstatic.net/hIyA4.jpg If I remove th ...

Can JavaScript be Utilized to Create Dynamic Layouts Successfully?

Are you curious about the viability of using Javascript to create fluid website layouts? While it is possible, how does it compare in terms of performance and complexity when compared to other methods like CSS3/HTML5? Take a look at the function below that ...