Skip to main content

CSS Exercises: Write your first rule: practice exercise

HTML5andCSS3 logo

0 of 103 solved

Make the #title blue.

Acceptance criteria

  1. 1.#title has color: rgb(0, 0, 255)

1
0 × 0

Your page appears here. Press Run to see it, or Submit to check your answer.

CSS exercises with solutions

Pick a result and write the CSS that makes it. Every exercise here runs without an account.

Exercises
103
Topics
18
Solved by you
0

103 shown

CSS Introduction

  1. Show solution
    #title { color: blue; }
  2. Show solution
    p { color: green; }
  3. 3. Add a stylesheet rule

    In this stylesheet, make #text navy.

    More on How to Add CSS
    Show solution
    #text { color: navy; }
  4. 4. Comment out a rule

    Write a red rule and a blue rule for #text, then comment out the red one.

    More on CSS Comments
    Show solution
    /* #text { color: red; } */
    #text { color: blue; }

CSS Foundations

  1. 5. Inherit a colour

    Set the colour on #parent so #box inherits red.

    More on Cascade & Inheritance
    Show solution
    #parent { color: red; }
  2. 6. Let the id win

    Write a .card rule that makes the text blue and an #one rule that makes it red. The id should win.

    More on CSS Specificity
    Show solution
    .card { color: blue; }
    #one { color: red; }
  3. 7. Use pixels

    Give #box a 150px width.

    More on CSS Units
    Show solution
    #box { width: 150px; }
  4. 8. Switch to border-box

    Make #box use border-box sizing.

    More on Box Sizing
    Show solution
    #box { box-sizing: border-box; }
  5. 9. Define a variable

    Define --brand on :root and use it for the box colour.

    More on CSS Variables
    Show solution
    :root { --brand: red; }
    #box { color: var(--brand); }
  6. Show solution
    #box { width: calc(100px + 50px); }

CSS Selectors

  1. Show solution
    p { color: red; }
  2. 12. Style reusable feature cards

    Use the .feature class to give all three cards a teal background, white text, and a 12px border radius.

    More on CSS Class Selector
    Show solution
    .features {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 12px;
      font-family: Arial, sans-serif;
    }
    
    .feature {
      padding: 24px 12px;
      text-align: center;
      background-color: #007a96;
      color: #ffffff;
      border-radius: 12px;
    }
  3. Show solution
    #one { color: red; }
  4. Show solution
    * { box-sizing: border-box; }
  5. 15. Group two elements

    Make both p and span red with one rule.

    More on CSS Group Selector
    Show solution
    p, span { color: red; }
  6. Show solution
    .card p { color: red; }
  7. 17. Match attribute presence

    Style any element that has a data-role attribute.

    More on CSS Attribute Selectors
    Show solution
    [data-role] { color: red; }
  8. 18. Style the first child

    Make the first .card red using :first-child.

    More on CSS Pseudo Classes
    Show solution
    .card:first-child { color: red; }
  9. 19. Add content before

    Add a ::before marker to .text and colour the paragraph red.

    More on CSS Pseudo Elements
    Show solution
    .text { color: red; }
    .text::before { content: "-> "; }

CSS Colors and Backgrounds

  1. Show solution
    #box { color: red; }
  2. 21. Use a six-digit hex

    Set the #box text colour to #ff5733.

    More on CSS HEX Colors
    Show solution
    #box { color: #ff5733; }
  3. 22. Use rgb()

    Set the #box text colour to rgb(255, 87, 51).

    More on CSS RGB Colors
    Show solution
    #box { color: rgb(255, 87, 51); }
  4. 23. Use oklch()

    Set the #box text colour to oklch(0.7 0.15 200).

    More on Modern Colors
    Show solution
    #box { color: oklch(0.7 0.15 200); }
  5. Show solution
    #box { background-color: #f0f0f0; }
  6. 25. Add a gradient

    Give #box a linear gradient from red to blue.

    More on CSS Background Image
    Show solution
    #box { background-image: linear-gradient(red, blue); }
  7. 26. Stack two gradients

    Layer a red-to-blue gradient over a green-to-yellow one.

    More on Layered Backgrounds
    Show solution
    #box { background-image: linear-gradient(red, blue), linear-gradient(green, yellow); }

CSS Text & Fonts

  1. 27. Colour a paragraph

    Set the #text colour to #333333.

    More on CSS Text Color
    Show solution
    #text { color: #333333; }
  2. Show solution
    #text { text-align: center; }
  3. Show solution
    #text { text-decoration: underline; }
  4. 30. Set a font stack

    Use Arial with a sans-serif fallback on #text.

    More on CSS Font Family
    Show solution
    #text { font-family: Arial, sans-serif; }
  5. Show solution
    #text { font-size: 24px; }
  6. Show solution
    #text { font-weight: bold; }
  7. Show solution
    #text { white-space: nowrap; }

CSS Box Model

  1. 34. Add margin on all sides

    Give #box a 20px margin on every side.

    More on CSS Margin
    Show solution
    #box { margin: 20px; }
  2. 35. Add padding on all sides

    Give #box 16px padding on every side.

    More on CSS Padding
    Show solution
    #box { padding: 16px; }
  3. 36. Add a solid border

    Give #box a 2px solid red border.

    More on CSS Border
    Show solution
    #box { border: 2px solid red; }
  4. 37. Add an outline

    Give #box a 3px solid outline.

    More on CSS Outline
    Show solution
    #box { outline: 3px solid #0f172a; }
  5. Show solution
    #box { width: 200px; }

CSS Layout

  1. 39. Pull an element out of flow

    Float #box so the text wraps beside it.

    More on Normal Flow
    Show solution
    #box { float: left; }
  2. 40. Use inline

    Make the #text paragraph flow inline.

    More on CSS Display
    Show solution
    #text { display: inline; }
  3. 41. Shift with relative

    Move #box 10px down without leaving the flow.

    More on CSS Position
    Show solution
    #box { position: relative; top: 10px; }
  4. Show solution
    #wrap { position: relative; }
  5. 43. Float left

    Float #box to the left.

    More on CSS Float
    Show solution
    #box { float: left; }
  6. 44. Clip the overflow

    Hide anything that overflows #box.

    More on CSS Overflow
    Show solution
    #box { overflow: hidden; }
  7. 45. Raise an element

    Give #box a z-index of 10 with relative positioning.

    More on CSS Z-index
    Show solution
    #box { position: relative; z-index: 10; }
  8. 46. Set a min-content minimum

    Give #box a min-width of min-content.

    More on Intrinsic Sizing
    Show solution
    #box { min-width: min-content; }
  9. Show solution
    #box { aspect-ratio: 16 / 9; }
  10. Show solution
    #wrap { column-count: 3; }

CSS Flexbox

  1. 49. Space out a navbar

    Make .navbar a flex container with its items spread apart using justify-content: space-between.

    More on Flex Container
    Show solution
    .navbar {
      display: flex;
      justify-content: space-between;
      padding: 16px 24px;
      background: white;
      border-radius: 10px;
    }
  2. 50. Stack items in a column

    Set the flex direction to column.

    More on Flex Direction
    Show solution
    #row { display: flex; flex-direction: column; }
  3. Show solution
    #row { display: flex; justify-content: center; }
  4. 52. Centre on the cross axis

    Centre the items vertically in a row.

    More on Align Items
    Show solution
    #row { display: flex; align-items: center; }
  5. 53. Allow wrapping

    Let flex items wrap onto new lines.

    More on Flex Wrap
    Show solution
    #row { display: flex; flex-wrap: wrap; }
  6. 54. Let items grow

    Give every item a flex-grow of 1.

    More on Flex Item Sizing
    Show solution
    #row { display: flex; }
    .item { flex-grow: 1; }

