HTML Style Sheets

王林
Release: 2024-09-04 16:16:28
Original
1254 people have browsed it

The following article provides an outline for 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.

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:

Code:

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

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:

Code:

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

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:

Syntax:

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

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:

Code:

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

  • CSS Transforms: Transforms in CSS allow 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 been 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:

Code:

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

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.

The above is the detailed content of HTML Style Sheets. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template