Tables and CSS, Links and Images
Tables and CSS, Links and Images
Tables and CSS, Links and Images
A table that does not include the border attribute will be a matrix of cells with neither a
border nor rules.
The browser has default widths for table borders and rules, which are used if the border
attribute is assigned the value “border.” Otherwise, a number can be given as border’s value,
which specifies the border width in pixels. For example, border = “3” specifies a border 3
pixels wide.
A border value of “0” specifies no border and no rules. The rules are set at 1 pixel when any
nonzero border value is specified. The border attribute is the most common attribute for the
<table> tag.
In most cases, a displayed table is preceded by a title, given as the content of a <caption> tag,
which can immediately follow the opening <table> tag.
The cells of a table are specified one row at a time. Each row of a table is specified with a
row tag, <tr>.
Within each row, the row label is specified by the table heading tag, <th> . Each data cell of
a row is specified with a table data tag, <td>
The first row of a table usually has the table’s column labels. For example, if a table has three
data columns and their column labels are, Apple, Orange, and Screwdriver respectively, the first
row can be specified by the following:
<tr>
<th> Apple </th>
<th> Orange </th>
<th> Screwdriver </th>
</tr>
Eg:
<html>
<body>
<br />
<table border="1">
<caption> Diet </caption>
<tr>
<th>Breakfast</th>
<th>Lunch</th>
<th>Dinner</th>
</tr>
<tr>
<td>Apple</td>
<td>Rice</td>
<td>Cucumber</td>
</tr>
<tr>
<td>Watermelon</td>
<td>Rice</td>
<td>Papaya</td>
</tr>
</table>
</body>
</html>
<tr>
<th colspan="3">Diet to be followed</th>
</tr>
The colspan attribute specification in a table header or table data tag tells the browser to make
the cell as wide as the specified number of rows.
Eg:
<html>
<body>
<table border="1">
<caption> Diet </caption>
<tr>
<th rowspan="2" ></th>
<th colspan="3">Fruit Juice Drinks</th>
</tr>
<tr>
<th>Apple</th>
<th>Orange</th>
<th>Strawberry</th>
</tr>
<tr>
<th>Breakfast</th>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
Prof. Pradnya Malaganve Introduction to Web Programming
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
2. Alignment
Alignment property is used to align the values entered in the table, and the alignment can be right,
left and center justified.
Ex: <!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
border: 1px solid black;
}
th{
text-align: left;
}
td {
text-align: center;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
Output:
3. Padding
To control the space between the border and the content in a table, use
the padding property on <td> and <th> elements:
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
border: thick solid;
table {
border-collapse: collapse;
width: 100%;
}
td {
padding: 15px;
text-align: right;
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
</table>
</body>
</html>
OUTPUT:
The <thead> element is used in conjunction with the <tbody> and <tfoot> elements to specify each
part of a table (header, body, footer).
Note: The <thead> element must have one or more <tr> tags inside.
The <thead> tag must be used in the following context: As a child of a <table> element, after
any <caption> and <colgroup> elements, and before any <tbody>, <tfoot>, and <tr> elements.
Tip: The <thead>, <tbody>, and <tfoot> elements will not affect the layout of the table by default.
However, you can use CSS to style these elements.
<!DOCTYPE html>
<html>
<head>
<style>
thead {color:green;}
tbody {color:blue;}
tfoot {color:red;}
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
</body>
</html>
Output:
The thead, tbody, and tfoot elements - Styled with CSS
Month Savings
January $100
February $80
Sum $180
Cell Spanning
Cell spanning in tables is basically merging the rows or columns of the table. Used attributes are:
rowspan and colspan.
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<h2>Cell spanning of 3 columns and 2 rows</h2>
<table style="width:100%">
<tr>
<th colspan="3">Details</th>
<th> Age</th>
<th>Code</th>
</tr>
<tr>
<td rowspan=2>Kevin N.</td>
Prof. Pradnya Malaganve Introduction to Web Programming
<td>Ross</td>
<td>US</td>
<td>23</td>
<td>456</td>
</tr>
<tr>
<td>Eve</td>
<td>UK</td>
<td>25</td>
<td>123</td>
</tr>
</table>
</body>
</html>
Output:
Most web accessibility efforts go toward helping users with visual disabilities, but web accessibility
also attempts to address the needs of users with hearing, cognitive, and motor skills disabilities.
Many countries have laws that regulate accessibility for websites. For example, https://www .access-
board.gov/guidelines-and-standards/communications-and-it describes web accessibility guidelines that
U.S. government agencies are required to follow.
To promote the social good (through equal opportunities for disabled people) and to promote their own
businesses, many companies have policies that require web developers and software purchasers to
follow web accessibility standards. Such policies can promote the company’s business not only by
attracting people who fall into the traditional disabled categories, but also by providing added value to
other people. For example, accessible websites tend to be better for users with slow Internet
connections and for users who need glasses.
Typically, visually impaired users have screen readers to read web pages. A screen reader is software
that figures out what the user’s screen is displaying and sends a text description of it to a speech
synthesizer. The speech synthesizer then reads the text aloud.
Specifically, used a display: inline property-value pair to display an address element (which is
normally a block element) in the flow of its surrounding sentence. The display property can be used
for much more than just inlining block element content. It can also be used to emulate the various parts
of a table.
Ex:
Prof. Pradnya Malaganve Introduction to Web Programming
<style>
.table {
display: table;
border-spacing: 20px;
}
.caption
{
display: table-caption;
font-size: xx-large;
text-align: center;
}
.row
{
display: table-row;
}
.row > *
{
display: table-cell;
text-align: center;
}
</style>
<body>
<div class=”caption”> WellCome Home </div>
<div class=”row”>
<span img src=”image1.jpg” alt=””> </span>
<span img src=”image1.jpg” alt=””> </span>
<span img src=”image1.jpg” alt=””> </span>
</div>
</body></html>
“a” Element
The <a> tag defines a hyperlink, which is used to link from one page to another.
The most important attribute of the <a> element is the href attribute, which indicates
the link's destination.
The text that appears between an a element’s start tag and end tag forms the link label that the user
sees and clicks on. So in this code, the link label is “VTU.” By default, browsers display link labels
with underlines. So this code renders like this”
VTU
The blue color indicates that the linked-to page has not been visited.
When the user clicks on a link, the browser loads the resource specified by the href attribute. For this
example, the “resource” is another web page, so when the user clicks on the “VTU” link, the browser
loads that web page—the one at https://vtu.ac.in.
As an alternative to specifying a web page for the href attribute’s resource, you can specify an image
file for the href attribute’s resource.
Besides enabling a user to load a resource, the ‘a element’ can be used as a mechanism that enables a
user to download a file of any type—image file, video file, PDF file, Microsoft Word file, and so on.
Relative URLs
A relative URL value allows to jump to a web page that is existed on the same web server as the
current web page.
It does so by specifying a path from the current directory to the destination web page. The current
directory is the directory where the current web page is existed.
The destination web page is the page that the user jumps to after clicking on the link.
Note the example directory tree in FIGURE 6.2. It shows the container relationships between all the
files and directories that are within the oleung directory. The oleung directory is the home page
directory for student Olivia Leung.
A home page is the default first page a user sees when the user visits a website.
It is a the directory where a home page resides, and a home page is the starting point for a user’s
browsing experience on a particular website.
links can be styled differently depending on what state they are in.
When setting the style for several link states, there are some order rules:
<!DOCTYPE html>
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* selected link */
a:active {
color: blue;
}
</style>
</head>
<body>
Output:
JPEG: JPEG stands for Joint Photographic Experts Group.JPEG files use a filename extension of
.jpeg or .jpg. In creating a JPEG file from an original picture, the original picture’s colors are mapped
to a 24-bit palette of colors. With 24 bits, there are approximately 16 million permutations of 0’s and
1’s in each color value (224 = 16,777,216). That means approximately 16 million unique colors can be
represented, and that’s more colors than the human eye can discern.
PNG: PNG stands for Portable Network Graphics, and PNG is pronounced “ping.” PNG files use a
file name extension of .png.
The PNG format provides more flexibility in terms of clarity versus file size. In creating a PNG file
that doesn’t require much storage, all the way up to 64 bits per pixel (to create an image file that is
very clear). The PNG format provides more flexibility in terms of transparency. Images can be created
with different levels of transparency for different parts of an image. GIF images can have only two
levels of transparency—completely opaque or completely transparent. PNG images can have 256
levels of transparency.
img Element
<img src=”file/image1.png” alt=”smiley face” width=”50” height=”50”>
The src attribute specifies the image’s filename and the location of the image’s file. In this img
element, the filename is image1.png. The location is indicated by the path that comes before the
filename.
<!DOCTYPE html>
<html>
<head>
<style>
img {
OUTPUT:
Responsive Images
Responsive web design (RWD) is the practice of writing code that dynamically generates web pages
that conform to different screen sizes and viewing orientations (portrait or landscape).
Background Images
Background images can also respond to resizing and scaling.
Here we will show three different methods:
1. If the background-size property is set to "contain", the background image will scale, and try to fit
the content area. However, the image will keep its aspect ratio (the proportional relationship between
the image's width and height)
.responssive{ width:100%;
Height:auto;
}
Using The max-width Property
If the max-width property is set to 100%, the image will scale down if it has to, but never scale up to
be larger than its original size
Positioning Images
The img element is an inline element (more formally, a phrasing element), so it gets displayed within
the normal flow of its surrounding text. That works well for small images (icons), but not so well for
medium and large images.
To avoid wasted space around a medium or large image, it has to be displayed on a line by itself by
surrounding it with a block element.
The CSS float property specifies how an element should float
The float property is used for positioning and formatting content e.g. let an image
float left to the text in a container.
<!DOCTYPE html>
<html>
<head>
<style>
img {
float: right;
}
</style>
</head>
<body>
<h2>Float Right</h2>
<p><img src="pineapple.jpg" alt="Pineapple" style="width:170px;height:170px;margin-left:15px;">
</p>
</body>
</html>
OUTPUT:
iframe Element
This element displays an image in a frame that’s embedded within the browser window.
The <iframe> tag specifies an inline frame.An inline frame is used to embed another
document within the current HTML document.
<!DOCTYPE html>
<html>
<body>
<h1>The iframe element + CSS</h1>
<p>An iframe with default borders:</p>
<iframe src="/default.asp" width="100%" height="300">
</iframe>
</body>
</html>
OUTPUT: