4

I'm trying to get a layout where two links next to each other break to their min-width, if and only if there is not enough space for them to display on one line. Here you can see the elements are wider than their content:

enter image description here

If I set width: min-content, I get the desired result, the link elements are as wide as their content:

enter image description here

BUT if there's more space, they are of course still displayed like that (what I don't want):

enter image description here

How could I solve this? On line break case having an element width same as the actual content?

a {
  display: inline-block;
}
.min-content a {
  margin-top: 20px;
  width: min-content;
}
a:first-child {
  border: 1px solid green;
  margin-right: 10px;
}
a:last-child {
  border: 1px solid yellow;
}
.wrapper {
  width: 200px;
  bordeR: 1px solid red;
  display: flex;
  padding: 5px;
}
.long {
  width: 400px;
  margin-top: 20px;
}
<div class="wrapper">
  <a>linklink linklink 1</a>
  <a>linklink linklink 3</a>
</div>

<div class="wrapper long">
  <a>linklink linklink 1</a>
  <a>linklink linklink 3</a>
</div>
<br/><br/>
min-content:
<div class="wrapper long min-content">
  <a>linklink linklink 1</a>
  <a>linklink linklink 3</a>
</div>
NthDegree
  • 1,301
  • 2
  • 15
  • 29
  • I'm not sure about what you're asking. It seems to me that you just need to remove `width: min-content;` on your `a` but I'm not sure. – Amaury Hanser Jan 12 '23 at 10:10
  • 1
    I want the width of the elements to behave like `min-content`, meaning the width of the element should only be as wide as the text BUT only if it line-breaks. If I set `min-content`, it will do the line-breaks also if there's enough space. – NthDegree Jan 12 '23 at 10:14
  • this is not possible with CSS – Temani Afif Jan 12 '23 at 11:27
  • @NthDegree Have you found a solution for this problem? – SaltyPotato Jul 15 '23 at 16:44

0 Answers0