CSS (Cascading Style Sheets) syntax is the foundation for styling web pages. It defines the rules for how HTML elements should be displayed on the screen. Learning this syntax is crucial for anyone looking to improve the look and feel of their websites. When you’re using CSS, you’ll mainly deal with three key concepts: selectors, properties, and values. Here’s what they mean:
Selector
This is the part of CSS code that identifies which part of the webpage you want to style. For example, if you want to change the text color of all the paragraphs on your page, you would start with a p selector, which stands for paragraph.
Property
This is the type of adjustment you want to make. For example, if you’re changing the color of your paragraphs, the property would be color.
Value
This is the specification you give to a property. It describes how you want to change the property. If you want your paragraphs to be blue, the value would be blue.
Also Read: What is Python?
Syntax
CSS (Cascading Style Sheets) syntax is the set of rules used to define styles for your web page. The syntax is straightforward and consists of selectors and declarations. Here’s a simple breakdown:
- Selector: This part targets the HTML element you want to style.
- Declaration: This part contains the properties and their values, which determine how the selected HTML elements will look.
Example
Let’s consider a basic example of CSS syntax:
p {
color: red;
font-size: 16px;
}
Output
Any paragraph (<p>) elements on the web page will appear with red text and a font size of 16 pixels.
Explanation
In this example:
- p is the selector, which targets all <p> elements on your HTML page.
- Inside the curly braces {}:
- color: red; is a declaration, where color is the property and red is the value.
- font-size: 16px; is another declaration, where font-size is the property and 16px is the value defining the size of the text.
- Each property influences a specific aspect of how the element appears, and the semicolon ; separates different declarations in the list.
Also Read: What is C#
The Type Selectors
In CSS, type selectors target HTML elements based on their type, such as p for paragraphs, h1 for main headings, etc.
Syntax
The syntax of a type selector is straightforward. You simply use the name of the HTML element followed by curly braces {} containing the style rules. Here’s the basic structure:
elementname {
property: value;
}
- elementname is the HTML tag you want to style.
- property is the aspect of the element you want to change (like color, font-size, etc.).
- value is what you want to set the property to.
Example
Suppose you are creating a webpage with the following HTML structure:
<!DOCTYPE html>
<html>
<head>
<title>Sample Page</title>
<style>
h1 {
color: blue;
text-align: center;
}
p {
color: green;
font-size: 18px;
}
div {
background-color: lightgray;
padding: 20px;
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Welcome to My Webpage</h1>
<div>
<p>This is a simple paragraph within a div.</p>
<p>Notice different styles applied to different elements.</p>
</div>
</body>
</html>
Output
- The <h1> element displays the text “Welcome to My Webpage” in blue color and centered horizontally on the page.
- The <p> elements within the <div> are displayed in green color and the font size is increased to 18 pixels. This makes them stand out against other texts.
- The <div> itself has a light gray background, a padding of 20 pixels around its content, and a black border, giving it a distinct boxed appearance on the webpage.
Explanation
- h1 Selector: Targets all <h1> HTML elements.
- color: blue; changes the text color of the <h1> elements to blue.
- text-align: center; centers the text inside the <h1> elements, typically used for headings to draw attention.
- p Selector: Affects all <p> (paragraph) elements.
- color: green; sets the text color of paragraphs to green, which is useful for distinguishing paragraph text from other text elements.
- font-size: 18px; increases the text size to 18 pixels, which helps improve readability.
- div Selector: Applies styles to all <div> elements.
- background-color: lightgray; gives a light gray background color to the div, which helps it stand out from the rest of the page.
- padding: 20px; adds space inside the div around its content, making the internal elements less cramped.
- border: 1px solid black; adds a solid black border around the div, defining its boundaries clearly on the page.
Each selector targets a specific type of element and applies styles that are appropriate for that element’s role in the page layout.
Also Read: What is SQL?
The Universal Selectors
The universal selector in CSS is used to apply styles to every element on your webpage. This is especially useful when you want a consistent baseline style across your site, such as resetting margins or setting a default font.
Syntax
The universal selector is represented by an asterisk *. The syntax follows the standard pattern of selector followed by curly braces containing the style declarations:
* {
property: value;
}
- * represents the universal selector, targeting all elements.
- property is the style attribute you want to change.
- value is the setting you want to apply to the property.
Example
Suppose you’re designing a simple webpage with various elements, such as headers, paragraphs, lists, and a navigation bar. You want to ensure that these elements have consistent spacing, typography, and box-sizing. Here’s the HTML structure:
<!DOCTYPE html>
<html>
<head>
<title>My Sample Page</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: ‘Roboto’, sans-serif;
color: #333;
line-height: 1.6;
}
header, nav, section, article {
border: 1px solid #ccc;
padding: 10px;
margin: 10px;
}
header {
background-color: #f8f8f8;
}
nav {
background-color: #e8e8e8;
margin-bottom: 20px;
}
h1 {
color: #007BFF;
}
p {
margin-bottom: 10px;
}
</style>
</head>
<body>
<header>
<h1>Welcome to My Webpage</h1>
</header>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
<section>
<article>
<h2>About Us</h2>
<p>This is a paragraph about our company.</p>
</article>
<article>
<h2>Our Services</h2>
<p>Details about services we offer.</p>
</article>
</section>
<footer>
<p>Copyright © 2024</p>
</footer>
</body>
</html>
Output
With the CSS styles applied using the universal selector:
- All elements have zero margin and padding by default.
- The box-sizing: border-box; rule ensures that padding and border thickness are included within the width and height of each element, making layout calculations straightforward.
- The font-family: ‘Roboto’, sans-serif; provides a uniform font across all text elements.
- The color: #333; and line-height: 1.6; ensure that text is easily readable with good contrast and spacing.
- Additional specific styles for structural elements (like headers, navigation, and sections) and text elements (like headings and paragraphs) refine the appearance and spacing further.
Explanation
- The universal selector * targets every single element on the page. It’s used here to set up a consistent base for all elements to build upon.
- By setting margin and padding to 0, you eliminate any default spacing that browsers apply, giving you a clean slate for designing your layout.
- box-sizing: border-box; makes layout design more intuitive by including the padding and borders in the element’s total width and height.
- The universal font family and color ensure that unless specifically overridden, all text has a uniform appearance, promoting visual coherence across the site.
The Descendant Selectors
The descendant selector in CSS targets elements that are nested within other elements in the HTML structure.
Syntax
The syntax of a descendant selector involves two or more selectors separated by a space. This targets elements that are nested within other specified elements, regardless of how deep they are within the structure. Here’s the basic form:
parent descendant {
property: value;
}
Example
Consider you have HTML code like this:
<div class=”parent”>
<p class=”child”>Hello, world!</p>
<div>
<p>Another paragraph.</p>
</div>
</div>
To style only the <p> tags that are inside the <div> with the class parent, your CSS would look like this:
.parent p {
color: red;
}
Output
When the above CSS is applied, both <p> tags inside the <div class=”parent”> will display text in red color, including “Hello, world!” and “Another paragraph.”
Explanation
In the example:
- .parent p selects all <p> elements that are descendants of elements with the class parent.
- This means any <p> element nested at any level inside .parent will have the specified styles applied.
- It’s a way to control the styling of specific content within a particular section of your webpage.
- This ensures that the styles do not affect <p> elements outside the specified parent container.
The Class Selectors
CSS class selectors are a fundamental part of designing a website, as they allow you to apply specific styles to elements that have the same class attribute.
Syntax
The syntax for a class selector starts with a period (.) followed by the class name. This tells the browser to apply the style rules to any element that has this class attribute. Here’s how you write it:
.classname {
property: value;
}
Example
Let’s say you have the following HTML code:
<div class=”info”>Welcome to our site!</div>
<div class=”info”>Check out our blog posts below.</div>
You want to style these <div> elements so they stand out. You could use the following CSS:
.info {
color: blue;
font-size: 20px;
border: 1px solid black;
}
Output
The CSS will change the text color to blue, increase the font size to 20px, and add a black border around both <div> tags with the class info.
Explanation
In this example
- .info is the class selector that targets all HTML elements with the class info.
- Each element with this class will adopt the styles defined within the curly braces {} of the CSS rule.
The ID Selectors
CSS ID selectors are a precise way to apply styles to a single, unique element on a webpage. Unlike class selectors, which can target multiple elements, an ID selector is used for styling one specific element.
Syntax
The syntax for an ID selector is a hashtag (#) followed by the ID value. This ID should match the ID attribute of an element in your HTML. Here’s how you write it:
#idname {
property: value;
}
Example
Let’s say you’re creating a special announcement on a webpage that you want to highlight distinctly from other content. Your HTML might look like this:
<div id=”special-announcement”>Big Sale this Weekend!</div>
You want this announcement to be immediately noticeable, so you decide to style it with the following CSS:
#special-announcement {
color: white;
background-color: red;
padding: 10px;
text-align: center;
font-size: 20px;
}
Output
- The text “Big Sale this Weekend!” will be displayed in white font on a red background.
- It will have padding around the text to make it stand out more, and the text will be centered and larger than the default text size.
Explanation
- The ID selector #special-announcement specifically targets the element with the ID special-announcement.
- This ID should be unique within the page, meaning no other element should have the same ID.
- Using strong visual styles (white text on a red background) makes the element stand out effectively on the page.
The Child Selectors
CSS child selectors are a great tool for applying styles to elements that are directly inside another specific element. This is helpful when you want to style child elements differently based on their parent container.
Syntax
The syntax for a child selector involves two selectors: the parent and the child, separated by a > symbol. This indicates that the style should only apply to direct children of the specified parent. Here’s how you write it:
parent > child {
property: value;
}
Example
Suppose you have a webpage with a list of items, and within that list, you want only the direct list items to have a specific style, not any list items that might be nested further within. Your HTML structure might look like this:
<ul class=”main-list”>
<li>Item 1</li>
<li>Item 2
<ul>
<li>Subitem 1</li>
</ul>
</li>
<li>Item 3</li>
</ul>
You decide to style the direct children (<li> elements directly under .main-list) differently from nested <li> elements. The CSS might be:
.main-list > li {
color: blue;
font-weight: bold;
}
Output
In the list, “Item 1”, “Item 2”, and “Item 3” will be in blue and bold. “Subitem 1”, however, will not be affected by this style and will inherit or retain default styling.
Explanation
- The selector .main-list > li targets only the <li> elements that are direct children of .main-list.
- This means it specifically styles the first level of <li> elements and does not affect any <li> elements that are further nested inside other elements, like in nested lists.
The Attribute Selectors
CSS allows you to style HTML elements based on their attributes. This feature is incredibly useful for applying styles to elements without adding classes or IDs.
Syntax
The syntax for using attribute selectors is simple. You enclose the attribute name in square brackets [ ]. You can specify conditions for the attribute’s value for more precise targeting. Here are a few patterns you might use:
- [attr]: Selects elements with the specified attribute, regardless of its value.
- [attr=value]: Selects elements with the attribute matching the exact value.
- [attr^=value]: Selects elements whose attribute value begins with the specified value.
- [attr$=value]: Selects elements whose attribute value ends with the specified value.
- [attr*=value]: Selects elements whose attribute value contains the specified value somewhere.
Example
Suppose you want to style all images that are specifically marked as thumbnails. Let’s say these images are marked with a data attribute called data-type and its value is “thumbnail”.
Here’s how you would write the CSS using an attribute selector:
img[data-type=”thumbnail”] {
border: 2px solid blue;
padding: 5px;
}
Output
- The output would be that all img elements with data-type=”thumbnail” will have a blue border and a padding of 5px.
- This will make thumbnails visually distinct from other images on your webpage.
Explanation
- The CSS rule targets all img elements that have a data-type attribute with the value “thumbnail”.
- The border: 2px solid blue; applies a solid blue border around each matching image, and padding: 5px; adds space around the image inside the border.
- This specific targeting is done without the need to add extra classes to the HTML, keeping the markup cleaner and focusing the CSS on specific attributes.
- Attribute selectors provide a powerful way to apply styles based on element attributes, making your stylesheets more dynamic and adaptable to your content structure.
Multiple Style Rules
In CSS, applying multiple style rules to HTML elements helps you control the look and feel of your webpage more effectively.
Syntax
When writing multiple style rules, you list them one after the other inside the curly braces { }. Each rule is a property-value pair, and pairs are separated by a semicolon ;.
selector {
property1: value1;
property2: value2;
property3: value3;
}
Example
Let’s say you want to style paragraphs (<p>) on your website. You decide that all paragraphs should have a gray background, white text, and a font size of 16 pixels.
Here’s how you would write this in CSS:
p {
background-color: gray;
color: white;
font-size: 16px;
}
Output
The output on your website will be that all paragraph texts appear white on a gray background, and the text is uniformly 16 pixels in size.
Explanation
In the example, the p selector targets all paragraph elements in your HTML. Each CSS property does the following:
- background-color: gray;: Sets the background color of the paragraphs to gray.
- color: white;: Changes the text color of the paragraphs to white, ensuring the text stands out against the dark background.
- font-size: 16px;: Sets the size of the text in the paragraphs to 16 pixels, making it more readable.
Conclusion
Mastering CSS syntax allows you to control the appearance of web content effectively. With these basics, you can start styling web pages more effectively. Remember, practice is crucial, so experiment with different styles to see how they affect your web designs. Keep this guide handy as a quick reference whenever you work on web projects.