Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent elements from stretching to 100% width of their flex container

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>
like image 263
Raphael Rafatpanah Avatar asked Jul 26 '16 13:07

Raphael Rafatpanah


1 Answers

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

Because .btn is a flex item, its display property is controlled by the flex container. Therefore, there's no reason to use the display property on flex items.

Since you're in a column-direction flex container, use align-self: flex-start for the layout to work in IE.

.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

Important Clarification

There is a problem with IE, but it's not the display property.

Your .btn element is already aligned with flex auto margins:

margin: auto auto 10px 0px;

The margin-right: auto component is what shifts the button to the left in most browsers. This should be all you need.

But IE11 doesn't respect this rule. That's why align-self: flex-start is needed as a fallback.

like image 114
Michael Benjamin Avatar answered Sep 20 '22 20:09

Michael Benjamin