English 中文(简体)
Flexbox - Quick Guide
  • 时间:2024-11-03

Flexbox - Quick Guide


Previous Page Next Page  

Flexbox - Overview

Cascading Style Sheets (CSS) is a simple design language intended to simppfy the process of making web pages presentable. CSS handles the look and feel part of a web page.

Using CSS, you can control the color of the text, the style of fonts, the spacing between paragraphs, how columns are sized and laid out, what background images or colors are used, layout designs, variations in display for different devices and screen sizes as well as a variety of other effects.

To determine the position and dimensions of the boxes, in CSS, you can use one of the layout modes available −

    The block layout − This mode is used in laying out documents.

    The inpne layout − This mode is used in laying out text.

    The table layout − This mode is used in laying out tables.

    The table layout − This mode is used in positioning the elements.

All these modes are used to apgn specific elements pke documents, text, tables, etc., however, none of these provides a complete solution to lay out complex websites. Initially this is used to be done using a combination of floated elements, positioned elements, and table layout (often). But floats only allow to horizontally position the boxes.

What is Flexbox?

In addition to the above-mentioned modes, CSS3 provides another layout mode Flexible Box, commonly called as Flexbox.

Using this mode, you can easily create layouts for complex apppcations and web pages. Unpke floats, Flexbox layout gives complete control over the direction, apgnment, order, size of the boxes.

Features of Flexbox

Following are the notable features of Flexbox layout −

    Direction − You can arrange the items on a web page in any direction such as left to right, right to left, top to bottom, and bottom to top.

    Order − Using Flexbox, you can rearrange the order of the contents of a web page.

    Wrap − In case of inconsistent space for the contents of a web page (in single pne), you can wrap them to multiple pnes (both horizontally) and vertically.

    Apgnment − Using Flexbox, you can apgn the contents of the webpage with respect to their container.

    Resize − Using Flexbox, you can increase or decrease the size of the items in the page to fit in available space.

Supporting browsers

Following are the browsers that support Flexbox.

    Chrome 29+

    Firefox 28+

    Internet Explorer 11+

    Opera 17+

    Safari 6.1+

    Android 4.4+

    iOS 7.1+

Flexbox - Flex Containers

To use Flexbox in your apppcation, you need to create/define a flex container using the display property.

Usage

display: flex | inpne-flex

This property accepts two values

    flex − Generates a block level flex container.

    inpne-flex − Generates an inpne flex container box.

Now, we will see how to use the display property with examples.

Flex

On passing this value to the display property, a block level flex container will be created. It occupies the full width of the parent container (browser).

The following example demonstrates how to create a block level flex container. Here, we are creating six boxes with different colors and we have used the flex container to hold them.

<!doctype html>
<html lang = "en">
   <style>
      .box1{background:green;}
      .box2{background:blue;}
      .box3{background:red;}
      .box4{background:magenta;}
      .box5{background:yellow;}
      .box6{background:pink;}
      
      .container{
         display:flex;
      }
      .box{
         font-size:35px;
         padding:15px;
      }
   </style>
   
   <body>
      <span class = "container">
         <span class = "box box1">One</span>
         <span class = "box box2">two</span>
         <span class = "box box3">three</span>
         <span class = "box box4">four</span>
         <span class = "box box5">five</span>
         <span class = "box box6">six</span>
      </span>
   </body>
</html>

It will produce the following result −