Javatpoint Logo
Javatpoint Logo

Style CSS

Introduction

When we are trying to build a web page without HTML, it is not possible. With the help of HTML, we can create page content, including headings, paragraphs, images, tables, forms, and lists inside our web page. But only with HTML can we not create a beautiful and responsive webpage. To achieve this, we have to take the help of CSS.

What is CSS?

CSS stands for Cascading Style Sheet. With the help of Cascading Style Sheet(CSS), we can provide all the style properties to the web page. With the help of CSS, we can set the background color, font-size, font-family, color, … etc., properties of elements on a web page. The CSS also determines how all the content should be displayed on the web page.

How to Add CSS to HTML

We can add CSS inside the HTML in three ways. These are as follows.

  • Inline CSS
  • Internal or Embedded CSS
  • External CSS

Inline CSS

In inline CSS, we have to provide the required CSS properties inside the opening tag in the body tag. This is known as inline CSS. We can perform this with the help of style attributes.

How to Add Inline CSS to HTML

Inline CSS is also known as an embedded stylesheet. We must follow the syntax below to add the CSS in HTML.

The inline CSS can override any other applied CSS because it is close to the HTML tag.

Example:

Output

Style CSS

Explanation:

We have taken inline CSS to apply different styles to the h1, p, and div elements in the above code.

Internal or Embedded CSS

When there is a single HTML file, and we have to provide the property to it, we have to go for Internal or Embedded CSS. We can write the CSS property inside the head tag in this CSS. We can write the CSS property with the help of <style> tag.

How to Add Internal CSS to HTML

We can write the internal CSS inside the head tag with the <style> tag. We can write the internal CSS with the help of the below syntax.

Using the internal css is the better practice in comparison to inline CSS. With the help of internal CSS, we can provide the style property to the group of elements at once. We can separate the CSS and HTML, but we have to write these in one document. All of our code is present in one file.

However, taking internal CSS is not a good practice when we have more than one HTML page. In that situation, we have to go for external CSS.

Example:

Output

Style CSS

External CSS:

In external CSS, we have to create an external CSS file and link it with the HTML element. We have to save that CSS file with the extension of the CSS file. We have to link the CSS file with the help of the CSS file. With the help of tags, it will be applied to the whole HTML file.

How to Add an External CSS File to HTML

We have to link the CSS file with the help of the below syntax. We have to write the syntax inside the <head> tag.

Syntax:

Before writing the example, let's know about some best practices for the reason during the writing of the code. These are as follows.

  • Whenever we need to provide the CSS property to the HTML file, we need to change this in the external file. It is the most time-effective method.
  • It is the most SEO-friendly software. It is very easy to read by the search engine.
  • It also enables the browser for the visitor to load the CSS file.

Example:

HTML CODE:

CSS CODE:

Output

Style CSS





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA