Thursday, June 11, 2020

HTML Block Elements

A block-level element is one that contains a significant block of content that should be displayed on its own line, to break apart long passages of text into manageable portions such as paragraphs, headings, and lists.  Many nonempty, block-level elements can contain other block-level elements, and all can contain text and inline elements. 

As with most word processors, HTML includes several tags to delimit, and hence format paragraphs of text. These tags include the following: 

  • <p>—Formatted paragraphs
  • <h1>through <h6>—Headings
  • <pre>—Preformatted text
  • <blockquote>- Blockquote 
  • <BR>Line Break, <HR> Horizontal Rule
  • <div>—A division of the document
  • <ul>,<ol>, <dl>—Unnumbered, ordered, and definition lists 

Each of the block elements results in a line break and noticeable space padding after the closing tag. As such, the block elements only work when used to format paragraph-like chunks of text—they cannot be used as inline tags. 

Formatted Paragraph

The paragraph tag p is used, quite simply, to mark up paragraphs of text:

Example:

<p> This is a paragraph of text……</p>

<p>…and this is another paragraph.</p>

The element is one of the most commonly used building blocks of HTML. When you use the element to begin a new paragraph in HTML, it automatically creates a line space above and below the content.

This element may contain any text content, but it can’t include any block-level elements: only inline or phrase elements can be included. The P element has an attribute called align that will center the content inside the two paragraphs.

Example:

<html>
  <head>
    <title>Paragraph Example</title>
  </head>
  <body>
    <p>
      This is the first paragraph in the example about the P tag. There really
      isn't much to say here.
    </p>
    <p align="center">
      This is the second paragraph. Again, more of the same. This time the
      paragraph is aligned in the center. This might not be such a good idea as
      it makes the text hard to read.
    </p>
    <p align="right">
      Here the paragraph is aligned to the right. Right-aligned text is also
      troublesome to read. The rest of the text of this paragraph is of little
      importance.
    </p>
    <p align="justify">
      Under HTML 4.0-compliant browsers, you are able to justify text. As you
      may notice, the way browsers tend to justify text is sometimes imprecise.
      Furthermore, not all browsers support this attribute value.
    </p>
  </body>
</html>

The align attribute affects the contents of the p, aligning content to the "left", "right", or "center",
or setting it to "justify" on the page.

To control the justification via CSS you would use the text-align attribute, the example below would set the paragraph justification to "center".

    p { text-aligncenter; }

It is better to use CSS to achieve any formatting of the elements within an html document, and this will also prepare you to make the transition to xhtml, and using CSS styles in your html documents.


Headings

The heading elements are used to create “headlines” in documents. Six different levels of headings are supported: <H1>, <H2>, <H3>, <H4>, <H5>, and <H6>. These range in importance from <H1>, the most important, to <H6>, the least important. Each heading uses a large, usually bold character-formatting style to identify itself as a heading. HTML automatically adds an extra blank line before and after a heading.

<html>
  <body>
        <h1>Heading 1</h1>
        <h2 align="center">Heading 2</h2>
        <h3 align="right">Heading 3</h3>
        <h4>Heading 4</h4>
        <h5>Heading 5</h5>
        <h6>Heading 6</h6>
        <p>Plain body text: The quick brown fox jumped over the lazy dog.</p>
  </body>
</html>

An attribute that aligns the text left, right, or center can be added to the heading elements. By default, headings are usually left-aligned, but by setting the ALIGN attribute of the various heading elements, the text may be aligned to the right, left, or center of the screen.

Note: The size of Html heading <h1> to <h6> in pixels and em unit. HTML Heading Elements

Preformatted Text

The <PRE> and </PRE> tags can be used to surround text that shouldn’t be formatted by the browser. The text enclosed within the <PRE> tags retains all spacing and returns, and doesn’t reflow when the browser is resized. Scroll bars and horizontal scrolling are required if the lines are longer than the width of the window. The browser generally renders the preformatted text in a monospaced font, usually Courier. Some text formatting, such as bold, italics, or links, can be used within the <PRE> tags.

The pre element in this example displays as shown in below. The second part of the figure shows the same content marked up as a paragraph (p) element for comparison. 

Example:

