Wednesday, 21 April 2021

HTML INTERMEDIATES PART II

HTML Block and Inline Elements

Every HTML element has a default display value, depending on what type of element it is.
There are two display values: block and inline.

Block-level Elements

A block-level element always starts on a new line.
A block-level element always takes up the full width available (stretches out to the left and right as far as it can).
A block level element has a top and a bottom margin, whereas an inline element does not.
Example:
<div>Hello World</div>
 
Here are the block-level elements in HTML:

<address>    <article>    <aside>    <blockquote>    <canvas>    <dd>    <div>    <dl>    <dt>    <fieldset>    <figcaption>    <figure>    <footer>    <form>    <h1>-<h6>    <header>    <hr>    <li>    <main>    <nav>    <noscript>    <ol>    <p>    <pre>    <section>    <table>    <tfoot>    <ul>    <video>

Inline Elements

An inline element does not start on a new line.
An inline element only takes up as much width as necessary.
Example:
<span>Hello World</span> 
 
Here are the inline elements in HTML:

<a>    <abbr>    <acronym>    <b>    <bdo>    <big>    <br>    <button>    <cite>    <code>    <dfn>    <em>    <i>    <img>    <input>    <kbd>    <label>    <map>    <object>    <output>    <q>    <samp>    <script>    <select>    <small>    <span>    <strong>    <sub>    <sup>    <textarea>    <time>    <tt>    <var>

Note: An inline element cannot contain a block-level element!

The <div> Element

The <div> element is often used as a container for other HTML elements.
The <div> element has no required attributes, but style, class and id are common.
When used together with CSS, the <div> element can be used to style blocks of content:

<div style="background-color:black;color:white;padding:20px;">
  <h2>London</h2>
  <p>London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
</div>

The <span> Element

The <span> element is an inline container used to mark up a part of a text, or a part of a document.
The <span> element has no required attributes, but style, class and id are common.
When used together with CSS, the <span> element can be used to style parts of the text:

<p>My mother has <span style="color:blue;font-weight:bold">blue</span> eyes and my father has <span style="color:darkolivegreen;font-weight:bold">dark green</span> eyes.</p>

Summary 

  • There are two display values: block and inline
  • A block-level element always starts on a new line and takes up the full width available
  • An inline element does not start on a new line and it only takes up as much width as necessary
  • The <div> element is a block-level and is often used as a container for other HTML elements
  • The <span> element is an inline container used to mark up a part of a text, or a part of a document 

HTML class Attribute

The HTML class attribute is used to specify a class for an HTML element.
Multiple HTML elements can share the same class.
The class attribute is often used to point to a class name in a style sheet. It can also be used by a JavaScript to access and manipulate elements with the specific class name.
To create a class; write a period (.) character, followed by a class name. 
Then, define the CSS properties within curly braces {}:

<style>
.city {
  background-color: tomato;
  color: white;
  padding: 10px;
}
</style>
<h2 class="city">London</h2>
<p>London is the capital of England.</p> 

Multiple Classes

HTML elements can belong to more than one class.
To define multiple classes, separate the class names with a space, e.g. <div class="city main">. The element will be styled according to all the classes specified.
Different HTML elements can point to the same class name.

Use of The class Attribute in JavaScript

The class name can also be used by JavaScript to perform certain tasks for specific elements.
JavaScript can access elements with a specific class name with the getElementsByClassName() method:
Example:
<script>
function myFunction() {
  var x = document.getElementsByClassName("city");
  for (var i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
}
</script>

Chapter Summary

  • The HTML class attribute specifies one or more class names for an element
  • Classes are used by CSS and JavaScript to select and access specific elements
  • The class attribute can be used on any HTML element
  • The class name is case sensitive
  • Different HTML elements can point to the same class name
  • JavaScript can access elements with a specific class name with the getElementsByClassName() method

HTML id Attribute 

The HTML id attribute is used to specify a unique id for an HTML element.
You cannot have more than one element with the same id in an HTML document.
The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document.
The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id.
The syntax for id is: write a hash character (#), followed by an id name. Then, define the CSS properties within curly braces {}.

<style>
#myHeader {
  background-color: lightblue;
  color: black;
  padding: 40px;
  text-align: center;
}
</style>
<h1 id="myHeader">My Header</h1>
Note: The id name is case sensitive!
Note: The id name must contain at least one character, and must not contain whitespaces (spaces, tabs, etc.).

HTML Bookmarks with ID and Links

HTML bookmarks are used to allow readers to jump to specific parts of a webpage.
Bookmarks can be useful if your page is very long.
To use a bookmark, you must first create it, and then add a link to it.
Then, when the link is clicked, the page will scroll to the location with the bookmark.

Example:
First, create a bookmark with the id attribute:
<h2 id="C4">Chapter 4</h2>

Then, add a link to the bookmark ("Jump to Chapter 4"), from within the same page:
Example:
<a href="#C4">Jump to Chapter 4</a>

Or, add a link to the bookmark ("Jump to Chapter 4"), from another page:
<a href="html_demo.html#C4">Jump to Chapter 4</a>

Using The id Attribute in JavaScript

The id attribute can also be used by JavaScript to perform some tasks for that specific element.
JavaScript can access an element with a specific id with the getElementById() method:

Example:
Use the id attribute to manipulate text with JavaScript:

<script>
function displayResult() {
  document.getElementById("myHeader").innerHTML = "Have a nice day!";
}
</script>

Chapter Summary

  • The id attribute is used to specify a unique id for an HTML element
  • The value of the id attribute must be unique within the HTML document
  • The id attribute is used by CSS and JavaScript to style/select a specific element
  • The value of the id attribute is case sensitive
  • The id attribute is also used to create HTML bookmarks
  • JavaScript can access an element with a specific id with the getElementById() method

 

0 comments:

Post a Comment