Mastering HTML Layout: A Comprehensive Guide to HTML and CSS Techniques

Mastering HTML Layout: A Comprehensive Guide to HTML and CSS Techniques

9 mins read97 Views Comment
Esha
Esha Gupta
Associate Senior Executive
Updated on Apr 29, 2024 15:58 IST

In HTML layout and techniques, semantic and non-semantic elements work together to structure and style web content. Techniques like CSS Flexbox, CSS Grid, and media queries enhance the design, ensuring responsive and accessible websites. These foundational elements and methods make our web experiences user-friendly and engaging across various devices and screen sizes. Let’s understand more!

2023_09_Copy-of-What-is-10.jpg

An HTML Layout refers to how elements are arranged on a web page. It involves structuring content, images, links, and other elements to present information clearly and attractively to users.

Table of Content

Brief Overview of HTML Layout

HTML layout refers to the structuring and positioning of various elements on a webpage, including text, images, and other multimedia components.

Importance of HTML Layout

A proper HTML layout is important for both the user experience and the technical performance of a website.

  1. A clean, structured HTML layout ensures the content is organized and easy to follow, improving the user’s ability to find information quickly and easily.
  2. Proper layout provides clear and consistent navigation, making it easier for all users to find what they need.
  3. Search engines prefer well-structured sites with clear navigation and semantic markup, potentially improving the site’s ranking in search results.
  4. A well-organized HTML layout makes the website easier to maintain and update, saving time and resources in the long run.
  5. Efficient layout design helps in the optimal usage of resources, enhancing the site’s overall performance.

A proper HTML layout is not just about making it beautiful; it is fundamental in ensuring a website is accessible, user-friendly, SEO-optimized, and easy to maintain and update.

Basic Structure of an HTML Document


 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Basic HTML Page </title>
</head>
<body>
<h1> Hello, World! </h1>
<p> This is a basic HTML document. </p>
</body>
</html>
Copy code

This is an extremely basic structure of an HTML document.

Semantic HTML5 Elements

Semantic elements in HTML5 provide a clear and structured layout, improving the clarity, accessibility, and SEO of web documents by offering meaningful tags for the content.

Importance of Semantic Elements

  • Assistive technologies, like screen readers, use semantic elements to provide better navigation and understanding.
  • Search engines use semantic elements to improve the ranking of a webpage, as they clearly define content sections.
  • It’s easier to read and maintain code with semantic tags as they inherently describe their purpose.

List and Explanation of Semantic Elements

2023_09_Screenshot-2023-09-25-122855-1.jpg

1. <header>

Represents a container for introductory content or navigational links.

<header>
  <h1>Website Title</h1>
  <nav>
    <ul>
      <li><a href="#home">Home</a></li>
    </ul>
  </nav>
</header>

2. <nav>

Defines a section of the document intended for navigation links.

<nav>
  <ul>
    <li><a href="#about">About</a></li>
  </ul>
</nav>

3. <section>

Represents a standalone section of a document.

<section>
  <h2>Section Title</h2>
  <p>This is a section of the content.</p>
</section>

4. <article>

Represents a self-contained composition in a document, page, or site.

<article>
  <h2>Article Title</h2>
  <p>This is an independent piece of content.</p>
</article>

5. <aside>

Represents content related to the main content but can be considered separate.

<aside>
  <h2>Related Links</h2>
  <ul>
    <li><a href="#related1">Related 1</a></li>
  </ul>
</aside>

6. <footer>

Represents the footer of a document or a section, typically containing authorship, copyright information, and related documents.

<footer>
  <p>Copyright © 2023 by Website Owner.</p>
</footer>

Incorporating these semantic elements in HTML documents ensures clearer structure and better web standards adherence.

An example using all the above semantic elements


 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome to Shiksha Online</title>
<style>
/* Adding basic styling for clarity */
body {
font-family: Arial, sans-serif;
}
header, nav, article, section, aside, footer {
border: 1px solid #ddd;
margin: 10px;
padding: 20px;
}
</style>
</head>
<body>
<header>
<h1>Welcome to Shiksha Online</h1>
</header>
<nav>
<ul>
<li><a href="#courses">Courses</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
<article>
<h2>About Shiksha Online</h2>
<p>Shiksha Online is a platform for online learning.</p>
</article>
<section id="courses">
<h2>Our Courses</h2>
<p>We offer a wide range of courses for everyone.</p>
</section>
<aside>
<h2>Related Links</h2>
<ul>
<li><a href="http://www.example.com">Example Link</a></li>
</ul>
</aside>
<footer>
<p>Copyright © 2023 by Shiksha Online.</p>
</footer>
</body>
</html>
Copy code

