Styling the User Interface with FastAPI Static Files
Introduction to Styling the Interface
Welcome to the second lesson of our course on building a story generation web application with FastAPI! In the previous lesson, you set up the foundational HTML structure for your application, organized the project layout, and created the main interface with tabs, forms, and containers. While the structure is in place, the application currently lacks visual appeal and usability.
In this lesson, you'll learn how to add styling to your application using FastAPI's approach to serving static files. Styling allows you to control the appearance of your HTML elements — such as colors, fonts, spacing, and layout — making your interface more attractive and user-friendly.
Defining Basic Page Style
Let's start by setting up the basic page style for your application. This will ensure a consistent look and feel across your interface.
-
Set the Font-Family and Background Color
Begin by defining the font family and background color for the entire page. In your Python project, you will write these styles in the
style.cssfile, which is served as a static file.Here, the
font-familyproperty specifies the typeface, andbackground-colorsets the page's background color. -
Remove Default Margins and Add Padding
Next, remove the default margins and add padding to the body to ensure content is not flush against the edges.
The
margin: 0;removes any default spacing around the body, whilepadding: 20px;adds space inside the body. -
Use Flex Layout for Centering Content
To center content both vertically and horizontally, use a flex layout. This will make the page more visually appealing and easier to navigate.
The
display: flex;enables flexbox,flex-direction: column;arranges content vertically, andalign-items: center;centers content horizontally.min-height: 80vh;ensures the body takes up most of the viewport height.
Creating a Responsive Container
Now, let's create a central white block to hold your content using the .container class.
-
Define the Container's Appearance
Set the background color, padding, and border-radius to create a visually distinct container.
Here,
background: white;sets the container's background color,padding: 30px;adds space inside the container, andborder-radius: 12px;softens the corners. Thebox-shadowproperty adds a subtle shadow effect, enhancing the container's appearance.width: 100%;andmax-width: 400px;ensure the container adapts to different screen sizes while maintaining a maximum width for readability.
