HTML Tutorials

Overview

There are many ways to do HTML layouts that are both semantic and able to accommodate varying screen sizes. For instance, for beginner’s the CSS Float and Flexbox layouts are simple to setup and easy to work with.

CSS Float Layout

It is common to do entire web layouts using the CSS float and clear properties. It’s easy to learn and works well for the most part.

Disadvantages: Floating elements are tied to the document flow, which can make things difficult to work with when accommodating a wide variety of screen sizes.

<!DOCTYPE HTML>
<html lang="en-US">

<head>

    <meta charset="UTF-8">
    <meta name="description" content="Free Web Tutorials">
    <meta name="keywords" content="HTML, CSS, JavaScript, PHP, SQL">
    <meta name="author" content="1SMARTchicken">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>CSS Float Layout</title>


    <!-- styles -->
    <style>
        * {
            box-sizing: border-box;
        }
        header {
            background-color: #2d2d2d;
            padding: 30px;
            text-align: center;
            font-size: 36px;
            color: #ffffff;
        }
        nav {
            float: left;
            width: 25%;
            background: #bababa;
            padding: 20px;
            min-height: 300px;
        }
        nav ul {
            list-style-type: none;
            padding: 0;
        }
        article {
            float: left;
            padding: 20px;
            width: 75%;
            background-color: #f1f1f1;
            min-height: 300px;
        }
        section::after {
            content: "";
            display: table;
            clear: both;
        }
        footer {
            background-color: #2d2d2d;
            padding: 10px;
            text-align: center;
            color: #ffffff;
        }
        @media (max-width: 600px) {
            nav,
            article {
                width: 100%;
                height: auto;
                min-height: 0;
            }
        }
    </style>

</head>


<body>

    <header>
        <h1>CSS Float Layout</h1>
    </header>

    <section>
        <nav>
            <ul>
                <li><a href="#">Lamborghini</a></li>
                <li><a href="#">Ferrari</a></li>
                <li><a href="#">Maserati</a></li>
            </ul>
        </nav>

        <article>
            <h2>Exotic Sports Cars</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit sapiente eveniet cupiditate non
                repudiandae in accusantium, possimus blanditiis deserunt at. Velit quo veritatis ullam? Delectus nam
                ullam facere molestiae iste!</p>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit sapiente eveniet cupiditate non
                repudiandae in accusantium, possimus blanditiis deserunt at. Velit quo veritatis ullam? Delectus nam
                ullam facere molestiae iste!</p>
        </article>
    </section>

    <footer>
        <p>&copy; Copyright. All Rights Reserved.</p>
    </footer>

</body>

</html>

CSS Flexbox Layout

Flexbox is only slightly more difficult to understand and learn than CSS float. But when used correctly, it allows for flexible layouts that easily accommodate all screen sizes without the extra work CSS float requires.

Notice that the structure of the page is exactly the same, but the CSS changes slightly using the flex properties to do all the work.

<!DOCTYPE HTML>
<html lang="en-US">

<head>

    <meta charset="UTF-8">
    <meta name="description" content="Free Web Tutorials">
    <meta name="keywords" content="HTML, CSS, JavaScript, PHP, SQL">
    <meta name="author" content="1SMARTchicken">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>CSS Flexbox Layout</title>


    <!-- styles -->
    <style>
        * {
            box-sizing: border-box;
        }
        header {
            background-color: #2d2d2d;
            padding: 30px;
            text-align: center;
            font-size: 36px;
            color: #ffffff;
        }
        section {
            display: flex;
        }
        nav {
            flex: 1;
            background: #bababa;
            padding: 20px;
        }
        nav ul {
            list-style-type: none;
            padding: 0;
        }
        article {
            flex: 3;
            padding: 20px;
            background-color: #f1f1f1;
        }
        footer {
            background-color: #2d2d2d;
            padding: 10px;
            text-align: center;
            color: #ffffff;
        }
        @media (max-width: 600px) {
            section {
                flex-direction: column;
            }
        }
    </style>

</head>


<body>

    <header>
        <h1>CSS Float Layout</h1>
    </header>

    <section>
        <nav>
            <ul>
                <li><a href="#">Lamborghini</a></li>
                <li><a href="#">Ferrari</a></li>
                <li><a href="#">Maserati</a></li>
            </ul>
        </nav>

        <article>
            <h2>Exotic Sports Cars</h2>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit sapiente eveniet cupiditate non
                repudiandae in accusantium, possimus blanditiis deserunt at. Velit quo veritatis ullam? Delectus nam
                ullam facere molestiae iste!</p>
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit sapiente eveniet cupiditate non
                repudiandae in accusantium, possimus blanditiis deserunt at. Velit quo veritatis ullam? Delectus nam
                ullam facere molestiae iste!</p>
        </article>
    </section>

    <footer>
        <p>&copy; Copyright. All Rights Reserved.</p>
    </footer>

</body>

</html>

HTML Notes:

  • In our HTML section the term “tag” and “element” are often used interchangeably to refer to both the tag used to create a page element and the element created by the tag (<p> tag = <p> element = paragraph on the page)
  • HTML5 is not case sensitive; so <P> is the same as <p>, <H1> is the same as <h1>
  • Global attributes can be used with all HTML tags and are therefore not mentioned on every tag page
  • To write clean, readable HTML code, it is best to use indentation whereas elements within elements are indented (tabbed or spaces) to create something that looks like a project outline
  • The browser will automatically remove any extra spaces and lines in your HTML code when the page is displayed
  • Double quotes or single quotes can be used around HTML attribute values, but when the attribute value itself contains one form of quote, it will be necessary to use the other around the attribute

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.