CSS Grid

  1. 55. Build a 3-column gallery

    Make .gallery a grid container with exactly three equal columns, using grid-template-columns: repeat(3, 1fr).

    More on CSS Grid Container
    Show solution
    .gallery {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 12px;
    }
    .tile { height: 100px; background: white; border-radius: 8px; display: flex; align-items: center; justify-content: center; }
  2. Show solution
    #grid { display: grid; grid-template-columns: 100px 200px; }
  3. 57. Define three rows

    Create rows of 60px, 80px and 100px.

    More on CSS Grid Rows
    Show solution
    #grid { display: grid; grid-template-rows: 60px 80px 100px; }
  4. 58. Add a uniform gap

    Give the grid a 16px gap on both axes.

    More on CSS Grid Gap
    Show solution
    #grid { display: grid; gap: 16px; }
  5. Show solution
    #grid { display: grid; grid-template-columns: repeat(3, 100px); gap: 12px; }
  6. 60. Span two columns

    Make every cell span two columns.

    More on Grid Placement
    Show solution
    #grid { display: grid; grid-template-columns: repeat(4, 60px); }
    .cell { grid-column: span 2; }
  7. 61. Name a single row of areas

    Define one row with three named areas.

    More on Grid Areas
    Show solution
    #grid { display: grid; grid-template-areas: "a b c"; }
  8. 62. Make the child a grid

    Turn .cell into its own grid container.

    More on CSS Subgrid
    Show solution
    #grid { display: grid; grid-template-columns: 100px 100px; }
    .cell { display: grid; }

CSS Lists, Tables & Forms

  1. 63. Change the marker

    Give #list square markers.

    More on CSS Lists
    Show solution
    #list { list-style-type: square; }
  2. 64. Collapse the borders

    Merge the cell borders of #table into single lines.

    More on CSS Tables
    Show solution
    #table { border-collapse: collapse; }
  3. 65. Style the input

    Give #input 8px padding and a 1px solid #cccccc border.

    More on CSS Form Styling
    Show solution
    #input { padding: 8px; border: 1px solid #cccccc; }
  4. 66. Round the field

    Give #input a 6px border radius.

    More on CSS Input Fields
    Show solution
    #input { border-radius: 6px; }

CSS Effects

  1. 67. Add a box shadow

    Give #box a red shadow offset 2px right, 4px down, blurred 6px.

    More on CSS Shadows
    Show solution
    #box { box-shadow: 2px 4px 6px red; }
  2. 68. Angle a gradient

    Give #box a 45deg red-to-blue gradient.

    More on CSS Gradients
    Show solution
    #box { background-image: linear-gradient(45deg, red, blue); }
  3. Show solution
    #box { filter: blur(4px); }
  4. 70. Half-fade an element

    Set #box to 50% opacity using a decimal.

    More on CSS Opacity
    Show solution
    #box { opacity: 0.5; }
  5. 71. Clip to a circle

    Clip #box to a circle covering half its width.

    More on Clipping & Masking
    Show solution
    #box { clip-path: circle(50%); }
  6. 72. Blend an element

    Blend #box with what is behind it using multiply.

    More on Blending & Backdrop
    Show solution
    #box { mix-blend-mode: multiply; }
  7. 73. Smooth the scrolling

    Make scrolling inside #wrap animate smoothly.

    More on Scroll Snap
    Show solution
    #wrap { scroll-behavior: smooth; }

CSS Animations & Transitions

  1. 74. Rotate an element

    Rotate #box by 45 degrees using the rotate property.

    More on CSS Transform
    Show solution
    #box { rotate: 45deg; }
  2. 75. Scale with a function

    Use transform to scale #box to twice its size.

    More on CSS 2D Transform
    Show solution
    #box { transform: scale(2); }
  3. 76. Add perspective

    Give #wrap an 800px perspective.

    More on CSS 3D Transform
    Show solution
    #wrap { perspective: 800px; }
  4. 77. Choose what transitions

    Transition only the opacity of #box.

    More on CSS Transitions
    Show solution
    #box { transition-property: opacity; }
  5. 78. Allow discrete transitions

    Let #box transition discrete properties such as display.

    More on Entry & Exit Transitions
    Show solution
    #box { transition-behavior: allow-discrete; }
  6. 79. Name the animation

    Attach the spin keyframes to #box.

    More on CSS Animations
    Show solution
    @keyframes spin { from { rotate: 0deg; } to { rotate: 360deg; } }
    #box { animation-name: spin; }
  7. 80. Drive an animation by scroll

    Tie the animation on #box to the scroll progress.

    More on Scroll Animations
    Show solution
    @keyframes fade { from { opacity: 0; } to { opacity: 1; } }
    #box { animation-name: fade; animation-timeline: scroll(); }
  8. 81. Name a transitioned element

    Give #box the view transition name card.

    More on View Transitions
    Show solution
    #box { view-transition-name: card; }
  9. 82. Define a motion path

    Move #box along a diagonal path from 0,0 to 100,100.

    More on Motion Paths
    Show solution
    #box { offset-path: path('M 0 0 L 100 100'); }

CSS Responsive Design

  1. Show solution
    @media (max-width: 600px) {
      #text { font-size: 14px; }
    }
  2. 84. Never overflow the screen

    Stop .cell growing wider than its container.

    More on Responsive Layout
    Show solution
    .cell { max-width: 100%; }
  3. 85. Start with one column

    One column by default, three from 700px up.

    More on Mobile First Design
    Show solution
    #grid { display: grid; grid-template-columns: 100px; }
    @media (min-width: 700px) {
      #grid { grid-template-columns: 100px 100px 100px; }
    }
  4. 86. Make an element a container

    Let #wrap be queried on its inline size.

    More on Container Queries
    Show solution
    #wrap { container-type: inline-size; }
  5. 87. Animate only when welcome

    When no reduced motion is requested, make #text teal.

    More on Preference Media Queries
    Show solution
    @media (prefers-reduced-motion: no-preference) {
      #text { color: teal; }
    }
  6. 88. Test for a feature

    Use grid for #grid only where grid is supported.

    More on Feature Queries
    Show solution
    @supports (display: grid) {
      #grid { display: grid; }
    }

Modern CSS Authoring

  1. 89. Declare the layer order

    Order base then theme, and let theme's blue win over base's red.

    More on Cascade Layers
    Show solution
    @layer base, theme;
    @layer theme { #text { color: blue; } }
    @layer base { #text { color: red; } }
  2. 90. Nest a rule

    Inside a #wrap rule, nest a p rule that colours the text blue.

    More on Nesting & Scope
    Show solution
    #wrap {
      color: red;
      & p { color: blue; }
    }
  3. 91. Space on the inline axis

    Give #text a 20px margin at the inline start.

    More on Logical Properties
    Show solution
    #text { margin-inline-start: 20px; }
  4. 92. Start a counter

    Reset a counter named section to 0 on #list.

    More on Generated Content
    Show solution
    #list { counter-reset: section 0; }

CSS Accessibility

  1. 93. Keep focus visible

    Give #link a 2px solid navy outline instead of removing it.

    More on Accessible CSS
    Show solution
    #link { outline: 2px solid navy; }
  2. 94. Opt into dark UI

    Tell the browser #wrap renders dark.

    More on CSS Theming
    Show solution
    #wrap { color-scheme: dark; }
  3. 95. Tint the checkbox

    Give #check a red accent colour.

    More on Modern Form Styling
    Show solution
    #check { accent-color: red; }
  4. Show solution
    #btn { cursor: pointer; }

CSS Output & Performance

  1. 97. Keep a block together

    Stop #wrap being split across pages.

    More on Print Styles
    Show solution
    #wrap { break-inside: avoid; }
  2. 98. Contain the layout

    Isolate #wrap's layout from the rest of the page.

    More on CSS Performance
    Show solution
    #wrap { contain: layout; }
  3. 99. Centralise a token

    Define --space as 16px on #wrap and pad with it.

    More on CSS Architecture
    Show solution
    #wrap { --space: 16px; padding: var(--space); }
  4. 100. Outline an element

    Outline #wrap in 2px solid red to see its box.

    More on Debugging CSS
    Show solution
    #wrap { outline: 2px solid red; }

CSS Reference

  1. 101. Style below a width

    At 500px wide, make #box red inside a max-width media query.

    More on CSS At-rules
    Show solution
    @media (max-width: 600px) {
      #box { color: rgb(255, 0, 0); }
    }
  2. 102. Calculate a width

    Make #box 100px wide using calc.

    More on CSS Functions
    Show solution
    #box { width: calc(60px + 40px); }

CSS Projects

  1. Show solution
    #list { display: flex; }

520 CSS exercises in all

The first in every lesson is free. The rest come with the practice plan, and your first month costs nothing.

See the practice plan

Stuck on a topic?

Read the lesson in the CSS tutorial, then come back and solve it.

CSS tutorial

Need the HTML first?

Practice the tags these exercises style.

HTML exercises

Ready to build?

Build one complete responsive page, step by step.

Start the project