• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

The “fit-content” CSS Style Rule Is Amazing!

Bhagwad Park

Published on: November 1, 2021

Categories: WordPress 0

One of the downsides of no longer using sidebars is that you lose space to put stuff like “Related Posts”. But I still stick with my decision to do away with sidebars on my site – mostly because when it comes to long-form content, the sidebars vanish quickly anyway. For lasting benefits, you’ll have to make them “sticky”, and that has its own share of problems. So now I implement “Related Posts” in the body of my site. And recently, I found a great CSS property “fit-content” to make it look the way I want.

For some reason, there’s not too much discussion of the “fit-content” CSS style. Chrome has supported it for a while, and Firefox supports its own “moz” version which I’ll discuss below. But first, here’s why I think the fit-content rule is needed.

How Blocks Look without “fit-content”

Let’s say I have a related posts block on my site. In this example, the article is about the NameHero reseller service, so I have a section on my site WP-Tweaks with posts related to the content. Here’s what it looks like without any layout considerations:

Box Extending on the Right
Box Extending on the Right

As you can see, the box extends to the right. Ideally, I want it to wrap around the content, and nothing more. For now, I don’t want to float it because the content underneath is a table of contents, and it wouldn’t make sense. So I just want the box to fit the maximum content – nothing more. We have a CSS style called max-content which seems to do get the job done.

max-content Doesn’t Work

But here’s what “max-content” looks like when the text is large and the screen is small:

Text Gets Cut off with Max-Content
Text Gets Cut off with Max-Content

In this example, I’ve artificially elongated the text of the mainline. You can see that with “max-content”, the element doesn’t wrap as the screen shrinks. And because of that, it gets cut off. If you look closely, you can also see that the right-hand borders are not rounded. Meaning the box isn’t resizing. It’s visually unacceptable.

“Min-content” has a similar problem, but in this case, it looks even worse.

Setting a Width Percentage Won’t Work Either

What if I just set the width to 50%? It seems to work fine on large screens, but begins it look ridiculous on small screens:

Width Percentage Doesn't Work Either
Width Percentage Doesn’t Work Either

Why None of these Solutions Work

What we want, is for the browser to somehow make the content fit the available space, and then draw the box around it. This would seem to contradict the paradigm of working from the top to the bottom – namely, determining the size of the box first, and then the contents.

Luckily, we have a rule that can save us.

“fit-content” Gets the Job Done

Here’s what happens when I set the width of the box to “fit-content”:

Text is Wrapping and the Border Remains Intact
Text is Wrapping and the Border Remains Intact

As you can see, the elongated text wraps around to fit the screen, and the box fits the content perfectly. The borders are rounded, which means the box has shrunk. Perfect! This is the only CSS rule which does this, and I’ll be using it for a lot of blocks going forward.

Special Considerations for Firefox

Firefox has its own version of “fit-content” called “-moz-fit-content”. You can use the two together just to cover all use-cases like this:

width: fit-content;
width: -moz-fit-content;

Chrome will use the “fit-content” rule and Firefox will use the “-moz-fit-content” rule. There’s an action item on bugzilla.mozilla.org that’s asking for the prefix “-moz-” to be removed, and it looks like it’s slated for release with Firefox version 94. As I write this, my version of Firefox is version 93, so it looks like it might be coming soon! Till then, we can use the above CSS rules, and it’ll work just fine.

I hope you find “fit-content” as useful as I have!

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

How To Start A Newsletter In WordPress

Check out our complete guide on why and how you should start your own newsletter in WordPress.

What Is Self-Hosted WordPress?

In this post, we’ll differentiate self-hosted WordPress from WordPress.com, and discuss the benefits of the former.

Blogger vs. WordPress: Which One to Use?

Let's compare WordPress and Blogger across five key areas. Then, we'll show you how to set up a professional blog with WordPress.

Joomla vs. WordPress: Which One Is Better?

In this post, we’ll consider five differences between Joomla and WordPress and how you might go forward with setting up a WordPress website.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA