HOME

TheInfoList



OR:

CSS Flexible Box Layout, commonly known as Flexbox, is a CSS 3 web layout model. It is in the
W3C The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web. Founded in 1994 and led by Tim Berners-Lee, the consortium is made up of member organizations that maintain full-time staff working to ...
's candidate recommendation (CR) stage. The flex layout allows responsive elements within a container to be automatically arranged depending on viewport (device screen) size.


Concepts

Most web pages are written in a combination of
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
(Hypertext Markup Language) and
CSS Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS is a cornerstone techno ...
(Cascading Style Sheets). In short, HTML specifies the ''content and logical structure of the page'', while the CSS specifies ''how it looks'': its colors, fonts, formatting, layout, and styling. CSS flex-box layout is a particular way to specify the layout of HTML pages. One of the most defining features of the flex layout is its ability to form-fit, based on its viewing environment. Flex boxes can adjust in size—either decreasing, to avoid unnecessarily monopolizing space, or increasing to make room for contents to fit within its boundaries. Moreover, the flex layout is less restrictive in terms of content flow than that of other CSS layout models, which are generally uni-directional. The flex directional flow can be specified rightwards, leftwards, upwards, or downwards. Individual items within a flex container may also be automatically rearranged to suit the available layout space.


History

In the 2000s the intensive use of the Web by mobile agents motivated "liquid layouts" and responsive elements for the growing variety of screen sizes. In the 2010s, the intensive use of popular
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
layout
frameworks A framework is a generic term commonly referring to an essential supporting structure which other things are built on top of. Framework may refer to: Computing * Application framework, used to implement the structure of an application for an op ...
, such as Bootstrap, inspired CSS flex-box and grid layout specifications. CSS 3 modules included solutions akin to this, like flexbox and
grid Grid, The Grid, or GRID may refer to: Common usage * Cattle grid or stock grid, a type of obstacle is used to prevent livestock from crossing the road * Grid reference, used to define a location on a map Arts, entertainment, and media * News g ...
. , 99.68% of installed browsers (99.59% of desktop browsers and 100% of mobile browsers) support CSS Flexible Box Layout.


Terminology

The following terms are associated with the flexbox layout model. ;Flex container :Parent element that holds all flex items. Using the CSS display property, the container can be defined as either flex or inline-flex. ;Flex item :Any direct child element held within the flex container is considered a flex item. Any text within the container element is wrapped in an unknown flex item. ;Axes :Each flex box contains two axes: the main and cross axes. The main axis is the axis on which the items align with each other. The cross axis is perpendicular to the main axis. ;Flex-direction :Establishes main axis. Possible arguments: row (default), row-reverse, column, column-reverse. ;Justify-content :Determines how content gets placed on the main axis on the current line. Optional arguments: left, right, center, space-between, space-around. ;Align-items :Determines the default for how flex items get placed on the cross axis on each line. ;Align-content :Determines the default for how cross axis lines are aligned. ;Align-self :Determines how a single item is placed along the cross axis. This overrides any defaults set by align-items.


Directions

;cross-start ;cross-end :The cross-start/cross-end sides determine where flex lines get filled with flex items from cross-start to cross-end. ;main-start ;main-end :The main-start/main-end sides determine where to start placing flex items within the flex container, starting from the main-start end and going to the main-end end. ;Order :Places elements in groups and determines which order they are to be placed in within the container. ;Flex-flow :Shorthands flex-direction and flex-wrap to place the flex content.


Lines

; Lines :Flex items can either be placed on a singular line or on multiple lines as defined by the flex-wrap property, which controls both the direction of the cross axis and how lines stack within the container.


Dimensions

; Main size ; Cross size :Main size and cross size are the height and width of the flex container, each dealing with the main and cross axes respectively.


Usage

Designating an element as a flex element requires setting the element's CSS display property to either flex or inline-flex, as follows: display: flex; Or: display: inline-flex; By setting the display to one of the two values above, an element becomes a flex container and its children, flex items. Setting the display to flex makes the container a
block-level element An HTML element is a type of HTML (HyperText Markup Language) document component, one of several types of HTML nodes (there are also text nodes, comment nodes and others). The first used version of HTML was written by Tim Berners-Lee in 1993 ...
, while setting the display to inline-flex makes the container an inline-level element.


Align to center

One of flexbox's advantages is the ability to easily align items within the container to the center of a page, both vertically and horizontally. display: flex; align-items: center; justify-content: center;


References

{{W3C Standards Web design World Wide Web Consortium standards