Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

CSS Box Model and Layout

Lesson 10 of 34 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

CSS Box Model and Layout

The CSS Box Model

Every HTML element is a rectangular box with four areas:

+---------------------------+
|         MARGIN            |
|  +---------------------+  |
|  |      BORDER         |  |
|  |  +---------------+  |  |
|  |  |    PADDING    |  |  |
|  |  |  +---------+  |  |  |
|  |  |  | CONTENT |  |  |  |
|  |  |  +---------+  |  |  |
|  |  +---------------+  |  |
|  +---------------------+  |
+---------------------------+
.box {
    width: 300px;       /* Content width */
    padding: 20px;      /* Inside spacing */
    border: 2px solid black;
    margin: 10px;       /* Outside spacing */
}
/* Total width = 300 + 40 + 4 + 20 = 364px */

box-sizing Property

/* Default: content-box — width = content only */
.default { box-sizing: content-box; }

/* Better: border-box — width includes padding + border */
* { box-sizing: border-box; }

Display Property

ValueBehavior
blockFull width, new line (div, p, h1)
inlineFlows with text (span, a)
inline-blockInline but accepts width/height
noneHidden, removed from flow
flexFlexbox container
gridGrid container

Positioning

/* Static (default) */
.static { position: static; }

/* Relative — offset from its normal position */
.relative { position: relative; top: 10px; left: 20px; }

/* Absolute — positioned relative to nearest positioned ancestor */
.absolute { position: absolute; top: 0; right: 0; }

/* Fixed — relative to viewport */
.fixed { position: fixed; bottom: 20px; right: 20px; }

/* Sticky — relative until scrolled past threshold */
.sticky { position: sticky; top: 0; }

Float and Clear

img { float: left; margin-right: 15px; }
.clearfix::after { content: ""; display: table; clear: both; }

Overflow

.container {
    width: 200px;
    height: 100px;
    overflow: auto;   /* scroll | hidden | visible */
}

Z-index and Stacking Context

.modal {
    position: fixed;
    z-index: 1000;   /* Higher = on top */
}
Key Takeaway: The CSS box model (content, padding, border, margin) defines how elements take up space. Use box-sizing: border-box globally and understand positioning modes for precise layouts.