CSS Basics
CSS Test – What to Study / What to Know
CSS Overview
CSS stands for Cascading Style Sheets.
Key Characteristics:
Used for styling web pages by controlling layout and appearance.
Ways to Add CSS
Internal Style Sheet:
This method utilizes styles defined within the same HTML file.
Styles are placed between the
<style>and</style>tags.External Style Sheet:
CSS is written in a separate file and linked to the HTML file.
The
<link>tag is used within the<head>section of the HTML document.Example:
<head> <link rel="stylesheet" href="mystyle.css"> </head>
Files typically have a
.cssextension.Inline CSS:
Applied directly within HTML elements.
Usage: Not recommended for extensive styling since it mixes HTML with CSS, making updates labor-intensive.
Identified by the
styleattribute.Example:
<p style="color:red;">This is a paragraph.</p>
CSS Selectors
Element Selector:
Targets all elements of a specific type.
Example:
p { color: red; }Effect: All paragraph elements (
<p>) within<body>will have red text.
ID Selector:
Denoted by a
#prefix.Example:
#para1 { color: red; }Usage in HTML:
html <body> ¨K12K </body>Class Selector:
Denoted by a
.prefix.Example:
.redtext { color: red; }Usage in HTML:
html <body> ¨K13K </body>
CSS Colors
Different methods to specify colors:
Color Names (e.g., "red")
RGB Values:
Format:
rgb(red_value, green_value, blue_value)Examples:
Red:
rgb(255,0,0)Green:
rgb(0,255,0)Blue:
rgb(0,0,255)
Hexadecimal Codes:
Format:
#RRGGBBExamples:
Red:
#FF0000Green:
#00FF00Blue:
#0000FF
CSS Comment Syntax
CSS Comment: Uses
/* comment_here */notation within style tags.HTML Comment: Uses
<!-- comment_here -->notation within body tags.
CSS Box Model
Defines the structure and layout of elements in terms of content, padding, border, and margin.
Calculation of Total Width of a Div Element:
css div { width: 400px; padding: 15px; border: 25px solid navy; margin: 30px; }Calculation breakdown:
Content width:
400px(counted once)Padding:
2 * 15px(counted twice for left and right)Border:
2 * 25px(counted twice for left and right)Margin:
2 * 30px(counted twice for left and right)Total Width Calculation:
Calculating Width of a Div Based on Image Width:
When using an image:
html <img src="Black_lab_pup.jpg" width="400" height="400" alt="Black Lab puppy">
Calculation Example:
Known image width:
400 pixelsDiv style to fill in example:
css div { width: ________; padding: 10px; border: 20px solid navy; margin: 0px; }Calculate Width:
Plugging in the values:
Result: Width of div should be
340px.