Creating Templates
Introduction
In this tutorial, we will explore how to create templates using HTML and CSS. Templates are essential for maintaining a consistent look and feel across multiple web pages. By the end of this tutorial, you will have a solid understanding of how to create and utilize templates effectively.
Setting Up the HTML Structure
To begin, let's set up a basic HTML structure that we will use as our template. This structure will include common elements such as the header, navigation menu, main content area, and footer.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Template Page</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="swf-lsn-container"> <header> <h1>My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <h2>Welcome to My Website</h2> <p>This is the main content area.</p> </main> <footer> <p>© 2023 My Website</p> </footer> </div> </body> </html>
Applying Styles with CSS
Next, we will apply some basic styles to our template using CSS. This will help us achieve a more visually appealing and consistent layout.
body { font-family: Arial, sans-serif; background-color: #f9f9f9; color: #000; margin: 0; padding: 0; } .container { width: 100%; margin: 0 auto; padding: 20px; background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } header, footer { text-align: center; padding: 10px 0; background-color: #333; color: #fff; } nav ul { list-style: none; padding: 0; } nav ul li { display: inline; margin: 0 10px; } nav ul li a { color: #fff; text-decoration: none; } main { padding: 20px 0; }
Using Template Parts
In real-world projects, it's common to split the template into reusable parts (e.g., header, footer) and include them in multiple pages. Here’s how you can achieve that with server-side includes (SSI) or templating engines like Handlebars.
Example using PHP Includes
Create separate files for the header, footer, and main content:
<!-- header.php --> <header> <h1>My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header>
<!-- footer.php --> <footer> <p>© 2023 My Website</p> </footer>
<!-- index.php --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Template Page</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="swf-lsn-container"> <?php include 'header.php'; ?> <main> <h2>Welcome to My Website</h2> <p>This is the main content area.</p> </main> <?php include 'footer.php'; ?> </div> </body> </html>
Conclusion
Creating templates is a fundamental skill for web development. It ensures consistency and makes maintenance easier. By following this tutorial, you should now have a good understanding of how to create and use templates in your projects.