Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use :fist-child correctly?

I have a html structure and would like to set the margin-top for the first div to 0. That is for the div that is surrounded with asterisk.

I tried to use the following css code but it does not work as required:

div:first-child {
    margin-top:0 !important;
}

as well as

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a>div:first-child {
    margin-top:0 !important;
}

as well as

.l-content-width.l-content-width--narrow.l-stack.l-stack--chicken{
     div:first-child {
        margin-top:0 !important;
    }
}

here is my html:

 <div class="l-content-width l-content-width--narrow l-stack l-stack--chicken}">
        <a href="https://something&lang=de" class="t-link-area" target="_blank">
            **<div class="l-stack t-link-area__link" data-products-links>**
                <strong class="t-condensed">something</strong>
                
        <i class="c-icon c-icon--ui-arrow-right c-icon--m t-link-area__icon">
            <svg viewBox="0 0 16 16" class="c-icon__svg c-icon__svg--ui-arrow-right">
                <use href="#ui-arrow-right"></use>
            </svg>
        </i>
    
            </div>
        </a>
    
        <a href="https://something&lang=de" class="t-link-area" target="_blank">
            <div class="l-stack t-link-area__link" data-products-links>
                <strong class="t-condensed">something</strong>
                
        <i class="c-icon c-icon--ui-arrow-right c-icon--m t-link-area__icon">
            <svg viewBox="0 0 16 16" class="c-icon__svg c-icon__svg--ui-arrow-right">
                <use href="#ui-arrow-right"></use>
            </svg>
        </i>
    
            </div>
        </a>
    
        <a href="https://something&lang=de" class="t-link-area" target="_blank">
            <div class="l-stack t-link-area__link" data-products-links>
                <strong class="t-condensed">something</strong>
                
        <i class="c-icon c-icon--ui-arrow-right c-icon--m t-link-area__icon">
            <svg viewBox="0 0 16 16" class="c-icon__svg c-icon__svg--ui-arrow-right">
                <use href="#ui-arrow-right"></use>
            </svg>
        </i>
    
            </div>
        </a>
    </div>

I am able to correctly solve this using JS/TypeScript but was wondering if there is a more elegant way to do it in pure CSS?

this.productsLinks = config.element.querySelector<HTMLSelectElement>('[data-products-links]');
if (this.productsLinks.classList.contains('l-stack')) {
       this.productsLinks.classList.remove('l-stack');
}

>Solution :

All the divs in the code you have provided are first children (including possibly the very first one since at minimum it will be within a body element).

It is the anchor element that is the first child so you need to select that, then this snippet selects the first direct child of the anchor element (rather than any child) as well:

.t-link-area:first-child > div.t-link-area__link:first-child {
  margin-top: 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading