A Simple Guide to Mastering the Flexbox Columns Concept

After exploring flexbox for some time and finally grasping its potential, I wanted to share an example showcasing how simple it is to create flexible column layouts without the need for CSS hacks involving excessive margins and paddings.

I understand that some individuals may not be fond of flexbox for valid reasons, but this post aims to provide a clear explanation and a practical example for those looking to understand its basics at first glance...

Let's start with some HTML:

<div class="header">100 setup</div>
   <div class="flexcontainer">
   <div class="flexbox flex100">100</div>
</div>

<div class="header">75/25 setup</div>
<div class="flexcontainer">
   <div class="flexbox flex75">75</div>
   <div class="flexbox flex25">25</div>
</div>

<div class="header">50/50 setup</div>
<div class="flexcontainer">
   <div class="flexbox flex50">50</div>
   <div class="flexbox flex50">50</div>
</div>

<div class="header">33/33/33 setup</div>
<div class="flexcontainer">
   <div class="flexbox flex33">33</div>
   <div class="flexbox flex33">33</div>
   <div class="flexbox flex33">33</div>
</div>

<div class="header">20/20/20/20 setup</div>
<div class="flexcontainer">
   <div class="flexbox flex20">20</div>
   <div class="flexbox flex20">20</div>
   <div class="flexbox flex20">20</div>
   <div class="flexbox flex20">20</div>
</div>

Next comes the CSS:

.flexcontainer {
   display: -webkit-flex;
   display: flex;
   -webkit-flex-direction: row;
   flex-direction: row;
   border:1px solid #3B3B3B;
   background-color: #919191;
   width:100%;
   min-height:100px;
}
.flexbox {
   margin:5px;
   border:1px solid #F2F2F2;
   background-color:#E3E3E3;
   padding:5px;
}
.header {
   font-size:0.75em;
   font-family:verdana;
   width:100%;
   display:block;
   line-height:20px;
}
.flex20 {
   -webkit-flex: 4 0 0;
   flex: 4 0 0;
}
.flex25 {
   -webkit-flex: 1 0 0;
   flex: 1 0 0;
}
.flex33 {
   -webkit-flex:3 0 0;
   flex: 3 0 0;
}
.flex50 {
   -webkit-flex:1 0 0;
   flex: 1 0 0;
}
.flex75 {
   -webkit-flex: 2 0 0;
   flex: 2 0 0;
}
.flex100 {
   -webkit-flex: 1 0 0;
   flex: 1 0 0;
}

Feel free to check out this JSFiddle link for a live preview and testing experience

Answer №1

Recently, I delved into the world of using the flex property and found it to be incredibly useful. In fact, I've integrated it heavily into the last trio of websites I developed. Working in an agency that prioritizes creating fully responsive websites, the flex property has become a staple tool for me.

It's important to note that certain browsers require a prefix such as -webkit- as demonstrated below:

display: -webkit-box; /* Older versions of iOS and Safari */
display: -moz-box; /* Older versions of Firefox */
display: -ms-flexbox; /* Internet Explorer 10 */
display: -webkit-flex; /* Chrome */
display: flex; /* Opera and modern versions of Firefox */

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Kendo Dropdownlist mysteriously disappears behind the PDFViewer when using Safari

Currently, I am immersed in a project based on the MVC framework and utilizing the Kendo DropDownList. An problem has arisen, which is visualized in the image below. Specifically, the items of the Kendo DropDownList are becoming obscured by the PDFViewer ...

The browser is struggling to interpret the incorrect HTML content

I am looking for a way to create a function that can retrieve account data from my database and organize it in a user-friendly HTML table where users can make selections and modifications. The code I have so far is as follows: $.ajax({ url: "./dat ...

The Challenge of Referencing Javascript Files (including jQuery)

Previously, I had the following code snippet: <head> <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script type="text/javascript"> var optPrompt = "- Select One -"; var subCats ...

Is it feasible to share HTML5 media captures and display them in real-time on another page within the website?

Recently, I just learned about the html5 media capture API and I am excited to start experimenting with it. One thing that caught my attention is capturing the camera on the same page, but I haven't come across any information about streaming through ...

Showing information from a table for editing purposes with the use of HTML input tags

As I navigate my way through the complexities of HTML and PHP coding, I’m faced with a challenge in displaying database content on an editable table within my web page. Clicking the edit button should lead to a separate page where data can be modified. ...

Tips for highlighting text in HTML without using a specific tag

My current project involves customizing a Wordpress plugin, but I've encountered a frustrating obstacle. I'm having trouble removing the three dots that appear at the bottom of the download card. Despite my efforts, I can't seem to find any ...

Ways to select the initial 10 rows, final 3 rows, and middle 2 rows using Javascript

As a newcomer to Javascript, I have a couple of questions: 1) How can I retrieve the first 10 rows, the last 3 rows, and the 2 rows in the center using the code var firstTable = $('#aaa table').eq(0); 2) Is there a way to create and assign new ...

