CSS Flexbox V/S Grid
display
The display
CSS
property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layout, grid or flex.
Formally, the display
property sets an element's inner and outer display types. The outer type sets an element's participation in flow layout and the inner type sets the layout of children.
Float
We can position elements using the float property. The float property is pretty versatile and can be used in a number of different ways.
An <img>
element floated to the side of a few paragraphs of text, for example, will allow the paragraphs to wrap around the image as necessary.
In page layout programs, the boxes that hold the text can be told to honor the text wrap, or to ignore it. Ignoring the text wrap will allow the words to flow right over the image like it wasn’t even there. This is the difference between that image being part of the flow of the page (or not). Web design is very similar.
In web design, page elements with the CSS float property applied to them are just like the images in the print layout where the text flows around them.
Essentially, the float
property allows us to take an element, remove it from the normal flow of a page (taking it out of the flow in relation to other block elements), and position it to the left or right of its parent element. All other elements on the page will then flow around the floated element. This is distinctly different than page elements that use absolute positioning. Absolutely positioned page elements are removed rom the flow of the webpage, like when the text box in the print layout was told to ignore the page wrap. Absolutely positioned page elements will not affect the position of other elements and other elements will not affect them, whether they touch each other or not.
Generally, a floated element should have an explicitly set width (unless it is an image). This ensures that the float behaves as expected and helps to avoid issues in certain browsers. In order to achieve a design layout for browsers that don’t understand flexbox or grid, we use float. It's used on block elements i.e. which have display: block.
!Floats May Change an Element’s Display Value
When floating an element, it is also important to recognize that an element is removed from the normal flow of a page, and that may change an element’s default display
value. The float
property relies on an element having a display
value of block
, and may alter an element’s default display
value if it is not already displayed as a block-level element.
For example, an element with a display
value of inline
, such as the <span>
, ignores any height
or width
property values. However, should that inline-level element be floated, its display
value will be changed to block, and it may then accept height
or width
property values.
As we float elements we must keep an eye on how their display
property values are affected.
The float
property was originally designed to allow content to wrap around images. An image could be floated, and all of the content surrounding that image could then naturally flow around it. Although this works great for images, the float
property was never actually intended to be used for layout and positioning purposes, and thus it comes with a few pitfalls.
One of those pitfalls is that occasionally the proper styles will not render on an element that it is sitting next to or is a parent element of a floated element. When an element is floated, it is taken out of the normal flow of the page, and, as a result, the styles of elements around that floated element can be negatively impacted.
Often margin
and padding
property values aren’t interpreted correctly, causing them to blend into the floated element; other properties can be affected, too.
Another pitfall is that sometimes unwanted content begins to wrap around a floated element. Removing an element from the flow of the document allows all the elements around the floated element to wrap and consume any available space around the floated element, which is often undesired.
Floats are also helpful for layout in smaller instances. Take for example this little area of a web page. If we use float for our little avatar image, when that image changes size the text in the box will reflow to accommodate:
Problems with Floats
Floats often get beat on for being fragile. The majority of this fragility comes from IE 6 and the slew of float-related bugs it has. As more and more designers are dropping support for IE 6, you may not care, but for the folks that do care here is a quick rundown.
- Pushdown is a symptom of an element inside a floated item being wider than the float itself (typically an image). Most browsers will render the image outside the float, but not have the part sticking out affect other layout. IE will expand the float to contain the image, often drastically affecting layout. A common example is an image sticking out of the main content push the sidebar down below.
Quick fix: Make sure you don’t have any images that do this, use overflow: hidden
to cut off excess.
- Double Margin Bug – Another thing to remember when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin. Quick fix: set
display: inline
on the float, and don’t worry it will remain a block-level element. - The 3px Jog is when text that is up next to a floated element is mysteriously kicked away by 3px like a weird forcefield around the float. Quick fix: set a width or height on the affected text.
- In IE 7, the Bottom Margin Bug is when if a floated parent has floated children inside it, bottom margin on those children is ignored by the parent. Quick fix: using bottom padding on the parent instead.
Flex
The Flexible Box Module, usually referred to as flexbox, was designed as a one-dimensional layout model, and as a method that could offer space distribution between items in an interface and powerful alignment capabilities. When we describe flexbox as being one dimensional we are describing the fact that flexbox deals with layout in one dimension at a time — either as a row or as a column. This can be contrasted with the two-dimensional model of CSS Grid Layout, which controls columns and rows together.
The flex container
An area of a document laid out using flexbox is called a flex container. To create a flex container, we set the value of the area's container's display
property to flex
or inline-flex
. As soon as we do this the direct children of that container become flex items. As with all properties in CSS, some initial values are defined, so when creating a flex container all of the contained flex items will behave in the following way.
- Items display in a row (the
flex-direction
property's default isro
w) - The items start from the start edge of the main axis.
- The items do not stretch on the main dimension, but can shrink.
- The items will stretch to fill the size of the cross axis.
- The flex-basis property is set to zero
- The flex-wrap property is set to
nowrap
A key feature of flexbox is the ability to align and justify items on the main- and cross-axes, and to distribute space between flex items. Note that these properties are to be set on the flex container, not on the items themselves.
The
align-items
property will align the items on the cross axis.
The
justify-content
property is used to align the items on the main axis, the direction in which flex-direction has set the flow.
The
justify-items
property is ignored in flexbox layouts.
With regard to flex items,
if an item was floated or cleared and then becomes a flex item due to the parent having display: flex applied, the floating and clearing will no longer happen
, and the item will not be taken out of normal flow in the way that floats are. If you have used the
vertical-align
property
, as used with inline-block or table layout for alignment, this will no longer affect the item and you can use the alignment properties of flexbox instead.
Grid
A grid is a set of intersecting horizontal and vertical lines defining columns and rows. Elements can be placed onto the grid within these column and row lines.
In Grid Layout you do the majority of sizing specification on the container, setting up tracks and then placing items into them. In flexbox, while you create a flex container and set the direction at that level, any control over item sizing needs to happen on the items themselves.
You should consider using flexbox when:
- You have a small design to implement: Flexbox is ideal when you have a small layout design to implement, with a few rows or a few columns
- You need to align elements: Flexbox is perfect for that, the only thing we should do is create a flex container using
display: flex
and then define the flex-direction that we want - You need a content-first design : Flexbox is the ideal layout system to create web pages if you don’t know exactly how your content is going to look, so if you want everything just to fit in, flexbox is perfect for that
CSS grid is better when:
- You have a complex design to implement: In some use cases, we have complex designs to implement, and that’s when the magic of CSS grid shows itself. The two-dimensional layout system here is perfect to create a complex design, we can use it in our favor to create more complex and maintainable web pages
- You need to have a gap between block elements: Another thing that’s very helpful in CSS grid, that we don’t have in Flexbox, is the gap property. We can define the gap between our rows or columns very easily, without having to use the margin property, which can cause some side effects especially if we’re working with many breakpoints
- You need to overlap elements: Overlap elements using CSS grid is very easy, you just need to use the grid-column and grid-row properties and you can have overlapping elements very easily. On the other hand, with flexbox we still need to use some hacks such as margins, transforms, or absolute positioning
- You need a layout-first design: When you already have your layout design structure, it’s easier to build with CSS grid, and the two-dimensional layout system helps us a lot when we’re able to use rows and columns together, and position the elements the way we want
Before you decide which one you should use, don’t forget:
CSS grid is for layout, flexbox is for alignment
The CSS float property controls the positioning and formatting of content on the page. Its most common use is to wrap text around images. However, you can use the float property to wrap any inline elements around a defined HTML element, including lists, paragraphs, divs, spans, tables, iframes, and blockquotes.
Bibliography:
https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/