<html>
  <head>
    <title>Preformatted Text Example</title>
  </head>
  <body>
    <pre> 
      This is            an            example of 
            text with a        lot of
                              curious
                              white space. 
      </pre>  
      <p>
      This is             a            example of 
            text with a         lot of
                                curious
                                white space. 
      </p>
  </body>
</html>

Blockquote Tag

The HTML <blockquote> tag defines a long block quotation in the HTML document from another source. Browsers traditionally render the text found within the <blockquote> tag as indented text. This tag is also commonly referred to as the <blockquote> element.

Syntax:

<blockquote> -----Content------</blockquote>
<!DOCTYPE html>
<html>
   <head>
      <title>HTML blockquote Tag</title>
   </head>
   <body>
    <blockquote>Browsers generally render blockquote text as indented text. If your
     quoted text needs to display within a non-quoted paragraph, you should use the
     HTML q tag. Most browsers surround q text with quotation marks.</blockquote>
     <q>Browsers generally render blockquote text as indented text. If your quoted 
     text needs to display within a non-quoted paragraph, you should use the HTML q 
     tag. Most browsers surround q text with quotation marks.</q>
   </body>
</html>

Horizontal Rule 

One way you can separate sections of your Web page is to use the hr tag. By default, this tag produces a thin, gray horizontal line called a horizontal rule. The <HR> element is an empty element, because it has no close tag and encloses no data, the W3C recommends using <hr/> to officially start and end this tag. Several attributes can change the appearance of the horizontal rules you use on your pages such as SIZE sets the bar’s thickness (height). WIDTH sets the bar’s width. ALIGN sets its vertical alignment. NOSHADE renders the bar without a surrounding shadow. 

<HR SIZE=4 WIDTH="50%"> 

Line Breaks

The <br> tag is used to add a line break in your HTML page. It causes the browser to stop printing text on that line and drop down to the next line on the page. Because the <br> tag has no end tag (or closing tag), it breaks one of the rules for future HTML (the XML based XHTML), namely that all elements must be closed. Writing it like <br /> is a future proof way of closing (or ending) the tag inside the opening tag, accepted by both HTML and XML.


Div Tag

The HTML <div> tag is used to group the large section of HTML elements together.

We know that every tag has a specific purpose e.g. p tag is used to specify paragraph, <h1> to <h6> tag are used to specify headings but the <div> tag is just like a container unit which is used to encapsulate other page elements and divides the HTML documents into sections.

The div tag is generally used by web developers to group HTML elements together and apply CSS styles to many elements at once. For example: If you wrap a set of paragraph elements into a div element so you can take the advantage of CSS styles and apply font style to all paragraphs at once instead of coding the same style for each paragraph element.

  • Div tag is Block level tag
  • It is a generic container tag
  • It is used to the group of various tags of HTML so that sections can be created and style can be applied to them.

As we know Div tag is block-level tag in this example div tag contain entire width. It will be displayed div tag each time on a new line, not on the same line.

Example:

<html>
  <head>
    <title>html div tags</title>
  </head>
  <body>
    <div
      id="myDiv"
      style="font-family: Helvetica; font-size: 12pt; border: 1px solid black;"
    >
      <div
        id="subDiv1"
        style="color: #ff0000; border: 1px dotted black; margin: 10px;"
      >
        <h5>Section 1</h5>
        <p>This paragraph would be your content paragraph...</p>
        <p>Here's another content article right here.</p>
      </div>
      <br />
      <div
        id="subDiv2"
        style="color: #ff00ff; border: 1px dashed black; margin: 10px;"
      >
        <h5>Section 2</h5>
        <p>This paragraph would be your content paragraph...</p>
        <p>Here's another content article right here.</p>
      </div>
    </div>
  </body>
</html>



Monday, June 8, 2020

HTML Basics

While getting started with HTML, you will likely encounter new and often strange terms. Over time you will become more and more familiar with all of them, but the three common HTML terms you should begin with are elements, tags, and attributes.

Elements: Elements are designators that define the structure and content of objects within a page. Some of the more frequently used elements include multiple levels of headings (identified as <h1> through <h6> elements) and paragraphs (identified as the <p> element); the list goes on to include the <a>, <div>, <span>, <strong>, and <em> elements, and many more.

Elements are identified by the use of less-than and greater-than angle brackets, < >, surrounding the element name.

Thus, an element will look like the following:

<a>

Tags: The use of less-than and greater-than angle brackets surrounding an element creates what is known as a tag. Tags most commonly occur in pairs of opening and closing tags.

