Site Discription

Welcome to JalalZoon Website

This is Educational website.You can search for Programming languages mostly HTML, CSS, PHP and Java.This website also contains data about Study Techniques and tips for study effectively. Math tutorials are included very Soon.

Search your content here!!!

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

Wednesday, May 2, 2018

precedence of css rules

How CSS Rules Cascade

If there are two or more rules that apply to the same element,
it is important to understand which will take precedence.

LAST RULE

If the two selectors are identical, the latter of the two will take
precedence. Here you can see the second i selector takes precedence
 over the first.

SPEC IFICITY

If one selector is more specific than the others, the more
specific rule will take precedence over more general ones.

Example.html

<h1>Potatoes</h1>
<p id="intro">There are <i>dozens</i> of different
<b>potato</b> varieties.</p>
<p>They are usually described as early, second early
and maincrop potatoes.</p>


CSS file
* {
font-family: Arial, Verdana, sans-serif;}
h1 {
font-family: "Courier New", monospace;}
i {
color: green;}
i {
color: red;}
b {
color: pink;}
p b {
color: blue !important;}
p b {
color: violet;}
p#intro {
font-size: 100%;}
p {
font-size: 75%;}

 In this example: h1 is more specific than * p b is more specific than p p#intro is more specific than p

IMPORTANT


You can add !important after any property value to indicate
that it should be considered more important than other rules that apply to the same element.
Understanding how CSS rules cascade means you can write simpler style sheets because you can create generic rules that apply to most elements and then override the properties on individual elements that need to appear differently.


CSS selectors

CSS Selectors
There are many different types of CSS selector that allow you to target rules to specific elements in an HTML document. The table on the opposite page introduces the most commonly used CSS selectors.

On this page, there is an HTML file to demonstrate which elements these CSS selectors would apply to. CSS selectors are case sensitive, so they must match element names and attribute values exactly.

There are some more advanced selectors which allow you to select elements based on attributes and their values. IE 7 was the first version of IE to support the last two selectors in the table (the sibling selectors), so their use is less common than the other selectors shown here

Code Example
<!DOCTYPE html>
<html>
<head>
<title>CSS Selectors</title>
</head>
<body>
<h1 id="top">Kitchen Garden Calendar</h1>
<p id="introduction">Here you can read our
handy guide about what to do when.</p>
<h2>Spring</h2>
<ul>
<li><a href="mulch.html">
Spring mulch vegetable beds</a></li>
<li><a href="potato.html">
Plant out early potatoes</a></li>
<li><a href="tomato.html">
Sow tomato seeds</a></li>
<li><a href="beet.html">
Sow beet seeds</a></li>
<li><a href="zucchini.html">
Sow zucchini seeds</a></li>
<li><a href="rhubarb.html">
Deadhead rhubarb flowers</a></li>
</ul>
<p class="note">
This page was written by
<a href="mailto:ivy@example.org">
ivy@example.org</a> for
<a href="http://www.example.org">Example</a>.
</p>
<p>
<a href="#top">Top of page</a>
</p>
</body>
</html>

Some important slectors are as following!!!

           
Selector
Meaning
Example
Universal Selector
Applies to all elements in the document
*  { }
Targets all elements on the page
Type Selector
Matches element names
h1, h2 , h3 { }
Targets the <h1>, <h2> and <h3>
elements
Class Selector
Matches an element whose class attribute has a value that matches the one specified after the period (or full stop) symbol
.note { }
Targets any element whose class attribute has a value of note
p.note { }
Targets only <p> elements whose class attribute has value of note
ID selector
Matches an element whose id attribute has a value that matches the on specified after the pound or hash symbol
#introduction { }
Targets the element whose id attribute has a value of instruction
Child Selector
Matches an element that is direct child of another
li>a { }
Targets any <a > elements that are children of an <li> element (but not other <a> elements in the page)
Descendant Selector
Matches an element that is a descendent of another specified element ( not just a direct child of that element)
P a { }
Targets any <a> elements that sit inside a <p> element, even if there are other elements nested between them
Adjacent sibling selector
Matches an element that is the next sibling of another
h1+p { }
Target the first <p> element after any <h1> element( but not other <p> elements)
General sibling selector
Matches an element that is a sibling of another, although it does not have to be the directly preceding element
h1~p { }
if you had two <p> elements that are siblings of an <h1> element, this rule would apply to both



write css in HTML file

Using Internal CSS
<style>
You can also include CSS rules within an HTML page by placing them inside a <style> element,
which usually sits inside the <head> element of the page. The <style> element should use the type attribute to indicate that the styles are specified in CSS. The value should be text/css.
When building a site with more than one page, you should use an external CSS style sheet. This:

