Get AI summaries of any video or article — Sign up free
Styling w/ CSS - Django Web Development with Python p.5 thumbnail

Styling w/ CSS - Django Web Development with Python p.5

sentdex·
5 min read

Based on sentdex's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Materialize CSS is integrated by loading its CSS and required JavaScript assets so UI components render with the framework’s styling and behaviors.

Briefing

Materialize CSS is used to give the Django tutorial site a polished, responsive look without hand-writing large amounts of CSS. Instead of wrestling with layout, typography, and UI styling from scratch, the project pulls in Materialize’s CSS and JavaScript assets so components like cards and navigation bars adopt “material design” styling—visually layered elements that behave consistently across screen sizes.

The workflow starts like adding any custom stylesheet: include the framework’s CSS (and the required JavaScript) in the template so the browser loads the styling immediately. After refreshing, the site’s typography changes from the default, while the overall structure remains the same—confirming the framework is active. The tutorial then points to Materialize’s built-in helpers, especially the grid system, which assumes a 12-column layout per row. By assigning column spans per breakpoint (small, medium, large), content can shift from side-by-side layouts on wider screens to stacked layouts on smaller devices.

To apply the grid, the homepage content is reorganized using a `div class="row"` wrapper and Materialize “cards” copied into the loop that iterates over tutorials. Each tutorial card is configured to take up different column widths depending on device size—full width on small screens, half width on medium screens, and a smaller fraction on large screens—so multiple tutorial entries can display in a responsive grid. The card content is populated with fields like the tutorial title and published information, and the card action area is adjusted (removed for now, with the option to add a call-to-action later).

Next comes the navigation bar. A Materialize navbar template is copied into the base page structure and its links are wired to Django routes (e.g., home, register, login). The navbar is treated as shared layout content, which leads to a cleaner Django templating approach: the homepage is refactored to use template inheritance. A new base template (named `header.html` in the tutorial) wraps common elements and defines a `block content` region. The `home.html` page then uses `{% extends %}` and fills only the unique homepage markup inside that block. This avoids copying the navbar and layout across multiple pages later.

Finally, the tutorial shifts toward customizing Materialize’s look via Sass rather than editing thousands of lines of compiled CSS. The process involves installing a Sass compiler (the tutorial uses Koala Sass), editing Materialize’s Sass variables—particularly color variables in the `color` component—then compiling to generate a new CSS file. The compiled CSS is placed into the project’s static directory and included by the base template so the updated theme (e.g., swapping Materialize’s default red palette for a chosen blue palette) propagates to UI elements like the navbar and buttons.

With styling largely handled through Materialize and template inheritance, the next step is to move away from design work and focus on Django user accounts: registration, login, and logout.

Cornell Notes

Materialize CSS is integrated into a Django project to speed up UI styling and ensure responsive layouts. The grid system (12 columns with breakpoint-based column spans) lets tutorial cards shift from multi-column layouts on large screens to stacked layouts on mobile. A Materialize navbar is added and then centralized using Django template inheritance: a shared base template wraps common layout and exposes a `block content` for page-specific markup. Theme customization is done through Sass variables (edited in Materialize’s source color settings) and then compiled into a new CSS file using a Sass compiler. This combination reduces repetitive HTML/CSS work and makes future page additions cleaner.

Why include both Materialize CSS and JavaScript, and what changes after refreshing?

Materialize’s styling relies on its CSS for visual rules and its JavaScript for interactive behaviors tied to components. After the assets are loaded and the page is refreshed, the most immediate visible change is typography—fonts and default styling shift away from the browser’s default. Layout and component styling (like cards and nav elements) then follow Materialize’s design conventions once both assets are present.

How does Materialize’s grid system enable responsive tutorial card layouts?

Materialize’s grid assumes a 12-column row. By wrapping content in `div class="row"` and using card columns with breakpoint classes, each tutorial card can occupy different spans depending on screen width. For example, a card can be set to take all 12 columns on small screens, 6 columns on medium screens (half width), and a smaller span on large screens so multiple cards appear side-by-side. As the viewport changes, cards automatically reflow according to those breakpoint rules.

How does the tutorial refactor the homepage to avoid repeating the navbar across pages?

Instead of copying the navbar HTML into every page, the project uses Django template inheritance. A shared template (created as `header.html`) wraps common layout and defines `{% block content %}{% endblock %}`. `home.html` then uses `{% extends "main/salah/header.html" %}` (path as shown) and places only homepage-specific markup inside the `block content` region. This keeps layout changes centralized.

What Materialize components are highlighted as useful for a Django app UI?

The tutorial calls out several Materialize component categories: cards for content blocks, navbars for top navigation, tables for structured data, pagination as a helpful but code-intensive feature, collapsibles for sidebar-like interactions, and toasts for pop-up messages. Buttons and other UI elements are also implied as part of the component ecosystem.

How is Materialize theming customized without editing compiled CSS line-by-line?

The approach uses Sass variables. Rather than modifying the huge generated CSS output, the tutorial edits Materialize’s Sass color variables (in the `sass/components/color` area). It changes the base palette values (e.g., Materialize red to a chosen blue palette) and may adjust related shades (lighter/darker variants). After editing, a Sass compiler (Koala Sass) compiles the Sass into a new CSS file, which is then placed in the project’s static directory and included by the base template.

Review Questions

  1. What is the practical benefit of using Django `{% extends %}` and `{% block %}` when adding new pages like tutorials, about, or forum pages?
  2. How would you configure a tutorial card to show 3 cards per row on large screens using a 12-column grid?
  3. Why does changing Sass variables require recompiling into a new CSS file before the browser reflects the theme update?

Key Points

  1. 1

    Materialize CSS is integrated by loading its CSS and required JavaScript assets so UI components render with the framework’s styling and behaviors.

  2. 2

    The 12-column grid system enables responsive layouts by assigning different column spans at small, medium, and large breakpoints.

  3. 3

    Tutorial cards are placed inside a `row` and populated within a Django loop, with card columns configured to reflow across device sizes.

  4. 4

    A Materialize navbar is wired to Django routes (home, register, login) and then centralized using template inheritance to prevent duplication.

  5. 5

    Django template inheritance is implemented by creating a shared base template with a `block content` region and extending it from `home.html`.

  6. 6

    Theme customization is performed by editing Materialize Sass color variables and compiling to a new CSS file, rather than editing the generated CSS directly.

  7. 7

    Compiled CSS must be correctly placed in the Django static directory and included by templates so the browser can load the updated theme.

Highlights

Materialize’s grid uses a 12-column layout with breakpoint-based spans, making it straightforward to switch from multi-column card grids to stacked layouts on smaller screens.
Template inheritance turns the navbar into shared layout: one base template with `block content` eliminates copy-paste across pages.
Sass-based theming avoids editing massive compiled CSS files; changing a few color variables and recompiling can recolor the entire UI.
Materialize’s “material design” look is recognizable through layered, card-like UI elements and consistent component styling.

Topics

  • Materialize CSS
  • Responsive Grid
  • Django Template Inheritance
  • Sass Theming
  • Navbar Integration

Mentioned

  • Daniel Aguiar
  • Yuri
  • Jackson Warren Hubert
  • Sergey
  • Hannes Kalama