22PLC15A - Module 3 - 2022-23
22PLC15A - Module 3 - 2022-23
22PLC15A - Module 3 - 2022-23
• A CSS rule set contains one or more selectors and one or more
declarations.
• The selector(s), which in this example is h1 , points to an HTML
element.
• The declaration(s), which in this example are color: blue and
text-align: center style the element with a property and value.
CSS Rules
• The selector points to the HTML element to style (h1).
• The declaration block (in curly braces) contains one or more declarations separated
by semicolons.
• Each declaration includes a CSS property name and a value, separated by a colon.
CSS Rules
Declaration of
Property: value
Selector
CSS Rules
Example for Selector and Declaration: Tree Poem web page
CSS Rules
Example for Selector and Declaration: Tree Poem web page
In the Tree Poem web page, the
• * {text-align: center;} rule causes the elements that contain text to be centered.
• The hr element does not contain text, so it’s not affected by the text-align
property and hr elements are centered by default.
• The hr {width: 50%;} rule causes the horizontal line to render with a width
that’s 50% of the web page body’s width.
• The h2, p {font-style: italic; color: blue;} rule causes the heading and paragraph
elements to be italicized and blue.
CSS Syntax and style
CSS Syntax
The style container positioned at the bottom of the web page’s head
container. In the style start tag, it’s legal to include a type attribute with a
value of "text/css", like this
CSS Syntax and style
CSS Syntax
In the Tree Poem web page, the type attribute is omitted. Currently, text/css is the
only legal value for the type attribute, and it’s the default value for the type
attribute.
Google’s Style Guide, which covers both HTML and CSS, recommends that you
reduce the size of your web page file by omitting the type attribute.
CSS Syntax and style
CSS Style
Style is used to style and layout the web pages — for example, to alter
the font, color, size, and spacing of your content, split it into multiple
columns, or add animations and other decorative features.
CSS Syntax and style
CSS Style
when there is block formatting for multi-line container elements, the start
tag and end tag are aligned at the left, and interior lines are indented.
The dot thing (.red in this example) is called a class selector because its purpose is to select elements
that have a particular value for their class attribute. So the class selector rule would select/match the
following element because it has a class attribute with a value of red:
<p class= red> a class selector that select elements that have a particular value for their class
attribute</p> In applying the class selector rule to this element, the quote gets displayed with a
tomato background color
CSS Class Selectors
Class selector: The three CSS rules with their class selectors .red, .white, and .blue
It is used apply a CSS rule to any element whose one of the classes matches specified prefix.
When the same class name is to be used for multiple elements, but each with a different style, yo
can prefix the dot with the HTML element name.
element-type.class-value {property1: value; property2: value;}
CSS Class Selectors
Class Selectors with * Prefixes
• Instead of prefacing a class selector with an element type, as an alternative, you can
preface a class selector with an *. Because * is the universal selector, it matches all
elements. Therefore, the following CSS rule is equivalent to a standard class selector rule
(with no prefix) *.class-value {property1: value; property2: value;}
• CSS property names follow the same coding convention as developer-defined class attribute
values—if there are multiple words, use hyphens to separate the words (e.g., font-size). CSS
property values usually follow the same use-hyphens-to-separate-multiple-words coding
convention.
CSS ID Selectors
• An ID selector is similar to a class selector in that it relies on an element attribute
value in searching for element matches.
• An ID selector uses an element’s id attribute (as opposed to a class selector, which
uses an element’s class attribute).
• A significant feature of an id attribute is that its value must be unique within a
particular web page. That’s different from a class attribute’s value, which does not
have to be unique within a particular web page.
• The ID selector’s unique-value feature means that an ID selector’s CSS rule matches
only one element on a web page.
• ID selectors are an important part of CSS.
CSS ID Selectors
To match an element with an id attribute, an ID selector rule iss needed, and the syntax is
The syntax is the same as for a class selector rule, except that you use a pound sign (#)
instead of a dot (.), and use an id attribute value instead of a class attribute value.
Span tags used to style inline content like a word in a line of text.
Div tags, used to style larger sections of content or act as a container for child elements.
• In the style attribute insert CSS property-value pairs directly in the code for an
individual element. So the preceding h2 element—but no other h2 elements—would be
rendered with an underline.
• The style attribute is a global attribute, which means it can be used with any element.
• The style attribute used to be referred to as “inline styles,” but the W3C no longer uses
style.
Cascading
Style Container
• The style element is a container for CSS rules that apply to the entire current web
page. The browser applies the CSS rules’ property values by matching the CSS
rules’ selectors with elements in the web page.
• You need one style container per page and it should be placed in the head container
of the web page. It's legal to put a style container in the body, but don't do it, as it
makes the CSS rules harder to find.
Cascading
Style Container
• For example, if a style attribute designates a paragraph as blue, but a rule in a
style container designates paragraphs as red, then what color will the browser use
to render the paragraph?
• According to the Priority the style attribute’s blue color wins, and the browser
renders that particular paragraph with blue text. This principle of more specific
rules beating more general rules should sound familiar. It parallels the principle
introduced earlier that says local things override global things
External CSS Files
• An external style sheet is a separate CSS file that can be accessed by creating a
link within the head section of the webpage. Multiple webpages can use the same
link to access the stylesheet.
• There are two steps necessary to tie a web page to an external file that contains
CSS rules. First, for the external file to be recognized as a CSS file, the external
file must be named with a .css extension.
• For example, in an upcoming web page, the filename pumpkinPatch.css. Second,
for the web page to access a CSS file’s CSS rules, the web page must use a link
element in the web page’s head container. The link element is comprised of just
one tag, with no end tag. Here’s the syntax:
External CSS Files
• In the code fragment, the href="name-of-external-file" attribute-value pair.
• The href stands for, “hypertext reference.”
• The href attribute’s value specifies the name of the file that holds the CSS rules
(e.g., pumpkinPatch.css).
• Note the rel="stylesheet" attribute-value pair, rel stands for “relationship,” and
its value tells the browser engine what to do with the href file.
• Having a rel value of stylesheet tells the browser engine to look for CSS rules in
the href file and apply them to the current web page.
External CSS Files
• Why is sharing an external CSS file helpful? With a shared external CSS file, it’s
easy to ensure that all the web pages on your site follow the same common CSS
rules. To change those rules, you change them in one place, in the external file, and
the change affects all the web pages that share the external file.
• External CSS files used to be referred to as “external style sheets,” but the W3C no
longer uses that term, and we won’t use it either. However, you should recognize the
term “external style sheets” because you’ll hear it being used every now and then.
External CSS Files
Example
External CSS Files
The comment syntax.
You must start with a /* and end with a */.
If you have a long comment, you should have the comment span several lines, like
this:
/* The following rules are for a CSS image sprite that enables hover effects for the
navigation bar at the left. */
CSS Validation Service
CSS validation service tool for verifying that the code in an external CSS file comports with
the W3C’s CSS standard.
• You can find the CSS validation service at https://jigsaw.w3.org/css-validator.
In Figure 3.11, note the CSS validation service’s three tabs. With the first tab, By URI, the user
enters a web address for the external file that is to be checked.
CSS Validation Service
CSS validation service tool
• With the second option, By file upload, the user selects a file on his or her local
computer. With the third option, By direct input, the user copies HTML code
• The second option, By file upload, because it’s a good idea to test a file stored
• RGB Values with Integers To specify an RGB value with integers, use this format:
rgb(red-integer,green-integer,blue-integer)
•
CSS Properties
Color Properties
2. RGB Values for Color
• RGB Values with Hexadecimal- base 16 (for the hexadecimal number system) means
there are 16 unique symbols—0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.
• rr = two hexadecimal digits that specify the amount of red
• gg = two hexadecimal digits that specify the amount of green
• bb = two hexadecimal digits that specify the amount of blue
The red portion of #0f42ba is 0f, The green portion of #0f42ba is 42, The blue
portion of #0f42ba is ba, and blue in the resulting color( out of red, green, blue, the
blue portion has larger number).
CSS Properties
Color Properties
3. Opacity Values for Color
• It is used to produce a partially transparent color by using the rgba construct with
values for red, green, blue, and opacity. The fourth value, opacity, determines how
opaque the color is, where opaque refers to the inability to see through something.
• The A in RGBA stands for alpha.
• The opacity value must be in the form of a decimal number between 0 and 1,
with 0 being completely transparent, 1 being completely opaque, and .5 in
between.
CSS Properties
Color Properties
3. Opacity Values for Color
CSS Properties
Color Properties
4. HSL Values for Color :
• HSL stands for hue, saturation, and lightness.
• Hue is a degree on the color wheel shown in FIGURE.
• The wheel is a circle, so the wheel’s degrees go from 0 to 360. 0 degrees is for red, 120 degrees
is for green, and 240 degrees is for blue.
• The second value in the hsl construct is the color’s percentage of saturation.
• The W3C says 0% means a shade of gray, and 100% is the full color.
• Saturation is the extent to which something is dissolved or absorbed compared with the
maximum possible absorption.
• The third value in the hsl construct is the color’s percentage of lightness. A lightness value of
0% generates black, lightness value of 100% generates white
CSS Properties
Color Properties
4. HSL and HSLA Values for Color
CSS Properties
Color Properties
5. HSLA Values for Color
hsla(hue-integer,saturation-percent,lightness-percent,opacity-number-between-0-
and-1)
The fourth argument specifies the opacity. The opacity value must be in the form of
a decimal number between 0 and 1, with 0 being completely transparent and
1 being completely opaque. Here’s an example CSS rule with an HSLA color value
Font Properties
• Font refers to the characteristics of text characters—height, width, thickness,
slantedness,7 body curvatures, and endpoint decorations.
• The font properties has the font-style, font-variant, font-weight, font-size, font-family,
and font shorthand properties.
1. font-style property
The font-style property specifies whether the text is to be displayed normally or
slanted. Here are the valid values for the font-style property
Font Properties
1. font-style property
• The font-variant property specifies how lowercase letters are displayed. Here are the
valid values for the font-variant property:
Font Properties
3. font-weight property
The font-weight property specifies the boldness of the text characters. Here are the valid
values for the font-weight property:
Font Properties
4. font-size property
The font-size property specifies the size of the text characters. There are quite a few
values allowed for the font-size property.
Font Properties
5. Absolute units
There are quite a few other techniques for specifying font size. For example, specify a
number along with an in, cm, mm, pt (point), or pc (pica) unit. The W3C refers to
those units rather disdainfully as “so-called absolute units.”
6. font-family Property
font-family, is more holistic in nature. The font-family property allows the web
developer to choose the set of characters that the browser uses when displaying the
element’s text.
Font Properties
6. font-family Property
Generic font names use lowercase, and you can verify that for the five generic
fonts shown
Font Properties
7. font- Shorthand Property
The font CSS shorthand property sets all the different properties of an element's font.
The font property can be used to specify all these more granular font properties—
font-style, font-variant, font-weight, font-size, line-height, and font-family
Font Properties
8. line-height Property
example class selector rule that uses the border-style property to draw a dashed
border .coupon {border-style: dashed;}
Border Properties
2. Border- width property: The border-width property specifies the width of the
border that surrounds the matched element.
Border Properties
2. Border- width property:
If you specify three values, then the first value applies to the top side, the second
value applies to the left and right sides, and the third value applies to the bottom
side. Thus, the preceding border-width property value pair could have been written
as follows and the result would be the same:
(border-top-width, border-right-width, border-bottom-width, and
border-left-width)
The dashed lines indicate the perimeters of the margin and padding areas. When a
web page is displayed, only the border can be made visible; the dashed lines shown
in the figure are only for illustration purposes.
Element Box, padding Property, margin
Property
2. padding and margin Properties
• The padding property specifies the width of the area on the interior of an
element’s border, whereas the margin property specifies the width of the area
on the exterior of an element’s border.
• Usually, the most appropriate type of value for the padding and margin properties
is a CSS pixel value.
• The margin and padding properties allow negative values. While a positive
value forces two elements to be separated by a specified amount, a negative
value causes two elements to overlap by a specified amount.
Element Box, padding Property, margin
Property
2. padding and margin Properties
Element Box, padding Property, margin
Property
2. padding and margin Properties
Padding Property, margin Property