Cascading Style Sheet (CSS) - week4.pptx PDF

Document Details

WholesomeAgate2961

Uploaded by WholesomeAgate2961

جامعة البلقاء التطبيقية

Tags

CSS Cascading Style Sheets Web Design HTML

Summary

This presentation provides an overview of Cascading Style Sheets (CSS). It explains how CSS styles HTML elements and streamlines the development process. It discusses the structure of CSS rules and the use of CSS for styling multiple web pages efficiently.

Full Transcript

Cascading Style Sheet What is CSS?  CSS stands for Cascading Style Sheets  CSSdescribes how HTML elements are to be displayed on screen, paper, or in other media  CSS saves a lot of work. It can control the layout of multiple web pages all at once  External stylesheets are stored in CS...

Cascading Style Sheet What is CSS?  CSS stands for Cascading Style Sheets  CSSdescribes how HTML elements are to be displayed on screen, paper, or in other media  CSS saves a lot of work. It can control the layout of multiple web pages all at once  External stylesheets are stored in CSS files Why Use CSS?  CSS is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.  CSS Example body { background-color: lightblue; } h1 { color: white; text-align: center; } p{ font-family: verdana; font-size: 20px; CSS Solved a Big Problem  HTML was created to describe the content of a web page, like: ◦ This is a heading ◦ This is a paragraph.  When tags like font, and color attributes were added to the HTML specification, it started a nightmare for web developers.  Development of large websites, where fonts and color information were added to every single page, became a long and expensive process.  To solve this problem, the World Wide Web Consortium (W3C) created CSS.  CSS removed the style formatting from the HTML page!  CSS Saves a Lot of Work! ◦ The style definitions are normally saved in external.css files. ◦ With an external stylesheet file, you can change the look of an entire website CSS Syntax  A CSS rule-set consists of a selector and a declaration block: ◦ The selector points to the HTML element you want to style. ◦ The declaration block contains one or more declarations separated by semicolons.  Each declaration includes a CSS property name and a value, separated by a colon.  Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded by curly braces. Example  In this example all elements will be center- aligned, with a red text color: p{ color: red; text-align: center; } ◦ p is a selector in CSS (it points to the HTML element you want to style: ). ◦ color is a property, and red is the property value ◦ text-align is a property, and center is the property value How To Add CSS  When a browser reads a style sheet, it will format the HTML document according to the information in the style sheet.  There are three ways of inserting a style sheet: ◦ External CSS ◦ Internal CSS ◦ Inline CSS 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 element, inside the head section. External CSS(Cont’s)  External styles are defined within the element, inside the section of an HTML page: This is a heading This is a paragraph.  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 like: body { background-color: lightblue; } h1 { color: navy; margin-left: 20px; }  Note: Do not add a space between the property value and the unit (such as margin-left: 20 px;). The correct way is: margin-left: 20px; 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 element, inside the head section.  Example body { background-color: linen; } h1 { color: maroon; margin-left: 40px; } This is a heading This is a paragraph. 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.  Inline styles are defined within the "style" attribute of the relevant element:  Example This is a heading This is a paragraph.  Tip: An inline style loses many of the advantages of a style sheet (by mixing content with presentation). Use this method sparingly. Multiple Style Sheets  If some properties have been defined for the same selector (element) in different style sheets, the value from the last read style sheet will be used.  Assume that an external style sheet has the following style for the element: h1 { color: navy; }  Then, assume that an internal style sheet also has the following style for the element: h1 { color: orange; }  If the internal style is defined after the link to the external style sheet, the elements will be "orange": h1 { color: orange; }  However, if the internal style is defined before the link to the external style sheet, the elements will be "navy": h1 { color: orange; } Cascading Order  All the styles in a page will "cascade" into a new "virtual" style sheet by the following rules, where number one has the highest priority: ◦ Inline style (inside an HTML element) ◦ External and internal style sheets (in the head section)  So, an inline style has the highest priority, and will override external and internal styles. CSS Selectors  CSS selectors are used to "find" (or select) the HTML elements you want to style.  Common CSS selectors ◦ The CSS element Selector ◦ The CSS id Selector ◦ The CSS class Selector ◦ The CSS Universal Selector ◦ The CSS Grouping Selector The CSS element Selector  The element selector selects HTML elements based on the element name.  Here, all elements on the page will be center-aligned, with a red text color: p{ text-align: center; color: red; } 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 rule below will be applied to the HTML element with id="para1": #para1 { text-align: center; color: red; } 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.  In this example all HTML elements with class="center" will be red and center-aligned:.center { text-align: center; color: red; } The CSS class Selector(Cont’s)  You can also specify that only specific HTML elements should be affected by a class.  In this example only elements with class="center" will be center-aligned: p.center { text-align: center; color: red; }  HTML elements can also refer to more than one class.  In this example the element will be styled according to class="center" and to class="large": This paragraph refers to two classes. The CSS Universal Selector  The universal selector (*) selects all HTML elements on the page.  The CSS rule below will affect every HTML element on the page: *{ text-align: center; color: blue; } Hello world! Every element on the page will be affected by the style. Me too! And me! The CSS Grouping Selector  The grouping selector selects all the HTML elements with the same style definitions.  Look at the following CSS code (the h1, h2, and p elements have the same style definitions): h1 { text-align: center; color: red; } h2 { text-align: center; color: red; } p{ text-align: center; color: red; }  It will be better to group the selectors, to minimize the code.  To group selectors, separate each selector with a comma. The CSS Grouping Selector(Cont’s)  In this example we have grouped the selectors from the code above: h1, h2, p { text-align: center; color: red; } h1, h2, p { text-align: center; color: red; } Hello World! Smaller heading! This is a paragraph. 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 element, and starts with :  Example p { color: red; }  You can add comments wherever you want in the code as shown in next example p{ color: red; }  Comments can also span multiple lines as shown next example p{ color: red; } HTML and CSS Comments  From the HTML tutorial, you learned that you can add comments to your HTML source by using the Hello World! This paragraph is styled with CSS. CSS comments are not shown in the output. CSS Colors  Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values.  CSS Color Names can be specified by using a predefined color name:  CSS Background Color You can set the background color for HTML elements:  Example Hello World Lorem ipsum... CSS Color(Cont’s)  You can set the color of text:  Example Hello World Lorem ipsum... Ut wisi enim...  You can set the color of borders:  Example Hello World Hello World Hello World CSS Color Values  In CSS, colors can also be specified using RGB values, HEX values, HSL values, RGBA values, and HSLA values: ◦ Same as color name "Tomato": ◦ rgb(255, 99, 71) ◦ #ff6347 ◦ hsl(9, 100%, 64%) ◦ rgba(255, 99, 71, 0.5) ◦ hsla(9, 100%, 64%, 0.5)  Example............ CSS RGB Colors  In CSS, a color can be specified as an RGB value, using this formula: rgb(red, green, blue)  Each parameter (red, green, and blue) defines the intensity of the color between 0 and 255.  For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255) and the others are set to 0.  To display black, set all color parameters to 0, like this: rgb(0, 0, 0).  To display white, set all color parameters to 255, like this: rgb(255, 255, 255). RGBA Colors  RGBA color values are an extension of RGB color values with an alpha channel - which specifies the opacity for a color.  An RGBA color value is specified with:  rgba(red, green, blue, alpha)  The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at all) HEX Colors  In CSS, a color can be specified using a hexadecimal value in the form: #rrggbb  Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00 and ff (same as decimal 0-255).  For example, #ff0000 is displayed as red, because red is set to its highest value (ff) and the others are set to the lowest value (00). HSL Colors  In CSS, a color can be specified using hue, saturation, and lightness (HSL) in the form: hsl(hue, saturation, lightness)  Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is green, and 240 is blue.  Saturation is a percentage value, 0% means a shade of gray, and 100% is the full color.  Lightness is also a percentage, 0% is black, 50% is neither light or dark, 100% is white HSLA Colors  HSLA color values are an extension of HSL color values with an alpha channel - which specifies the opacity for a color.  An HSLA color value is specified with:  hsla(hue, saturation, lightness, alpha)  The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at all): 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: h1 { background-color: green; opacity: 0.3;}  An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque). h1 { background: rgba(0, 128, 0, 0.3) } CSS Backgrounds  The CSS background properties are used to define the background effects for elements.  Here, we will learn about the following CSS background properties: ◦ background-color ◦ background-image ◦ background-repeat ◦ background-attachment ◦ background-position  CSS background-color ◦ The background-color property specifies the background color of an element as shown in next example that set background color of a page like this: body { background-color: lightblue; } ◦ You can set the background color for any HTML elements as shown in next example h1 { background-color: green; } p { background-color: yellow; } 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.  Example: Set the background image for a page: body { background-image: url("paper.gif"); }  Note: When using a background image, use an image that does not disturb the text.  The background image can also be set for specific elements, like the element: p{ background-image: url("paper.gif"); } CSS Background Image (Conts)  By default, the background-image property repeats an image both horizontally and vertically. ◦ For horizontally repeating (background-repeat: repeat-x;) body { background-image: url("gradient_bg.png"); background-repeat: repeat- x; } ◦ For vertically repeating (background-repeat: repeat-y;) body { background-image: url("gradient_bg.png"); background-repeat: repeat- y; }  Showing the background image only once is also specified by the background-repeat property: body { background-image: url("img_tree.png"); background-repeat: no- repeat; }  The background-position property is used to specify the position of the background image. The next example set background image in the top-right corner: body { background-image: url("img_tree.png"); background-repeat: no-repeat; CSS Background Image (Conts)  The background-attachment property specifies whether the background image should scroll or be fixed (will not scroll with the rest of the page):  Example: Specify that the background image should be fixed: body { background-image: url("img_tree.png"); background-repeat: no-repeat; background-position: right top; background-attachment: fixed; }  Example: Specify that the background image should scroll with the rest of the page: body { background-image: url("img_tree.png"); background-repeat: no-repeat; CSS Background Shorthand  To shorten the code, it is also possible to specify all the background properties in one single property. This is called a shorthand property.  Instead of writing: body { background-color: #ffffff; background-image: url("img_tree.png"); background-repeat: no-repeat; background-position: right top; }  You can use the shorthand property background: body { background: #ffffff url("img_tree.png") no-repeat right top; }  When using the shorthand property the order of the property values is: ◦ background-color ◦ background-image ◦ background-repeat ◦ background-attachment ◦ background-position  It does not matter if one of the property values is missing, as long as the other ones are in this order. Note that we do not use the background-attachment property in the examples above, as it does not have a value. CSS Borders  TheCSS border properties allow you to specify the style, width, and color of an element's border. CSS Border Style  The border-style property specifies what kind of border to display.  The following values are allowed: ◦ 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). Example The border-style Property p.dotted {border-style: dotted;} This property specifies what kind of p.dashed {border-style: dashed;} border to display: p.solid {border-style: solid;} p.double {border-style: double;} A dotted border. p.groove {border-style: groove;} A dashed p.ridge {border-style: ridge;} border. p.inset {border-style: inset;} A solid border. p.outset {border-style: outset;} A double border. p.none {border-style: none;} A groove border. p.hidden {border-style: hidden;} A ridge border. p.mix {border-style: dotted dashed solid An inset border. double;} An outset border. No border. A hidden border. A mixed 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 unit or by using one of the three pre-defined values: thin, medium, or thick:  Example: Demonstration of the different border widths: p.one { border-style: solid; border-width: 5px; } p.two { border-style: solid; border-width: medium; } p.three { border-style: dotted; border-width: 2px; } p.four { border-style: dotted; border-width: 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):  Example The border-width Property The border-width property can have p.one { from one to four values (for the top border, border-style: solid; right border, bottom border, and the left border-width: 5px 20px; } Some text. Some text. p.two { Some text. border-style: solid; border-width: 20px 5px; } p.three { border-style: solid; border-width: 25px 10px 4px 35px; } CSS Border Color  The border-color property is used to set the color of the four borders.  The color can be set by: ◦ name - specify a color name, like "red" ◦ HEX - specify a HEX value, like "#ff0000" ◦ RGB - specify a RGB value, like "rgb(255,0,0)" ◦ HSL - specify a HSL value, like "hsl(0, 100%, 50%)" ◦ Transparent  Example: Demonstration of the border color p.one { border-style: solid; border-color: red; }  Note: If border-color is not set, it inherits the color of the element. Specific Side Colors  The border-color property can have from one to four values (for the top border, right border, bottom border, and the left border).  Example p.one { border-style: solid; border-color: red green blue yellow; } p.one { border-style: solid; border-color: red green blue yellow; } The border-color Property The border-color property can have from one to four values (for the top border, right border, bottom border, and the left border): A solid multicolor border CSS Border - Individual Sides  It is possible to specify a different border for each side.  In CSS, there are also properties for specifying each of the borders (top, right, bottom, and left):  Example p{ border-top-style: dotted; border-right-style: solid; border-bottom-style: dotted; CSS Border - Shorthand Property  Like you saw in the previous page, there are many properties to consider when dealing with borders.  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  Example p{ border: 5px solid red; } CSS Rounded Borders  The border-radius property is used to add rounded borders to an element:  Example The border-radius Property This property is used to add p.normal { rounded borders to an element: border: 2px solid red; } Normal border Round border p.round1 { Rounder border border: 2px solid red; Roundest border-radius: 5px; border } p.round2 { border: 2px solid red; border-radius: 8px; } p.round3 { border: 2px solid red; border-radius: 12px; } CSS Margins  The CSS margin properties are used to create space around elements, outside of any defined borders.  With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).  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 Example  Set different margins for all four sides of a element: p{ margin-top: 100px; margin-bottom: 100px; margin-right: 150px; margin-left: 80px; } Margin - Shorthand Property  To shorten the code, it is possible to specify all the margin properties in one property.  The margin property is a shorthand property for the following individual margin properties: ◦ margin-top ◦ margin-right ◦ margin-bottom ◦ margin-left  So, here is how it works:  If the margin property has four values:  margin: 25px 50px 75px 100px; ◦ top margin is 25px ◦ right margin is 50px ◦ bottom margin is 75px ◦ left margin is 100px  Example: Use the margin shorthand property with four values: p{ margin: 25px 50px 75px 100px; } Example  If the margin property has three values:  margin: 25px 50px 75px; ◦ top margin is 25px ◦ right and left margins are 50px ◦ bottom margin is 75px  Example: Use the margin shorthand property with three values: p{ margin: 25px 50px 75px; }  If the margin property has two values:  margin: 25px 50px; ◦ top and bottom margins are 25px ◦ right and left margins are 50px  Example: Use the margin shorthand property with two values: p{ margin: 25px 50px; }  If the margin property has one value:  margin: 25px; ◦ all four margins are 25px  Example: Use the margin shorthand property with one value: p{ margin: 25px; } 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.  Example: Use margin auto: p{ width: 300px; margin: auto; border: 1px solid red; } CSS Padding  The CSS padding properties are used to generate space around an element's content, inside of any defined borders.  With CSS, you have full control over the padding. There are properties for setting the padding for each side of an element (top, right, bottom, and left).  Padding - Individual Sides  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 Example Set different padding for all four sides of a element: p{ padding-top: 50px; padding-right: 30px; padding-bottom: 50px; padding-left: 80px; } Padding - Shorthand Property  To shorten the code, it is possible to specify all the padding properties in one property.  The padding property is a shorthand property for the following individual padding properties: ◦ padding-top ◦ padding-right ◦ padding-bottom ◦ padding-left  So, here is how it works:  If the padding property has four values:  padding: 25px 50px 75px 100px; ◦ top padding is 25px ◦ right padding is 50px ◦ bottom padding is 75px ◦ left padding is 100px  Example: Use the padding shorthand property with four values: p{ padding: 25px 50px 75px 100px; }  If the padding property has three values:  padding: 25px 50px 75px; ◦ top padding is 25px ◦ right and left paddings are 50px ◦ bottom padding is 75px  Example: Use the padding shorthand property with three values: p{ padding: 25px 50px 75px; } Padding - Shorthand Property(Conts)  If the padding property has two values:  padding: 25px 50px; ◦ top and bottom paddings are 25px ◦ right and left paddings are 50px  Example: Use the padding shorthand property with two values: p{ padding: 25px 50px; }  If the padding property has one value:  padding: 25px; ◦ all four paddings are 25px  Example: Use the padding shorthand property with one value: p{ padding: 25px; } CSS Setting height and width  The height and width properties are used to set the height and width of an element.  The height and width properties do not include padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.  CSS height and width Values  The height and width properties may have the following values: ◦ auto - This is default. The browser calculates the height and width ◦ length - Defines the height/width in px, cm etc. ◦ % - Defines the height/width in percent of the containing block ◦ initial - Sets the height/width to its default value CSS Setting height and width(Conts)  Example: Set the height and width of a element: p{ height: 200px; width: 50%; background-color: powderblue; }  Example: Set the height and width of another element: p{ height: 100px; width: 500px; background-color: powderblue; } Setting max-width  The max-width property is used to set the maximum width of an element.  The max-width can be specified in length values, like px, cm, etc., or in percent (%) of the containing block, or set to none (this is default. Means that there is no maximum width).  The problem with the above occurs when the browser window is smaller than the width of the element (500px). The browser then adds a horizontal scrollbar to the page.  Using max-width instead, in this situation, will improve the browser's handling of small windows.  This element has a height of 100 pixels and a max-width of 500 pixels. Example This element has a height of 100 pixels and a max-width of 500 pixels: p{ max-width: 500px; height: 100px; background-color: powderblue; } The CSS Box Model  All HTML elements can be considered as boxes. In CSS, the term "box model" is used when talking about design and layout.  The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content. The image below illustrates the box model: The CSS Box Model(Conts)  Explanation of the different parts: ◦ 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 box model allows us to add a border around elements, and to define space between elements.  Example: Demonstration of the box model: p{ width: 300px; border: 15px solid green; padding: 50px; margin: 20px; } CSS Outline  An outline is a line that is drawn around elements, OUTSIDE the borders, to make the element "stand out".  CSS has the following outline properties: ◦ outline-style ◦ outline-color ◦ outline-width ◦ outline-offset ◦ outline  Note: Outline differs from borders! Unlike border, the outline is drawn outside the element's border, and may overlap other content. Also, the outline is NOT a part of the element's dimensions; the element's total width and height is not affected by the width of the outline. CSS Outline Style  The outline-style property specifies the style of the outline, and can have one of the following values: ◦ dotted - Defines a dotted outline ◦ dashed - Defines a dashed outline ◦ solid - Defines a solid outline ◦ double - Defines a double outline ◦ groove - Defines a 3D grooved outline ◦ ridge - Defines a 3D ridged outline ◦ inset - Defines a 3D inset outline ◦ outset - Defines a 3D outset outline ◦ none - Defines no outline ◦ hidden - Defines a hidden outline CSS Outline Style(Conts)  Example: Demonstration of the different outline styles:  p.dotted {outline-style: dotted;} p.dashed {outline-style: dashed;} p.solid {outline-style: solid;} p.double {outline-style: double;} p.groove {outline-style: groove;} p.ridge {outline-style: ridge;} p.inset {outline-style: inset;} p.outset {outline-style: outset;} CSS Outline Width  The outline-width property specifies the width of the outline, and can have one of the following values: ◦ thin (typically 1px) ◦ medium (typically 3px) ◦ thick (typically 5px) ◦ A specific size (in px, pt, cm, em, etc)  The following example shows some outlines with different widths: p.ex1 { border: 1px solid black; outline-style: solid; outline-color: red; outline-width: thin; } p.ex2 { border: 1px solid black; outline-style: solid; outline-color: red; outline-width: medium; } p.ex3 { border: 1px solid black; outline-style: solid; outline-color: red; outline-width: thick; } p.ex4 { border: 1px solid black; outline-style: solid; outline-color: red; outline-width: 4px;} CSS Outline Color  The outline-color property is used to set the color of the outline.  The color can be set by: ◦ name - specify a color name, like "red" ◦ HEX - specify a hex value, like "#ff0000" ◦ RGB - specify a RGB value, like "rgb(255,0,0)" ◦ HSL - specify a HSL value, like "hsl(0, 100%, 50%)"  Example p.ex1 { border: 2px solid black; outline-style: solid; outline-color: red; } p.ex2 { border: 2px solid black; outline-style: dotted; outline-color: blue; } p.ex3 { border: 2px solid black; outline-style: outset; outline-color: grey; } CSS Outline - Shorthand property  The outline property is a shorthand property for setting the following individual outline properties: ◦ outline-width ◦ outline-style (required) ◦ outline-color  Example p.ex1 {outline: dashed;} p.ex2 {outline: dotted red;} p.ex3 {outline: 5px solid yellow;} p.ex4 {outline: thick ridge pink;} CSS Outline Offset  The outline-offset property adds space between an outline and the edge/border of an element. The space between an element and its outline is transparent.  The following example specifies an outline 15px outside the border edge:  Example p{ margin: 30px; border: 1px solid black; outline: 1px solid red; outline-offset: 15px; } CSS Text  Text Color ◦ The color property is used to set the color of the text. The color is specified by:  a color name - like "red"  a HEX value - like "#ff0000"  an RGB value - like "rgb(255,0,0)"  Text Color and Background Color ◦ In this example, we define both the background- color property and the color property: ◦ Example body { background-color: lightgrey; color: blue; } h1 { background-color: black; color: white; } CSS Text(Cont’s)  Text Alignment ◦ The text-align property is used to set the horizontal alignment of a text. ◦ A text can be left or right aligned, centered, or justified. ◦ The following example shows center aligned, and left and right aligned text (left alignment is default if text direction is left-to-right, and right alignment is default if text direction is right-to-left): ◦ Example h1 { text-align: center; } h2 { text-align: left; } h3 { text-align: right; CSS Text(Cont’s)  Text Direction ◦ The direction and unicode-bidi properties can be used to change the text direction of an element: ◦ Example p{ direction: rtl; } CSS Text(Cont’s)  Text Decoration ◦ The text-decoration property is used to set or remove decorations from text. ◦ The value text-decoration: none; is often used to remove underlines from links: ◦ Example a{ text-decoration: none; } ◦ The other text-decoration values are used to decorate text: ◦ Example h2 { text-decoration: line-through; } h3 { text-decoration: underline; } CSS Text(Cont’s)  Text Transformation ◦ The text-transform property is used to specify uppercase and lowercase letters in a text. ◦ It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter of each word: ◦ Example p.uppercase { text-transform: uppercase; } p.lowercase { text-transform: lowercase; } p.capitalize { text-transform: capitalize; } CSS Text(Cont’s)  Text Spacing ◦ Text Indentation  The text-indent property is used to specify the indentation of the first line of a text:  Example p{ text-indent: 50px; } ◦ Letter Spacing  The letter-spacing property is used to specify the space between the characters in a text.  The following example demonstrates how to increase or decrease the space between characters:  Example h1 { letter-spacing: 3px; } h2 { letter-spacing: -3px; } CSS Text(Cont’s)  Line Height ◦ The line-height property is used to specify the space between lines: ◦ Example p.small { line-height: 0.8; } p.big { line-height: 1.8; }  Word Spacing ◦ The word-spacing property is used to specify the space between the words in a text. ◦ The following example demonstrates how to increase or decrease the space between words: ◦ Example h1 { word-spacing: 10px; } h2 { word-spacing: -5px; CSS Text(Cont’s)  White Space ◦ The white-space property specifies how white- space inside an element is handled. ◦ This example demonstrates how to disable text wrapping inside an element: ◦ Example p{ white-space: nowrap; } CSS Text(Cont’s)  Text Shadow ◦ The text-shadow property adds shadow to text. ◦ In its simplest use, you only specify the horizontal shadow (2px) and the vertical shadow (2px): ◦ Example h1 { text-shadow: 2px 2px; } ◦ Next, add a color (red) to the shadow: ◦ Example h1 { text-shadow: 2px 2px red; } ◦ Then, add a blur effect (5px) to the shadow: ◦ Example h1 { text-shadow: 2px 2px 5px red; } CSS Font  Font Style ◦ The font-style property is mostly used to specify italic text. ◦ This property has three values: ◦ normal - The text is shown normally ◦ italic - The text is shown in italics ◦ oblique - The text is "leaning" (oblique is very similar to italic, but less supported) ◦ Example p.normal { font-style: normal; } p.italic { font-style: italic; } p.oblique { font-style: oblique; } CSS Font(Cont’s)  Font Weight ◦ The font-weight property specifies the weight of a font: ◦ Example p.normal { font-weight: normal; } p.thick { font-weight: bold; }  Font Variant ◦ The font-variant property specifies whether or not a text should be displayed in a small-caps font. ◦ In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted uppercase letters appears in a smaller font size than the original uppercase letters in the text. ◦ Example p.normal { font-variant: normal; } p.small { font-variant: small-caps; } CSS Font(Cont’s)  Font Size ◦ The font-size property sets the size of the text. ◦ If you do not specify a font size, the default size for normal text, like paragraphs, is 16px (16px=1em).  Set Font Size With Pixels ◦ Setting the text size with pixels gives you full control over the text size: ◦ Example h1 { font-size: 40px; } h2 { font-size: 30px; } p{ font-size: 14px; } CSS Font(Cont’s)  Set Font Size With Em(emphemeral unit) ◦ To allow users to resize the text (in the browser menu), many developers use em instead of pixels. ◦ 1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of 1em is 16px. ◦ The size can be calculated from pixels to em using this formula: pixels/16=em ◦ Example h1 { font-size: 2.5em; } h2 { font-size: 1.875em; } p{ font-size: 0.875em; } CSS Lists  HTML Lists and CSS List Properties  In HTML, there are two main types of lists:  unordered lists () - the list items are marked with bullets  ordered lists () - the list items are marked with numbers or letters  The CSS list properties allow you to: ◦ Set different list item markers for ordered lists ◦ Set different list item markers for unordered lists ◦ Set an image as the list item marker ◦ Add background colors to lists and list items CSS Lists(Conts)  Different List Item Markers ◦ The list-style-type property specifies the type of list item marker. ◦ The following example shows some of the available list item markers: ◦ Example ul.a { list-style-type: circle; } ul.b { list-style-type: square; } ol.c { list-style-type: upper-roman; } ol.d { list-style-type: lower-alpha; } CSS Lists(Conts)  An Image as The List Item Marker ◦ The list-style-image property specifies an image as the list item marker: ◦ Example ul { list-style-image: url('sqpurple.gif'); }  Position The List Item Markers ◦ The list-style-position property specifies the position of the list-item markers (bullet points). ◦ "list-style-position: outside;" means that the bullet points will be outside the list item. The start of each line of a list item will be aligned vertically. This is default: ◦ "list-style-position: inside;" means that the bullet points will be inside CSS Lists(Conts)  Remove Default Settings ◦ The list-style-type:none property can also be used to remove the markers/bullets. ◦ Note that the list also has default margin and padding. To remove this, add margin:0 and padding:0 to or : ◦ Example ul { list-style-type: none; margin: 0; padding: 0; } CSS Tables  Table Borders ◦ To specify table borders in CSS, use the border property. ◦ The example below specifies a black border for , , and elements: ◦ Example table, th, td { border: 1px solid black; } CSS Tables(Cont’s)  Table Width and Height ◦ The width and height of a table are defined by the width and height properties. ◦ The example below sets the width of the table to 100%, and the height of the elements to 70px: ◦ Example table { width: 100%; } th { height: 70px; } CSS Tables(Cont’s)  Horizontal Alignment ◦ The text-align property sets the horizontal alignment (like left, right, or center) of the content in or. ◦ By default, the content of elements are center- aligned and the content of elements are left- aligned. ◦ To center-align the content of elements as well, use text-align: center: ◦ Example td { text-align: center; CSS Tables(Cont’s) Table Color ◦ The example below specifies the background color and text color of elements: ◦ Example th { background-color: #4CAF50; color: white; } CSS Pseudo-classes  What are Pseudo-classes? ◦ A pseudo-class is used to define a special state of an element. ◦ For example, it can be used to: ◦ Style an element when a user mouses over it ◦ Style visited and unvisited links differently ◦ Style an element when it gets focus ◦ Syntax ◦ The syntax of pseudo-classes: selector:pseudo-class { property: value; } CSS Links  Links can be styled with any CSS property (e.g. color, font- family, background, etc.).  Example a{ color: hotpink; }  Links can be styled differently depending on what state they are in.  The four links states are: ◦ a:link - a normal, unvisited link ◦ a:visited - a link the user has visited ◦ a:hover - a link when the user mouses over it CSS Links(Cont’s)  Example a:link { color: red; } a:visited { color: green; } a:hover { color: hotpink; } a:active { color: blue; }  When setting the style for several link states, there are some order rules:  a:hover MUST come after a:link and a:visited  a:active MUST come after a:hover CSS Links(Cont’s)  Text Decoration ◦ The text-decoration property is mostly used to remove underlines from links: ◦ Example a:link { text-decoration: none; } a:visited { text-decoration: none; } a:hover { text-decoration: underline; } a:active { text-decoration: underline; } CSS Links(Cont’s)  Background Color ◦ The background-color property can be used to specify a background color for links: ◦ Example a:link { background-color: yellow; } a:visited { background-color: cyan; } a:hover { background-color: lightgreen; } a:active { background-color: hotpink; } Pseudo-classes and CSS Classes  Pseudo-classes can be combined with CSS classes:  When you hover over the link in the example, it will change color:  Example a.highlight:hover { color: #ff0000; } CSS Pseudo-elements  What are Pseudo-Elements? ◦ A CSS pseudo-element is used to style specified parts of an element. ◦ For example, it can be used to:  Style the first letter, or line, of an element  Insert content before, or after, the content of an element  The syntax of pseudo-elements: selector::pseudo-element { property: value; } The ::first-line Pseudo-element  The ::first-line pseudo-element is used to add a special style to the first line of a text.  The following example formats the first line of the text in all elements:  Example p::first-line { color: #ff0000; font-variant: small-caps; } The ::first-letter Pseudo-element  The ::first-letter pseudo-element is used to add a special style to the first letter of a text.  The following example formats the first letter of the text in all elements:  Example p::first-letter { color: #ff0000; font-size: xx-large; } The ::before Pseudo-element  The ::before pseudo-element can be used to insert some content before the content of an element.  The following example inserts an image before the content of each element:  Example h1::before { content: url(smiley.gif); } The ::after Pseudo-element  The ::after pseudo-element can be used to insert some content after the content of an element.  The following example inserts an image after the content of each element:  Example h1::after { content: url(smiley.gif); } CSS Opacity / Transparency  The opacity property specifies the opacity/transparency of an element.  Transparent Image ◦ The opacity property can take a value from 0.0 - 1.0. The lower value, the more transparent: ◦ Example img { opacity: 0.5; }  Transparent Hover Effect ◦ The opacity property is often used together with the :hover selector to change the opacity on mouse-over: ◦ Example img { opacity: 0.5; } img:hover {

Use Quizgecko on...
Browser
Browser