At the end of this Tutorial you will be able to:
You can view a finished version of the sample two-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.
Your sample web pages and files
Working with the two-column web page
Setting up the flexbox child columns
Setting up the flexbox parent elements
Adding coloured backgrounds to parent containers
Changing the text colours inside a parent element
Adding a coloured background to a child column
Changing the text colours inside a child column
About child columns and padding
Adding padding to a child column
Your first step is to download the files you need for this Tutorial.
You are now ready to work with the sample files you have downloaded.
In this Tutorial, you will work with the sample two-column web page, the sample two-column stylesheet, and two of the image files you have downloaded.
/* ========== 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 three container-block parent elements.
Your next task is to set the width of the .item-col-2 child columns to create your two-column layout on desktop and laptop screens.
/* Desktops: flexbox child columns */ @media (min-width:768px) { .item-col-2 { width: 47% } }
As you can see:
For the web browser to display the item-col-2 child columns at your set width of 47%, 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 two-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 two-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.
Note the two-levels of parent-child relationships here:
In your flex-two-columns.css stylesheet, your could ‘target’ the text content of the child columns by entering the following:
.container-block.container-flexbox.bg-dark .item-col-2 h3 { color: #fff } .container-block.container-flexbox.bg-dark .item-col-2 p { color: #fff }
However, for this web page, it would be enough to type the following:
.container-block.bg-dark .item-col-2 h3 { color: #fff } .container-block.bg-dark .item-col-2 p { color: #fff }
Or, alternatively:
.container-flexbox.bg-dark .item-col-2 h3 { color: #fff } .container-flexbox.bg-dark .item-col-2 p { color: #fff }
Either will work.
But what if your later added to the item-col-2 child columns other text elements with different HTML tags – such as a <h1>, <h2> or <h4>?
The ideal solution is to use the universal or ‘wildcard’ CSS selector of * to target all text inside item-col-2 child columns – regardless of their particular HTML tag.
Here are the steps:
/* Colours for sub-headings and paragraphs */
Add the following code under it:
.container-flexbox.bg-dark .item-col-2 * { color: #fff }
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-2 child column inside one of the three 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-2 * { color: #fff }
/* Colours for sub-headings and paragraphs */ .container-flexbox.bg-dark .item-col-2 * { color: #fff } .container-flexbox .item-col-2.bg-dark * { color: #fff }This new style rule with the ‘wildcard’ CSS selector of * will target all text inside item-col-2 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.
As a general rule, you will always want to add padding to a child column that has a coloured background.
So it makes sense to develop a general solution that will work for all multi-column layouts – whether they have two, three, four or even more columns.
You can achieve this by following these two approaches:
The next exercise provides an example of these two approaches in action.
In these next steps, you will apply padding to a particular child column of your layout.
/* Desktops: inner padding for child columns */ @media all and (min-width:768px) { [class*="item-col-"].item-col-padding { padding: 1.8% 2% 2.2% 2% } } /* Mobiles: inner padding for child columns */ @media all and (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-3 and item-col-4.
Finally, here is an example of a two-column flexbox layout used to create a so-called hero block.
<div class="container-block container-flexbox bg-hero"> <div class="item-col-2"> <h1>Hook Head</h1> <h2>Guiding ships <br>for 800 years.</h2> </div> </div>Save your web page.
/* =============== HERO BLOCK ================= */ .bg-hero { display: flex; flex-direction: column; justify-content: center; background-image: url('hook-head.jpg'); background-position: center center; background-size: cover; background-repeat: no-repeat; height: 510px; } .bg-hero h1, .bg-hero h2 { letter-spacing: -2px; color: #fff; font-weight: bold; text-shadow: 2px 2px #222 } .bg-hero h1 { font-size: calc(80px + (100 - 80) * ((100vw - 320px) /(1600 - 320))); } .bg-hero h2 { font-size: calc(36px + (56 - 36) * ((100vw - 320px) /(1600 - 320))); } /* Desktop */ @media all and (min-width:768px) { .bg-hero h1 { margin-bottom: 1% } } /* Mobiles */ @media all and (max-width:767px) { .bg-hero { background-position: right center; height: 420px } .bg-hero h1, .bg-hero h2 { text-align: center } .bg-hero h1 { margin-bottom: 10% } }
On desktop/laptop and on mobile-sized screens, your web page should now look as shown below.
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-two-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-two-columns.html">Two 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-two-columns.html
It may take a few minutes for your uploaded files to appear on GitHub.
Return to Contents.