Day-12 CSS(Cascading  Style sheet)

Day-12 CSS(Cascading Style sheet)

Using CSS

CSS can be added to HTML documents in 3 ways:

  • Inline - by using the style attribute inside HTML elements

  • Internal - by using a <style> element in the <head> section

  • External - by using a <link> element to link to an external CSS file

The most common way to add CSS, is to keep the styles in external CSS files.

Inline CSS

An inline CSS is used to apply a unique style to a single HTML element.

An inline CSS uses the style attribute of an HTML element.

The following example sets the text color of the <h1> element to blue, and the text color of the <p>element to red:

<h1 style="color:orange;">An Orange Heading</h1> <p style="color:red;">A red paragraph.</p>

Internal CSS

An internal CSS is used to define a style for a single HTML page.

An internal CSS is defined in the <head> section of an HTML page, within a <style> element.

The following example sets the text color of ALL the <h1> elements (on that page) to blue, and the text color of ALL the <p> elements to red. In addition, the page will be displayed with a "powderblue" background color:

<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1   {color: blue;}
p    {color: red;}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

External CSS

An external style sheet is used to define the style for many HTML pages.

To use an external style sheet, add a link to it in the <head> section of each HTML page:

The external style sheet can be written in any text editor. The file must not contain any HTML code, and must be saved with a .css extension.

External style sheets can be referenced with a full URL or with a path relative to the current web page.

\=> To add css file we need to add link element in html file like this

<head> <link rel="stylesheet" href="styles.css"> </head>

style.css

body { background-color: powderblue; } h1 { color: blue; } p { color: red; }

CSS Colors, Fonts and Sizes

Here, let's see commonly used CSS properties.

The CSS color property defines the text color to be used.

The CSS font-family property defines the font to be used.

The CSS font-size property defines the text size to be used.

<html>
<head>
<style>
h1 {
  color: blue;
  font-family: verdana;
  font-size: 300%;
}
p {
  color: red;
  font-family: courier;
  font-size: 160%;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

CSS Border

The CSS border property defines a border around an HTML element.

p { border: 2px solid powderblue; }

CSS Padding

The CSS padding property defines a padding (space) between the text and the border.

p { border: 2px solid powderblue; padding: 30px; }

CSS Margin

The CSS margin property defines a margin (space) outside the border.

p { border: 2px solid blue; margin: 50px; }

CSS Fonts

Forms are the way of representing text characters on your website .

Using a font that is easy to read is important. The font adds value to your text. It is also important to choose the correct color and text size for the font.

In CSS there are five generic font families:

  1. Serif fonts have a small stroke at the edges of each letter. They create a sense of formality and elegance.

  2. Sans-serif fonts have clean lines (no small strokes attached). They create a modern and minimalistic look.

  3. Monospace fonts - here all the letters have the same fixed width. They create a mechanical look.

  4. Cursive fonts imitate human handwriting.

  5. Fantasy fonts are decorative/playful fonts.

All the different font names belong to one of the generic font families.

Some Font Examples

style.css

.p1 {
  font-family: "Times New Roman", Times, serif;
}

.p2 {
  font-family: Arial, Helvetica, sans-serif;
}

.p3 {
  font-family: "Lucida Console", "Courier New", monospace;
}

What are Web Safe Fonts?

Web safe fonts are fonts that are universally installed across all browsers and devices.


Fallback Fonts

However, there are no 100% completely web safe fonts. There is always a chance that a font is not found or is not installed properly.

Therefore, it is very important to always use fallback fonts.

This means that you should add a list of similar "backup fonts" in the font-family property. If the first font does not work, the browser will try the next one, and the next one, and so on. Always end the list with a generic font family name.

p {
font-family: Tahoma, Verdana, sans-serif;
}

Best Web Safe Fonts for HTML and CSS

The following list are the best web safe fonts for HTML and CSS:

  • Arial (sans-serif)

  • Verdana (sans-serif)

  • Tahoma (sans-serif)

  • Trebuchet MS (sans-serif)

  • Times New Roman (serif)

  • Georgia (serif)

  • Garamond (serif)

  • Courier New (monospace)

  • Brush Script MT (cursive)

  • Let's Practice in codepen