Advanced Topics

Explore advanced techniques like Flexbox, Grid, animations, and responsive design.

CSS Flexbox

Flexbox is a layout model that allows elements to align and distribute space within a container, even when the sizes are unknown or dynamic.

Flex Container Properties

  • display: flex: Creates a flex container
  • flex-direction: Defines the direction of the flex items
  • justify-content: Aligns items along the main axis
  • align-items: Aligns items along the cross axis
  • flex-wrap: Defines whether items are forced in a single line or can wrap
  • align-content: Aligns flex lines when there is extra space
.flex-container {
    display: flex;
    flex-direction: row;          /* Default: row */
    justify-content: center;      /* Main axis alignment */
    align-items: center;          /* Cross axis alignment */
    flex-wrap: nowrap;            /* Default: nowrap */
    align-content: stretch;       /* For multi-line flex containers */
}

Flex Item Properties

  • order: Controls the order of flex items
  • flex-grow: Defines how much a flex item grows relative to others
  • flex-shrink: Defines how much a flex item shrinks relative to others
  • flex-basis: Defines the initial size of a flex item
  • flex: Shorthand for flex-grow, flex-shrink, and flex-basis
  • align-self: Allows individual flex items to override align-items
.flex-item {
    order: 1;           /* Default: 0 */
    flex-grow: 1;       /* Default: 0 */
    flex-shrink: 1;     /* Default: 1 */
    flex-basis: auto;   /* Default: auto */
    flex: 1 1 auto;     /* Shorthand: grow shrink basis */
    align-self: center; /* Override container align-items */
}

CSS Grid Layout

CSS Grid Layout is a two-dimensional layout system for the web. It works with both rows and columns, making it easier to design web pages without floats and positioning.

Grid Container Properties

  • display: grid: Creates a block-level grid container
  • display: inline-grid: Creates an inline-level grid container
  • grid-template-columns: Defines the number and sizes of columns
  • grid-template-rows: Defines the number and sizes of rows
  • grid-template-areas: Defines grid areas using template strings
  • grid-gap: Shorthand for grid-row-gap and grid-column-gap
.grid-container {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;    /* Three columns */
    grid-template-rows: auto 100px 1fr;    /* Three rows */
    grid-gap: 10px;                        /* Gap between items */
    grid-template-areas: 
        "header header header"
        "sidebar main aside"
        "footer footer footer";
}

Grid Item Properties

  • grid-column-start/end: Sets the horizontal placement of an item
  • grid-column: Shorthand for grid-column-start and grid-column-end
  • grid-row-start/end: Sets the vertical placement of an item
  • grid-row: Shorthand for grid-row-start and grid-row-end
  • grid-area: Sets grid item's grid area name or position
  • justify-self: Aligns a grid item inside its container along the inline axis
  • align-self: Aligns a grid item inside its container along the block axis
.grid-item {
    grid-column-start: 1;
    grid-column-end: 3;
    grid-column: 1 / 3;              /* Shorthand */
    grid-row: 1 / 3;                /* From row 1 to row 3 */
    grid-area: main;                 /* Or "row / col" format */
    justify-self: center;            /* Align horizontally */
    align-self: start;               /* Align vertically */
}

CSS Animations

CSS animations allow you to animate transitions from one CSS style to another. They offer more control than CSS transitions.

Animation Properties

  • @keyframes: Defines the animation keyframes
  • animation-name: Specifies the name of the @keyframes
  • animation-duration: Sets how long the animation takes
  • animation-timing-function: Defines the speed curve
  • animation-delay: Sets a delay before the animation starts
  • animation-iteration-count: Defines how many times the animation plays
  • animation-direction: Sets the direction of the animation
  • animation-fill-mode: Sets styles when the animation is not playing
  • animation-play-state: Controls whether the animation is running or paused
/* Define the animation */
@keyframes slideIn {
    0% {
        transform: translateX(-100%);
        opacity: 0;
    }
    100% {
        transform: translateX(0);
        opacity: 1;
    }
}

.animated-element {
    animation-name: slideIn;
    animation-duration: 0.5s;
    animation-timing-function: ease-out;
    animation-delay: 0s;
    animation-iteration-count: 1;
    animation-direction: normal;
    animation-fill-mode: forwards;
}

Transform Properties

Transforms allow you to rotate, scale, skew, or translate elements.

.transformed-element {
    transform: rotate(45deg);          /* Rotate 45 degrees */
    transform: scale(1.5);             /* Scale to 150% */
    transform: translate(50px, 20px);  /* Move 50px right, 20px down */
    transform: skew(20deg, 10deg);     /* Skew X and Y axes */
    
    /* Multiple transforms combined */
    transform: rotate(30deg) scale(1.2) translate(20px, 10px);
}

Responsive Design

Responsive design makes web pages render well on various devices and window or screen sizes.

Media Queries

Media queries allow you to apply different CSS rules based on device characteristics.

/* Mobile First Approach */
/* Base styles for all devices */
.container {
    width: 100%;
    padding: 10px;
}

/* Tablet styles */
@media screen and (min-width: 768px) {
    .container {
        width: 750px;
        margin: 0 auto;
    }
}

/* Desktop styles */
@media screen and (min-width: 1024px) {
    .container {
        width: 1000px;
    }
}

/* High-resolution displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .logo {
        background-image: url('logo@2x.png');
        background-size: 100px 50px;
    }
}

Responsive Units

  • em: Relative to the font-size of the element
  • rem: Relative to the font-size of the root element
  • %: Percentage of the parent element
  • vw/vh: Percentage of the viewport width/height
  • fr: Fractional unit for Grid layouts
/* Using responsive units */
.responsive-text {
    font-size: 1.5rem;      /* 1.5 times the root font size */
    width: 80%;             /* 80% of parent width */
    height: 50vh;           /* 50% of viewport height */
    margin: 2em;            /* 2 times the element's font size */
}

.grid-layout {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;  /* Flexible columns */
}