SlideShare a Scribd company logo
Using the                                   CSS
Prepared by: Sanjay Raval | http://www.usableweb.in/
What is CSS?
CSS is a language that’s used to define the formatting applied to a Website, including
colors, background images, typefaces (fonts), margins, and indentation.

Let’s look at a basic example to see how this is done.


  <!DOCTYPE html>
  <html>
  <head>
  <title>A Simple Page</title>
  <style type="text/css">
  h1, h2 {
  font-family: sans-serif; color: #3366CC;
  }
  </style>
  </head>
  <body>
  <h1>First Title</h1><p>…</p>
  <h2>Second Title</h2><p>…</p>
  <h2>Third Title</h2><p>…</p>
  </body>
  </html>
Using CSS in HTML
lnline Styles
The simplest method of adding CSS styles to your web pages is to use inline styles.
An inline style is applied via the style attribute, like this:


     <p style="font-family: sans-serif; color: #3366CC;">
     Amazingly few discotheques provide jukeboxes.
     </p>




Inline styles have two major disadvantages:
1)     They can’t be reused. For example, if we wanted to apply the style above to another p element,
       we would have to type it out again in that element’s style attribute.
2)     Additionally, inline styles are located alongside the page’s markup, making the code difficult to
       read and maintain.
Using CSS in HTML
Embedded or Internal Styles
In this approach, you can declare any number of CSS styles by placing them between the opening
and closing <style> tags, as follows:


   <style type="text/css">
   CSS Styles here
   </style>



While it’s nice and simple, the <style>tag has one major disadvantage: if you want to use a
particular set of styles throughout your site, you’ll have to repeat those style definitions within
the style element at the top of every one of your site’s pages.
Using CSS in HTML
External Style Sheets
An external style sheet is a file (usually given a .css filename) that contains a web site’s CSS
styles, keeping them separate from any one web page. Multiple pages can link to the same .css
file, and any changes you make to the style definitions in that file will affect all the pages that
link to it.


To link a document to an external style sheet (say, styles.css), we simply place a link element in
the document’s header:

   <link rel="stylesheet" type="text/css" href="styles.css" />
CSS Selectors
Every CSS style definition has two components:
     A list of one or more selectors, separated by commas, define the element or elements to which the
      style will be applied.
     The declaration block specifies what the style actually does.
CSS Selectors
Type Selectors – Tag Selector
By naming a particular HTML element, you can apply a style rule to every occurrence of that
element in the document.

For example, the following style rule might be used to set the default font for a web site:

  p, td, th, div, dl, ul, ol {
      font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif;
      font-size: 1em;
      color: #000000;
  }
CSS Selectors
Class Selectors
CSS classes comes in when you want to assign different styles to identical elements that occur in
different places within your document.

