Raphael Rafatpanah Raphael Rafatpanah - 4 months ago 20
HTML Question

How to prevent inline-block elements' widths from stretching to 100% of their flexbox containers in IE?

I have inline-block elements whose width stretches to 100% of their flexbox containers in IE11 and Safari. However, in Chrome and Firefox, the width is the width of the content.

This jsfiddle illustrates the issue.

Using

max-width: -webkit-max-content;
in Safari, the inline-block elements' widths are restored to the width of the content.
max-content
is not available in IE11 according to caniuse.com.

Is there a way to achieve this behavior in IE11?



.row {
display: flex;
}
section {
display: flex;
flex-direction: column;
flex: 1 1 0px;
}
.btn {
display: inline-block;
position: relative;
background: black;
color: white;
padding: 10px 40px 20px 15px;
text-decoration: none;
border: 1px solid black;
margin: auto auto 10px 0px;
}
.btn:after {
display: block;
height: 10px;
width: 10px;
border-radius: 5px;
position: absolute;
right: 11px;
bottom: 11px;
content: "";
background: white;
}

<div class="row">
<section>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
<a href class="btn">inline-block</a>
</section>
<section>
<p>p into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum pa</p>
<a href class="btn">inline-block</a>
</section>
<section>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content
here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
<a href class="btn">This is an<br>inline-block too</a>
</section>
</div>




Answer

The display: inline-block is being ignored in all browsers.

Because .btn is a flex item, its display property is controlled by the flex container. There's no point in using the display property on flex items.

Since you're in a column-direction flex container, just use align-self: flex-start.

.btn {
  /* display: inline-block; */
  align-self: flex-start /* new */
  position: relative;
  background: black;
  color: white;
  padding: 10px 40px 20px 15px;
  text-decoration: none;
  border: 1px solid black;
  margin: auto auto 10px 0px;
}

Revised Fiddle