Welcome to the second lesson of our course on building an story generation web application with Flask! 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 a Flask application, static files like CSS are stored in a folder called static
. We've already linked our CSS file in the HTML using the Flask url_for
function:
This tells Flask to look for a file named style.css
in the static
folder. Now, let's create this file and add our styles to it.
Let's start by setting up the basic page style using the body
selector. This will ensure a consistent look and feel across our application.
-
Set the Font-Family and Background Color:
Begin by defining the font family and background color for the entire page. We'll use
'Segoe UI'
for a clean and modern look and a light gray background color for a subtle appearance.Here,
font-family
specifies the typeface, andbackground-color
sets 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,justify-content: center;
centers content horizontally, andalign-items: center;
centers content vertically.min-height: 100vh;
ensures the body takes the full viewport height.
Now, let's create a central white block to hold our 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. -
Add Depth with Box-Shadow:
Apply a box-shadow to give the container a sense of depth and separation from the background.
The
box-shadow
property adds a subtle shadow effect, enhancing the container's appearance. -
Ensure Responsiveness:
Set the container's width to 100% and limit the maximum width to 600px for a responsive design.
This ensures the container adapts to different screen sizes while maintaining a maximum width for readability.
Let's style the tab navigation system using the .tabs
and .tab-button
selectors.
-
Set Up the Tabs:
Use flexbox to align the tabs and add spacing below them.
The
display: flex;
andjustify-content: center;
center the tabs, whilemargin-bottom: 20px;
adds space below. -
Style the Tab Buttons:
Define the appearance of the tab buttons, including padding, margin, background color, and text color.
Here,
padding
andmargin
control spacing,background-color
andcolor
define the button's appearance, andcursor: pointer;
changes the cursor to a pointer on hover. -
Add Hover Effect:
Apply a transition and hover state to enhance interactivity.
The
:hover
pseudo-class changes the background color on hover, providing visual feedback to users.
Next, we'll style the form elements, including inputs, selects, and buttons.
-
Style Inputs and Selects:
Ensure inputs and selects use full width with appropriate padding and borders.
The
width: 80%;
ensures full width,padding
adds space inside, andborder
andborder-radius
define the appearance. -
Style the Button:
Define the button's appearance, including background color, text color, and hover state.
The
button
selector styles the button, and the:hover
pseudo-class changes the background color on hover.
Finally, let's add styles for media display areas, including #story-container
, #history-container
-
Add Margins to Containers:
Add a top margin to separate these containers from other content.
The
margin-top: 20px;
adds space above the containers.
In this lesson, we covered essential CSS techniques to style the interface of our AI Short Story Generator web application. We defined basic page styles, created a responsive container, implemented a tab navigation system, styled form elements, and enhanced media display areas. These skills are crucial for creating visually appealing and user-friendly web applications. Now, it's time to apply these techniques in the practice exercises that follow. Remember, practice is key to mastering these concepts, so take your time and experiment with different styles to see how they affect the overall look and feel of your application.
