close
The Wayback Machine - https://web.archive.org/web/20240922133949/https://www.geeksforgeeks.org/css-syntax/
Open In App

CSS Syntax

Last Updated : 23 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A CSS Syntax rule consists of a selector, property, and its value. The selector points to the HTML element where the CSS style is to be applied. The CSS property is separated by semicolons. It is a combination of the selector name followed by the property: value pair that is defined for the specific selector. let us see the syntax and how we can use the CSS to modernize the website.

Syntax:

selector { Property: value; }

For instance, we have declared a heading tag (h1) along with having assigned some property: value pair that is used to style the heading tag. Here, h1 is the selector, { color: green; font-family: sans-serif; } is a declaration block and it can contain one or more declarations separated by semicolons, color: green; is a property: value pair that is applied to the HTML element to style them.

CSS-Syntax

Every declaration has a CSS property name and a value, separated by a colon(:) and is surrounded by curly braces({ }). For declaring the multiple CSS properties, it can be separated by the semicolon(;).

Let’s define each of these:

  • Declaration: A combination of a property and its corresponding value.
  • Selector: Used to target and select specific HTML elements to apply styles to.
  • Property: Defines the specific aspect or characteristic of an element that you want to modify.
  • Value: Assigned setting or parameter for a given property, determining how the selected element should appear or behave.

Example: This example illustrates the use of CSS Syntax for the styling of HTML elements.

HTML
<!DOCTYPE html>
<html>
  
<head>
    <!-- Style on h1 elements -->
    <style>
        h1 {
            color: green;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
</body>
  
</html>

Output: In the above code, h1 is the selector of h1 tags, it select the h1 element that you want to style. The color is a property and green is the value of that property, similar text-align is the property and value of that property is center.

CSS Syntax and Selectors


To learn more about the CSS Selectors, and Properties please go through the attached links.

CSS Syntax – FAQs

What is the basic syntax of CSS?

CSS syntax consists of a selector and a declaration block. The selector targets the HTML element, and the declaration block contains one or more declarations separated by semicolons.

What are CSS selectors?

Selectors are used to target HTML elements to apply styles. Common types include element selectors, class selectors, ID selectors, and attribute selectors.

What is a declaration block?

A declaration block is a part of the CSS rule that contains one or more declarations, each consisting of a property and a value, enclosed in curly braces {}.

What is a CSS property?

A CSS property is an aspect of an element you want to style, such as color, font-size, margin, or padding.

What is a CSS value?

A CSS value specifies the setting for a CSS property, such as blue for color, 16px for font-size, 10px for margin, etc.

What is the syntax for a CSS rule?

A CSS rule consists of a selector followed by a declaration block. For example, selector { property: value; }.


Previous Article
Next Article

Similar Reads

Explain the benefits of spread syntax &amp; how it is different from rest syntax in ES6 ?
Spread Operator: Spread operator or Spread Syntax allow us to expand the arrays and objects into elements in the case of an array and key-value pairs in the case of an object. The spread syntax is represented by three dots (...) in JavaScript. Syntax: var my_var = [...array]; Benefits of using Spread syntax: 1. It allows us to include all elements
4 min read
ES6 Top features and syntax
ES6 or as it is officially called: ECMAScript2015 is a new JavaScript implementation and is arguably the hottest topic in the JS developer's conventions and meetups why it should not be: JavaScript rules the web and is gaining a foothold in every other field possible, be it robotics(nodebots), desktop applications(using ion framework), chatbots, et
3 min read
XML | Syntax
Prerequisite: XML | Basics In this article, we are going to discuss XML syntax rule which is used while writing an XML document or an XML application. It is a very simple and straight forward to learn and code. Below is a complete XML document to discuss each component in detail. C/C++ Code &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=
3 min read
Shorthand Syntax for Object Property Value in ES6
Objects in JavaScript are the most important data-type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScript primitive data-types (Number, String, Boolean, null, undefined, and symbol) in the sense that while these primitive data-types all store a single value each (depending on their types). The sho
1 min read
SASS Syntax
SASS supports two types of syntax. Each one can be differently used to load your required CSS or even the other syntax. 1. SCSS: The SCSS syntax uses .scss file extension. It is quite similar to CSS. You can even say that SCSS is a superset of CSS, meaning that all the valid CSS is also valid SCSS too. Due to its similarity with CSS, it is the easi
2 min read
How to do HTML syntax highlighting inside PHP strings ?
Syntax highlighting is the work of code editors such as Sublime Text, Visual Studio, Dev CPP, etc, which highlights all the different parts of the source code depending on their syntax by color, modified fonts, or through graphical changes. Since color highlighting these days is integrated into all common editors and development areas. Highlighting
3 min read
How to highlight syntax in files using Node.js ?
Node.js supports modules and packages that can be installed using NPM, which allows us to exploit various functionalities. One such functionality is syntax highlighting using Node.js that helps us to render highlighted code as static HTML, or we can also use it for dynamic syntax highlighting. The following approach covers how to highlight syntax f
2 min read
How to do syntax checking using PHP ?
Syntax checking is one of the most important tasks in programming. Our compiler checks our code and shows relevant errors if there is any in the code i.e. compile time, run time, syntax, etc. We can do the same thing i.e. syntax checking in PHP. In this article, we are going to learn how we can do syntax checking in PHP. The syntax is basically a s
2 min read
Which tag is used to find the version of XML and syntax ?
Extensible Markup Language (XML) is a markup language, defining a ruleset for encoding documents in both formats that is human-readable and machine-readable. The design goals of XML focus on simplicity, generality, and usability across the Internet. XML is designed to be self-descriptive along with storing and transporting the data. It is a textual
2 min read
What is the syntax for leading bang! in JavaScript function ?
Before we get to know the syntax for the leading bang! in a JavaScript function, let's see what functions in JavaScript are actually are. JavaScript functions are a set of statements(procedures) that perform some tasks or calculate some value. A function may take some input and return the result to the user. The main idea to use functions is to avo
2 min read
How to use class syntax in Typescript ?
Classes: The class keyword was introduced in ES2015. TypeScript fully supports 'class' keyword. classes are a template for creating objects. A Class is a user defined data-type which has data members and member functions. Data members are the data variables and member functions are the functions used to manipulate these variables and together these
2 min read
Less.js Extend Syntax & Inside Ruleset
LESS.js is one of the most popular CSS preprocessor languages because of its many features like mixins, imports, variables, and, so on, which help to reduce the complexity of CSS code. One such important and useful feature of LESS is the @extend directive. In this article, we will see the basic usage of the extend feature in LESS.js, along with kno
2 min read
JavaScript Spread Syntax (...)
The spread syntax is used for expanding an iterable in places where many arguments or elements are expected. It also allows us the privilege to obtain a list of parameters from an array. The spread syntax was introduced in ES6 JavaScript. The spread syntax lists the properties of an object in an object literal and adds the key-value pairs to the ne
4 min read
How to Hide the VueJS Syntax While the Page is Loading ?
Vue.js is a JavaScript framework used in building powerful and elegant user interfaces. In this article, we will learn how to hide the VueJS syntax while the page is loading. This approach ensures that users don't see the uncompiled Vue.js syntax during the initial page load, providing a smoother user experience. The following approaches can be use
3 min read
Provide the syntax for optional parameters in TypeScript
In TypeScript, optional parameters allow you to specify that a function parameter may be omitted when calling the function. You denote optional parameters by adding a question mark (?) after the parameter name in the function declaration. Syntax:function functionName(param1: type, param2?: type, param3?: type) { // Function body } Parameters:param1
2 min read
What is the Syntax for Declaring Functions in JavaScript ?
Generally, in JavaScript, the function keyword is used to declare a variable. But, there are some more ways available in JavaScript that can be used to declare functions as explained below: Using function keyword: This is the most common way to declare functions in JavaScript by using the function keyword before the name of the function.Declaring a
1 min read
What is the syntax of creating classes in TypeScript?
Classes in TypeScript are defined as a blueprint for creating objects to utilize the properties and methods defined in the class. Classes are the main building block of the Object Oriented Programming in TypeScript. The classes in TypeScript can be created in the same way as we create them in Vanilla JavaScript using the class keyword. Classes help
1 min read
Bulma Modifiers Syntax
Modifiers in Bulma are used to manipulate a particular class in order to get the desired output. To implement the modifiers, one has to either use the is- or the has- modifier class before the modifier name. It is essential to know the syntax of these modifiers in order to implement them in our code. Syntax: &lt;div class = "is-modifier-name" ...
2 min read
How to Handle Syntax Errors in Node.js ?
If there is a syntax error while working with Node.js it occurs when the code you have written violates the rules of the programming language you are using. In the case of Node.js, a syntax error might occur if you have mistyped a keyword, or if you have forgotten to close a parenthesis or curly brace. Example: Of Syntax Error [GFGTABS] JavaScript
4 min read
Explain the arrow function syntax in TypeScript
Arrow functions in TypeScript are implemented similarly to JavaScript (ES6). The main addition in TypeScript is the inclusion of data types or return types in the function syntax, along with the types for the arguments passed into the function. What is arrow function syntax in TypeScript?Arrow functions in TypeScript offer a concise syntax for defi
3 min read
Syntax to create function overloading in TypeScript
Function overloading is a feature in object-oriented programming where multiple functions can have the same name but different parameters. The parameters can differ in number, types, or both. This allows a single function name to perform different tasks based on the input parameters. Syntax: function function_name(parameter1 : data_type, parameter2
2 min read
How ReactJS ES6 syntax is different compared to ES5 ?
Both ES6 and ES5 are Javascript scripting languages in the development industry. ECMA Script or ES is a trademarked scripting language made by ECMA International. The European Computer Manufacture Association or ECMA is used for client-side scripting for the worldwide web. ES5 was released in 2009 and ES6 in 2015. ES5 was good but lengthy. The new
6 min read
jQuery Syntax
The jQuery syntax is essential for leveraging its full potential in your web projects. It is used to select elements in HTML and perform actions on those elements. jQuery Syntax$(selector).action()Where - $ - It the the shorthand for jQuery function.(selector) - It defines the HTML element that you want to selectaction() - It is the jQuery method u
2 min read
JavaScript Syntax
JavaScript syntax refers to the rules and conventions dictating how code is structured and arranged within the JavaScript programming language. This includes statements, expressions, variables, functions, operators, and control flow constructs. Syntax console.log("Basic Print method in JavaScript");JavaScript syntax refers to the set of rules that
6 min read
Binding Syntax In Angular
In Angular, binding syntax lets you determine the channel of data transmission between the component class and the template. Among various types of bindings supported by Angular are interpolation, property binding, event binding, and two-way-data-binding. Therefore, it is important to understand various type of binding in order to approximately rel
3 min read
How to Create and Use a Syntax Highlighter using JavaScript?
A syntax highlighter is a tool that colorizes the source code of programming languages, making it easier to read by highlighting keywords, operators, comments, and other syntax elements in different colors and fonts. In JavaScript, you can create a syntax highlighter by either manually writing your own code or by using existing libraries. These are
3 min read
PHP Syntax
PHP, a powerful server-side scripting language used in web development. It’s simplicity and ease of use makes it an ideal choice for beginners and experienced developers. This article provides an overview of PHP syntax. PHP scripts can be written anywhere in the document within PHP tags along with normal HTML. Basic PHP SyntaxPHP code is executed b
4 min read
Difference between bootstrap.css and bootstrap-theme.css
Bootstrap is an HTML, CSS and JS front-end-framework used for developing responsive and mobile-first projects on the web. It is the framework that comes up with predefined terms, thus giving you the ability to use these codes instead of having to create the codes from scratch. It is a collection of HTML, CSS and JS code that is designed to help bui
5 min read
Which CSS framework is better Tailwind CSS or Bootstrap ?
In this article, we will see which CSS framework is better tailwind CSS or bootstrap. Both are widely used CSS frameworks for styling the front end. We will understand each one of them in detail. Both frameworks have their own pros and cons so it depends upon the type of application you are going to build and accordingly you can choose the best fra
5 min read
CSS Cheat Sheet - A Basic Guide to CSS
What is CSS? CSS i.e. Cascading Style Sheets is a stylesheet language used to describe the presentation of a document written in a markup language such as HTML, XML, etc. CSS enhances the look and feel of the webpage by describing how elements should be rendered on screen or in other media. What is a CSS Cheat Sheet? CSS Cheat Sheet provides you wi
13 min read