Introduction to CSS in Our Express.js Application
Introduction to CSS in Our Express.js Application
Welcome to the second lesson of our course on building an image generation web application with Express.js! In our previous lesson, we created the HTML structure for our application, setting up the tabs, form elements, and containers that will make up our user interface. While the structure is in place, our application currently lacks visual appeal and usability.
In this lesson, we'll focus on styling our application using CSS (Cascading Style Sheets). CSS is a styling language that allows us to control the appearance of HTML elements, including colors, fonts, spacing, and layout. By adding CSS to our application, we'll transform the basic HTML structure into an attractive, user-friendly interface.
Remember that in an Express.js application, static files like CSS are served using the built-in express.static middleware. We've already set this up in our main application file:
This tells Express to serve static files from the public directory. We've already linked our CSS file in the HTML using a direct path:
This tells the browser to look for a file named style.css in the css folder inside the public directory. Now, let's create this file and add our styles to it.
Setting Up the Page Layout
Let's start by styling the basic page layout, including the body element and the main container. This will set the foundation for our application's appearance.
We start with a universal box-sizing: border-box rule. By default, CSS uses content-box sizing, where width describes only the content area and any padding is added on top of it. Under that default, an element with width: 100% and padding: 30px ends up 60 pixels wider than its parent and spills outside it. Setting box-sizing: border-box on every element makes width include the padding, so width: 100% means exactly "as wide as my parent" no matter how much padding we add later. We'll rely on this repeatedly as we style containers and form controls.
Next, we're styling the body element to use a modern sans-serif font stack, with 'Segoe UI' as the primary font. We've set a light gray background color (#f0f0f0) and removed the default margin. We've also added padding to the top and bottom of the page.
The display: flex, flex-direction: column, justify-content: center, and align-items: center properties work together to center our content both horizontally and vertically on the page. The min-height: 100vh ensures that the body takes up at least the full height of the viewport, which helps with the centering.
For the .container class, we've set a white background, added padding for spacing, and used border-radius to round the corners. The box-shadow property adds a subtle shadow effect, giving the container a slight elevation above the background. We've set the width to 100% but limited the maximum width to 600 pixels. Because of the border-box rule above, the container fills the available width on a narrow screen without its 30px of padding pushing it past the edge of the viewport, and it stops growing once it reaches 600 pixels on a large screen.
With these styles in place, our application already looks much better. The content is centered on the page, and the white container stands out against the light gray background.
