Introduction To CSS
Introduction To CSS
Introduction to CSS
Cascading Style Sheets fondly referred to as CSS, is a simply designed language that
simplifies making web pages presentable. CSS allows you to apply styles to web pages.
More importantly, CSS enables you to do this independently of the HTML that makes up
each web page. It describes how a webpage should look: it prescribes colors, fonts, spacing,
and much more. In short, you can make your website look however you want. CSS lets
developers and designers define how it behaves, including how elements are positioned in
the browser.
While HTML uses tags, CSS uses rulesets. CSS is easy to learn and understand, but it
provides robust control over the presentation of an HTML document.
Why CSS?
CSS saves time: You can write CSS once and reuse the same sheet in multiple HTML
pages.
1) Easy Maintenance: To make a global change simply change the style, and all elements
in all the web pages will be updated automatically.
2) Search Engines: CSS is considered a clean coding technique, which means search
engines won’t have to struggle to “read” its content.
3) Superior styles to HTML: CSS has a much wider array of attributes than HTML, so you
can give a far better look to your HTML page in comparison to HTML attributes.
4) Offline Browsing: CSS can store web applications locally with the help of an offline
cache. Using this we can view offline websites.
CSS is used to define styles for your web pages, including the design, layout, and variations
in display for different devices and screen sizes.
Input:
Output:
CSS Syntax
CSS comprises style rules that are interpreted by the browser and then applied to the
corresponding elements in your document. A style rule set consists of a selector and
declaration block.
The selector points to the HTML element you want to style.
● HTML was NEVER intended to contain tags for formatting a web page!
● HTML was created to describe the content of a web page, like
- <h1>This is a heading</h1>
- <p>This is a paragraph.</p>
● When tags like <font>, and color attributes were added to the HTML 3.2 specification,
it started a nightmare for web developers.
● Development of large websites, where fonts and color information were added to
every single page, became a long and expensive process.
● To solve this problem, the World Wide Web Consortium (W3C) created CSS.
● CSS removed the style formatting from the HTML page!
CSS Selectors
CSS selectors are used to “find” (or select) HTML elements based on their element name,
id, class, attribute, and more.
1) Universal Selectors
Rather than selecting elements of a specific type, the universal selector quite simply
matches the name of any element type.
Output:
This rule renders the content of every element in our document in black.
2) Element Selectors
The element selector selects elements based on the element name. You can select all p
elements on a page like this (in this case, all p elements will be center-aligned, with a red
text color).
Output:
3) Descendant Selectors
Suppose you want to apply a style rule to a particular element only when it lies inside a
particular element. As given in the following example, the style rule will apply to the em
element only when it lies inside the ul tag.
Output:
4) ID Selectors
● The id selector uses the id attribute of an HTML element to select a specific element.
● The id of an element should be unique within a page, so the id selector is used to
select one unique element!
● To select an element with a specific id, write a hash (#) character, followed by the id
of the element.
● The style rule below will be applied to the HTML element with id=”para1″
Output:
5) Class Selectors
6) Grouping Selectors
If you have elements with the same style definitions, like this:
It will be better to group the selectors, to minimize the code. To group selectors, separate
each selector with a comma. In the example below we have grouped the selectors from the
code above:
Output:
CSS Effects on HTML Elements
Output:
CSS Applications
As mentioned before, CSS is one of the most widely used style languages on the web. I'm
going to list a few of them here:
3) Easy maintenance
To make a global change, simply change the style, and all elements in all the web pages will
be updated automatically.
Link: https://link.almabetter.com/9w63