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 .  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.  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: the text appears here

Example:

Cascading Style Sheet

//inline.

Output:- Inline Style Sheet Cascading Style Sheet

Cascading Inline Style Sheet Style Sheet

Inline Style Sheet

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 text-transform:uppercase; } p{ font-family:algerian; font-style:italic; //internal.html:- } Internal Style Sheet USING INTERNAL STYLE SHEET First Para

Using Internal Style Sheet

First Para

Second para

Second para

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 tag which should be placed in the head section of an HTML document. This tag takes three attributes.

Attributes of the 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:- //external.html //style.css

h1{ text-align:center; External Style Sheet font-weigt:bold;

href="style.css"> p{ font-size:20;

font-style:oblique;

Introduction

color:blue;

ESS is defines the css file } seperately.

It includes many html document.

Output:-

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: Using classes

This is a paragraph that uses the p.first CSS code

This is a paragraph that uses the p.second CSS code!

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:-

Using Class 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. font-style Specifies the font style for text font-style: normal | italic | oblique

7. font-weight Specifies the weight of a font font-weight: normal | bold | 900

8. color Sets the color of text (either by name or color: red | #00ff00 | hexadecimal or rgb colors) rgb(0,0,255)

9. background-color Specifies the background color of an element background-color: orange

10.height Sets the height of an element height: auto | 50 | 25%

11.width Sets the width of an element Width: auto | 50px | 30%

12.margin Set the margin for all sides as top, right, bottom margin: 35px and left

13. background-image Specifies one or more background image for an background-image: element path(“paper.gif”)

14. border-color Sets the color of the four borders border-color: red

15. border-style Sets the style of the four border Border-style: dashed | solid | double | groove

16. direction Specifies the text direction / writing direction direction: rtl | ltr default : ltr

17. text-decoration-color Specifies the color of the text-decoration text-decoration-color : red

18. white-space Specifies how white-space inside an element is white-space: nowrap handled

19. visibility Specifies whether or not an element is visible Visibility: visible | hidden

20. z-index Sets the stack order of a positioned element z-index: -1

4. Discuss about Formatting Blocks of Information:

All the HTML elements can be categorized into two categories a) Block Level Elements b) Inline Elements a) Block Level Elements:

Block level elements appear on the screen as if they have a line break before and after them.

For example, the

,

,

,

,

,

,
,
    ,
      ,
      ,
      , 
      ,
      and
      elements are all block level elements. They all start on their own new line, and anything that follows them appears on its own new line. b) Inline Elements:

      Inline elements, on the other hand, can appear within sentences and do not have to appear on a new line of their own. The , , , , , , , , ,

    1. , , , , , , , and elements are all inline elements.

      Grouping HTML Elements:

      There are two important tags which we use very frequently to group various other HTML tags i)

      tag and ii) tag i)
      tag:

      This is very important block level tag which plays a big role in grouping various other HTML tags and applying CSS on group of elements. Even now

      tag can be used to create webpage layout where we define different parts (Left, Right, Tot etc.) of the page using
      tag. This tag does not provide any visual change on the block but this has more meaning when it is used with CSS.

      Example:

      Output: HTML div tag

      This is Second group

      List of Fruits

      • Apple
      • Mango
      • This is first group

      • Banana
      • List of vegetables

      • Tomatto
      • Potato
      • Radish

      (ii) tag:

      The HTML is an inline element and it can be used to group inline - elements in an HTML document. This tag also does not provide any visual change on the block but has more meaning when it is used with CSS.

      The difference between the tag and the

      tag is that the tag is used with inline elements whereas the
      tag is used with block – level elements.

      Example: Output:

      HTML div tag

      This is red and this is

      green

      5. Write about Layers.

      With CSS, it is possible to work with layers; pieces of HTML that are placed on top on the regular page with pixel precision (accuracy).

      Layer Basics:

      First Look at this example: Layer 1 on TOP

      Layers

      LAYER 1
      LAYER 2

      Layer 2 on TOP:

      Layers

      LAYER 1
      LAYER 2
      To create a layer all you need to do is assign the position attribute to your style. The position can be either absolute (or) relative.

      The position itself is defined with the top and left properties. Finally, which layer is on top is defined with the z-index attribute.

      Relative versus Absolute Positioning:

      You can either position your layer calculated from the upper left corner (absolute) or calculated from the position where the layer itself is inserted (relative).

      Defining the Position:

      While the position property indicates the out spring of our coordinate system, the left and top properties defines the exact position of our layer.

      You can enter both positive and negative values for these properties – thus it is possible to place content higher up and further to the left on the page than the logical position in the HTML code where the layer itself is defined.

      Both left and top properties can be dynamically changed with JavaScript. Now, it is possible to move things around on the screen even after the page has finished loading.

      Position in the Stack – the Z-index:

      Picture a game of 52 cards. If the ace of spades was at the bottom we did say it had z-index:1; If the queen of hearts was at the top we did say she had z-index:52; The z-index can be dynamically changed with JavaScript.

      You could create several “pages” on top of each other all on the same page. When the user clicks a link it will simply move the layer with the desired info on top rather than load a new page.

      Visible versus Hidden Layers:

       A final property is the visibility property that will allow you to create invisible layers.  Dynamic HTML is possible to change the visibility of layer according to certain events.  The most common use of this is to create menus that pop out. The trick behind these menus is to create all submenus as invisible layers.  When a mouse-over is detected on a link the according layer becomes visible.  Valid values for the visibility property are: visible and hidden.

      Example:

      Hello