packnob890 packnob890 - 18 days ago 5
CSS Question

Get first CSS child of div

I want to get the first element after the

.wrapper
and set that element to have a
padding:20px
. For example, in this case, i want to have the
<h1>
to have a padding of 20px.

<div class="wrapper">
<h1>Text</h1>
</div>





but in this case, I want the
div
class
description
to have padding of 20px.

<div class="wrapper">
<div class="description">Text</div>
</div>


how can i do that? I tried
:first-child
but it didnt work. thanks.

Answer

.wrapper > :first-child {
  padding: 20px;
  background: red;
}
<div class="wrapper">
    <h1>Text</h1>
    <h2>Other Text</h1>
</div>
<div class="wrapper">
    <div class="description">Text</div>
    <div class="description">Other Text</div>
</div>

You can omit > if you are certain there will never be any elements nested within the child elements.

Comments