Home >Web Front-end >HTML Tutorial >HTML Style Sheets
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.
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.
There are 3 ways in which we can include the styles:
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>
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>
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">
Styles should be included in the head tag, which is above the body tag (i.e. actual content) of the HTML.
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:
Usage: @import ‘./process.css’;
These are basically states of the selected elements and not really the exact elements.
The various features are mentioned below:
Example:
Code:
-webkit-transition: width 2s, height 4s;
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; } }
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!