2

I was taking a course on HTML and CSS when I found an inconsistency that confused me: (Context: https://codepen.io/Hobror/pen/yLQoqMa)

.ex13f-post-attachment {
  font-family: Arial;
  width: 350px;
  display: flex;
  border-style: solid;
  border-width: 1px;
  border-radius: 15px;
  border-color: rgb(220, 220, 220);
  align-items: center;
  overflow: hidden;
}

.ex13f-attachment-preview {
  width: 100px;
  height: 100px;
  background-color: rgb(220, 220, 220);
  margin-right: 10px;
}

.ex13f-attachment-info {
  flex: 1;
}
<div class="ex13f-post-attachment">
  <div class="ex13f-attachment-preview"></div>
  <div class="ex13f-attachment-info">
    <div class="ex13f-atachment-link">youtube.com</div>
    <div class="ex13f-atachment-description">Backend web development - a complete overview 2021) </div>
  </div>
</div>

I have the preview portion of what I am trying to make set to 100px, and want a square shaped, rounded corners section as shown in the image link below. When I set flex to 1 under ex13f-attachment-info I get to see proper width of 100px for the thumbnail portion of the element I am trying to make.

enter image description here

However, when I don't have flex set to one for the ex13f-attachment-info (which contains the text portion of the attachment) I find that this affects the thumbnail portion, and it is no longer 100px long.

enter image description here

What is the reasoning for this? For context I also have the entire element limited to 350px wide.

I did play around with flex values to try narrow down the cause of this, and it seems to be tied to flex-grow being set to one that makes the thumbnail width display the way I want it to. I don't understand how the flex-grow property for attachment-info is affecting the width of attachment-preview.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
hobror
  • 23
  • 3

1 Answers1

2

A default setting on a flex container is flex-shrink: 1. This means that flex items will automatically shrink to avoid overflowing the container.

Hence, your title: "Why does flex:1 on neighboring element in flexbox affect the fixed width of another element?" has a faulty premise, because the other element doesn't have a "fixed width" until shrinking is disabled.

Add flex-shrink: 0 to override the default and disable shrinking.

.ex13f-post-attachment {
    width: 350px;
    display: flex;
    border-style: solid;
    border-width: 1px;
    border-radius: 15px;
    border-color: rgb(220, 220, 220);
    align-items: center;
    overflow: hidden;
}

.ex13f-attachment-preview {
    width: 100px;
    height: 100px;
    background-color: rgb(220, 220, 220);
    flex-shrink: 0; /* NEW */
}

.ex13f-attachment-info {
    /* flex: 1; */
}
<div class="ex13f-post-attachment">
  <div class="ex13f-attachment-preview"></div>
  <div class="ex13f-attachment-info">
    <div class="ex13f-atachment-link">youtube.com</div>
    <div class="ex13f-atachment-description">Backend web development - a complete overview 2021)     </div>
  </div>
</div>

With regard to your info element, it's not flex-grow that's having an impact but flex-basis.

flex: 1 is equivalent to:

  • flex-grow: 1
  • flex-shrink: 1
  • flex-basis: 0

With flex-basis: 0 the element can use all space on the line.

But when flex: 1 is not in use, flex-basis defaults to auto, and the length of the content is factored into the width calculation, squeezing the sibling element, in this case.

For complete details see here:

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701