Output

2023_09_so.gif
Concept of HTML Colors
Concept of HTML Colors
HTML Colors play a pivotal role in creating the visual identity of a webpage, influencing its aesthetics, readability, user experience, and even its brand messaging. HTML colors are a way...read more
Understanding HTML Doctype
Understanding HTML Doctype
Have you ever wondered about the importance of the HTML doctype declaration? The HTML doctype declaration is a crucial element at the beginning of an HTML document that defines its...read more
HTML Iframe (With Examples)
HTML Iframe (With Examples)
An HTML iframe is an HTML tag that allows you to embed another HTML document within a current HTML document. Importance of Using HTML iframes. Let’s see some main attributes...read more

Non-Semantic Elements

HTML non-semantic elements like <div> and <span> do not have any inherent meaning and are used as generic containers to style and organize content.

<div> and <span>

1. <div>

It’s a block-level element often used as a container for other elements.

<div id="container">
  <p>This is a block-level container element.</p>
</div>

2. <span>

It’s an inline element that applies styles or scripting to a portion of text.

<p>This is a <span style="color: blue;">colored</span> text.</p>

An example using the above non-semantic elements


 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shiksha Online: Non-Semantic Elements Example</title>
<style>
/* Styling for the <div> element */
#container {
border: 2px solid black;
padding: 20px;
margin: 10px;
}
/* Styling for the <span> element */
.highlight {
color: blue;
font-weight: bold;
}
</style>
</head>
<body>
<!-- Using <div> as a container for a block of content -->
<div id="container">
<h1>Welcome to Shiksha Online!</h1>
<p>At <span class="highlight">Shiksha Online</span>, you can find a wide range of online courses that are tailored to help you gain new skills and knowledge. Join us and embark on a journey of learning and personal growth.</p>
</div>
</body>
</html>
Copy code

Output

2023_09_Screenshot-2023-09-25-153542-1.jpg

When to Use Non-Semantic Elements

  • Non-semantic elements are used when other elements do not apply or you have to accomplish specific design or interactive tasks without semantic elements.
  • When you want to manipulate elements with JavaScript, you can use <div> or <span> to target specific sections of your webpage.
  • For generic containers that don’t fit the definition of semantic elements like <article>, <section>, <header>, etc
  • Offers flexibility without implying any particular meaning, ensuring no confusion regarding the element’s role.
 

CSS for Layout

Introduction to CSS

CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML or XML. It enables you to control the layout and appearance of your web pages, including the design, colours, fonts, spacing, and positioning of elements.

Must Read Difference Between HTML and CSS

Box Model

Every element in CSS is treated as a rectangular box, and the CSS box model has the following components:

  • Content: The actual content of the box, where text and images appear.
  • Padding: Clears an area around the content inside of the border.
  • Border: A border surrounding the padding (and content) of the box.
  • Margin: Clears an area outside the border, creating space between the element and its neighbours.

Example


 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.box {
width: 300px;
padding: 20px;
border: 5px solid black;
margin: 30px;
}
</style>
</head>
<body>
<div class="box">Welcome to Shiksha Online! This is a box model example.</div>
</body>
</html>
Copy code

Output

2023_09_Screenshot-2023-09-25-163645.jpg

Flexbox

Flexbox is a one-dimensional layout model in CSS that allows you to design a complex layout structure in a more efficient and predictable way than traditional models.

