Flexible boxes or flexbox, is the latest method for controlling layouts in CSS. Using grids that are ‘flexible’ is so easy to predict how each element behaves on different screen sizes. Its greatest feature is its ability to manipulate an item’s width/height to occupy all the space within its container (also known as a “flex container”) – otherwise, it shrinks to prevent overflow.
Unlike the regular CSS box model, Flexbox offers an enhanced model for block elements without the use of floats. And, it supports direction – which means you can control the flow of each item vertically or horizontally. Almost all browsers that are active today support it, so it’s good to know how it can be used on different kinds of projects.
To give you an idea of how amazing flexbox is, I wrote this quick tutorial to show how you can use it on your website. We’ll use flexbox properties and media queries to enable the responsive layout of each element on different screen sizes and show you the advantages of using it on your projects. Let’s get started.
If you’re new to flexbox, you might also like to take a look at these Guides, Tools & Frameworks for getting started with CSS Flexbox.
Flexbox is the perfect for creating a responsive navigation. You can lay out the navigation in one column on smaller viewports, scale it up on medium size viewports and position it in one row on large and extra-large viewports. Let’s look at how to create a navigation using flexbox.
For the HTML, we will wrap our navigation within a header tag. We will use an unordered list (ul) for our links with their respective classes. Here is the HTML:
For the CSS, we will first add the basic styles that we need:
When the viewport is 769px or wider, we will use flexbox to change the vertical layout of the navigation to a horizontal layout. That will fill the horizontal space of the large and extra-large screens and devices.
To do that we will use media queries to manipulate the layout. We will declare the display: flex to set the flexbox on all elements. We will also set the flex-direction: column to set the axis of our element from top to bottom.
On a screen size with a minimum width of 1025px, we will set the flex-direction: row with justify-content: space-between, this means that our header will be displayed horizontally instead of vertically.
Here is the final output:
Thumbnails are smaller versions of images that are organized in a particular area of a website to give you a hint of how the full-sized picture looks. Usually thumbnails are used for image galleries. With flexbox, you can create thumbnails using the row property while manipulating the display size of the image.
For our thumbnail example, we will create a div with a class of thumb that will wrap all of our thumbnail images. This will also be our flex container.
For our CSS, we will set a width for the flex container which is the thumb class. We will then set the display to flex to set the flexbox property inside it. There are three properties that we need:
flex-wrap – to set our flexible items in a single line or can be flowed into multiple lines. In this case we use wrap. This means our thumbnails will be set to the multi-lines direction.
flex-direction – to specify the direction of the flexible items. In this case we set it to columns. This means our thumbnails will be displayed from top to bottom.
flex-flow – which is a shorthand property for the flex-direction and the flex-wrap.
Next, we will set the width on our images using the box-flex property to auto. To specify a -moz-box or -webkit-box grows to fill the box that contains it. The rest of the code is simple media queries to manipulate the padding on a viewport with a maximum width of 480px.
Checkout the final output:
A form is a vital part of every website. It enables users to input data using checkboxes, radio buttons, or text fields that are sent to a server for processing. With flexbox, you can create responsive forms without depending too much on media queries.
Let’s first create our text fields and wrap them within a form tag. For our container, we will use a div with a class of form.
For our CSS, we’ll add first the basic styles:
Now to make it responsive, let’s add display:flex on the paragraph label tag so that it collapses on smaller screens. We will also add flex: 1 on our input type to enable all of the flexible items on the same length inside the same container. We will then add our media queries for screens with a maximum width of 600px and display the labels of each field as block elements as well as the button element.
Check the final output below:
A sticky footer is a footer that “sticks” to the bottom of the screen no matter how long or short the content of the web page is. While there are several ways to create one, flexbox is a great option.
For our HTML, let’s create a simple web page using the header, section and footer tags:
And now for our CSS, we will set the display: flex property to our body tag to enable flexbox. We will also set the flex-direction column to set the direction of the flex items from top to bottom, along with some basic styles.
Since we are using the section tag as our container, we will set the flex property to 1. This sets the section content to the same length regardless of its content relative to the rest of the container.
The rest are just simple styles for our elements.
Check out the demo below:
The CSS3 Flexible Box is a very helpful feature nowadays. Many web designers and developers find it easier to use on their websites than the typical CSS layout model.
The post Getting Started With CSS Flexbox Using Practical Examples appeared first on Speckyboy Design Magazine.
No related posts.