Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select only elements which are first children of a container by class?

Tags:

html

css

I have complex blocks of DIVs netted to one another and I cannot edit them because this html code is provided from the system. Only thing I could do is from CSS.

I want to select only "Child 1". My CSS does select "Child 1", but because of some related class netted to one another, it selects "Grand child 1" and "Great grandChild 1", as well, unfortunately!

Please give me a hand on how to select only "Child 1".

Thanks!

.container > .child:first-child {
  border: 1px solid red;
}
<div class="container">
Parent
  <div class="child">
    Child 1
  </div>
  <div class="child">
    Child 2
     <div class="container">
        Child of Child 2
        <div class="child">
          Grand Child 1
        </div>
        <div class="child">
          Grand Child 2
          <div class="container">
            Parent
              <div class="child">
                Great grandChild 1
              </div>
                <div class="child">
                  Great grandChild 2
              </div>
            </div>
        </div>
      </div>
  </div>
</div>
like image 353
abcid d Avatar asked Apr 01 '21 20:04

abcid d


People also ask

How do I select the first child of a class in CSS?

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

How do you target first child in SCSS?

The :first-child selector allows you to target the first element immediately inside another element. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.

What will match the given CSS selector body * First child?

The most likely answer is #containingElement > h1:first-child .

Which is the correct way to select child elements?

The CSS child selector has two selectors separated by a > symbol. The first selector indicates the parent element. The second selector indicates the child element CSS will style.

How do you select only the first child of a class?

To select the first child of a class, you can use the class selector in combination with the :first-child selector. The selector allows you to select only the first child of its parent element. selector. selector to select the first child of a class.

How do I get the first child element of an element?

To get the first child element of a specified element, you use the firstChild property of the element: If the parentElement does not have any child element, the firstChild returns null. The firstChild property returns a child node which can be any node type such as an element node, a text node, or a comment node.

How to select all child elements of the element with ID?

The following example selects all child elements of the element with the Id main: let menu = document .getElementById ( 'menu' ); let children = menu.children; console .log (children); The firstChild and lastChild return the first and last child of a node, which can be any node type including text node, comment node, and element node.

How to select only the last child element of the menu?

If you want to select only the last child element with the element node type, you use the lastElementChild property: The following code returns the list item which is the last child element of the menu:


1 Answers

This will only select .container that not child of the .child.

:not(.child) > .container > .child:first-child {
  border: 1px solid red;
}
<div class="container">
Parent
  <div class="child">
    Child 1
  </div>
  <div class="child">
    Child 2
     <div class="container">
        Child of Child 2
        <div class="child">
          Grand Child 1
        </div>
        <div class="child">
          Grand Child 2
          <div class="container">
            Parent
              <div class="child">
                Great grandChild 1
              </div>
                <div class="child">
                  Great grandChild 2
              </div>
            </div>
        </div>
      </div>
  </div>
</div>
like image 117
doğukan Avatar answered Oct 27 '22 17:10

doğukan