Div Tags, ID Selectors, & Basic Page Formatting
Master HTML div tags and CSS fundamentals
Core Concepts in This Tutorial
Div Tag Structure
Learn to create block-level containers that stack vertically and group content sections for better organization.
CSS ID Selectors
Master unique identifiers for styling specific elements with the hash (#) selector syntax.
Layout Control
Implement width, padding, margins, and centering techniques for professional page layouts.
If you completed the previous exercise, index.html should still be open in your code editor, and you can proceed directly to the next section. If you closed the file, navigate to your News website folder and reopen index.html. We strongly recommend completing the foundational exercises (1C–2A) before tackling this advanced layout work, as they establish essential concepts you'll build upon here.
Div vs Span Elements
Feature Div Tags Span Tags Display Type Block elements Inline elements Layout Behavior Stack vertically Flow horizontally Use Cases Page sections, containers Text styling, small portions Recommended: Use div tags for major content sections and layout containers
Since professional websites typically contain multiple divs for different content sections, we need a way to target this specific div with CSS. Add a unique identifier to distinguish it from future divs:
<body>
<div id="wrapper">
<h1>
This ID creates a unique hook for CSS styling. Remember: IDs must be unique within each HTML page (unlike classes, which can be applied to multiple elements), and they should use descriptive names without spaces or special characters.
Now we'll create the CSS rule to control this div's width. In your style section, add the following rule between the existing p and .author rules:
p {
Code Omitted To Save Space
}
#wrapper {
width: 580px;
}.author {
Code Omitted To Save Space
}
The hash symbol (#) creates an ID selector, telling CSS to target the element with that specific ID. ID selectors have high specificity in CSS, making them powerful tools for precise styling control.
Replace the fixed width with a flexible maximum width approach:
#wrapper {
max-width: 580px;
}
This modern CSS technique provides the best of both worlds: controlled width on large screens and flexible adaptation on smaller devices.
Test the new responsive behavior by resizing your browser window. The content now intelligently adapts: maintaining the 580-pixel maximum width on larger screens while fluidly resizing to fit smaller viewports without horizontal scrolling. This approach ensures optimal user experience across all devices—a critical consideration in today's mobile-first web environment.
Essential Keyboard Shortcuts
Basic Indentation
Use Tab to indent and Shift-Tab to unindent selected lines of code quickly.
Advanced Shortcuts
Mac users: Cmd-] to indent, Cmd-[ to unindent. Windows users: Ctrl-] to indent, Ctrl-[ to unindent.
Apply background colors to both the body and wrapper div to create visual depth. The body background shows around the content area while the wrapper background highlights the main content.
Padding Implementation
Setting left and right margins to 'auto' automatically makes them equal, centering any element with a defined width within its container. This is the standard method for horizontal centering in CSS.
Margin vs Padding
| Feature | Margin | Padding |
|---|---|---|
| Location | Outside the element | Inside the element |
| Affects | Space between elements | Space within element borders |
| Background Color | Does not show | Shows element background |
Border Specifications
Border Style Options
Solid
Creates a continuous unbroken line around the element. Most commonly used for clean, professional designs.
Dashed
Produces a series of short dashes. Useful for creating subtle separations or indicating optional sections.
Dotted
Creates a series of dots around the border. Good for decorative purposes or informal designs.
Key Takeaways
