CSS Syntax
The CSS provides the style to the HTML element, which the browser interprets. After being interpreted by the browser, the CSS style property will be applied to all the elements of the HTML. We can provide style property to the HTML element in three parts. These three parts are as follows.
Key Points on CSS:
- 1. Selector is an HTML tag. All the style properties of the CSS will be applied to the selector. The selector tag like <h1> or <table> etc.
- 2. Property is a type of attribute that is present in HTML tags. All the attributes of the HTML will be converted to the CSS properties. The CSS properties like color, border, etc
- 3. Value HTML, these are assigned to the properties. For example, the color property can have a value of either red or #F1F1F1, etc.
Syntax
We must provide the CSS property to the HTML element in a proper way. We must follow the syntax below to implement the CSS property.
Syntax Example
selector { property: value;}
Example
Code Example: Styled Button with Header
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
>meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f2f2f2;
}
#header {
background-color: #333;
color: white;
text-align: center;
padding: 20px;
}
h1 {
font-size: 36px;
margin-bottom: 20px;
}
p {
font-size: 18px;
margin-bottom: 10px;
}
.button {
background-color: pink;
color: white;
padding: 10px 20px;
border: none;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 5px;
cursor: pointer;
}
.button:hover {
background-color: #990000;
}
</style>
</head>
<body>
<div id="header">
<h1>Hello, CSS Example!</h1>
<p>Welcome to Shorat. Here is a styled button:</p>
<button class="button">Click Me!</button>
</div>
</body>
</html>
Output
Hello, CSS Example!
Welcome to Shorat. Here is a styled button:Explanation Of Above Example
- In the above example, there is a body tag. In the body tag, we set the entire page's font family, margin, padding, and background color.
- Also, there is a #header selector. Inside the #header selector, we have set the header section's background color, text color, text alignment, and padding.
- Inside the h1 selector, we have set the font size and margin for the main heading.
- Inside the p selector, we have set font size and margin for paragraphs.
- We have set the button with a red background, white text color, padding, border, and other properties inside the button selector.
- Inside the .button:hover selector, we have set the background color to change when the button hovers, giving it a darker shade of red.