CSS and CSS Selectors
CSS and CSS Selectors
CSS and CSS Selectors
CSS Example
In this tutorial, you will get a lot of CSS examples, you can edit and run these examples with our online CSS editor tool.
<html>
<head>
<style>
h1{
color:white;
background-color:red;
padding:5px;
p{
color:blue;
</style>
</head>
<body>
<p>This is Paragraph.</p>
</body>
</html>
CSS Syntax
A CSS rule set contains a selector and a declaration block.
Selector: Selector indicates the HTML element you want to style. It could be any tag like <h1>, <title> etc.
Declaration Block: The declaration block can contain one or more declarations separated by a semicolon. For the above example, there are two declarations:
1. color: yellow;
2. font-size: 11 px;
Property: A Property is a type of attribute of HTML element. It could be color, border etc.
Value: Values are assigned to CSS properties. In the above example, value "yellow" is assigned to color property.
CSS Selector
CSS selectors are used to select the content you want to style. Selectors are the part of CSS rule set. CSS selectors select HTML elements according to its id, class,
type, attribute etc.
<html>
<head>
<style>
p{
text-align: center;
color: blue;
</style>
</head>
<body>
<p>Me too!</p>
<p>And me!</p>
</body>
</html>
OUTPUT
2) CSS Id Selector
The id selector selects the id attribute of an HTML element to select a specific element. An id is always unique within the page so it is
chosen to select a single, unique element.
It is written with the hash character (#), followed by the id of the element.
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue;
</style>
</head>
<body>
</body>
</html>
OUTPUT
<html>
<head>
<style>
.center {
text-align: center;
color: blue;
</style>
</head>
<body>
</body>
</html>
OUTPUT
<html>
<head>
<style>
*{
color: green;
font-size: 20px;
}
</style>
</head>
<body>
<p>And me!</p>
</body>
</html>
OUTPUT
Grouping selector is used to minimize the code. Commas are used to separate each selector in grouping.
text-align: center;
color: blue;
h2 {
text-align: center;
color: blue;
p{
text-align: center;
color: blue;
As you can see, you need to define CSS properties for all the elements. It can be grouped in following ways:
h1,h2,p {
text-align: center;
color: blue;
}
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: blue;
</style>
</head>
<body>
<h1>Hello NEXON.com</h1>
<p>This is a paragraph.</p>
</body>
</html>