Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    { }CSS

    Styling foundations

    • What is CSS?
    • Cascade and specificity
    • Selectors and element states
    • Colours and text

    Boxes and sizing

    • Padding, border and margin
    • Units and flexible sizing
    • Overflow and image fitting

    Layout and responsiveness

    • Flexbox
    • CSS Grid
    • Positioning and stacking
    • Responsive design
    • Container-based responsiveness

    Design values, direction and motion

    • CSS custom properties and functions
    • Logical properties for RTL and LTR
    • CSS transitions and transforms
    • Keyframes and bounded animation
    • Project: a product card
    CSS

    Boxes and sizing · Lesson 5 of 17

    Padding, border and margin

    Every element is a box, and how to control its spacing.

    8 minutes3 games

    Every element is a box with 4 layers, inside out:

    • content: the content itself.
    • padding: space inside the box around the content.
    • border: the frame.
    • margin: space outside the box, between it and its neighbours.
    HTML
    <style>
      .card {
        padding: 16px;
        border: 3px solid #0ea5e9;
        margin: 20px;
        border-radius: 12px;
        background: #f0f9ff;
      }
    </style>
    
    <div class="card">Padding inside, margin outside.</div>
    <div class="card">Another card</div>
    margin
    padding
    content

    width on screen = 120 + 2×16 + 2×6 = 164px
    (margin adds space outside, not width)

    Important

    border-box includes padding and border in the declared width; margin remains outside.

    Interview question

    How wide is a 100px border-box with 10px padding?

    Put box-sizing: border-box on everything: the width you set then includes padding and border, which makes sizing far easier.

    Quick question

    You want space between a button's text and its border. Which property?

    What's the output?

    .box {
      box-sizing: content-box;
      width: 100px;
      padding: 10px;
      border: 5px solid;
    }

    What will this code print?

    Exercise

    Give .box 24px padding and a 2px solid border.

    HTML
    <style>
      .box {
        
      }
    </style>
    <div class="box">Box</div>
    

    Learn more from

    • MDN: CSS referencedeveloper.mozilla.org
    • web.dev: box-modelweb.dev

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousColours and textNextUnits and flexible sizing
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.