At the end of this Tutorial you will be able to:
You can view a finished version of the sample four-column web page you style in this Tutorial by clicking the link below. The finished sample page will open in a new tab of your web browser.
Working with the four-column web page
Setting up the flexbox child columns
Setting up the flexbox parent elements
Adding coloured backgrounds to a parent container
Changing the text colours inside a parent element
Adding a coloured background to a child column
Changing the text colours inside a child column
Adding padding to a child column
In this Tutorial, you will work with the sample four-column web page, the sample four-column stylesheet and four of the image files you downloaded in the previous CSS Flexbox: Two Column Layouts Tutorial.
/* ========== MAIN CONTENT CONTAINERS ========= */ /* Desktops */ @media (min-width:768px) { .container-block { padding: 4% 8% } } /* Mobiles */ @media (max-width:767px) { .container-block { padding: 11% 8% } }
On desktop/laptop and on mobile-sized screens, you should now see spacing inside the red-coloured borders of the two container-block parent elements.
Your next task is to set the width of the .item-col-4 child columns to create your four-column layout on desktop and laptop screens.
/* Desktops: flexbox child columns */ @media (min-width:768px) { .item-col-4 { width: 22% } }
As you can see:
For the web browser to display the item-col-4 child columns at your set width of 22%, you need to make their parent container, named container-flexbox, a flexbox element.
/* Flexbox parent container */ .container-flexbox { display: flex; justify-content: space-between; flex-wrap: wrap; }
Your child elements should now display correctly in a four-column layout on desktop/laptop screens.
Use your web browser’s screen resizing feature to confirm your layout displays as a single column on mobile-sized screens.
Next, you will add coloured backgrounds to your four-column layout: first, to a parent container and all its child columns; and then to an individual child column.
Follow these steps to add coloured backgrounds to two of the three parent container elements in your web page.
On desktop/laptop and on mobile-sized screens, your web page should now look as shown below.
For the <h3> sub-headings and <p> paragraphs inside the parent container with the bg-dark background, you need to change the colour of the text.
Here are the steps:
/* Colours for sub-headings and paragraphs */
Add the following code under it:
.container-flexbox.bg-dark .item-col-4 * { color: #fff }This ‘wildcard᾿ CSS selector of * will target all text inside item-col-4 child columns – regardless of the particular HTML tag.
On desktop/laptop and on mobile-sized screens, your web page should now look as shown below.
Next, you will add a background colour to a particular item-col-4 child column inside one of the two parent containers in your web page. Follow these steps.
As you can see, you need to perform two more tasks:
For the <h3> sub-headings and <p> paragraphs inside the child column with the bg-dark background, you need to change the colour of the text.
Here are the steps:
/* Colours for sub-headings and paragraphs */ .container-flexbox.bg-dark .item-col-4 * { color: #fff }
/* Colours for sub-headings and paragraphs */ .container-flexbox.bg-dark .item-col-4 * { color: #fff } .container-flexbox .item-col-4.bg-dark * { color: #fff }This new style rule with the ‘wildcard’ CSS selector of * will target all text inside item-col-4 child columns that have the bg-dark class – regardless of their particular HTML tag.
On desktop/laptop and on mobile-sized screens, your web page should now look as shown below.
You can see that you need to add some padding to the child column with the coloured background.
In these next steps, you will apply padding to a particular child column of your layout.
/* Desktops: inner padding for child columns */ @media (min-width:768px) { [class*="item-col-"].item-col-padding { padding: 1.8% 2% 2.2% 2% } } /* Mobiles: inner padding for child columns */ @media (max-width:767px) { [class*="item-col-"].item-col-padding { padding: 6.5% 7% 7% 7% } }These updated style rules will also apply to child columns with class names such as item-col-2 and item-col-3.
You are now finished working with this sample web page and stylesheet.
So you can now remove from your stylesheet the two styles of red borders around parent containers and blue borders around child elements.
Click flex-four-columns.html to view a finished sample of this web page in a new tab of your web browser.
Now that you have created and styled a new sample web page, you need to add a new hyperlink to the home page of your web site.
<p><a href="flex-four-columns.html">Four Column Flexbox Layout</a></p>
Save your index.html web page and view the result in your browser.
After finishing your web pages and stylesheets, you are now ready to upload them to your account on GitHub.
Your web pages and stylesheets are now published on GitHub at web addresses similar to the following, where username is the username you have chosen for your GitHub account:
https://username.github.io/flex-four-columns.html
It may take a few minutes for your uploaded files to appear on GitHub.
Return to Contents.