What could be the reason for Selenium refusing to accept dynamic variables in this specific function?

What is the reason for the difference in behavior between these two code snippets? monday_div = driver.find_element(By.XPATH, '//*[@id="GXPMonday"]') And why does this one not work as expected? weekdays = ['Monday', 'Tue ...

Manipulate the lines in an HTML map and showcase the distinctions between them

I've been searching through various inquiries on this particular subject, but none have provided me with a satisfactory response. I have created a map where I've set up 4 axes using the following code: function axis() { var bounds = ...

Troubleshooting a navigation issue in Angular involving the mat-sidenav element of material UI while transitioning between components

I would greatly appreciate your assistance in resolving the issue I am facing. I have been trying to navigate from one view to another, but when I use an <a> tag nested within a <mat-sidenav>, I encounter the following error: "Uncaught (in prom ...

Leveraging JavaScript Functions in HTML

I am having an issue with a JavaScript file containing two similar functions that are executed through an HTML form. While the first function runs smoothly, the second function does not display correctly. It seems like I might be calling or executing the ...

What could be the reason behind the lack of vertical centering for the Spartan

I can't seem to figure out why my font isn't aligning vertically correctly. I'm using the Spartan MB font from Google, but it just doesn't look right, you can see here. https://i.stack.imgur.com/wbLc2.png This is my HTML code: <htm ...

Create an excel spreadsheet using HTML tables with the help of jQuery

Upon clicking a button, I aim to generate an excel sheet. Essentially, I am trying to achieve what is being discussed here (Kalle H. Väravas answer). If the following links are not working within Mozilla browser, then maybe my code requires ActiveXObject ...

Dynamically Loading CSS files in a JQuery plugin using a Conditional Test

I'm trying to figure out the optimal way to dynamically load certain files based on specific conditions. Currently, I am loading three CSS files and two javascript files like this: <link href="core.min.css" rel="stylesheet" type="text/css"> & ...

Display an HTML tag with JavaScript

My code is in both HTML and TS files. The content stored in the Description variable looks like this: <div>aaaa</div><div>bbbb</div><div>cccc</div> Currently, the output displays as follows: aaaabbbbcccc I want to modi ...

What is the best way to make the children of a parent div focusable without including the grandchildren divs in the focus?

I want to ensure that only the children of the main div are able to receive focus, not the grandchildren. Here is an example: <div class="parent" > <div class="child1" > <!-- should be focused--> <div class="g ...

Can we expect to receive multiple returns?

I have a piece of code and I am looking to add 2 returns to it. What is the correct way to achieve this in PHP? function two_tables($atts, $content = null) { return '<div id="table1"><table class="table table-bordered">'.$co ...

What is the reason behind receiving a CSS warning stating "Expected color but found '0' " when using Phaser's setText() function?

Here's how I created my text object: statusBarHP.text = game.add.text(0, 0, '', { font:"16px Arial", fill:"#ffffff", stroke:"#000000", strokeThickness:2 }); For the object that holds the text: statusBarHP = game.add.sprite ...

Animate an image to the right when clicked, then return it to the left with a second click

Seeking help with animating a set of images to move individually 300px right on first click, and then 300px left when clicked again. I'm currently facing an issue where my code is not working. It could be due to A) syntax errors or B) the images not ...

"Struggling with the 2-Column Layout Glitch

I attempted to follow a tutorial for creating a 2 column layout from this person: Here is the result of my implementation: / http://jsfiddle.net/WrpA8/ The CSS: #container { width: 800px; margin: 40px auto; border: 1px solid black; } #header ...