Lecture 3 - CSS
Lecture 3 - CSS
•p is a selector in CSS (it points to the HTML element you want to style: <p>).
•color is a property, and red is the property value
•text-align is a property, and center is the property value
Try it out: https://www.w3schools.com/css/tryit.asp?filename=trycss_syntax1
CSS SELECTORS
CSS SELECTORS
• A CSS selector selects the HTML element(s) you want to style.
• There are 5 types of selectors:
• Simple selectors (select elements based on name, id, class)
• Combinator selectors (select elements based on a specific relationship
between them)
• Pseudo-class selectors (select elements based on a certain state)
• Pseudo-elements selectors (select and style a part of an element)
• Attribute selectors (select elements based on an attribute or attribute value)
• Try it out: https://www.w3schools.com/css/css_selectors.asp
The CSS element Selector
• The element selector selects HTML elements based on the element
name.
The CSS id Selector
• The id selector uses the id attribute of an HTML element to select a
specific element.
• The id of an element is unique within a page, so the id selector is used
to select one unique element!
• To select an element with a specific id, write a hash (#) character,
followed by the id of the element.
The CSS class Selector
• The class selector selects HTML elements with a specific class
attribute.
• To select elements with a specific class, write a period (.) character,
followed by the class name.
The CSS class Selector (cont)
• You can also specify that only specific HTML elements should be
affected by a class.
The CSS class Selector (cont)
• HTML elements can also refer to more than one class.
The CSS Universal Selector
• The universal selector (*) selects all HTML elements on the page.
The CSS Grouping Selector
• The grouping selector selects all the HTML elements with the same
style definitions.
• It will be better to group the selectors, to minimize the code.
• To group selectors, separate each selector with a comma.
• Look at the following CSS code (the h1, h2, and p elements have the
same style definitions):
CSS HOW TO
How To Add CSS
• There are three ways of inserting a style sheet:
• External CSS (best option)
• Internal CSS
• Inline CSS
• Try it out: https://www.w3schools.com/css/css_howto.asp
External CSS
• With an external style sheet, you can change the look of an entire
website by changing just one file!
• Each HTML page must include a reference to the external style sheet
file inside the <link> element, inside the head section.
External CSS (cont)
• An external style sheet can be written in any text editor, and must be
saved with a .css extension.
• The external .css file should not contain any HTML tags.
• Here is how the "mystyle.css" file looks:
Internal CSS
• An internal style sheet may be used if one single HTML page has a
unique style.
• The internal style is defined inside the <style> element, inside the
head section.
Inline CSS
• An inline style may be used to apply a unique style for a single
element.
• To use inline styles, add the style attribute to the relevant element.
The style attribute can contain any CSS property.
CSS COMMENTS
CSS Comments
• Comments are used to explain the code, and may help when you edit
the source code at a later date.
• Comments are ignored by browsers.
• A CSS comment is placed inside the <style> element, and starts with
/* and ends with */:
CSS Comments (cont)
• You can add comments wherever you want in the code:
CSS Comments (cont)
• Comments can also span multiple lines:
HTML and CSS Comments
CSS COLORS
Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values.
CSS Color Names
• Try it out: https://www.w3schools.com/css/css_colors.asp
• In CSS, a color can be specified by using a predefined color name:
CSS Background Color
• You can set the background color for HTML elements:
CSS Text Color
• You can set the color of text:
CSS Border Color
• You can set the color of borders:
CSS Color Values
• In CSS, colors can also be specified using RGB values, HEX values, HSL
values, RGBA values, and HSLA values:
CSS HEX Colors
• A hexadecimal color is specified with: #RRGGBB, where the RR (red),
GG (green) and BB (blue) hexadecimal integers specify the
components of the color.
CSS HEX Colors
CSS HEX Colors
Exercise 1: Get Color code from website
• Install ColorZilla plugin in Google Chrome (other Color Pickers)
• Get color code of KidsPlaza Cyan and Concung Pink
Exercise 2: Get Color idea from Adobe Colors
• Given Concung Pink / KidsPlaza Cyan color in background. Define the
suitable color for TEXT
CSS BACKGROUNDS
CSS background properties
• CSS background properties:
• background-color
• background-image
• background-repeat
• background-attachment
• background-position
• background (shorthand property)
• Try it out: https://www.w3schools.com/css/css_background.asp
CSS background-color
• The background-color property specifies the background color of an
element.
• With CSS, a color is most often specified by:
• a valid color name - like "red"
• a HEX value - like "#ff0000"
• an RGB value - like "rgb(255,0,0)"
Opacity / Transparency
The opacity property specifies the opacity/transparency of an element.
It can take a value from 0.0 - 1.0. The lower value, the more
transparent:
CSS Background Image
• The background-image property specifies an image to use as the
background of an element.
• By default, the image is repeated so it covers the entire element.
CSS Background Image Repeat
• By default, the background-image property repeats an image both
horizontally and vertically.
• Some images should be repeated only horizontally or vertically, or
they will look strange, like this:
CSS background-position
• The background-position property is used to specify the position of
the background image.
CSS background - Shorthand property
• To shorten the code, it is also possible to specify all the background
properties in one single property. This is called a shorthand property.
CSS BORDERS
CSS Borders
• The CSS border properties allow you to specify the style, width, and
color of an element's border.
• Check it out: https://www.w3schools.com/css/css_border.asp
CSS Border Style
• The border-style property specifies what
kind of border to display.
• dotted - Defines a dotted border
• dashed - Defines a dashed border
• solid - Defines a solid border
• double - Defines a double border
• groove - Defines a 3D grooved border. The effect
depends on the border-color value
• ridge - Defines a 3D ridged border. The effect
depends on the border-color value
• inset - Defines a 3D inset border. The effect
depends on the border-color value
• outset - Defines a 3D outset border. The effect
depends on the border-color value
• none - Defines no border
• hidden - Defines a hidden border
• The border-style property can have from one to
four values (for the top border, right border,
bottom border, and the left border).
CSS Border Width
• The border-width property specifies the width of the four borders.
• The width can be set as a specific size (in px, pt, cm, em, etc) or by using one
of the three pre-defined values: thin, medium, or thick:
Specific Side Widths
• The border-width property can have from one to four values (for the top
border, right border, bottom border, and the left border):
CSS Border Color
• The border-color property is used to set the color of the four borders.
CSS Border - Shorthand Property
• To shorten the code, it is also possible to specify all the individual border properties in one
property.
• The border property is a shorthand property for the following individual border properties:
• border-width
• border-style (required)
• border-color
CSS Rounded Borders
• The border-radius property is used to add rounded borders to an
element:
CSS BOX MODEL
Margin, Padding
The CSS Box Model
• Content - The content of the box, where text and images appear
• Padding - Clears an area around the content. The padding is
transparent
• Border - A border that goes around the padding and content
• Margin - Clears an area outside the border. The margin is transparent
The CSS Box Model
CSS Margins
• Margins are used to create space around elements, outside of any defined
borders.
• CSS has properties for specifying the margin for each side of an element:
• margin-top
• margin-right
• margin-bottom
• margin-left
• All the margin properties can have the following values:
• auto - the browser calculates the margin
• length - specifies a margin in px, pt, cm, etc.
• % - specifies a margin in % of the width of the containing element
• inherit - specifies that the margin should be inherited from the parent element
CSS Margins (cont)
CSS Margins (cont)
The auto Value
• You can set the margin property to auto to horizontally center the
element within its container.
• The element will then take up the specified width, and the remaining
space will be split equally between the left and right margins.
CSS Margins (cont)
The inherit Value
• This example lets the left margin of the <p class="ex1"> element be
inherited from the parent element (<div>):
CSS Padding
CSS has properties for specifying the padding for each side of an
element:
padding-top
padding-right
padding-bottom
padding-left
All the padding properties can have the following values:
length - specifies a padding in px, pt, cm, etc.
% - specifies a padding in % of the width of the containing element
inherit - specifies that the padding should be inherited from the parent element
CSS Padding (cont)
Exercise 3: TRY BOX MODEL BY CHROME INSPECT TOOL
Resource: https://www.w3schools.com/css/css_website_layout.asp
References
[1] W3Schools - CSS
[2] Jon Duckett - HTML & CSS - Design and Build Websites - Wiley
(2011)
THANK YOUR FOR YOUR LISTENING