·         Allows all pages to use the same style rules (rather than repeating them in each page).
·         Keeps the content separate from how the page looks.
·         Means you can change the styles used across all pages by altering just one file (rather than each individual page).

In HTML 4 and Transitional XHTML, you could also use a style attribute on most of the elements that appear in the body of a page. The CSS rules that appeared within the value of the attribute would only apply to that one element. You should avoid using this attribute in any new site but I mention it here

Because you may see it used in older code. Here is an example that changes the color of the text in a single paragraph red:  <p style="color:red;">

Example.html file

<!DOCTYPE html>
<html>
<head>
<title>Using Internal CSS</title>
<style type="text/css">
body {
font-family: arial;
background-color: rgb(185,179,175);}
h1 {
color: rgb(255,255,255);}
</style>
</head>
<body>
<h1>Potatoes</h1>
<p>There are dozens of different potato
varieties. They are usually described as
early, second early and maincrop.</p>
</body>
</html>

how to link external css file with html

Using External CSS

<link>

The <link> element can be used in an HTML document to tell the browser where to find the CSS file used to style the page. It is an empty element (meaning it does not need a closing tag), and it lives inside the <head> element. It should use three attributes:

href

This specifies the path to the CSS file (which is often placed in a folder called css or styles).

type

This attribute specifies the type of document being linked to. The value should be text/css.

rel

This specifies the relationship between the HTML page and the file it is linked to. The value should be stylesheet when linking to a CSS file. An HTML page can use more than one CSS style sheet. To do this it could have a <link> element for every CSS file it uses. For example, some authors use one CSS file to control the presentation (such as fonts and colors) and a second to control the layout.


Example.html
<!DOCTYPE html>
<html>
<head>
<title>Using External CSS</title>
<link href="css/styles.css" type="text/css"
rel="stylesheet" />
</head>
<body>
<h1>Potatoes</h1>
<p>There are dozens of different potato
varieties. They are usually described as
early, second early and maincrop.</p>
</body>
</html>

Style.css file
body {
font-family: arial;
background-color: rgb(185,179,175);
}
h1 {
color: rgb(255,255,255);

}

Example code of CSS

Example
Intruducing CSS
Here you can see a simple web page that is
styled using CSS.
This example uses two documents: the HTML file (example.html)
and a separate CSS file (example.css). The fifth line of HTML uses the
<link> element to indicate where the CSS file is located.

<!DOCTYPE html>
<html>
<head>
<title>Introducing CSS</title>
<link href="css/example.css" type="text/css"
rel="stylesheet" />
</head>
<body>
<h1>From Garden to Plate</h1>
<p>A <i>potager</i> is a French term for an
ornamental vegetable or kitchen garden ... </p>
<h2>What to Plant</h2>
<p>Plants are chosen as much for their functionality
as for their color and form ... </p>
</body>
</html>
body {
font-family: Arial, Verdana, sans-serif;}
h1, h2 {
color: #ee3e80;}
p {
color: #665544;}


CSS properties affect , how elements are displayed

CSS properties affect how elements are displayed

CSS declarations sit inside curly brackets and each is made up of two parts: a property and a value, separated by a colon. You can specify several properties in one declaration, each separated by a semi-colon.



This rule indicates hat all <h1> , <h2> and <h3> elements should be shown in the Arial typeface, in a yellow color.
Properties indicate the aspects of the element you want to change. For example, color, front, width, height and border.
Value:
Specify the settings you want to use for the chosen properties. For example, if you want to specify a color property then the value is the color you want the text in this element to be.


How CSS is works

CSS Associates style rules with html elements
Css works by associating rules with HTML element. These rules govern how the content of specified elemens should be displayed. A CSS rule contains two parts: a selector and a declaration.


This rule indicates that all <p> elements should be shown in the Arial typeface.
Selectors indicate which element the rule applies to. The same rule can apply to more than one elements if you separate the element names with commas.

Declarations indicate how the elements referred to in the selector should be styled. Declarations are split into two part (a property and a value), and are separated by a colon.

Type your Comments

Comment Box is loading comments...

Like website using facebook

12th Class Math || Ch 1 Function and Limits || Exercise 1.3 Question no 2

12th Class Math || Ch 1 Function and Limits || Exercise 1.3 Question no 2 12th Class Math || Ch 1 Function and Limits || F.S.C. & I.C.S ...

Contact Us for more help

Name

Email *

Message *