CSS Notes

3.3 Cascading Style Sheets (CSS)

In this section, we will learn about the management and presentation of the whole webpage along with the components as handled by a stylesheet language. There are various stylesheet languages available like Cascading Style Sheets (CSS), Document Style Semantics and Specification Language (DSSSL), Extensible Stylesheet Language (XSL), etc. The most common and frequently used with HTML is CSS, so, this way we'll continue to the brief introduction of CSS. This way, it is easier to manage and troubleshoot your web designing code for extension and changes in the future.

Types of CSS

There are three ways we which we can use CSS in our HTML webpage:

1. Inline CSS

Any CSS attributes that we want to incorporate can be added using a "style" tag after the class definition of HTML tag.

<h1 style="color:red; font-style:italic; text-align:center;">My Teaching Academy</h1>

2. Embedded (Internal) CSS

Instead of assigning styles for every heading and other component at the time of its first occurrence in the code, we can define all the styles in the header section with the tag-pair of 'style'.

<style> h1 { color:red; font-style:italic; text-align:center; } </style>

3. External CSS

Alternatively, a file with extension '.css' can be made and all relevant CSS code according to your schema can be present there. Once, the contents of HTML are finalized, just attach the CSS file in the head portion of HTML by passing the link. External CSS are useful with large projects, like in commercial purposes.

<link rel="stylesheet" href="my_own.css" />

NOTE: The priority of inline is highest, followed by embedded styles and lastly the attributes of external are considered if all three are present in a webpage.

3.3.1 Decorating Tables with CSS

By using CSS, you can provide borders to table as well. For this purpose, we initially need to state the style tag-pair just before the start of table and can make the block, we need to set within, style to opt for and where to apply it.

<style> table, th, td { border: 1px solid black; } </style>

We may add background color of table by adding the following code:

<style> table { background-color: aqua; } </style>

3.3.2 Homepage Decor

The color of the body is similar to that of 'Bg-11'. As a first step, we define 'style' tag-pair in the body and add lines to change the color of 3rd level headings in the body to green. We can also add an image tag to underline with an alternate wording which describes the picture that is being shown on the page, in a short phrase.

<body style="background-color:#E6E6FA;"> <h1 style="text-align:center;color:green">My Teaching Academy</h1> <h2 style="text-align:center;color:blue">Lets Learn</h2> ... </body>

Advanced Web Features

Scrolling Messages

A simple and impressive decor on the website can be a scrolling message. To create this effect:

  1. Define a container and assign the parameters of the text.
  2. Define the horizontal scrolling text using the marquee element.
  3. Adjust properties like direction, scrollamount, and loop to control the scrolling behavior.
<body style="background-color:#E6E6FA;"> <div id="scroll-container"> <marquee direction="left" scrollamount="12" loop=""> <div id="scroll-text">Welcome message which continuously scrolls in blue</div> </marquee> </div> </body>

Adding a Video Clip to Website

To embed a video clip in a website, use the <video> tag and specify width and height attributes:

<video width="320" height="240" controls> <source src="Pakistan&apos;sFirst Ever Win in International Football.mp4" type="video/mp4"> Congratulations! <p>Your browser doesn't support HTML5 video.</p> </video>

Using Autoplay for Videos

The autoplay parameter can be used instead of controls for automatic video playback:

<video width="320" height="240" autoplay muted> <source src="Pakistan&apos;sFirst Ever Win in International Football.mp4" type="video/mp4"> Your browser does not support the video tag. </video>

Note: Autoplay with sound is often blocked by browsers. Using the muted attribute ensures autoplay works in most cases.

Tips for Web Development

  • Use meaningful alt text for images to improve accessibility.
  • Consider browser compatibility when using advanced features.
  • Optimize video content for web performance.
  • Provide additional video formats for better compatibility:
    <source src="video.mp4" type="video/mp4" />

Example Layout

My Teaching Academy

Let's Learn

About Us | Contact Us