To create responsive HTML layouts using CSS and Go, you can follow these steps:
Start by setting up a basic HTML template. You can create an HTML file and define the basic structure of your page using HTML tags.
Add a CSS stylesheet to your HTML file. Create a new CSS file and link it to your HTML file using the <link>
tag. In this CSS file, you will define the styles and layout for your responsive design.
Use CSS media queries to make your layout responsive. Media queries allow you to apply different styles based on the size of the viewport. By defining different CSS styles for different viewport sizes, you can make your layout adapt to different screen sizes.
In your Go web application, serve the HTML file and CSS stylesheet. You can use the http
package in Go to handle incoming HTTP requests and respond with the HTML and CSS files. Use the http.FileServer
function to serve static files like HTML and CSS.
Test your responsive layout by resizing your browser window or viewing it on different devices. You should see that the layout responds and adjusts itself based on the viewport size.
Here's an example of how your HTML file may look:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<header>
<h1>My Responsive Layout</h1>
</header>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<main>
<section>
<h2>About Me</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</section>
<section>
<h2>Contact Me</h2>
<p>Email: [email protected]</p>
</section>
</main>
<footer>
<p>© 2021 My Website</p>
</footer>
</body>
</html>
And here's an example of how your CSS file (styles.css) may look:
@media (max-width: 768px) {
header {
font-size: 1.5em;
}
nav ul {
display: none;
}
nav.active ul {
display: block;
}
}
section {
margin-bottom: 20px;
}
In the above example, the CSS media query is used to make changes to the header and navigation elements when the viewport size is less than or equal to 768 pixels. The @media
rule defines the styles within the media query block.
Using the above steps, you can create responsive HTML layouts in Go using CSS.