FOC-M4-part 2
FOC-M4-part 2
CSS
◤
Introduction
▪ Consistency –
▪ Ensures a uniform look across multiple web pages.
▪ Improved Performance –
▪ External CSS files can be cached, reducing load times.
▪ Responsive Design –
▪ Helps create mobile-friendly websites that adapt to different screen
sizes.
◤
Basic CSS Syntax
▪ CSS (Cascading Style Sheets) follows a simple syntax to define styles for
HTML elements.
•Selector:
•Targets the HTML element to apply styles.
•Property:
•Specifies the style aspect to be changed (e.g., color, font-size).
•Value:
•Defines the setting for the property (e.g., blue, 16px).
•Declaration:
•A combination of property and value.
•Declaration Block:
•Multiple declarations enclosed in { } and separated by semicolons (;).
◤
Example
p{
color: blue;
font-size: 16px;
}
•p → Selector (Targets all <p> elements)
•color → Property (Defines text color)
•blue → Value (Sets text color to blue)
•font-size → Property (Defines text size)
•16px → Value (Sets text size to 16 pixels)
◤
Types of CSS
◤
1. Inline CSS
▪ Inline CSS is applied directly within an HTML tag using the style attribute.
▪ Inline CSS refers to applying CSS styles directly within an HTML element
using the style attribute.
▪ This method is useful for quick, one-off styles or testing but is not
recommended for large-scale projects due to its limitations in scalability and
maintainability.
Example
◤
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
◤
Advantages
•Useful when you need to override external or internal styles for specific elements.
•Does not require a separate CSS file, which can simplify small projects.
◤
Disadvantages
•Poor Maintainability:
•Inline CSS makes the HTML code messy and difficult to manage for larger
projects.
•Limited Reusability:
•Styles cannot be reused, leading to redundancy and inefficiency.
•Performance Issues:
•Increases HTML file size and reduces performance as styles are not cached like
external CSS.
•Low Readability:
•Hard to read and debug when scattered throughout the HTML.
◤
Internal CSS
▪ Internal CSS refers to defining styles within the <style> tag in the <head> section
of an HTML document.
▪ These styles apply to all elements in the same HTML file, offering better
organization compared to inline CSS while maintaining control within a single file.
<!DOCTYPE html>
◤
<html lang="en">
<head>
<style>
</style>
</head>
<body>
<h1>Welcome to My Page</h1>
</body>
</html>
◤
Advantages
1. File Consolidation:
▪ Styles are included in the same file as the HTML, making it easy to share
and distribute a single file.
2. Scope:
▪ Styles affect only the document in which they are defined, avoiding
unintentional application across multiple pages.
3. Ease of Debugging:
▪ No need to switch between files to modify styles.
◤
Disadvantages
•Limited Reusability:
•Styles cannot be reused across multiple files, leading to redundancy.
•Increased File Size:
•Mixing styles and content can make the HTML file larger and harder to maintain.
•Performance:
•External style sheets are cached, but internal CSS is processed each time the
page loads, potentially slowing down rendering.
◤
External CSS
▪ External CSS refers to defining CSS rules in a separate .css file and linking it
▪ To use an external CSS file, you link it to your HTML document using the
background-color: #FFFF00;
margin: 0;
padding: 0; }
h1 {
color: darkblue;
text-align: center; }
p{
color: gray;
font-size: 16px;
line-height: 1.5; }
Example
◤
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
</body>
</html>
◤
Advantages
•Reusability:
•One CSS file can style multiple HTML pages, reducing redundancy.
•Better Maintainability:
•Changes made to the CSS file automatically reflect across all linked HTML files.
•Clean Code:
•Caching:
•Browsers cache external CSS files, improving performance for subsequent visits.
◤
Disadvantages
▪ Dependency:
▪ The webpage relies on the external file, and styles won't load if the file is
missing or inaccessible.
▪ Latency:
▪ Additional HTTP requests to fetch the CSS file may increase initial page
load time.
▪ Complexity:
▪ Requires managing multiple files, which may be challenging for beginners.
◤
When to Use External CSS?
•Large Projects:
•Reusable Styles:
•Team Projects:
Performance Slower (large projects) Faster (cached resources) Fastest (cached resources)
◤
A CSS Style Primer
▪ You now have a basic knowledge of CSS style sheets and how they are based on
style rules that describe the appearance of information in web pages.
▪ CSS includes various style properties that are used to control fonts, colors,
alignment, and margins, to name just a few.
▪ The style properties in CSS can be generally grouped into two major categories: .
◤
Layout Properties
◤
Type Selector
▪ The type selector is a CSS selector used to target all elements of a specific
type, such as <p>, <h1>, <p> etc.
▪ It applies styles to every instance of the specified element on the webpage.
▪ The type selector targets elements by their tag name and applies styles to
all occurrences of the specified tag in the document.
▪ Syntax:
element
{
property: value;
}
◤
Type Selector
▪ Example usage:
{
color: blue;
}
◤
Class Selectors
▪ The class selector in CSS is used to target HTML elements that have a specific
class attribute.
▪ It allows for more specific styling than type selectors and is often used for
grouping styles that apply to multiple elements.
▪ Example usage:
<style>
.highlight
{
background-color: yellow;
}
</style>
<p class="highlight">This paragraph is highlighted.</p>
▪ The highlight class applies the style to the elements with the class="highlight" attribute.
CSS:
.button {
background-color: blue;
color: white;
border-radius: 5px;
HTML:
.highlight {
background-color: yellow;
font-weight: bold;
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Class Selector Example</title>
<style>
.header {
background-color: #0044cc;
color: white;
text-align: center;
}
.footer {
background-color: #333;
color: white;
text-align: center;
margin-top: 20px;
}
.main {
font-size: 16px;
line-height: 1.5;
}
</style>
<body>
<p class="main">This is the main content of the page, styled uniquely using the ID
selector.</p>
</body>
</html>
</head>
<body>
</body>
</html>
◤
ID Selectors
▪ The ID selector in CSS is used to target a specific element with a unique id
attribute.
▪ The ID selector is highly specific and applies styles to one element per
page.
▪ Syntax:
#idName {
property: value;
}
ID Selectors
CSS: ◤
<style>
#main-title {
font-size: 24px;
color: red;
</style>
HTML:
<body>
<p id="main">This is the main content of the page, styled uniquely using the ID
selector.</p>
</body>
</html>
◤
JavaScript
◤
JavaScript
▪ You use HTML tags to describe how you want your document
formatted, and the browser obeys your commands and shows the
formatted document to the user.
◤
Learning Web Scripting Basics
◤
Introducing JavaScript
▪ Display messages to the user as part of a web page, in the browser’s status line,
or in alert boxes
▪ Validate the contents of a form and make calculations (for example, an order form
can automatically display a running total as you enter item quantities)
▪ Animate images or create images that change when you move the mouse over
them
▪ Create ad banners that interact with the user, rather than simply displaying a
graphic
◤
Introducing JavaScript
▪ Detect the browser in use or its features and perform advanced functions
only on browsers that support them
▪ Modify all or part of a web page without requiring the user to reload it
◤
How JavaScript Fits into a Web Page
▪ Using the <script> tag, you can add a short to a web document.
▪ The <script> tag tells the browser to start treating the text as a script, and
the closing </script> tag tells the browser to return to HTML mode
<body> ◤
<script>
document.write(document.lastModified);
</script>
</p>
</body>
◤
How JavaScript Fits into a Web Page
▪ In this example, we placed the script within the body of the HTML document.
▪ There are actually four different places where you might use scripts:
◤
Exploring JavaScript’s Capabilities
Improving Navigation
• Examples:
• Examples:
◤
Exploring JavaScript’s Capabilities
JavaScript and Special Effects
• Modern effects:
• Drag-and-drop elements.
• Uses:
• Examples:
◤
Displaying Time with JavaScript
◤
Displaying Time with JavaScript
▪ Creating Output.
◤
Beginning a JavaScript Script
•Example:
<script type="text/javascript">
----------------------------
----------------------------
</script>
case, a date.
•Example:
•This statement creates a variable called now and stores the current date and
time in it.
•This statement use JavaScript’s built-in Date object, which enables you to
◤
Calculating the Results
•localtime = now.toString();
•utctime = now.toGMTString();
current time and date in a nice readable format, and utctime, containing the
•Example:
<body>
utctime = now.toGMTString();
</script>
</body>
◤
Testing the Script
◤
Modifying the Script for a Large Clock Display
▪ These statements load the hours, mins, and secs variables with the
components of the time using JavaScript’s built-in date functions.
▪ After the hours, minutes, and seconds are in separate variables, you can
create document.write statements to display them:
document.write(“<h1>”);
document.write(hours + “:” + mins + “:” + secs);
document.write(“</h1>”);
OR
document.write("<h1>" + hours + ":" + mins + ":" + secs + "</h1>");
<body>
◤
<h1>Current Date and Time</h1>
<script type=”text/javascript”>
localtime = now.toString();
utctime = now.toGMTString();
hours = now.getHours();
mins = now.getMinutes();
secs = now.getSeconds();
document.write("<h1>" + hours + ":" + mins + ":" + secs + "</h1>");
</script>
</body>