HTML Tables And Links

HTML Tables:

Define an HTML Table:-

The <table> tag defines an HTML table.

Each table row is defined with a <tr> tag. Each table header is defined with a <th> tag. Each table data/cell is defined with a <td> tag.

By default, the text in <th> elements are bold and centered.

By default, the text in <td> elements are regular and left-aligned.

Example:-
<table style=”width:100%”>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
<td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>

HTML Table – Add a Border:-

To add a border to a table, use the CSS border property:

Example:-
table, th, td {
border: 1px solid black;
}

HTML Table – Collapsed Borders:-

To let the borders collapse into one border, add the CSS border-collapse property:

Example:-
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}

HTML Table-Add cell padding:-

Cell padding specifies the space between the cell content and its borders.

If you do not specify a padding, the table cells will be displayed without padding.

To set the padding, use the CSS padding property:

Example:-

th,td{

padding:15px;

}

HTML Table – Left-align Headings:-

By default, table headings are bold and centered.

To left-align the table headings, use the CSS text-align property:

Example:-

th{

text-align:left;

}

HTML Table – Add Border Spacing:-

Border spacing specifies the space between the cells.

To set the border spacing for a table, use the CSS border-spacing property:

Example:-

table{

     border spacing:5px;

}

HTML Table – Cell that Spans Many Columns:-

To make a cell span more than one column, use the colspan attribute:

Example:-
<table style=”width:100%”>
  <tr>
    <th>Name</th>
    <th colspan=”2″>Telephone</th>
  </tr>
  <tr>
    <td>Bill Gates</td>
    <td>55577854</td>
    <td>55577855</td>
  </tr>
</table>

HTML Table – Cell that Spans Many Rows:-

To make a cell span more than one row, use the rowspan attribute:

Example:-

<table style=”width:100%”>
  <tr>
    <th>Name:</th>
    <td>Bill Gates</td>
  </tr>
  <tr>
    <th rowspan=”2″>Telephone:</th>
    <td>55577854</td>
  </tr>
  <tr>
    <td>55577855</td>
  </tr>
</table>

 

HTML Table – Add a Caption:-

To add a caption to a table, use the <caption> tag:

Example:-
<table style=”width:100%”>
  <caption>Monthly savings</caption>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$50</td>
  </tr>
</table>

A Special Style for One Table:-

To define a special style for one particular table, add an id attribute to the table:

Example:-
<table id=”t01″>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>

HTML Links:

HTML Links – Hyperlinks:-

HTML links are hyperlinks.

You can click on a link and jump to another document.

When you move the mouse over a link, the mouse arrow will turn into a little hand.

HTML Links – Syntax:-

The HTML <a> tag defines a hyperlink. It has the following syntax:

<a href=”url>link text</a>

The most important attribute of the <a> element is the href attribute, which indicates the link’s destination.

The link text is the part that will be visible to the reader.

Clicking on the link text, will send the reader to the specified URL address.

Example:-

<a href=”https://www.w3schools.com/”>Visit W3Schools.com!

HTML Links – The target Attribute:-

By default, the linked page will be displayed in the current browser window. To change this, you must specify another target for the link.

The target attribute specifies where to open the linked document.

The target attribute can have one of the following values:

  • _self – Default. Opens the document in the same window/tab as it was clicked
  • _blank – Opens the document in a new window or tab
  • _parent – Opens the document in the parent frame
  • _top – Opens the document in the full body of the window
Example:-
<a href=”https://www.w3schools.com/” target=”_blank”>Visit W3Schools!</a>

HTML Links – Use an Image as a Link:-

To use an image as a link, just put the <img> tag inside the <a> tag:

Example:-
<a href=”default.asp”>
<img src=”smiley.gif” alt=”HTML tutorial” style=”width:42px;height:42px;”>
</a>

Link to an Email Address

Use mailto: inside the href attribute to create a link that opens the user’s email program (to let them send a new email):

Example:-

<a href=”mailto:someone@example.com”>Send email</a>

 

Button as a Link:-

To use an HTML button as a link, you have to add some JavaScript code.

JavaScript allows you to specify what happens at certain events, such as a click of a button:

Example:-
<button onclick=”document.location=’default.asp'”>HTML Tutorial</button>

 

Link Titles:-

The title attribute specifies extra information about an element. The information is most often shown as a tooltip text when the mouse moves over the element.

Example:-
<a href=”https://www.w3schools.com/html/” title=”Go to W3Schools HTML section”>Visit our HTML Tutorial</a>

More on Absolute URLs and Relative URLs

Example:-

Use a full URL to link to a web page:

<a href=”https://www.w3schools.com/html/default.asp”>HTML tutorial</a>
Example:-

Link to a page located in the html folder on the current web site:

<a href=”/html/default.asp”>HTML tutorial</a>
Example:-

Link to a page located in the same folder as the current page:

<a href=”default.asp”>HTML tutorial</a>