In this tutorial, we will break down the essential components of building a responsive website homepage, using HTML and CSS. The example provided is structured to create an engaging and user-friendly homepage for “DevTechTutor”, a fictional company focused on tech tutorials and development. This guide will help students understand how to structure their HTML and ensure their site is responsive across different devices.
To create a responsive website homepage using HTML and CSS, follow these simple step-by-step instructions:
- First, create a folder with any name you like. Then, make the necessary files inside it.
- Create a file called
index.html
to serve as the main file. - Create a file called
style.css
for the CSS code.
Document Structure and Meta Tags
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Responsive Website Homepage HTML and CSS | DevTechTutor</title>
<link rel="stylesheet" href="style.css" />
</head>
<!DOCTYPE html>
: This declaration defines the document type and version of HTML. Here, it specifies HTML5.<html lang="en">
: This element is the root of the HTML document, withlang="en"
indicating that the primary language is English.<meta charset="UTF-8" />
: Sets the character encoding to UTF-8, which includes most characters from the majority of written languages.<meta name="viewport" content="width=device-width, initial-scale=1.0" />
: This is crucial for responsive design. It tells the browser to match the screen’s width in device-independent pixels and set the scale to 1.0.<title>
: Specifies the title of the webpage, which appears in the browser tab.<link rel="stylesheet" href="style.css" />
: Links to an external CSS file that contains the styling rules for the webpage.
Header and Navigation
<body>
<header class="header">
<nav class="navbar">
<h2 class="logo"><a href="#">DevTechTutor</a></h2>
<input type="checkbox" id="menu-toggle" />
<label for="menu-toggle" id="hamburger-btn">
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24">
<path d="M3 12h18M3 6h18M3 18h18" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
</svg>
</label>
<ul class="links">
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
<div class="buttons">
<a href="#" class="signin">Sign In</a>
<a href="#" class="signup">Sign Up</a>
</div>
</nav>
</header>
- The
<header>
tag contains the entire navigation menu, marked with a class for easy CSS targeting. <nav>
: Wraps the main navigation system of the webpage.<h2 class="logo">
: Displays the site logo, which is also a clickable link to return to the homepage.<input>
and<label>
: Used for creating a responsive hamburger menu. The checkbox toggles the visibility of the menu on smaller screens.<ul class="links">
: Contains navigation links. These are typically displayed inline on larger screens and stacked vertically when the hamburger menu is active.<div class="buttons">
: Holds buttons for user actions like sign-in and sign-up, enhancing the functional aspect of the navbar.
Hero Section
<section class="hero-section">
<div class="hero">
<h2>Mobile App Development</h2>
<p>
Join us in the exciting world of programming and turn your ideas into
reality. Unlock the world of endless possibilities - learn to code and
shape the digital future with us.
</p>
<div class="buttons">
<a href="#" class="join">Join Now</a>
<a href="#" class="learn">Learn More</a>
</div>
</div>
<div class="img">
<img src="hero-bg.png" alt="hero image" />
</div>
</section>
</body>
</html>
<section class="hero-section">
: Defines a major thematic area of the webpage, designed to catch the visitor’s attention immediately upon arrival.- Inside, there is a combination of headings, paragraphs, and links/buttons aimed at providing initial information and call-to-action (CTA) options.
- The image
<img>
within<div class="img">
serves as a visual complement to the textual content, often enhancing the theme or subject discussed in the hero text.
Here is the full html code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Responsive Website Homepage HTML and CSS | DevTechTutor</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header class="header">
<nav class="navbar">
<h2 class="logo"><a href="#">DevTechTutor</a></h2>
<input type="checkbox" id="menu-toggle" />
<label for="menu-toggle" id="hamburger-btn">
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24">
<path d="M3 12h18M3 6h18M3 18h18" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
</svg>
</label>
<ul class="links">
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
<div class="buttons">
<a href="#" class="signin">Sign In</a>
<a href="#" class="signup">Sign Up</a>
</div>
</nav>
</header>
<section class="hero-section">
<div class="hero">
<h2>Mobile App Development</h2>
<p>
Join us in the exciting world of programming and turn your ideas into
reality. Unlock the world of endless possibilities - learn to code and
shape the digital future with us.
</p>
<div class="buttons">
<a href="#" class="join">Join Now</a>
<a href="#" class="learn">Learn More</a>
</div>
</div>
<div class="img">
<img src="hero-bg.png" alt="hero image" />
</div>
</section>
</body>
</html>
Now will dissect a CSS stylesheet designed to create a responsive homepage. This breakdown will help students understand how CSS is used to enhance the visual design and user experience of a website, focusing on responsiveness and interactivity.
CSS Explanation and Comments
/* Importing Google font - Open Sans with different weights for versatile typography */
@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;500;600;700&display=swap");
/* Global Style Resets */
* {
margin: 0; /* Remove default margins from all elements */
padding: 0; /* Remove default padding from all elements */
box-sizing: border-box; /* Content-box size will include border and padding */
font-family: "Open Sans", sans-serif; /* Setting a default web-safe font for the whole website */
}
/* Body Styles */
body {
height: 100vh; /* Set the height to fully cover the viewport */
width: 100%; /* Ensure the body covers the full width of the viewport */
background: linear-gradient(to bottom, #175d69 23%, #330c43 95%); /* A gradient background for visual depth */
}
/* Header Positioning */
.header {
position: fixed; /* Fix the header at the top of the viewport */
top: 0;
left: 0;
width: 100%; /* Full width */
}
/* Navbar Styling */
.navbar {
display:flex; /* Use flexbox for easy alignment of navbar items */
align-items: center; /* Center items vertically */
justify-content: space-between; /* Space out navbar content */
max-width: 1200px; /* Max width to constrain navbar width */
margin: 0 auto; /* Center the navbar */
padding: 20px 15px; /* Padding for aesthetics */
}
/* Navbar logo styling */
.navbar .logo a {
font-size: 1.8rem; /* Larger text for visibility */
text-decoration: none; /* No underline to keep it clean */
color: #fff; /* White color for contrast */
}
/* Navbar link styles */
.navbar .links {
display: flex; /* Display links in a row */
align-items: center;
list-style: none; /* No bullet points */
gap: 35px; /* Space between each link */
}
.navbar .links a {
font-weight: 500; /* Medium font weight for better readability */
text-decoration: none; /* Remove underline */
color: #fff; /* White text color */
padding: 10px 0; /* Padding to increase clickable area */
transition: 0.2s ease; /* Smooth transition for hover effects */
}
.navbar .links a:hover {
color: #47b2e4; /* Change color on hover to signify interactivity */
}
/* Button styles in the navbar */
.navbar .buttons a {
text-decoration: none; /* Remove default underline */
color: #fff; /* White text color for contrast */
font-size: 1rem; /* Standard font size for readability */
padding: 15px 0; /* Padding for visual separation and interaction */
transition: 0.2s ease; /* Smooth transition for hover effects */
}
.navbar .buttons a:not(:last-child) {
margin-right: 30px; /* Space between buttons */
}
.navbar .buttons .signin:hover {
color: #47b2e4; /* Hover color change for interactivity */
}
/* Specific styles for the signup button */
.navbar .buttons .signup {
border: 1px solid #fff; /* White border for visibility */
padding: 10px 20px; /* Padding for form and visibility */
border-radius: 0.375rem; /* Rounded corners for aesthetics */
text-align: center;
transition: 0.2s ease; /* Smooth transition for hover effects */
}
.navbar .buttons .signup:hover {
background-color: #47b2e4; /* Background color change on hover */
color: #fff; /* Keep text color constant */
}
/* Hero section styling */
.hero-section {
display: flex; /* Use flexbox for layout */
justify-content: space-evenly; /* Evenly space children horizontally */
align-items: center; /* Align children vertically */
height: 95vh; /* Use 95% of viewport height */
padding: 0 15px; /* Padding for aesthetics */
max-width: 1200px; /* Maximum width to maintain layout consistency */
margin: 0 auto; /* Centering the content horizontally */
}
.hero-section .hero {
max-width: 50%; /* Limit width to 50% of its container */
color: #fff; /* White text for contrast */
}
.hero h2 {
font-size: 2.5rem; /* Large font size for heading */
margin-bottom: 20px; /* Spacing after the heading */
}
.hero p {
font-size: 1.2rem; /* Readable paragraph font size */
margin-bottom: 20px; /* Spacing after the paragraph */
color: #c9c7c7; /* Light grey for paragraph text */
}
.hero-section .img img {
width: 517px; /* Fixed width for consistency */
}
.hero-section .buttons {
margin-top: 40px; /* Top margin for separation from text */
}
.hero-section .buttons a {
text-decoration: none; /* Remove underline from links */
color: #fff; /* White text for visibility */
padding: 12px 24px; /* Padding for button size */
border-radius: 0.375rem; /* Rounded corners for button aesthetics */
font-weight: 600; /* Bold font for emphasis */
transition: 0.2s ease; /* Smooth transition for hover effects */
display: inline-block; /* Ensure the links are lined up correctly */
}
.hero-section .buttons a:not(:last-child) {
margin-right: 15px; /* Space between buttons */
}
.buttons .join {
background-color: #47b2e4; /* Specific color for the 'Join' button */
}
.hero-section .buttons .learn {
border: 1px solid #fff; /* White border for the 'Learn' button for distinction */
border-radius: 0.375rem; /* Rounded corners for aesthetics */
}
.hero-section .buttons a:hover {
background-color: #47b2e4; /* Background color change on hover for all buttons */
}
/* Hamburger menu styles for mobile view */
#menu-toggle {
display: none; /* Hide the checkbox by default */
}
#hamburger-btn {
font-size: 1.8rem; /* Large font size for visibility */
color: #fff; /* White color for contrast */
cursor: pointer; /* Indicates clickable */
display: none; /* Initially hidden, shown in mobile view */
order: 1; /* Order used in flexbox for positioning */
}
/* Media Queries for Responsive Adjustments */
@media screen and (max-width: 1023px) {
.navbar .logo a {
font-size: 1.5rem; /* Reduce font size for smaller screens */
}
.links {
position: fixed; /* Fixed position to stay in view */
left: -100%; /* Start off-screen to the left */
top: 75px; /* Offset from the top */
width: 100%; /* Full width */
height: 100vh; /* Full viewport height */
padding-top: 50px; /* Padding at the top */
background: #175d69; /* Background color matching the header */
flex-direction: column; /* Stack links vertically */
transition: 0.3s ease; /* Smooth transition for sliding in */
}
.navbar #menu-toggle:checked ~ .links {
left: 0; /* Move in to show the menu when checkbox is checked */
}
.navbar #hamburger-btn {
display: block; /* Show the hamburger button */
}
.header .buttons {
display: none; /* Hide buttons in header on smaller screens */
}
.hero-section .hero {
max-width: 100%; /* Allow hero content to use full width */
text-align: center; /* Center text for better readability */
}
.hero-section img {
display: none; /* Hide image in the hero section for smaller screens */
}
}
Welcome to DevTechTutor.com, your ultimate resource for mastering web development and technology! Whether you're a beginner eager to dive into coding or an experienced developer looking to sharpen your skills, DevTechTutor.com is here to guide you every step of the way. Our mission is to make learning web development accessible, engaging, and effective.