CSS - max-inline-size Property: A Beginner's Guide

Hello there, future CSS wizards! Today, we're going to dive into the exciting world of the max-inline-size property. Don't worry if you're new to this; I'll be your friendly guide through this CSS adventure. So, grab a cup of coffee (or tea, if that's your thing), and let's get started!

CSS - Max Inline Size

What is max-inline-size?

Before we jump into the nitty-gritty, let's understand what max-inline-size is all about. Imagine you're trying to fit a really long word into a small box. That's kind of what max-inline-size helps us with in CSS. It sets the maximum size of an element in the inline direction.

"But wait," you might ask, "what's the inline direction?" Great question! In English and many other languages, the inline direction is horizontal - from left to right. However, in some languages like Arabic or Hebrew, it's right to left. And in vertical writing modes, it can even be top to bottom!

Possible Values

Now, let's look at the different values we can use with max-inline-size. Here's a handy table for you:

Value Description
<length> A fixed length like 300px or 20em
<percentage> A percentage of the containing block's size
none No limit on the size (this is the default)
max-content The intrinsic preferred size
min-content The intrinsic minimum size
fit-content Uses the available space, but never less than min-content and never more than max-content

Don't worry if some of these seem confusing now. We'll explore each one with examples!

Applies To

The max-inline-size property applies to all elements except:

  • Non-replaced inline elements
  • Table rows
  • Row groups

Syntax

The basic syntax for max-inline-size is pretty straightforward:

max-inline-size: value;

Easy peasy, right? Now, let's dive into some specific examples!

CSS max-inline-size - Value

Let's start with a simple example using a fixed length:

.box {
  max-inline-size: 300px;
  background-color: lightblue;
  padding: 10px;
}
<div class="box">
  This is a box with max-inline-size set to 300px. The content will wrap if it exceeds this width.
</div>

In this example, we've set the max-inline-size to 300 pixels. This means that no matter how much content we put inside the .box, it will never stretch beyond 300px in the inline direction (which is horizontal in this case). If the content is too long, it will wrap to the next line.

CSS max-inline-size - Value

Now, let's try using a percentage value:

.container {
  width: 500px;
  border: 2px solid black;
}

.box {
  max-inline-size: 50%;
  background-color: lightgreen;
  padding: 10px;
}
<div class="container">
  <div class="box">
    This box has max-inline-size set to 50% of its container's width.
  </div>
</div>

In this case, the .box will have a maximum inline size of 50% of its container's width. So, if the container is 500px wide, the box will never exceed 250px in width.

CSS max-inline-size - Value

The max-content value is particularly interesting. It sets the maximum inline size to the preferred size of the content. Let's see it in action:

.box {
  max-inline-size: max-content;
  background-color: lightyellow;
  padding: 10px;
}
<div class="box">
  This box will expand to fit its content, but won't wrap unless forced to.
</div>

With max-content, the box will expand to fit all its content on one line, only wrapping if it's forced to (like if it hits the edge of the viewport).

CSS max-inline-size - With Vertical Text

Now, for something a bit different! Let's see how max-inline-size works with vertical text:

.vertical-box {
  writing-mode: vertical-rl;
  max-inline-size: 100px;
  background-color: lightpink;
  padding: 10px;
}
<div class="vertical-box">
  This text is vertical, and max-inline-size limits its height!
</div>

In this example, we've used writing-mode: vertical-rl to make the text vertical and right-to-left. Now, max-inline-size is actually limiting the height of the box, because in vertical writing mode, the inline direction is vertical!

Wrapping Up

And there you have it, folks! We've explored the max-inline-size property from top to bottom (or should I say, from inline start to inline end?). Remember, CSS is all about experimentation. Don't be afraid to play around with these properties and see what happens. That's how we all learned!

Before I let you go, here's a little joke to remember max-inline-size by: Why did the CSS property go to the gym? To work on its max-inline-size, of course!

Happy coding, and may your layouts always be flexible and your content always fit just right!

Credits: Image by storyset