Angular course · Module 13: UI and Styling

Responsive Design

2 min read ·
In this lesson4

Just as a castle adapts its defense to different attacks, so the UI must adapt to different devices.

Media Queries

// mixins.scss
@mixin mobile {
  @media (max-width: 599px) { @content; }
}

@mixin tablet {
  @media (min-width: 600px) and (max-width: 959px) { @content; }
}

@mixin desktop {
  @media (min-width: 960px) { @content; }
}

// Usage
.samurai-grid {
  display: grid;
  gap: 1rem;

  @include mobile {
    grid-template-columns: 1fr;
  }

  @include tablet {
    grid-template-columns: repeat(2, 1fr);
  }

  @include desktop {
    grid-template-columns: repeat(4, 1fr);
  }
}

CDK BreakpointObserver

import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';

@Component({
  template: `
    @if (isMobile()) {
      <app-mobile-nav />
    } @else {
      <app-desktop-nav />
    }

    <div [class]="layoutClass()">
      <ng-content />
    </div>
  `
})
export class ResponsiveLayoutComponent {
  private breakpointObserver = inject(BreakpointObserver);

  isMobile = toSignal(
    this.breakpointObserver.observe([Breakpoints.Handset]).pipe(
      map(result => result.matches)
    ),
    { initialValue: false }
  );

  layoutClass = computed(() => this.isMobile() ? 'mobile-layout' : 'desktop-layout');
}

Flexbox Layouts

@Component({
  template: `
    <div class="flex-container">
      <aside class="sidebar">Menu</aside>
      <main class="content">
        <ng-content />
      </main>
    </div>
  `,
  styles: [`
    .flex-container {
      display: flex;
      flex-wrap: wrap;
      min-height: 100vh;
    }

    .sidebar {
      flex: 0 0 250px;
    }

    .content {
      flex: 1;
      min-width: 0;  /* Important for overflow */
    }

    @media (max-width: 768px) {
      .sidebar {
        flex: 0 0 100%;
        order: 2;
      }
      .content {
        order: 1;
      }
    }
  `]
})
export class LayoutComponent {}

Container Queries (CSS)

// Modern approach
.card-container {
  container-type: inline-size;
  container-name: card;
}

.card {
  @container card (min-width: 400px) {
    display: grid;
    grid-template-columns: 1fr 2fr;
  }

  @container card (max-width: 399px) {
    display: block;
  }
}

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. What is BreakpointObserver from @angular/cdk/layout used for?

  2. 2. Which function was used in the lesson to convert the Observable from BreakpointObserver into a signal?

These are 2 of 3 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Horizontal ordering

    Arrange on a single line the media query header for mobile devices.

  • Code editor

    Complete the component that reacts to the screen size.

  • Vertical ordering

    Order the breakpoints from the lesson from the smallest to the largest screen.

  1. QuestionBreakpointObserver
  2. Put in orderMobile media query mixin
  3. QuestionConverting an Observable to a Signal
  4. Coding taskPractice: BreakpointObserver
  5. Put in orderBreakpoints from smallest
  6. QuestionContainer Queries

Useful articles