0

I have the following elements:

.nav {
  width: 100%;
  background: red;
  display: flex;
}

.parent {
  background: blue;
  flex-grow: 1
}

.child {
  background: yellow;
}
<div class="nav.">
  <div class="parent">
    <div class="child">
      Logo
    </div>
  </div>
</div>

I want the Logo to be colored in Yellow, but I also want to see the color of the parent, blue color.

which basically means, I don't want the .child to take up the width of its parent, I want it to have its own width depending on its content.

depending on its content means that I don't want to give it static width such as 100px or 200px, I want instead it to have a dynamic width.

How to achieve that in CSS?

Normal
  • 1,616
  • 15
  • 39

1 Answers1

2

You can make your child inline-block, then it will only be as big as the content (plus any padding)

.nav {
  width: 100%;
  background: red;
  display: flex;
}

.parent {
  background: blue;
  flex-grow: 1
}

.child {
   display:inline-block;
  background: yellow;
}
<div class="nav.">
  <div class="parent">
    <div class="child">
      Logo
    </div>
  </div>
</div>
Pete
  • 57,112
  • 28
  • 117
  • 166