Explore the fundamentals of CSS, and review all the web-styling tools needed for front-end efficiency.
- CSS handles the look and feel part of a web page. Using CSS, you can control the color of the text, the style of fonts, the spacing between paragraphs, how columns are sized and laid out, what background images or colors are used, layout designs,variations in display for different devices and screen sizes as well as a variety of other effects.
- Cascading Style Sheets, fondly referred to as CSS, is a simple design language intended to simplify the process of making web pages presentable.
- CSS is easy to learn and understand but it provides powerful control over the presentation of an HTML document. Most commonly, CSS is combined with the markup languages HTML or XHTML.
CSS saves time − You can write CSS once and then reuse same sheet in multiple HTML pages. You can define a style for each HTML element and apply it to as many Web pages as you want.
Pages load faster − If you are using CSS, you do not need to write HTML tag attributes every time. Just write one CSS rule of a tag and apply it to all the occurrences of that tag. So less code means faster download times.
Easy maintenance − To make a global change, simply change the style, and all elements in all the web pages will be updated automatically.
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.
Multiple Device Compatibility − Style sheets allow content to be optimized for more than one type of device. By using the same HTML document, different versions of a website can be presented for handheld devices such as PDAs and cell phones or for printing.
Global web standards − Now HTML attributes are being deprecated and it is being recommended to use CSS. So its a good idea to start using CSS in all the HTML pages to make them compatible to future browsers.
Offline Browsing − CSS can store web applications locally with the help of an offline catche.Using of this, we can view offline websites.The cache also ensures faster loading and better overall performance of the website.
Platform Independence − The Script offer consistent platform independence and can support latest browsers as well.
A CSS comprises of style rules that are interpreted by the browser and then applied to the corresponding elements in your document. A style rule is made of three parts
h1 {
color: #36CFFF;
}
- This is the same selector we have seen above. Again, one more example to give a color to all level 1 headings
* { color: #000000; }
- Rather than selecting elements of a specific type, the universal selector quite simply matches the name of any element type
.black { color: #000000; }
- You can define style rules based on the class attribute of the elements. All the elements having that class will be formatted according to the defined rule.
- This rule renders the content in black for every element with class attribute set to black in our document. You can make it a bit more particular. For example:
h1.black { color: #000000; }
<p class="center bold"> This para will be styled by the classes center and bold. </p>
- You can apply more than one class selectors to given element. Consider the following example:
#black { color: #000000; }
- You can define style rules based on the id attribute of the elements. All the elements having that id will be formatted according to the defined rule.
- This rule renders the content in black for every element with id attribute set toblack in our document. You can make it a bit more particular. For example −
h1#black { color: #000000; }
#black h2 { color: #000000; }
- The true power of id selectors is when they are used as the foundation for descendant selectors, For example:
- In this example all level 2 headings will be displayed in black color when those headings will lie with in tags having id attribute set to black.
body > p { color: #000000; }
- You have seen the descendant selectors. There is one more type of selector, which is very similar to descendants but have different functionality. Consider the following example
- This rule will render all the paragraphs in black if they are direct child of <body> element. Other paragraphs put inside other elements like <div> or <td> would not have any effect of this rule.
input[type = "text"]{ color: #000000; }
- You can also apply styles to HTML elements with particular attributes. The style rule below will match all the input elements having a type attribute with a value of text −
h1 { color: #36C; font-weight: normal; letter-spacing: .4em; margin-bottom: 1em; text-transform: lowercase; }
- You may need to define multiple style rules for a single element. You can define these rules to combine multiple properties and corresponding values into a single block as defined in the following example −
h1, h2, h3 { color: #36C; font-weight: normal; letter-spacing: .4em; margin-bottom: 1em; text-transform: lowercase; }
- You can apply a style to many selectors if you like. Just separate the selectors with a comma, as given in the following example −
#content, #footer, #supplement { position: absolute; left: 510px; width: 200px; }
- You can combine the various class selectors together as shown below −
- You can put your CSS rules into an HTML document using the <style> element. This tag is placed inside <head>...</head> tags. Rules defined using this syntax will be applied to all the elements available in the document. Here is the generic syntax
<style type = "text/css" media = "all">
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
<head>
<link type = "text/css" href = "..." media = "..." />
</head>
- The <link> element can be used to include an external stylesheet file in your HTML document. An external style sheet is a separate text file with .css extension. You define all the Style rules within this text file and then you can include this file in any HTML document using <link> element.
The background-color property is used to set the background color of an element.
The font-family property is used to change the face of a font.
The color property is used to set the color of a text.
The border-color specifies the color of a border.
Border Style Properties - none - groove
- solid - ridge
- dotted - inset
- dashed - outset
- double - hidden
The margin specifies a shorthand property for setting the margin properties in one declaration.
The padding serves as shorthand for the preceding properties.
The height property is used to set the height of a box.
- CSS provides a property called overflow which tells the browser what to do if the box's contents is larger than the box itself. This property can take one of the following values