Session 1.6 – Basic CSS Properties
Module 1: Introduction to Web Design | Duration: 1 hr
Learning Objectives
By the end of this session, students will be able to:
- Understand and apply text alignment properties in CSS
- Master margin properties for spacing between elements
- Use padding properties for internal spacing within elements
- Control element display behavior using display property
- Understand the CSS Box Model concept
- Create well-structured layouts using these fundamental properties
Introduction
CSS properties control the visual presentation of HTML elements. Understanding fundamental properties like alignment, margin, padding, and display is essential for creating professional web layouts. These properties form the foundation of CSS styling and layout control.
Key Insight
The CSS Box Model is fundamental to understanding how these properties work together. Every HTML element is essentially a rectangular box with content, padding, border, and margin areas.
Text Alignment
The text-align property controls the horizontal alignment of text and inline content within an element.
Text Alignment Values
Visual Demonstration
Important Notes
text-alignonly affects inline content (text, images, inline elements)- It does not center block-level elements themselves
- Justify alignment can create uneven spacing in short lines
Margin Property
Margin creates space outside an element, between the element and its neighbors. Margins are transparent and don't have a background color.
Margin Syntax Options
Margin Values
Length Values
Special Values
Visual Example: Margin Spacing
Margin Collapse
When two vertical margins meet, they collapse into a single margin equal to the larger of the two. This doesn't happen with horizontal margins.
Padding Property
Padding creates space inside an element, between the content and the element's border. Padding is affected by the element's background color or image.
Padding Syntax
Visual Example: Padding vs No Padding
Padding with Background
Padding Characteristics
- Padding values cannot be negative (unlike margin)
- Padding adds to the total width and height of an element (unless using box-sizing: border-box)
- Padding inherits the element's background
- Percentage padding is calculated based on parent's width (even for top/bottom)
Display Property
The display property controls how an element is displayed in the document flow. It's one of the most important CSS properties for layout control.
Common Display Values
display: block
Element takes up full width available, starts on new line
display: inline
Element takes only needed width, flows with text
display: inline-block
Combines inline flow with block properties
display: none
Element is completely hidden and removed from flow
Note: Different from visibility: hidden which hides but keeps space
Modern Display Values
The CSS Box Model
Every HTML element is rendered as a rectangular box. The CSS box model describes how the dimensions of these boxes are calculated.
Box Model Components
Actual element content
Box Model Properties
Box-Sizing Property
The box-sizing property changes how width and height are calculated:
- content-box (default): width/height only includes content
- border-box (recommended): width/height includes content, padding, and border
Practical Examples
Example 1: Creating a Centered Card
Example 2: Navigation Bar with Inline-Block
Example 3: Article with Proper Spacing
Common Layout Patterns
Centering Block Element
Equal Spacing
Remove Margins
Session Summary
Key Points
- Text-align: Controls horizontal alignment of inline content (left, center, right, justify)
- Margin: Creates space outside elements, can be negative, margins collapse vertically
- Padding: Creates space inside elements, inherits background, always positive
- Display: Controls element layout behavior (block, inline, inline-block, none, flex, grid)
- Box Model: Every element is a box with content, padding, border, and margin
- Box-sizing: Use border-box for more intuitive width calculations
Best Practices
- Use
margin: 0 auto;to center block elements - Apply
box-sizing: border-box;globally for easier layouts - Use consistent spacing units (rem or px) throughout your design
- Reset default margins and padding on elements when needed
- Use padding for internal spacing, margin for external spacing
Next Session Preview
In the next session, we'll explore CSS styling for backgrounds and borders, learning how to create visually appealing designs with colors, gradients, images, and border styles.