An opening tag marks the beginning of an element. It consists of a less-than sign followed by an element’s name, and then ends with a greater-than sign; for example, <div>.

A closing tag marks the end of an element. It consists of a less-than sign followed by a forward slash and the element’s name, and then ends with a greater-than sign; for example, </div>.

The content that falls between the opening and closing tags is the content of that element. An anchor link, for example, will have an opening tag of <a> and a closing tag of </a>. What falls between these two tags will be the content of the anchor link.

So, anchor tags will look a bit like this:

<a>....</a>

Attributes: Attributes are properties used to provide additional information about an element. The most common attributes include the id attribute, which identifies an element; the class attribute, which classifies an element; the src attribute, which specifies a source for embeddable content; and the href attribute, which provides a hyperlink reference to a linked resource.

Attributes are defined within the opening tag, after an element’s name. Generally, attributes include a name and a value. The format for these attributes consist of the attribute name followed by an equals sign and then a quoted attribute value. For example, an <img> element including an src and alt attributes would look like the following:


Now that you know what HTML elements, tags, and attributes are, let’s take a look at putting together our first web page.

The Basic Structure

A basic HTML page is a document that typically has the file extension .html, though HTML frequently appears in the content of other file types as well. All HTML documents follow the same basic structure so that the browser that renders the file knows what to do. Before you can start adding content to your document, there's a basic structure you need to set up in your file. This structure isn't only required for your document to be compliant but will also allow you to provide useful information about your document. The basic structure of any HTML document consists of the following sections or elements:

  • The DTD (!DOCTYPE declaration).
  • The main container (html element).
  • The head section (head element).
  • The body section (body element).

Doctype: The first line of code, <!DOCTYPE html>, is called a doctype declaration and tells the browser which version of HTML the page is written in. In this case, we’re using the doctype that corresponds to HTML5, the most up-to-date version of the HTML language. There are a number of different doctype declarations that correspond to various versions of HTML.

HTML Root Element: Next, the <html> element wraps around all of the other code and content in our document. This element, known as the HTML root element, always contains one <head> element and one <body> element.

Head Element: The HTML head element is a container that can include a number of HTML elements that are not visible parts of the page rendered by the browser. These elements are either metadata that describe information about the page or are helping pull in external resources like CSS stylesheets or JavaScript files.

The <title> element is the only element that is required to be contained within the <head> tags. The content within this element is displayed as the page title in the tab of the browser and is also what search engines use to identify the title of a page.

All of the HTML elements that can be used inside the <head> element are:

  • <base>
  • <link>
  • <meta>
  • <noscript>
  • <script>
  • <style>
  • <title> (required)

Body Element: There can only be one <body> element in an HTML document because this element is the container that holds the content of the document. All of the content that you see rendered in the browser is contained within this element. In the example above, the content of the page is a headline and simple paragraph.

Self-Closing Elements

In the previous example, the <meta> element had only one tag and didn’t include a closing tag. Fear not, this was intentional. Not all elements consist of opening and closing tags. Some elements simply receive their content or behavior from attributes within a single tag. The <meta> element is one of these elements. The content of the previous <meta> element is assigned with the use of the charset attribute and value. Other common selfclosing elements include

<br>     <embed>     <hr>        <img>       <input>  

<link>   <meta>      <param>     <source>    <wbr>

HTML Comment <!-- ... -->

HTML comments are enclosed between <!-- and -->. Comments are ignored by the browser. You need to look into the HTML codes (via "View Source") to read them. Comments are extremely important in programming to explain and document a section of programming codes and logic. HTML documents are textual and self-explanatory, comments are less important (but still nice to have) to describe the various part of the documents.

Comments are also useful in temporarily disable a certain part of the HTML codes during development.

Block Elements vs. Inline Elements

HTML Elements can be classified as:

  • Block Elements: A block element (such as <p><h1> to <h6> and <div>) starts on a new line, takes the full width, and ends with a new line. It is rectangular in shape with a line-break before and after the element.
  • Inline Elements (or Character Elements): An inline element (such as <em><strong><code> and <span>) takes up as much space as it needs. It does not force a line-break before and after the element, although it can span a few lines.

In brief, a block element is always rectangular in shape, while an inline element spans a continuous run of characters. Will go deep-in in the next topic.