
Short answer: flex-shrink and flex-basis properties are most likely the solutions you are looking for.
Detailed answers
Suppose you want the image and text to be arranged side by side like this:
Now, let's say you use Flexbox to achieve this. Setting the parent element to display: flex; is a good start.
<code>.container { display: flex; }</code>
turn out……
Oops! OK, I think this is acceptable. It makes sense that the image is close to the text, because we do not set the width of the image. Ideally, we want the image to have a fixed width, and the text then takes up the remaining space.
OK, let's do it!
<code>.container { display: flex; } img { width: 50px; margin-right: 20px; }</code>
This looks great in Chrome. But wait, what? If we check the image tag in Firefox DevTools, we will find that it is not the width value we set at all:
We can use min-width to force the image to reach the 50px width we want:
<code>img { min-width: 50px; margin-right: 20px; }</code>
However, this will only help set the width, so we have to add a margin as well.
<code>img { min-width: 50px; margin-right: 20px; }</code>
That's it. Better in Firefox, and still works in Chrome.
More detailed answers
I realized that the image is compressed because we need to use flex-shrink property to tell Flex items not to reduce size, regardless of whether they have width or not.
flex-shrink value of all Flex projects is 1. We need to set the image element to 0:
<code>.container { display: flex; } img { width: 50px; margin-right: 20px; flex-shrink: 0; }</code>
Better and better! But we can still make more improvements.
Final answer
We can organize it further because flex-shrink is included in flex abbreviation property.
<code>flex: none | [ ? || ]</code>
If we set the flex-shrink value to 0 and set flex-basis value to the default width we want the image to have, then we can get rid of width attribute completely.
<code>.container { display: flex; } img { flex: 0 0 50px; margin-right: 20px; }</code>
Oh, yeah:
Another example
flex-shrink property solves a lot of other problems, and it is very important if you want to start using Flexbox. Here is another example of why: I stumbled upon another question similar to the above, which I mentioned in my recent newsletter. I'm building a navigation component that allows the user to scroll multiple projects left and right. While checking my work, I noticed the following issues:
Longer navigation items shouldn't be wrapping like that - but I finally got why this happens, thanks to previous questions. If you set flex-shrink property to 0, it will tell each item in this navigation not to shrink, but to take the width of the content, as follows:
Yes, we can take the extra step again to use the flex attribute, this time using auto as flex-basis because we want to consider as much space as possible for all items when allocating space in the navigation container.
Long live! We solved it. Although the answer is just a line of code, it is crucial to creating truly flexible elements.
The above is the detailed content of Making width and flexible items play nice together. For more information, please follow other related articles on the PHP Chinese website!
A Little Reminder That Pseudo Elements are Children, Kinda.Apr 19, 2025 am 11:39 AMHere's a container with some child elements:
Menus with 'Dynamic Hit Areas'Apr 19, 2025 am 11:37 AMFlyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should
Improving Video Accessibility with WebVTTApr 19, 2025 am 11:27 AM"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee
Weekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteApr 19, 2025 am 11:25 AMIn this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.
Making width and flexible items play nice togetherApr 19, 2025 am 11:23 AMThe short answer: flex-shrink and flex-basis are probably what you’re lookin’ for.
Weekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryApr 19, 2025 am 11:18 AMIn this week's look around the world of web platform news, Google Search Console makes it easier to view crawled markup, we learn that custom properties
IndieWeb and WebmentionsApr 19, 2025 am 11:16 AMThe IndieWeb is a thing! They've got a conference coming up and everything. The New Yorker is even writing about it:


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software








