UNIT –II Cascading Style Sheets
Total Page:16
File Type:pdf, Size:1020Kb
UNIT –II Cascading Style Sheets: Introduction, using Styles, simple examples, your own styles, properties and values in styles, style sheet, formatting blocks of information, layers. Introduction:- Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language. CSS is designed primarily to enable the separation of presentation and content, including aspects such as the layout, colors, and fonts. This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, enable multiple HTML pages to share formatting by specifying the relevant CSS in a separate. css file, and reduce complexity and repetition in the structural content. It can also display the web page differently depending on the screen size or viewing device. Readers can also specify a different style sheet, such as a CSS file stored on their own computer, to override the one the author specified. Advantages of CSS :- 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. 1.Types of CSS:- There are three types of cascading style sheet(css) are listed below: 1. Inline Style Sheet 2. Internal Style Sheet / Embedded Style Sheet and 3. External Style Sheet 1.Inline Style Sheet:- Inline style sheets are declared within individual tags and affect those tags only. Inline style sheets are declared with the style attribute. Every CSS change has to be made in every tag that has the inline style applied to it. The inline style is good for one an individual CSS change that you do not use repeatedly through the site. Syntax: <element style=”declarations”>the text appears here</element> Example: <p style=”text-align:center; font-size:40; text-decoration:underline;”> Cascading Style Sheet</p> //inline.html <html> Output:- <head> <title>Inline Style Sheet</title> </head> <body> Cascading Style Sheet <p style="text-align:center;text- decoration:underline;font-size:40;">Cascading Inline Style Sheet Style Sheet</p> <h3 style="color:orange;">Inline Style Sheet</h3> </body> </html> 2.Internal Style Sheet / Embedded Style Sheet:- An internal style sheet holds the CSS code for the webpage in the head section of the particular file. This makes it easy to apply styles like classes or id’ in order to reuse the code. The downside(body section) of using an internal style sheet is that changes to the internal style sheet only effect the page the code is inserted into. The <style> tag specifies the content type of a stylesheet with its type attribute which should be set to "text/css". Syntax: Example: <head> <head> <style type=”text/css”> <style type="text/css"> h1{ styles go here text-align:center; </style> text-transform:uppercase; </head> } p{ font-family:algerian; font-style:italic; //internal.html:- } </style> <html> <head> </head> <title>Internal Style Sheet </title> <style type="text/css"> h1{ text-align:center; text-transform:uppercase; } Output:- p{ font-family:algerian; font-style:italic; } </style> USING INTERNAL STYLE SHEET </head> <body> First Para <h1>Using Internal Style Sheet</h1> <p>First Para</p> <p>Second para</p> Second para </body> </html> 3.External Style Sheet:- Use an external stylesheet when you want to apply one style to many pages. If you make one change in an external stylesheet, the change is universal on all the pages where the stylesheet is used. An external stylesheet is declared in an external file with a .css extension. It is called by pages whose interface it will affect. External stylesheets are called using the <link> tag which should be placed in the head section of an HTML document. This tag takes three attributes. Attributes of the <link> tag: rel - When using an external stylesheet on a webpage, this attribute takes the value "stylesheet" type - When using an external stylesheet on a webpage, this attribute takes the value "text/css" href - Denotes the name and location of the external stylesheet to be used. Syntax:- <head> <link rel=”stylesheet” type=”text/css” href=”filename.css”> </head> //external.html //style.css <html> h1{ <head> text-align:center; <title>External Style Sheet</title> font-weigt:bold; <link rel="stylesheet" type="text/css" } href="style.css"> p{ </head> font-size:20; <body> font-style:oblique; <h1>Introduction</h1> color:blue; <p>ESS is defines the css file } seperately.</p> <p>It includes many html document.</p> </body> Output:- </html> Introduction ESS is defines the css file seperately. It includes many html document. 2.Explain Defining your own styles in CSS. Defining styles:- Styles are defined by simple rules. A style can contain as many rules as we want and as with processing HTML, if something doesn’t makes sense it can be ignored. Cascading Styles: Conventionally, styles are cascaded. This means that we do not have to use a single set of styles inside a document. We can import as many styles sheets as we like. This is useful if we define a set of organizational can take place in stylesheets, this means that the first is overriding can also happen by defining styles within the body of the page. Rules: A style rule has two parts: a selector and a set of declarations 1. The Selector is used to create a link between the rule and the HTML tag. Selectors can be placed into classes so that a tag can be formatted in a variety of ways. 2. The Declaration has two parts: a property and a value. Property and value must be separated with colons and terminated with semicolon( ; ). Syntax:- selector { property : value; property : value; ……………….; } Example: body{ background-color:blue; } h1{ text-align:center; text-transform:uppercase; } p{ font-family:algerian; font-style:italic; } Classes:- You may be wondering if it is possible to give an HTML element multiple looks with CSS. For example, sometimes you want the font to be large and white color, while other times you would prefer the font to be small and black color. CSS allows you to do just that with the use of classes. Example 1:- CSS Code: p.first { color:blue; } p.second { color:red;} HTML Code: <html> <head> <title> Using classes </title> </head> <body> <p class=”first”>This is a paragraph that uses the p.first CSS code</p> <p class=”second”>This is a paragraph that uses the p.second CSS code!</p> </body> </html> User Stylesheet are an exciting feature of CSS. In CSS, the presentation of a document is controlled by the combination of user and author style preference. This mechanism is needed(text only) to allow CSS to describe fully(and the extend) the behavior of browsers. Users can define their own styles sheets to format pages based on their preferences. In below example, user defines different declaration for own styles (using class ). Example 2:- <html> <head> <title>Using Class</title> <style type="text/css"> .head{ text-align:center; color:brown; text-decoration:underline; } .para{ <body> color:green; <h1 class="head">Define you own style using class</h1> font-size:40; <p class="para">Some of the questions are listed:</p> font-family:algerian; <p class="question">What is your name?</p> } <p class="answer">My name is Krishna.</p> .question{ <p class="question">What is your Qualification?</p> color:#00ff00; <p class="answer">My Qualification is Bachelor of Computer font-size:30; Science.</p> </body> } .answer{ </html> color:#0000ff; font-size:25; text-transform:uppercase; } </style> </head> Output:- Define you own style using class Some of the questions are listed: What is your name? MY NAME IS KRISHNA. What is your Qualification? MY QUALIFICATION IS BACHELOR OF COMPUTER SCIENCE 3.Properties and Values in CSS:- The following are the properties and its values used in styles: Property Meaning Values 1. text-align Specifies the horizontal alignment of text. text-align: left | center |right 2. text-decoration This text decoration can be done by specifying text-decoration: underline | underline,overline and line-through overline | line-through 3. text-transform Controls the capitalization of text text-transform: uppercase | lowercase | capitalize 4. font-family Specifies the font family for text font-family: aria | algerian | verdana | Calibri 5. font-size The size of the text can be given( in pixels(px), font-size: 20px | 50% | 80in percentage(%), inches(in)) 6.