Mastering CSS: A Beginner’s Guide to Styling Web Pages with Flexbox, Grid, and More
CSS (Cascading Style Sheets) is the language used to style and design web pages. The term "cascading" means that styles applied to parent elements cascade down to child elements unless overridden.
At its core, CSS involves two key actions:
- Selecting elements on a webpage.
- Styling those elements based on the selection.
Inline Styling
Inline styling is applied directly within the HTML element:
Here, the style
attribute specifies the styling directly within
the line of code.
Page Styling (Internal CSS)
Add a <style>
tag inside the <head>
to
define styles for the entire page:
Using Classes and IDs:
-
Classes: Used for multiple elements, selected using a dot (
.
). -
IDs: Unique to a single element, selected using a hash (
#
).
External CSS
Create a separate .css
file for styling and link it to your HTML
file:
Box Model
The box model determines how much space an element occupies:
- Content: Actual content of the element.
- Padding: Space between content and border.
- Border: Surrounds the padding.
- Margin: Space outside the border, separating elements.
Example:
-
content-box
: Default behavior where padding and border are added to content width. -
border-box
: Includes padding and border within the total width.
Flexbox
Flexbox provides powerful layout control for aligning and positioning elements.
Basic Syntax:
Key Properties:
-
justify-content
: Aligns items along the main axis (e.g., center, space-around). -
align-items
: Aligns items along the cross axis (e.g., center). flex-wrap
: Controls whether items wrap to the next line.
Grid Layout
CSS Grid enables precise control over rows and columns.
Example:
Control inner elements using grid-column
and
grid-row
:
Media Queries
Use media queries to make styles responsive:
Selectors Overview
-
Universal Selector:
* {}
(Applies to all elements) -
Type Selector:
h1, p {}
(Targets specific elements) - Class Selector:
.classname {}
- ID Selector:
#id {}
-
Pseudo-classes:
a:hover {}
(Styling on user action) - Pseudo-elements:
p::first-letter {}
Comments
Post a Comment
Please share your opinions and suggestion and do not enter any spam links in the comment box