Comprehensive Guide to HTML Elements, CSS Display Properties, and the CSS Box Model
HTML Block-Level Elements and Container Structuring
Boilerplate Generation Shortcut
Pressing
Shift + 1in modern code editors (such as Visual Studio Code using Emmet) automatically generates the standard HTML5 boilerplate template structure inside a file namedBox model.html.
Default Behavior of Block-Level Elements
Heading tags such as
<h1>,<h2>, and<h3>are block-level elements by default in HTML.Block-level elements always start on a new line and occupy the entire horizontal width available in their parent container's row.
Even when consecutive block elements are written adjacent to each other on the same line in the source code file (e.g.,
<h1>welcome</h1><h2>Welcome to my</h2><h3>website</h3>), the browser forces each element onto its own distinct row.
Encapsulation Using Outer Containers
The
<div>(Division) tag acts as a generic outer structural container or bag to group multiple HTML elements together.Placing heading elements inside an outer
<div>wrapper groups them logically, but does not inherently alter their default layout or visual appearance on the browser page.
CSS Display Property Manipulation: Block vs. Inline
Linking Stylesheets
External CSS stylesheets (e.g.,
style.cssorstyles.css) are linked inside the HTML document using standard link syntax in the head element.
Outer Container Display Adjustments
Changing the display property of an outer container (
div) todisplay: inline;does not automatically alter the block-level nature of its child elements.Because child elements like
<h1>,<h2>, and<h3>maintain their block-level properties internally, they continue to reserve full rows and create newlines.
Individual Element Conversion via IDs
To place block-level elements onto a single horizontal line, each individual element's display behavior must be changed from
blocktoinline.First Element: Assigning
id="1st"to<h1>and declaring#1st { display: inline; }removes its block-level row reservation, allowing subsequent content to sit on the same line.Middle Element: Assigning
id="middle"to<h2>and declaring#middle { display: inline; }alters its behavior to inline, though subsequent block elements still force newlines.Last Element: Assigning
id="last"to<h3>and declaring#last { display: inline; }removes its newline enforcement.Once all three heading elements are explicitly set to
display: inline;, all three text strings (welcome,Welcome to my,website) render continuously on a single line.
Element Selection and Group Styling: IDs vs. Classes
Unique IDs vs. Reusable Classes
IDs (
id="...") uniquely target single individual elements. Stylistically modifying multiple elements using unique IDs requires repetitive CSS selectors (#1st,#middle,#last) and excessive code.Classes (
class="...") group multiple HTML elements together under a single identifier for identical style application.
Class Selector Implementation
Assigning a shared class attribute to all elements (e.g.,
<h1 class="heading">,<h2 class="heading">,<h3 class="heading">) allows targeting them simultaneously.In CSS, class selectors are denoted using dot notation (e.g.,
.heading).
Dynamic Display Toggling
Setting
.heading { display: inline; }toggles all associated headings into inline elements, placing them on a single row.Setting
.heading { display: block; }reverts all elements back to their default block-level behavior, forcing each heading to start on a new line and block adjacent elements.
CSS Box Model: Padding, Border, and Margin
Box Model Core Concepts
Every HTML element is treated as a rectangular box consisting of the central content area, internal padding, surrounding borders, and external margins.
Padding Property
Padding adds internal clearance space between the actual element content and its surrounding border.
padding-top: Adds internal top spacing. Tested values include4px,40px, and100px.padding-left: Adds internal left spacing. Tested values include4pxand40px.
Border Property
Border adds an external visual frame surrounding the element's padding region.
border-top: Defines the top border dimensions (e.g.,10px).
Margin Property
Margin creates external clearance space between the border of the element and adjacent surrounding elements.
margin-top: Defines top external space (e.g.,20px).
CSS Spacing Shorthand Notations
Shorthand Syntax
Rather than declaring individual directional properties (
padding-top,padding-right,padding-bottom,padding-left), CSS provides shorthand syntax to declare all four directional spaces in a single line.
Four-Value Clockwise Order
Directional values in shorthand notation are evaluated in clockwise order:
Top,Right,Bottom,Left.Example syntax format:
padding: 10px 10px 10px 10px;sets equal top, right, bottom, and left internal spaces of10px.Two-value combinations (such as specifying top/bottom alongside left values like
100px) streamline layout management and clean up redundant rule declarations.