What is HTML Style Sheets?

HTML Style Sheets

What is HTML Style Sheets?

HTML Cascading Style Sheet is a sheet with rules and properties that tell the browser how to render HTML using all styles specified.

CSS is the way with which we style any web pages. CSS has all the properties like background, colors, fonts, spacing, borders, etc., which we can define for every element on the pages.

HTML Style Sheets are also used to set the layout of the page, like where the header, footers or any other elements are to be placed on the page. CSS is always talked along with HTML as pages without any styling are very pale with no highlighting of any headings etc., and same font size all over the page, which does not give a good look at all to the users.

How to Use HTML Style Sheets?

In the past, styles, scripts, HTML everything was written all over on the same page. This made the pages extremely lengthy and extremely difficult to read and edit. Then came the way to separate HTML, styles, and Javascript.

Ways to include HTML Style Sheets on a Webpage

There are 3 ways in which we can include the styles:

1. Inline Styling

This is a way of writing styles for every element within HTML itself inside an attribute called style. Popular Course in this categoryHTML Training (12 Courses, 19+ Projects, 4 Quizzes)12 Online Courses | 19 Hands-on Projects | 89+ Hours | Verifiable Certificate of Completion | Lifetime Access | | 4 Quizzes with Solutions
4.5 (6,492 ratings)Course Price
₹6999 ₹41999
View Course


Related CoursesBootstrap Training (2 Courses, 6+ Projects)XML Training (5 Courses, 6+ Projects)CSS Training (9 Courses, 9+ Projects)

This way of styling is not at all recommended as the HTML looks cluttered, and we cannot follow the approach “Write Once, Use at many places.”

Example:

<h1 style=”font-size: 10px;margin-top: 10px;”>Hello World!</h1>

2. Internal Styling

This is having styles included in a style tag and placing it within a web page on top of HTML. This way of styling is still better than inline styling as we can have common styles clubbed together in case it has to be used for multiple elements at a time.

It is easier to edit the HTML file at the development stage, and we need not every time open the corresponding CSS file and edit it every time.

Example:

<html>
<head>
<style>
container-block{
font-size: 10px;
margin-top: 10px;
}
</style>
</head>
<body>
<div class=”container-block”>Hello World!</div>
</body>

3. External Styling

This is the most common and the best way of having styles for a web page. This is similar to the internal styling, but the difference is that the styles are written in a separate file with extension .css, and reference to it is placed in the head tag of the web page.

The syntax for linking CSS file on the web page is:

<link rel="stylesheet" type="text/css" href="theme.css">

Styles should be included in the head tag, which is above the body tag (i.e. actual content) of the HTML

What is the Priority between Inline, Internal, External Styling?

Inline styles take more priority than internal, and then the last priority comes for the external styling.

Inline>Internal>External

Best practices while using CSS:

  • CSS can be separated into a number of files instead of just one.
  • Separated CSS files can either be included one by one in a head tag using link tags.
  • Or one CSS file can have multiple import statements to import the rest of the CSS files. This will logically separate the CSS but gets ultimately; all styles get rendered from the same file.

Usage: @import ‘./process.css’;

  • Styles can be defined for any web page elements with selectors as HTML tag itself, class names, ids, any attribute names.
  • There are some pseudo selectors available like:
    • before
    • after
    • nth-child
    • first-child
    • last-child
    • hover
    • visited

These are basically states of the selected elements and not really the exact elements.

  • When multiplied CSS files are included in the page, the last one takes the highest priority and overrides the existing styles of previous files having the same selector.
  • Stylesheets should be used before HTML itself so that styles get applied while the page is loading. If included at the end, HTML would first load and then slowly styles get applied, which gives a very bad experience to the user.

Various Features of HTML Cascading Style Sheets

The various features are mentioned below:

  • CSS Provides Animations: Previously, javascript was only used for animations. But latest CSS, i.e. CSS3, provides animations using properties themselves.
  • Vendor Prefixes: Before browsers release the standard version/property name for any new features, browsers provide us with some vendor prefixes for some time for some period of time as an experiment. Developers need to wait till the browser releases its standard versions, and in the meantime, experimental features can be used with vendor prefixes.
  • CSS Transforms: Transition is used to progressively go to one value from another of a property within a given duration.

Example:

-webkit-transition: width 2s, height 4s;

  • CSS Transforms: Transforms in CSS allows you to translate, rotate, scale and skew elements.

Media Queries

Mobile, Desktops, iPads behave differently; however, we cannot style the pages in the same way. Previous web standards have designed in such a way that we had different CSS for every type of device.

With the progress of web standards and the way the web is built, browsers are developed to have a single CSS that can be used for any type of device. To change the styles for devices based on width and height, media queries are used with which we can specify the min or max-width of the device and write styles within it.

Example:

@media screen and (max-width: 767px){
container{
width: 60%;
padding: 20px;
}
}

Styles are definitely a boon for the web. And as web development has increased exponentially in recent times, CSS3 has definitely gained a lot of demand to make the pages extremely interactive and intuitive.

Recommended Articles

This has been a guide to HTML Style Sheets. Here we discuss how to use HTML Style Sheets, Various Features and the Ways to include CSS on a Webpage. You may also look at the following articles to learn more –

  1. What is CSS?
  2. HTML Reset Button
  3. CSS Inline Style
  4. HTML Inline-Style

Leave a Comment

Your email address will not be published. Required fields are marked *