In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In CSS, the Flexbox layout model provides a more efficient way to lay out, align, and distribute space among items in a container, even when their size is unknown or dynamic. Flexbox is designed to provide space distribution between items in an interface and powerful alignment capabilities. However, when working with Flexbox, designers and developers sometimes wonder about the absence of justify-items and justify-self properties. This article explains why these particular properties are not part of the Flexbox module.
Understanding Flexbox Basics
Before diving into why certain properties are absent, it's important to understand some of the core concepts and properties of Flexbox:
- Flex Container: The element on which
display: flexordisplay: inline-flexis applied. It becomes the flex container. - Flex Items: The direct children of the flex container.
- Main Axis and Cross Axis: The main axis is the primary axis along which flex items are laid out. It can be horizontal (default) or vertical, depending on the
flex-directionproperty. The cross axis is perpendicular to the main axis. - justify-content: This property aligns the flex items along the main axis of the container. For example,
space-between,center,flex-start, etc. - align-items: This aligns the flex items along the cross axis of the container.
- align-self: Allows the alignment of individual flex items on the cross axis, overriding the
align-itemsproperty.
Justification and Alignment in Flexbox
In Flexbox, alignment and justification properties concern themselves with the placement of flex items along the main or cross axes:
justify-contentdeals with the distribution of space between and around flex items along the main axis.align-itemsandalign-selfdeal with alignment of items along the cross axis.
Why No justify-items and justify-self?
The absence of justify-items and justify-self in Flexbox comes down to the design and intended use of the Flexbox model. The reason lies in the fact that Flexbox is primarily focused on the distribution of space in one-dimensional layouts, either along the main or cross axis.
- Redundancy: In a one-dimensional layout context, properties such as
justify-content,align-items, andalign-selfare sufficient to handle the alignment and distribution of items along both axes.justify-itemsandjustify-selfare conceptually and functionally fit into grid layout, which handles two-dimensional layouts (both rows and columns simultaneously). - Scope of Flexbox: Flexbox’s scope is managing layouts in a single dimension, either as a row or a column. Thus, the existing properties adequately provide the necessary controls without adding redundant or confusing options. Simply put,
justify-itemsandjustify-selfare not necessary within the conceptual model of Flexbox.
Table: Flexbox Properties and Functions
| Property | Axis | Description |
justify-content | Main Axis | Distributes space between and around items along the main axis. |
align-items | Cross Axis | Aligns items along the cross axis of the container. |
align-self | Cross Axis | Allows override of alignment for individual items on the cross axis. |
Conclusion
Flexbox is designed with simplicity and efficiency in mind for laying out elements in one dimension. It does not include justify-items and justify-self because it is not necessary in a Flexbox context, where justify-content, align-items, and align-self sufficiently meet the layout requirements. Understanding these limitations and functionalities helps in effectively using CSS Flexbox to create responsive and flexible layouts.
Related reading
- In Javascript / ES6, how do I wait for Python code to finish executing in a Jupyter Notebook?
- In JavaScript, how to conditionally add a member to an object?
- In Node.js, how do I "include" functions from my other files?
- In TypeScript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
- Include another HTML file in a HTML file
- Include jQuery in the JavaScript Console
- Infinite recursion in JavaScript quicksort?
- Initial state in React with Redux not working
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.