Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
Is it possible to add margin between parent divs depending on child class with pure CSS?

I have multiple <div> wrapping some children which have a class. But the problem is that the parent <div> doesn't have a class or id. Is it possible to add a margin-bottom between the wrapping parent divs which has a specific child with just pure CSS or do I need to use jQuery or JS to achieve this?

Example of how the HTML could look like:

// parent 
    <div> 
    // children
       <h2 class="child-class-1" ></h2>     
       <p class="child-class-2" ></p>
    </div>
    
    // margin-bottom between the wrapping parent divs
    
    <div>
       <h2 class="child-class-1" ></h2>    
       <p class="child-class-2" ></p>
    </div>

I found an solution with pure CSS.

Solution here:

div > .child-class-1, .child-class-2 {
margin-bottom: 80px;
}
   <div style="background-color:red; height: 400px; width:400px;"> 
       <h2 class="child-class-1" ></h2>     
       <p class="child-class-2" ></p>
    </div>
    
    <div style="background-color:red; height: 400px; width:400px;">
       <h2 class="child-class-1" ></h2>    
       <p class="child-class-2" ></p>
    </div>
    
        <div style="background-color:red; height: 400px; width:400px;">
       <h2></h2>    
           <p>Dosent affect the div which dosen't have the child class</p>
    </div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Maybe you can add a properties for the <div> tag in your HTML with external/internal style, but it's affect all the tag in your current / all html pages

example for your css code :

div {
  margin-bottom: 12px
}

or maybe you can add custom style for just one <div> only with inline style, you can use :

<div style="margin-bottom:12px">
     <h2 class="child-class-1" ></h2>    
     <p class="child-class-2" ></p>
</div>

but it quite bother, since you have another <div> in your HTML

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda