Introduction to Styling the Interface

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.

Defining Basic Page Style

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.

  1. 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, and background-color sets the page's background color.

  2. 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, while padding: 20px; adds space inside the body.

  3. 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, and align-items: center; centers content vertically. min-height: 100vh; ensures the body takes the full viewport height.

Creating a Responsive Container

Now, let's create a central white block to hold our content using the .container class.

  1. 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, and border-radius: 12px; softens the corners.

  2. 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.

  3. 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.

Implementing Tab Navigation System

Let's style the tab navigation system using the .tabs and .tab-button selectors.

  1. Set Up the Tabs:

    Use flexbox to align the tabs and add spacing below them.

    The display: flex; and justify-content: center; center the tabs, while margin-bottom: 20px; adds space below.

  2. Style the Tab Buttons:

    Define the appearance of the tab buttons, including padding, margin, background color, and text color.

    Here, padding and margin control spacing, background-color and color define the button's appearance, and cursor: pointer; changes the cursor to a pointer on hover.

  3. 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.

Styling Form Elements

Next, we'll style the form elements, including inputs, selects, and buttons.

  1. 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, and border and border-radius define the appearance.

  2. 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.

Enhancing Media Display Areas

Finally, let's add styles for media display areas, including #story-container, #history-container

  1. Add Margins to Containers:

    Add a top margin to separate these containers from other content.

    The margin-top: 20px; adds space above the containers.

Summary and Preparation for Practice

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.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal