- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Syntax
Writing CSS is essentially like giving a set of design instructions to a web browser. The syntax of CSS (Cascading Style Sheets) is straightforward once you understand its "grammar." A CSS rule-set consists of a selector and a declaration block:
- Selectors: Think of selectors as the "address" on an envelope. They tell the browser exactly which HTML elements you want to style.
- Element selectors: Target all tags of a specific type (e.g.,
ptargets all paragraphs). - Class selectors: Target elements with a specific class attribute (e.g.,
.btn-primary). These are reusable. - ID selectors: Target a single, unique element (e.g.,
#main-header). These should only be used once per page. - Attribute selectors: Target elements based on their attributes (e.g.,
[type="text"]targets text input fields).
- Element selectors: Target all tags of a specific type (e.g.,
.card) for styling instead of IDs (like #card-1). Classes are reusable and keep your code flexible, whereas IDs are highly specific and can make your CSS harder to override later.
- Declaration Block: This is the container for your styles, wrapped in curly braces {}. It tells the browser, "Apply these specific looks to the element I just selected." Each instruction inside is called a declaration.
- Properties: These are the specific features you want to change. CSS has hundreds of properties, such as
color,margin,font-family, andborder. - Values: Values are the specific settings for a property. For example, if the property is
color, the value might beredor#ff0000.
;) at the end of a declaration. If you miss one, the browser might ignore the next line of code, leading to frustrating layout bugs.
Here's an example of the basic syntax of a CSS rule:
selector {
property: value;
property: value;
/* Each line inside the braces is a declaration */
}
For instance, if you are building a blog and want all your paragraphs to be easy to read with a bit of extra space, you would use this rule:
p {
color: #333333;
font-size: 1.1rem;
line-height: 1.6;
}
rem or em units for font sizes instead of px. This ensures your text scales correctly if a user changes their browser's default font size for accessibility.
Selectors can be combined to target specific elements more precisely. For example, if you want to style a "Submit" button differently than a standard link, you might use a class:
.submit-button {
font-weight: bold;
background-color: #28a745;
color: white;
padding: 10px 20px;
border-radius: 5px;
}
Or, to target a unique navigation bar that appears only once on your page, you might use an ID:
#main-navigation {
border-bottom: 2px solid #eaeaea;
display: flex;
justify-content: space-between;
}
Additionally, CSS supports comments to help you (and your teammates) understand why certain styles were added. In standard CSS, comments must be wrapped in /* */.
/* Standard CSS Comment */
.hero-section {
height: 400px; /* Set a fixed height for the landing image */
}
// for comments in a standard .css file. While // works in modern pre-processors like SASS or Less, it will cause errors in plain CSS and may stop your styles from loading.
This basic syntax forms the foundation of CSS. By mastering how to select elements and apply properties, you gain full control over the visual presentation of your web projects, from simple text tweaks to complex, responsive layouts.