×

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

How to center a table in CSS

The table is used to store and arrange the data in tabular format. The data here can be of any form like text, image, links, etc. The table is made...

3 minutes read.

CSS White Space Property

White Space in CSS The white-space CSS property describes how to show the content inside an element. This property is applied to manage a white space within an element. Values of White-space...

2 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: nth-selector

CSS nth selector can be used to match an element according to its position regardless of its parent’s type. Here, n can be any number, formula, or a keyword. These selectors...

2 minutes read.

CSS Browser Support

The desk underneath lists of all CSS properties and the way each belongs to it is supported in the generally unique browsers: - The quantity to the right of the browser...

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.

CSS Links

CSS Link is used to create styled link. There are various ways to implement it. Styling Links Link can be styled with CSS properties like color, font-family, background etc. Example: <!DOCTYPE html>   <html>   <head>   <style>   a {       color: red;   }   </style>   </head>   <body>   <p><b><a href="https://www.tutorialandexample.com"    target="_blank">This is a link</a></b></p>   </body>   </html> Try Now There are four...

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.

CSS Background-clip

CSS Background-clip: The background-clip property describes the background’s painting area. It limits an area where the image or color of the background appears by using any clipping box. Everything will...

2 minutes read.

CSS Zoom

CSS Zoom: Zoom property in CSS extents the content. This property controls the content’s level of magnification. Rather than the use of this CSS property, we may also apply the...

3 minutes read.

CSS Word Wrap

Word Wrap in CSS: The word-wrap property in CSS is referred to break any lengthy word and wrap towards the next or following line. It is employed for preventing overflow...

1 minute read.

CSS Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.

CSS Overflow

CSS Overflow: The Overflow property in CSS is used to manage the content if it overflows from its container of the block level. We have seen that all the elements...

3 minutes read.

CSS Content Property

Content Property The content attribute produces dynamic content. It can be applied with any pseudo-element ::after and ::before. These CSS properties are entirely supported inside every browser and applied to include...

5 minutes read.

CSS Clip

CSS Clip The Clip property in CSS describes the element’s visible area. It applies over the elements which are absolutely positioned (position: absolute ;). Generally, it is used when an image...

2 minutes read.

CSS Loader

CSS Loader The loader is the animation that can alert a visitor regarding the loading of the page. We need to wait for some time. It will be helpful if any...

4 minutes read.

How to change font size in CSS

How to change font size in CSS? Font Size: - Users may attract attention to content on a website page in various ways. One element in a sentence can be highlighted. You...

5 minutes read.

What is CSS used for

What is CSS used for? CSS stands for Cascading Style Sheet. CSS is a stylesheet language for web pages. It specifies how a document created in a markup language looks and...

4 minutes read.

CSS Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash. Rules of CSS3 @keyframes The...

3 minutes read.

CSS Counters

CSS Counters: The CSS counter property is similar to the variables. CSS counters are controlled via CSS, and its values are incremented via rules of CSS to record how much...

4 minutes read.