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

Element doesn't maintain its width when there is not enough space in Flexbox

The "avatar" has a fixed with and height of 32px. But it’s width decreases when there is not enough horizontal space inside of the flexbox. How can I prevent this? I rather want the "text" to shrink in width.

.container {
  display: flex;
  align-items: center;
  background: #eee;
  max-width: 200px;
  padding: 0.5rem;
}

.avatar {
  width: 32px;
  height: 32px;
  background: #222;
  border-radius: 100%;
}

.text {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  margin-left: 0.5rem;
  margin-right: 0.5rem;
}

.button {
  white-space: nowrap;
}
<div class="container">
  <div class="avatar"></div>
  <p class="text">This is an example text</p>
  <button class="button">Click me</button>
</div>

>Solution :

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

.avatar {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    background: #222;
    border-radius: 100%;
}

Update your css to this

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