Example


 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.flex-container {
display: flex;
justify-content: space-between;
}
.flex-item {
width: 45%;
border: 1px solid black;
padding: 20px;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="flex-item">Welcome to Shiksha Online! Flexbox Item 1.</div>
<div class="flex-item">Item 2</div>
</div>
</body>
</html>
Copy code

Output

2023_09_Screenshot-2023-09-25-165520.jpg

CSS Grid

CSS Grid Layout provides a two-dimensional grid-based layout system. It’s a great tool for building web page layouts with CSS.

Example


 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto;
}
.grid-item {
border: 1px solid black;
padding: 20px;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">Welcome to Shiksha Online! This is a CSS Grid example (Item 1).</div>
<div class="grid-item">Item 2</div>
</div>
</body>
</html>
Copy code

Output

2023_09_Screenshot-2023-09-25-170430.jpg

Media Queries for Responsive Layout

Media queries are a feature of CSS that enables webpage content to adapt to different screen sizes and resolutions. They are a crucial part of responsive web design, allowing developers to create layouts that work on everything from desktop computers to small mobile devices.


 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background-color: lightblue;
font-size: 18px;
}
/* If the screen size is 600px or less, set the background color to lightgreen */
@media only screen and (max-width: 600px) {
body {
background-color: lightgreen;
font-size: 16px;
}
}
</style>
</head>
<body>
<p>Welcome to Shiksha Online!</p>
</body>
</html>
Copy code

Output

2023_09_res.gif

In the above code, by default, the background colour of the body is set to lightblue, and font-size is 18px. If the screen size is 600px or less, the background-color changes to lightgreen, and font-size changes to 16px.

Media queries are essential for building websites and web applications across various devices and screen sizes. Properly utilizing media queries ensures that your website is accessible and user-friendly, providing a consistent and enjoyable user experience regardless of the device used to access your site.

Thus, we discussed HTML layouts and techniques. We learned about the basic structure of an HTML document, the role and use of semantic and non-semantic elements, and the importance of each. We learnt how to use CSS for layout, understanding the Box Model, Flexbox, and CSS Grid to design stylish and structured layouts. Media queries helped us ensure our web pages look good on all devices by making them responsive. In essence, mastering these elements and techniques equips us to create more organized, accessible, and attractive web pages, enhancing the user’s experience and interaction.

Keep Learning, and Keep Exploring!

FAQs

What is the box model in CSS, and why is it important in HTML layout?

The CSS box model describes the design and layout of elements. Each element is treated as a box with margins, borders, padding, and the actual content area. Understanding the box model is crucial for controlling spacing, border, and resizing elements accurately on a webpage.

How do you use Flexbox to create responsive layouts?

Flexbox is a CSS layout module that allows space distribution between items in an interface and powerful alignment capabilities. To create responsive layouts, you can use properties like flex-direction to arrange items horizontally or vertically, justify-content to align them along the main axis, and align-items to align them along the cross axis.

What is CSS Grid, and how does it differ from Flexbox?

CSS Grid is a two-dimensional layout system that handles both columns and rows, unlike Flexbox which is primarily a one-dimensional system. CSS Grid is particularly useful for creating complex layouts and aligning content precisely even in areas that don't directly line up.

How can you use CSS positioning to layer elements?

CSS offers several positioning methods like static, relative, absolute, fixed, and sticky. To layer elements, use position: absolute or position: relative along with z-index to control the stacking order. Absolute positioning removes the element from the normal flow, and it’s positioned relative to its nearest positioned ancestor.

What are media queries, and how do they enhance responsive web design?

Media queries are a CSS technique used to apply styles based on the type of device, its characteristics, or the environment. They are essential for responsive design, allowing developers to create different styles for different screen sizes or orientations, enhancing user experience across devices.

How do you implement multi-column layouts using CSS?

Multi-column layouts can be implemented using CSS column properties such as column-count and column-gap. These properties allow text to flow into multiple columns, similar to a newspaper layout, which can be adjusted to fit various design needs.

What are some techniques to ensure your HTML layout is accessible?

To ensure accessibility:

  • Use semantic HTML elements (header, nav, main, footer) for better screen reader interpretation.
  • Ensure adequate contrast between text and background.
  • Provide alt text for images.
  • Use ARIA roles and properties when necessary.

What is the purpose of the display property in CSS, and how is it used?

The display property specifies how an element is displayed. Common values include block, inline, inline-block, flex, and grid. Each value changes the layout behaviors of elements and their children, crucial for implementing different design patterns.

How can you use the viewport meta tag to improve mobile web browsing?

The viewport meta tag controls the layout on mobile browsers. By setting the viewport width to device-width and initial-scale to 1.0, the page will render at the width of the device, ensuring that the layout is optimized for mobile screens.

What is the importance of the overflow property in controlling layout?

The overflow property specifies what should happen if content overflows an element's box. This is particularly important in managing layout where you want to control the visibility of excess content, whether it scrolls (overflow: scroll;), is hidden (overflow: hidden;), or visibly spills out (overflow: visible;).

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio