Suggestions

TLDR; Not Your Typical Privacy Agreement

Powered by Cohere

Samsung Galaxy S10e

Specifications

  • Dimensions: 142.2 x 69.9 x 7.9 mm (5.60 x 2.75 x 0.31 in)
  • Weight: 150 g (5.29 oz)
  • Display: Dynamic AMOLED, HDR10+
  • Resolution: 1080 x 2280 pixels, 19:9 ratio (~438 ppi density)
  • OS: Android 9.0 (Pie), upgradable to Android 12, One UI 4.1
  • CPU: Octa-core (2x2.73 GHz Mongoose M4 & 2x2.31 GHz Cortex-A75 & 4x1.95 GHz Cortex-A55) - EMEA/LATAM
  • Main Camera: 12 MP, f/1.5-2.4, 26mm (wide)
  • Selfie Camera: 10 MP, f/1.9, 26mm (wide), 1/3", 1.22Β΅m, dual pixel PDAF
  • Battery: Li-Ion 3100 mAh, non-removable
All Notes

CSS Layout (Display Property)

Wednesday, May 10, 2023
Author:
Share to Reddit
Share to Facebook
Share to X
Share to LinkedIn
Share to WhatsApp
Share by email
Describing the associated blog post


Why Layout Matters

A great web developer once said that "HTML is the skeleton, JavaScript is the muscle, and CSS is the skin and hair." I believe that this is a great analogy mainly because the skeleton isn't all that appealing without the skin that covers it.

CSS has many properties that serve various purposes such as adding color, stacking elements, and defining size. This article will go through CSS use cases for positioning HTML elements on a web page. Specifically, I will be demonstrating ways of using the display property.

Creating a skeleton

To start off any given project, it's important to have a prototype, mockup, or a mental image of the kind of user interface you want to build. Having some kind of blueprint helps with staying in line with the aims of the project. The HTML code needs to be semantic, this helps when the time comes to refer to class names in the CSS declarations. I previously wrote an article on ways of writing clean code which goes into detail about good naming conventions. Read it here.

For this specific example, the HTML code that goes in between the <body></body> tags is shown below. The division tag with the class of 'veggies' can be replicated many times to display another vegetable.

<div class="garden">
        <h1>Tomatoes</h1>
        <div class="veggies">
            <p>πŸ…</p>
            <p>πŸ…</p>
            <p>πŸ…</p>
            <p>πŸ…</p>
            <p>πŸ…</p>
        </div>
</div>

I'll use CSS to create a layout for a garden of emoji vegetables. Speaking of emojis, here's an article I wrote about 10 emoji and their meaning.

Flex Box Layout

The flex layout is useful in aligning several elements along an axis, it allows us to specify the position of our elements along the main axis (horizontal), and the cross axis (vertical). The garden example is perfect to demonstrate this idea because plants are usually placed in order according to rows or columns. We can simply use display: flex to place the contents of the container along the main axis which in this case is horizontal. The two axes are interchanged when we define flex-direction: column;.

.garden .veggies{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 50px 100px;
}

Output of the code:

A screenshot of a web page

Grid Layout

A similar outcome can be achieved by writing display: grid;. The Grid property creates a tabular layout that allows us to specify the number of columns as well. The code block below demonstrates this.

.garden .veggies{
    justify-content: center;
    padding: 50px 100px;
    display: grid;
    gap: 30px;
    grid-template-columns: 100px 100px 100px 100px 100px;
}

The most essential property in the above CSS declaration is grid-template-columns. This property defines the number of items we can place in a single row. Removing this property will achieve the same outcome as writing display: inline-block;.

For a more responsive layout, we can modify the grid-template-columns to accept a function called repeat(), this enables auto-fill features so that the layout changes depending on the size of the device. This can be written as:

    grid-template-columns: repeat(auto-fill, 100px);

Below are screenshots of the result of using the repeat() function and auto-fill on different device sizes.

iPhone SE: Screenshot of an iPhone SE

iPad Mini: Screenshot of an iPad Mini

Surface Duo: Screenshot of an iPad Mini

Nest Hub: Screenshot of an iPad Mini

This is a good example of responsive web design because we don't have to do the tedious task of writing media queries for each and every device screen width in the world😑.

Table Layout

The table layout is similar to writing HTML markup using the <table> tag and its various descendants. You could either write display:inline-table; or display: table; which both produce a different result for aligning the elements.

Wrapping Up

My preference is display: flex; because of all the supporting properties that it comes with. I've discovered that it works very well for aligning content vertically and horizontally. I know everyone has their favorite tool to use and there really is no right or wrong way to do it, the main aim is to ensure that the UI mockups are implemented on a web page.

If you read this far I'd like to recommend that you read another article I wrote about the Hypertext Markup Language. If you enjoyed reading this, feel free to share your feedback using the contact section of my website.

~ Thank You For Reading ✌🏾

Tawanda Andrew Msengezi

Tawanda Andrew Msengezi is a Software Engineer and Technical Writer who writes all the articles on this blog. He has a Bachelor of Science in Computer Information Systems from Near East University. He is an expert in all things web development with a specific focus on frontend development. This blog contains articles about HTML, CSS, JavaScript and various other tech related content.

User Notice

Dear Visitor,

This website stores your color theme preference, you can toggle your theme preference using the lightbulb icon in the top right of the webpage.

Clicking on the robot icon that says "Chat" in the bottom-left corner will open a chat with an AI assistant. Click the button below to close this message.