Consider the following style, which turns all the paragraph text on a page blue:


   p { color: #0000FF; }


Great! But what would happen if you had a sidebar on your page with a blue background? You
wouldn’t want the text in the sidebar to display in blue as well—then it would be invisible. What
you need to do is define a class for your sidebar text, then assign a CSS style to that class.

To create a paragraph of the sidebarclass, first add a classattribute to the opening tag:


   <p class="sidebar">
   This text will be white, as specified by the CSS style definition
        below.
   </p>
CSS Selectors
Class Selectors
Now we can write the style for this class:


   p { color: #0000FF; }
   .sidebar { color: #FFFFFF; }



This second rule uses a class selector to indicate that the style should be applied to any element
of the sidebar class. The period indicates that we’re naming a class—not an HTML element.
CSS Selectors
ID Selectors
In contrast with class selectors, ID selectors are used to select one particular element, rather
than a group of elements. To use an ID selector, first add an id attribute to the element you wish
to style. It’s important that the ID is unique within the document:


   <p id="tagline">This paragraph is uniquely identified by the ID
       "tagline".</p>


To reference this element by its ID selector, we precede the id with a hash (#). For example, the
following rule will make the above paragraph white:


   #tagline { color: #FFFFFF; }


ID selectors can be used in combination with the other selector types.

         #tagline .new {
                      font-weight: bold;
                      color: #FFFFFF;
         }
CSS Selectors
Descendant or Compound Selectors
A descendant selector will select all the descendants of an element.

         p { color: #0000FF; }
         .sidebar p { color: #FFFFFF; }



         <div class="sidebar">
                      <p>This paragraph will be displayed in white.</p>
                      <p>So will this one.</p>
         </div>



In this case, because our page contains a div element that has a class of sidebar, the descendant
selector .sidebar p refers to all p elements inside that div.
CSS Selectors
Child Selectors
A descendant selector will select all the descendants of an element, a child selector only targets
the element’s immediate descendants, or “children.”

Consider the following markup:

         <div class="sidebar">
             <p>This paragraph will be displayed in white.</p>
             <p>So will this one.</p>
             <div class="tagline">
                      <p>If we use a descendant selector, this will be
             white too.        But if we use a child selector, it will be
             blue.</p>
             </div>
         </div>
CSS Selectors
Child Selectors
In this example, the descendant selector we saw in the previous section would match the
paragraphs that are nested directly within div.sidebar as well as those inside div.tagline. If you
didn’t want this behavior, and you only wanted to style those paragraphs that were direct
descendants of div.sidebar, you’d use a child selector.

A child selector uses the > character to specify a direct descendant.

Here’s the new CSS, which turns only those paragraphs directly inside .sidebar (but not those
within .tagline) white:


         p { color: #0000FF; }
         .sidebar>p { color: #FFFFFF; }




   Note: Unfortunately, Internet Explorer 6 doesn’t support the child selector.
Most Common

CSS Mistakes
ID vs. Class What
An ID refers to a unique instance in a document, or something that will only appear once on a
page. For example, common uses of IDs are containing elements such as page wrappers,
headers, and footers. On the other hand, a CLASS may be used multiple times within a document,
on multiple elements. A good use of classes would be the style definitions for links, or other types
of text that might be repeated in different areas on a page.


In a style sheet, an ID is preceded by a hash mark (#), and might look like the following:


         #header { height:50px; }
         #footer { height:50px; }
         #sidebar { width:200px; float:left; }
         #contents { width:600px; }
ID vs. Class What
Classes are preceded by a dot (.). An example is given below.

         .error {
         font-weight:bold;
         color:#C00;
         }
         .btn{
         background:#98A520;
         font-weight:bold;
         font-size:90%;
         height:24px;
         color:#fff;
         }
Redundant Units for Zero Values
The following code doesn't need the unit specified if the value is zero.

   padding:0px 0px 5px 0px;


It can be written instead like this:


   padding:0 0 5px 0;


Don't waste bytes by adding units such as px, pt, em, etc, when the value is zero. The only
reason to do so is when you want to change the value quickly later on. Otherwise declaring the
unit is meaningless. Zero pixels is the same as zero points.
Avoid Repetition
We should avoid using several lines when just one line will do.
For example, when setting borders, some people set each side separately:

         border-top:1px solid #00f;
         border-right:1px solid #00f;
         border-bottom:1px solid #00f;
         border-left:1px solid #00f;



But why? When each border is the same! Instead it can be like:


   border:1px solid #00f;
Excess Whitespace
Usually we have tendency to include whitespace in the code to make it readable. It'll only make
the stylesheet bigger, meaning the bandwidth usage will be higher.


Of course it's wise to leave some space in to keep it readable.
Grouping Identical Styles together
So when we want a same style to a couple of elements. It is good to group them together and
define the style instead of defining the style for each element separately.

         h1, p, #footer, .intro {
         font-family:Arial,Helvetica,sans-serif;
         }



It will also make updating the style much easier.
Margin vs. Padding
As margins and paddings are generally be used interchangeably, it is important to know the
difference. It will give you greater control over your layouts. Margin refers to the space around
the element, outside of the border. Padding refers to the space inside of the element, within the
border.

Example: No Padding, 10px Margin

          p {
          border: 1px solid #0066cc;
          margin:10px;
          padding:0;
          }




Result:
Margin vs. Padding
Example: No Margin, 10px Padding

          p {
          border: 1px solid #0066cc;
          padding:10px;
          margin:0;
          }




Result:
Ad

More Related Content

What's hot (20)

How Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) WorksHow Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) Works
Amit Tyagi
 
Css
CssCss
Css
jayakarthi
 
CSS
CSS CSS
CSS
Sunil OS
 
Cascading style sheet
Cascading style sheetCascading style sheet
Cascading style sheet
Michael Jhon
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
Nitin Bhide
 
css.ppt
css.pptcss.ppt
css.ppt
bhasula
 
CSS
CSSCSS
CSS
People Strategists
 
Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01
Hatem Mahmoud
 
Css
CssCss
Css
Abhishek Kesharwani
 
What is CSS?
What is CSS?What is CSS?
What is CSS?
HalaiHansaika
 
Cascading Style Sheet
Cascading Style SheetCascading Style Sheet
Cascading Style Sheet
KushagraChadha1
 
Css
CssCss
Css
shanmuga rajan
 
An Introduction to Cascading Style Sheets (CSS3)
An Introduction to Cascading Style Sheets (CSS3)An Introduction to Cascading Style Sheets (CSS3)
An Introduction to Cascading Style Sheets (CSS3)
Ardee Aram
 
Class 2: CSS Selectors, Classes & Ids
Class 2: CSS Selectors, Classes & IdsClass 2: CSS Selectors, Classes & Ids
Class 2: CSS Selectors, Classes & Ids
Erika Tarte
 
CSS
CSSCSS
CSS
ARJUN
 
Css
CssCss
Css
Yudha Arif Budiman
 
CSS
CSSCSS
CSS
venkatachalam84
 
CSS Foundations, pt 1
CSS Foundations, pt 1CSS Foundations, pt 1
CSS Foundations, pt 1
Shawn Calvert
 
Web Design Assignment 1
Web Design Assignment 1 Web Design Assignment 1
Web Design Assignment 1
beretta21
 
Css Basics
Css BasicsCss Basics
Css Basics
Jay Patel
 

Viewers also liked (11)

Basic css-tutorial
Basic css-tutorialBasic css-tutorial
Basic css-tutorial
mohamed ashraf
 
[CSS Drawing] Basic Tutorial (라이언 그리기)
[CSS Drawing] Basic Tutorial (라이언 그리기)[CSS Drawing] Basic Tutorial (라이언 그리기)
[CSS Drawing] Basic Tutorial (라이언 그리기)
Hyejin Oh
 
JakartaJS - How I Learn Javascript From Basic
JakartaJS - How I Learn Javascript From BasicJakartaJS - How I Learn Javascript From Basic
JakartaJS - How I Learn Javascript From Basic
Irfan Maulana
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)
Chris Poteet
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
Amit Tyagi
 
Introduction to html
Introduction to htmlIntroduction to html
Introduction to html
vikasgaur31
 
HTML CSS Basics
HTML CSS BasicsHTML CSS Basics
HTML CSS Basics
Mai Moustafa
 
HTML presentation for beginners
HTML presentation for beginnersHTML presentation for beginners
HTML presentation for beginners
jeroenvdmeer
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
Sehwan Noh
 
Introduction to HTML
Introduction to HTMLIntroduction to HTML
Introduction to HTML
MayaLisa
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS Presentation
Shawn Calvert
 
[CSS Drawing] Basic Tutorial (라이언 그리기)
[CSS Drawing] Basic Tutorial (라이언 그리기)[CSS Drawing] Basic Tutorial (라이언 그리기)
[CSS Drawing] Basic Tutorial (라이언 그리기)
Hyejin Oh
 
JakartaJS - How I Learn Javascript From Basic
JakartaJS - How I Learn Javascript From BasicJakartaJS - How I Learn Javascript From Basic
JakartaJS - How I Learn Javascript From Basic
Irfan Maulana
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)
Chris Poteet
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
Amit Tyagi
 
Introduction to html
Introduction to htmlIntroduction to html
Introduction to html
vikasgaur31
 
HTML presentation for beginners
HTML presentation for beginnersHTML presentation for beginners
HTML presentation for beginners
jeroenvdmeer
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
Sehwan Noh
 
Introduction to HTML
Introduction to HTMLIntroduction to HTML
Introduction to HTML
MayaLisa
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS Presentation
Shawn Calvert
 
Ad

Similar to CSS Basic and Common Errors (20)

Css
CssCss
Css
actacademy
 
Css
CssCss
Css
actacademy
 
Rational HATS and CSS
Rational HATS and CSSRational HATS and CSS
Rational HATS and CSS
Strongback Consulting
 
IP - Lecture 6, 7 Chapter-3 (3).ppt
IP - Lecture 6, 7 Chapter-3 (3).pptIP - Lecture 6, 7 Chapter-3 (3).ppt
IP - Lecture 6, 7 Chapter-3 (3).ppt
kassahungebrie
 
Css
CssCss
Css
Jahid Blackrose
 
Css
CssCss
Css
Er. Nawaraj Bhandari
 
CSS_Day_ONE (W3schools)
CSS_Day_ONE (W3schools)CSS_Day_ONE (W3schools)
CSS_Day_ONE (W3schools)
Rafi Haidari
 
HTML to CSS Basics Exer 2.pptx
HTML to CSS Basics Exer 2.pptxHTML to CSS Basics Exer 2.pptx
HTML to CSS Basics Exer 2.pptx
JJFajardo1
 
Introduction to css by programmerblog.net
Introduction to css by programmerblog.netIntroduction to css by programmerblog.net
Introduction to css by programmerblog.net
Programmer Blog
 
Css
CssCss
Css
Balakumaran Arunachalam
 
CSS 101
CSS 101CSS 101
CSS 101
dunclair
 
Css training tutorial css3 &amp; css4 essentials
Css training tutorial css3 &amp; css4 essentialsCss training tutorial css3 &amp; css4 essentials
Css training tutorial css3 &amp; css4 essentials
QA TrainingHub
 
David Weliver
David WeliverDavid Weliver
David Weliver
Philip Taylor
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
Folasade Adedeji
 
CSS Foundations, pt 2
CSS Foundations, pt 2CSS Foundations, pt 2
CSS Foundations, pt 2
Shawn Calvert
 
Css
CssCss
Css
NIRMAL FELIX
 
3.2 introduction to css
3.2 introduction to css3.2 introduction to css
3.2 introduction to css
Bulldogs83
 
3.2 introduction to css
3.2 introduction to css3.2 introduction to css
3.2 introduction to css
Bulldogs83
 
Unitegergergegegegetgegegegegegeg-2-CSS.pptx
Unitegergergegegegetgegegegegegeg-2-CSS.pptxUnitegergergegegegetgegegegegegeg-2-CSS.pptx
Unitegergergegegegetgegegegegegeg-2-CSS.pptx
VikasTuwar1
 
Css basics
Css basicsCss basics
Css basics
mirza asif haider
 
Ad

More from Hock Leng PUAH (20)

ASP.net Image Slideshow
ASP.net Image SlideshowASP.net Image Slideshow
ASP.net Image Slideshow
Hock Leng PUAH
 
Visual basic asp.net programming introduction
Visual basic asp.net programming introductionVisual basic asp.net programming introduction
Visual basic asp.net programming introduction
Hock Leng PUAH
 
Using iMac Built-in Screen Sharing
Using iMac Built-in Screen SharingUsing iMac Built-in Screen Sharing
Using iMac Built-in Screen Sharing
Hock Leng PUAH
 
Hosting SWF Flash file
Hosting SWF Flash fileHosting SWF Flash file
Hosting SWF Flash file
Hock Leng PUAH
 
PHP built-in functions date( ) and mktime( ) to calculate age from date of birth
PHP built-in functions date( ) and mktime( ) to calculate age from date of birthPHP built-in functions date( ) and mktime( ) to calculate age from date of birth
PHP built-in functions date( ) and mktime( ) to calculate age from date of birth
Hock Leng PUAH
 
PHP built-in function mktime example
PHP built-in function mktime examplePHP built-in function mktime example
PHP built-in function mktime example
Hock Leng PUAH
 
A simple php exercise on date( ) function
A simple php exercise on date( ) functionA simple php exercise on date( ) function
A simple php exercise on date( ) function
Hock Leng PUAH
 
Integrate jQuery PHP MySQL project to JOOMLA web site
Integrate jQuery PHP MySQL project to JOOMLA web siteIntegrate jQuery PHP MySQL project to JOOMLA web site
Integrate jQuery PHP MySQL project to JOOMLA web site
Hock Leng PUAH
 
Responsive design
Responsive designResponsive design
Responsive design
Hock Leng PUAH
 
Step by step guide to use mac lion to make hidden folders visible
Step by step guide to use mac lion to make hidden folders visibleStep by step guide to use mac lion to make hidden folders visible
Step by step guide to use mac lion to make hidden folders visible
Hock Leng PUAH
 
Beautiful web pages
Beautiful web pagesBeautiful web pages
Beautiful web pages
Hock Leng PUAH
 
Connectivity Test for EES Logic Probe Project
Connectivity Test for EES Logic Probe ProjectConnectivity Test for EES Logic Probe Project
Connectivity Test for EES Logic Probe Project
Hock Leng PUAH
 
Logic gate lab intro
Logic gate lab introLogic gate lab intro
Logic gate lab intro
Hock Leng PUAH
 
Ohm's law, resistors in series or in parallel
Ohm's law, resistors in series or in parallelOhm's law, resistors in series or in parallel
Ohm's law, resistors in series or in parallel
Hock Leng PUAH
 
Connections Exercises Guide
Connections Exercises GuideConnections Exercises Guide
Connections Exercises Guide
Hock Leng PUAH
 
Design to circuit connection
Design to circuit connectionDesign to circuit connection
Design to circuit connection
Hock Leng PUAH
 
NMS Media Services Jobshet 1 to 5 Summary
NMS Media Services Jobshet 1 to 5 SummaryNMS Media Services Jobshet 1 to 5 Summary
NMS Media Services Jobshet 1 to 5 Summary
Hock Leng PUAH
 
Virtualbox step by step guide
Virtualbox step by step guideVirtualbox step by step guide
Virtualbox step by step guide
Hock Leng PUAH
 
Nms chapter 01
Nms chapter 01Nms chapter 01
Nms chapter 01
Hock Leng PUAH
 
Pedagogic Innovation to Engage Academically Weaker Students
Pedagogic Innovation to Engage Academically Weaker StudentsPedagogic Innovation to Engage Academically Weaker Students
Pedagogic Innovation to Engage Academically Weaker Students
Hock Leng PUAH
 
ASP.net Image Slideshow
ASP.net Image SlideshowASP.net Image Slideshow
ASP.net Image Slideshow
Hock Leng PUAH
 
Visual basic asp.net programming introduction
Visual basic asp.net programming introductionVisual basic asp.net programming introduction
Visual basic asp.net programming introduction
Hock Leng PUAH
 
Using iMac Built-in Screen Sharing
Using iMac Built-in Screen SharingUsing iMac Built-in Screen Sharing
Using iMac Built-in Screen Sharing
Hock Leng PUAH
 
Hosting SWF Flash file
Hosting SWF Flash fileHosting SWF Flash file
Hosting SWF Flash file
Hock Leng PUAH
 
PHP built-in functions date( ) and mktime( ) to calculate age from date of birth
PHP built-in functions date( ) and mktime( ) to calculate age from date of birthPHP built-in functions date( ) and mktime( ) to calculate age from date of birth
PHP built-in functions date( ) and mktime( ) to calculate age from date of birth
Hock Leng PUAH
 
PHP built-in function mktime example
PHP built-in function mktime examplePHP built-in function mktime example
PHP built-in function mktime example
Hock Leng PUAH
 
A simple php exercise on date( ) function
A simple php exercise on date( ) functionA simple php exercise on date( ) function
A simple php exercise on date( ) function
Hock Leng PUAH
 
Integrate jQuery PHP MySQL project to JOOMLA web site
Integrate jQuery PHP MySQL project to JOOMLA web siteIntegrate jQuery PHP MySQL project to JOOMLA web site
Integrate jQuery PHP MySQL project to JOOMLA web site
Hock Leng PUAH
 
Step by step guide to use mac lion to make hidden folders visible
Step by step guide to use mac lion to make hidden folders visibleStep by step guide to use mac lion to make hidden folders visible
Step by step guide to use mac lion to make hidden folders visible
Hock Leng PUAH
 
Connectivity Test for EES Logic Probe Project
Connectivity Test for EES Logic Probe ProjectConnectivity Test for EES Logic Probe Project
Connectivity Test for EES Logic Probe Project
Hock Leng PUAH
 
Ohm's law, resistors in series or in parallel
Ohm's law, resistors in series or in parallelOhm's law, resistors in series or in parallel
Ohm's law, resistors in series or in parallel
Hock Leng PUAH
 
Connections Exercises Guide
Connections Exercises GuideConnections Exercises Guide
Connections Exercises Guide
Hock Leng PUAH
 
Design to circuit connection
Design to circuit connectionDesign to circuit connection
Design to circuit connection
Hock Leng PUAH
 
NMS Media Services Jobshet 1 to 5 Summary
NMS Media Services Jobshet 1 to 5 SummaryNMS Media Services Jobshet 1 to 5 Summary
NMS Media Services Jobshet 1 to 5 Summary
Hock Leng PUAH
 
Virtualbox step by step guide
Virtualbox step by step guideVirtualbox step by step guide
Virtualbox step by step guide
Hock Leng PUAH
 
Pedagogic Innovation to Engage Academically Weaker Students
Pedagogic Innovation to Engage Academically Weaker StudentsPedagogic Innovation to Engage Academically Weaker Students
Pedagogic Innovation to Engage Academically Weaker Students
Hock Leng PUAH
 

Recently uploaded (20)

What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 

CSS Basic and Common Errors

  • 1. Using the CSS Prepared by: Sanjay Raval | http://www.usableweb.in/
  • 2. What is CSS? CSS is a language that’s used to define the formatting applied to a Website, including colors, background images, typefaces (fonts), margins, and indentation. Let’s look at a basic example to see how this is done. <!DOCTYPE html> <html> <head> <title>A Simple Page</title> <style type="text/css"> h1, h2 { font-family: sans-serif; color: #3366CC; } </style> </head> <body> <h1>First Title</h1><p>…</p> <h2>Second Title</h2><p>…</p> <h2>Third Title</h2><p>…</p> </body> </html>
  • 3. Using CSS in HTML lnline Styles The simplest method of adding CSS styles to your web pages is to use inline styles. An inline style is applied via the style attribute, like this: <p style="font-family: sans-serif; color: #3366CC;"> Amazingly few discotheques provide jukeboxes. </p> Inline styles have two major disadvantages: 1) They can’t be reused. For example, if we wanted to apply the style above to another p element, we would have to type it out again in that element’s style attribute. 2) Additionally, inline styles are located alongside the page’s markup, making the code difficult to read and maintain.
  • 4. Using CSS in HTML Embedded or Internal Styles In this approach, you can declare any number of CSS styles by placing them between the opening and closing <style> tags, as follows: <style type="text/css"> CSS Styles here </style> While it’s nice and simple, the <style>tag has one major disadvantage: if you want to use a particular set of styles throughout your site, you’ll have to repeat those style definitions within the style element at the top of every one of your site’s pages.
  • 5. Using CSS in HTML External Style Sheets An external style sheet is a file (usually given a .css filename) that contains a web site’s CSS styles, keeping them separate from any one web page. Multiple pages can link to the same .css file, and any changes you make to the style definitions in that file will affect all the pages that link to it. To link a document to an external style sheet (say, styles.css), we simply place a link element in the document’s header: <link rel="stylesheet" type="text/css" href="styles.css" />
  • 6. CSS Selectors Every CSS style definition has two components:  A list of one or more selectors, separated by commas, define the element or elements to which the style will be applied.  The declaration block specifies what the style actually does.
  • 7. CSS Selectors Type Selectors – Tag Selector By naming a particular HTML element, you can apply a style rule to every occurrence of that element in the document. For example, the following style rule might be used to set the default font for a web site: p, td, th, div, dl, ul, ol { font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 1em; color: #000000; }
  • 8. CSS Selectors Class Selectors CSS classes comes in when you want to assign different styles to identical elements that occur in different places within your document. Consider the following style, which turns all the paragraph text on a page blue: p { color: #0000FF; } Great! But what would happen if you had a sidebar on your page with a blue background? You wouldn’t want the text in the sidebar to display in blue as well—then it would be invisible. What you need to do is define a class for your sidebar text, then assign a CSS style to that class. To create a paragraph of the sidebarclass, first add a classattribute to the opening tag: <p class="sidebar"> This text will be white, as specified by the CSS style definition below. </p>
  • 9. CSS Selectors Class Selectors Now we can write the style for this class: p { color: #0000FF; } .sidebar { color: #FFFFFF; } This second rule uses a class selector to indicate that the style should be applied to any element of the sidebar class. The period indicates that we’re naming a class—not an HTML element.
  • 10. CSS Selectors ID Selectors In contrast with class selectors, ID selectors are used to select one particular element, rather than a group of elements. To use an ID selector, first add an id attribute to the element you wish to style. It’s important that the ID is unique within the document: <p id="tagline">This paragraph is uniquely identified by the ID "tagline".</p> To reference this element by its ID selector, we precede the id with a hash (#). For example, the following rule will make the above paragraph white: #tagline { color: #FFFFFF; } ID selectors can be used in combination with the other selector types. #tagline .new { font-weight: bold; color: #FFFFFF; }
  • 11. CSS Selectors Descendant or Compound Selectors A descendant selector will select all the descendants of an element. p { color: #0000FF; } .sidebar p { color: #FFFFFF; } <div class="sidebar"> <p>This paragraph will be displayed in white.</p> <p>So will this one.</p> </div> In this case, because our page contains a div element that has a class of sidebar, the descendant selector .sidebar p refers to all p elements inside that div.
  • 12. CSS Selectors Child Selectors A descendant selector will select all the descendants of an element, a child selector only targets the element’s immediate descendants, or “children.” Consider the following markup: <div class="sidebar"> <p>This paragraph will be displayed in white.</p> <p>So will this one.</p> <div class="tagline"> <p>If we use a descendant selector, this will be white too. But if we use a child selector, it will be blue.</p> </div> </div>
  • 13. CSS Selectors Child Selectors In this example, the descendant selector we saw in the previous section would match the paragraphs that are nested directly within div.sidebar as well as those inside div.tagline. If you didn’t want this behavior, and you only wanted to style those paragraphs that were direct descendants of div.sidebar, you’d use a child selector. A child selector uses the > character to specify a direct descendant. Here’s the new CSS, which turns only those paragraphs directly inside .sidebar (but not those within .tagline) white: p { color: #0000FF; } .sidebar>p { color: #FFFFFF; } Note: Unfortunately, Internet Explorer 6 doesn’t support the child selector.
  • 15. ID vs. Class What An ID refers to a unique instance in a document, or something that will only appear once on a page. For example, common uses of IDs are containing elements such as page wrappers, headers, and footers. On the other hand, a CLASS may be used multiple times within a document, on multiple elements. A good use of classes would be the style definitions for links, or other types of text that might be repeated in different areas on a page. In a style sheet, an ID is preceded by a hash mark (#), and might look like the following: #header { height:50px; } #footer { height:50px; } #sidebar { width:200px; float:left; } #contents { width:600px; }
  • 16. ID vs. Class What Classes are preceded by a dot (.). An example is given below. .error { font-weight:bold; color:#C00; } .btn{ background:#98A520; font-weight:bold; font-size:90%; height:24px; color:#fff; }
  • 17. Redundant Units for Zero Values The following code doesn't need the unit specified if the value is zero. padding:0px 0px 5px 0px; It can be written instead like this: padding:0 0 5px 0; Don't waste bytes by adding units such as px, pt, em, etc, when the value is zero. The only reason to do so is when you want to change the value quickly later on. Otherwise declaring the unit is meaningless. Zero pixels is the same as zero points.
  • 18. Avoid Repetition We should avoid using several lines when just one line will do. For example, when setting borders, some people set each side separately: border-top:1px solid #00f; border-right:1px solid #00f; border-bottom:1px solid #00f; border-left:1px solid #00f; But why? When each border is the same! Instead it can be like: border:1px solid #00f;
  • 19. Excess Whitespace Usually we have tendency to include whitespace in the code to make it readable. It'll only make the stylesheet bigger, meaning the bandwidth usage will be higher. Of course it's wise to leave some space in to keep it readable.
  • 20. Grouping Identical Styles together So when we want a same style to a couple of elements. It is good to group them together and define the style instead of defining the style for each element separately. h1, p, #footer, .intro { font-family:Arial,Helvetica,sans-serif; } It will also make updating the style much easier.
  • 21. Margin vs. Padding As margins and paddings are generally be used interchangeably, it is important to know the difference. It will give you greater control over your layouts. Margin refers to the space around the element, outside of the border. Padding refers to the space inside of the element, within the border. Example: No Padding, 10px Margin p { border: 1px solid #0066cc; margin:10px; padding:0; } Result:
  • 22. Margin vs. Padding Example: No Margin, 10px Padding p { border: 1px solid #0066cc; padding:10px; margin:0; } Result:
  翻译: