Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Friday, June 10, 2011

Advance rules to define External CSS, and CSS Inheritance

Inheritance in HTML with respect to CSS:

As we know that a web page is composition of Markup tags. One tag contains many tags, and many tag can be defined in single tag. If you assume like Parent-Child relation, that will represents inheritance.

Example 1:

<div id="div1">
   <div id="div2">
        <div id="div3">
         ...
        </div>
   </div>
</div>

Here, div1 is parent of div2, and div2 is parent of div3 or you can say div3 is child of div2 and div2 is child of div1 

OR

Example 2:

<div id="div1">
   <div id="div2">...</div>
   <div id="div3">...</div>
</div>

div1 is parent of div2 & div3 And div2 & div3 are child of div1

In Example 1, if we define any css for div1, common properties will be automatically implemented on its all child contents. Suppose we define CSS

body{
 font-family:arial;
 color:green;
}

All the Markup tag that will be defined in body tag of an HTML document, will inherits font-family and color as defined. i.e because of body tag is parent of all other Markup tags that are defined in HTML document.

Advance rules:

Question is that, if you need to modify, or you need to apply any specific css rule on any child content, without effecting parent rules, is it possible to implement?
The answer is YES.

Solution to this problem is,

1. Inline Style for that child content can be defined in HTML.

<body style="font-family:arial;color:green;">
   <h1 style="color:red;">This is Red H1</h1>
   <p>abcdefghijk.........</p>
</body>



2. Using CSS (Embedded or External), defined in HTML and CSS files

In HTML:

<body>
   <h1>This is Red H1</h1>
   <p>abcdefghijk.........</p>
</body>

In CSS document:

body{
 font-family:arial;
 color:green;
}

body h1{
 color:red;
}

The code represents that all the contents font-family will be arial, and color will be green except all h1 tags.

















We'll discuss in next topic about advance External CSS tips.

Sunday, June 5, 2011

Comparison of CSS techniques, External CSS (Style Sheet) use?

In today's post, we'll discuss External Styles of web page. i.e How do we use External Style Sheets.

(3) External Styles


In this technique, we define all CSS rules in a style sheet file (extension .css) and then give reference to that css file in web page. The advantage of using this technique is, re-usability of CSS rules and making style consistent on all the pages. If we use this technique, we don't need to define Embedded Styles or Inline Styles.

Link tag is used in Head section, that refers to an external style sheet (reference to CSS file)

<head>
 <link type="text/css" rel="stylesheet" href="style.css">
</head>


href: contains CSS path.


Example for 2 web pages: Suppose we define two web pages and one CSS



Comparison:


Inline Styles: are used to define rule within tag using its style properly, specifically for that tag within web page.
Embedded Style: are used to make presentation of the web page consistent for defined Selectors within web page, i.e consistency between same tags.
External Styles: are used to apply consistent look on more then one pages, and all the styling rules are defined  in a separate file with extension (.css)

Friday, June 3, 2011

What is CSS? Why we use CSS in HTML?

What is CSS?
Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation (that is, the look and formatting) of a document written in a markup language.

Why we use CSS in HTML?
It works as cosmetics for an HTML document (a web page).

How to use CSS?
Headings (h1 elements), sub-headings (h2), sub-sub-headings (h3), etc., are defined structurally using HTML. In print and on the screen, choice of font , size, color and emphasis for these elements is presentational.

By default, the presentation that you look for any Webpage is known as User Agent style. i.e., the browser's default settings for element presentation.

Better approach to define css is Author styles (provided by the web page author), in the form of:
  • Inline styles, inside the HTML document, style information on a single element, specified using the "style" attribute
  • Embedded style, blocks of CSS information inside the HTML itself
  • External style sheets, i.e., a separate CSS file referenced from the document

(1) Inline styles:
 
<h1 style="color:red; font-size:12px; font-family:Arial;">This is Heading (h1) Text</h1>
 style attribute will be used within any tag h1, h2, span, div, a etc.

color:   is a key word, where red is user defined value, you can use any color by name, by hexacode like black (#000000) etc

Size: is a key word of CSS where you define the size of text in pixel (px) normally. other options related to this units can be used, but we will discuss it later.

Font Family: is a keyword to define what font style will be presented on screen when you'll open web page in browser. Just remember, you must those fonts, which are known to be Web Safe Fonts (that mean web supported font family).

Example is attached:


We'll discuss other techniques in next post.