#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS3 Syntax

CSS syntax is the set of rules for how a stylesheet is written: which element to target, which property to change, and what value to give it. Every style you write follows the same basic pattern, so once the pattern is clear you can read and write any CSS file.

Anatomy of a rule

A CSS rule, also called a ruleset, has two parts. The selector chooses which elements the rule applies to. The declaration block, inside curly braces, lists what to change.

/* selector     declaration block */
h1 {
  color: #1a1a2e;        /* property: value; */
  font-size: 2rem;
  margin-bottom: 0.5em;
}

.price,
.discount {
  font-weight: 700;
}

nav a:hover {
  text-decoration: underline;
}

Each declaration is a property name, a colon, a value, and a semicolon. The semicolon separates declarations; it is optional after the last one, but adding it every time saves you from bugs when you add another line later.

Selectors you will meet first

SelectorMatches
pEvery <p> element (type selector).
.priceElements with class="price".
#headerThe element with id="header".
nav a<a> elements anywhere inside <nav>.
a:hoverLinks while the pointer is over them (pseudo-class).
.price, .discountBoth groups, separated by a comma.

A comma groups selectors so they share one declaration block. A space means "inside of", which is a completely different thing, and mixing the two up is a classic beginner mistake.

Values, units and comments

Values can be keywords (bold, center), numbers with units (16px, 1.5rem, 50%), colours (#ff6600, rgb(255 102 0)), or functions like calc(100% - 2rem). A zero length needs no unit, so margin: 0 is fine. Any other number needs one: width: 200 is invalid and gets ignored.

Comments start with /* and end with */. CSS has no single-line // comment, and using one can quietly break the rule that follows.

What the browser does with errors

CSS is forgiving. When the browser finds a declaration it does not understand, such as a misspelled property or a value without a unit, it skips just that declaration and carries on with the rest. This is great for compatibility, but it also means mistakes fail silently. Nothing crashes; the style simply does not appear.

When a style "does not work", check these in order:

  1. Is the property name spelled correctly, with a hyphen where needed (background-color, not backgroundcolor)?
  2. Did a missing colon, semicolon or closing brace swallow the next declaration or rule?
  3. Does the selector actually match the element? A class needs a dot, an id needs a hash.
  4. Is another rule with higher specificity overriding it? The browser's developer tools show crossed-out declarations.

Where CSS can be written

The same syntax works in an external .css file linked with <link rel="stylesheet">, inside a <style> element in the page head, or in a style attribute. In the attribute version there is no selector and no braces, only declarations. External files are the usual choice, since one file can style a whole site.

Takeaway: selector, braces, then property: value; pairs. Keep that shape consistent and use developer tools to spot the declarations the browser skipped.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course