×

What is a CSS File?

What is a CSS File and How to use it in Html?

CSS: -

CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of a document written in HyperText Markup Language, known as HTML. CSS files show how the HTML elements will appear on the screen. CSS was released in 1996 and is now maintained by World Wide Web Consortium(W3C). The extension for saving cascading style sheet files is ".css".

A CSS file is used for making the content of a web page look customized. In simple words, we created a web page and displayed some content on it, and it appears all in a simple font, so to make it look more beautiful and readable, we use CSS. In short, we design a web page using CSS properties.

We can change a web page's layout, font, border, indentation, etc.

CSS has a simple syntax and has several English keywords to specify the name of various style properties.

What is a CSS File and How to use it in Html?

CSS is a set of rules which consist of a selector and declaration block.

Syntax:

Selector Declaration block
H2 {font-size: 16px; background-color: green;}

Here in the above syntax, h2, i.e., heading 2 of html is the selector and, in the declaration, block the font-size, and background-color are the property which we want to set for h2 tag and 16px and green are the values which will be applied on the html element.

Example: -

p
{
color: blue;
font-size:12px;
text-align: right;
}

Output: -

What is a CSS File and How to use it in Html?

Explanation: -

Here p is the HTML element: paragraph tag

color, font size, and text-align are the multiple properties.

Blue,12px, right is the respective value of properties applied to the selector.

In simple words, the blue color is applied to the text written in the p tag, its font size is 12px, and it is aligned on the right-hand side.

There are some basic CSS Selectors, and they are as follows:

1) Element Selector: -

This selector is used to select the HTML element for styling.

Example: -

Here, all <h1> elements on the page will be aligned to center and the text color will be blue.

h1 {
  text-align: center;
  color: blue;
}

2) Id Selector: -

The Id Selector is used to selecting the HTML element's id attribute. As the Id of an element is unique, it is used to select only one unique element. For id selection, we use (#)

Example: -

Here for selecting id we need to apply it on HTML element like id=” p1”

 #p1{
  text-align: center;
  color: blue;
}

3) Class selector: - The class selector is used to select a specific class attribute within the HTML element. The class selector is written (.) dot character and then the name of the class.

Example: -

For selecting a class, we need to apply it to an HTML element like class=" p1," which will be blue in color, and the text will be aligned in the center.

.p1{
  text-align: center;
  color: blue;
}

4) Universal Selector: - The universal selector is used to select all the HTML elements. The universal selector is denoted by using the asterisk symbol (*).

Example: -

The styling will be applied to all html element.

*p1{
  text-align: center;
  color: blue;
}

There are three ways to use a CSS in for our web page and they are as follows:

  1. Inline CSS
  2. Internal CSS
  3. External CSS

So now let’s see each of these CSS is used for designing the web pages.

A) Inline CSS: -

An Inline CSS is used to apply a style uniquely for a single element. We can use an inline CSS just by adding the style attribute to its relevant element, and it can contain any of the property which belongs to CSS properties.

Example: -

<html>
<body>


<h1 style="color: red; text-align: center; font-size:58px">This is first h1 heading </h1>
<p style="color: blue;">It is second paragraph</p>
</body>
</html>

Output: -

What is a CSS File and How to use it in Html?

So, in the above example, for the h1 tag and p tag, both have their CSS mentioned for their attribute, and this is how inline CSS works.

B) Internal CSS: -

An internal CSS is used to apply styling properties on every element which we want to style on an html page.

The internal style sheet is defined inside <style> tag which comes under <head> section.

Example: -

<html>
<head>
<style>
body {
  background-color: cyan;
}


h2 {
  color: yellow;
  margin-left: 50px;
}
</style>
</head>
<body>


<h2>This is first h2 heading</h2>
<p> It is second paragraph </p>


</body>
</html>

Output: -

What is a CSS File and How to use it in Html?

 For the above example, all tag styling properties are written inside the style tag.

C) External CSS: -

By using the external CSS, we can change the look and feel of our web page. It is the most commonly used CSS for styling html elements and the web pages.

For doing so, we first need to include the reference of external CSS into each html page by using the <link>tag inside the head section. An external CSS can be written in any editor but must be saved with a .css extension only. And this file should not include any html tag in it. So now, for our example purpose, let us create a style1.css to see the implementation of external CSS.

Example: -

style1.css

body {
  background-color : yellow;
}


h3 {
  color: green;
  margin-left: 40px;
}
<html>
<head>
<link rel="stylesheet" href="style1.css">
</head>
<body>
<h3>This is third h3 heading</h3>
<p>It is 3rd paragraph</p>


</body>
</html>

Output: -

What is a CSS File and How to use it in Html?

Related Topics

CSS Transition

CSS Transition The transitions in CSS are influenced, included to modify the component from one design to another, without the use of the JavaScript or Flash. We should describe two of the...

2 minutes read.

CSS Outline

CSS Outline: This CSS property is quite similar to the border property of CSS. It eases us to design an additional border around the element to obtain visual attention. The...

4 minutes read.

CSS Selector

CSS Selector: There are some selectors used in CSS, which allows us to choose the content we wish to style. Various selectors are considered as the separation of the set...

5 minutes read.

CSS Color

CSS Colors: The CSS color property is used for setting the HTML element’s color. The color of the fonts and the color of the background can be set-up by using...

5 minutes read.

CSS Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 minutes read.

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it...

6 minutes read.

CSS Masking

CSS Masking The CSS mask property is applied to hide a component with the use of the masking or clipping an image at any specific point. The masking depicts the use...

3 minutes read.

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified...

3 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS Descendant Selector

Descendant Selector The Descendant selector in CSS is used to match the descendant components of a particular component. The term descendant depicts nested in whatever place, inside a DOM tree. This...

2 minutes read.

How to resize an image in CSS

How to resize an image in CSS? The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute...

3 minutes read.

CSS calc()

CSS calc(): The CSS calc() function is a CSS function of inbuilt type, which permits us to implement calculations. It is used for calculating the angle, integer frequency, number, time,...

3 minutes read.

How to create blinking text using CSS

The blinking effect is used in HTML and CSS to take the attraction towards a particular word or section. If you need such a blinking effect, we need to take...

2 minutes read.

CSS Reference

The CSS Reference lists all of the basic standard CSS properties, pseudo-classes, pseudo-elements, and data types, for all intents and purposes functional notations, and at-rules alphabetically.  The site also includes...

5 minutes read.

CSS Text-decoration

CSS Text-decoration: It is an essential property of CSS that is used to decorate the content of the text. It can add-on the lines above, under, and over the text....

3 minutes read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

CSS Margin

CSS Margin: The CSS margin property is employed to describe the space throughout the elements. It clears the range throughout the element. These properties do not have a background color...

2 minutes read.

CSS Variables

CSS Variables These variables of CSS are applied to add the custom property values to our various web-pages. The custom properties are often called as CSS variables or cascading variables. Various...

4 minutes read.

Combine Background Image with Gradient Overlay

With the help of CSS gradient, we can display smooth and straightforward transitions between two or more colours. We can add the background images by combining the gradient property with...

2 minutes read.

CSS Images

Images in CSS: Images are a critical part of a web application. It includes various types of images in the web application that cannot be recommended. However, it is essential...

4 minutes read.