Web Technology U2 Part2
Web Technology U2 Part2
A block-level element always starts on a new line, and the browsers automatically
add some space (a margin) before and after the element.
A block-level element always takes up the full width available (stretches out to the
left and right as far as it can).
<p>Hello World</p>
<div>Hello World</div>
Try it Yourself »
<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>
CSS Tables
Tables in CSS are used to style HTML table elements, allowing data to be presented
in a structured, organized format with rows and columns. CSS provides a variety of
properties that can be applied to tables to enhance their appearance and
functionality.
CSS Table Properties
1. Border
The border property defines the appearance of borders around table elements (e.g.,
table, tr, td, th). It specifies the border’s width, style, and color.
Syntax:
border: table_width table_color;
Example 1: This example describes the CSS Table to apply the border property.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table,
th,
td {
<body>
<h1>GeeksforGeeks</h1>
<h2>Add border to table:</h2>
<table>
<tr>
<th>Roll No.</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
2. Border Collapse
The border-collapse property controls whether the borders of adjacent cells are
merged into a single border or kept separate.
Syntax:
border-collapse: collapse/separate;
Example 2: This example describes the CSS Table by applying the border-collapse
property.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table.one {
table.two {
table,
td,
th {
border: 1.5px solid blue;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>borders collapsed:</h2>
<table class="one">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>borders separated:</h2>
<table class="two">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
3. Border Spacing
Border Spacing property specifies the distance between the borders of adjacent
cells when border-collapse is set to separate.
Syntax:
border-spacing: value;
Example 3: This example describes the CSS Table by applying the border-spacing
property.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table.one {
border-collapse: separate;
table.two {
border-collapse: separate;
table,
td,
th {
border: 1.5px solid blue;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>border spacing:</h2>
<table class="one">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>border spacing:</h2>
<table class="two">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
4. Caption Side
Caption Side property specifies the placement of the table caption relative to the
table.
Syntax:
caption-side: top/bottom;
Example 4: This example describes the CSS Table by applying the caption-side
property to control the placement of the Table caption.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table.one {
border-collapse: separate;
border-spacing: 10px;
table.two {
border-collapse: separate;
border-spacing: 10px;
table,
td,
th {
border: 1.5px solid blue;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>Caption on top:</h2>
<table class="one">
<caption>Caption at the top of the table.</caption>
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>Caption at bottom:</h2>
<table class="two">
<caption> Caption at the bottom of the table </caption>
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
5. Empty cells
Empty cells property specifies whether or not to display borders and background
on empty cells in a table.
Syntax:
empty-cells:show/hide;
Example 5: This example describes the CSS Table by applying the empty-cell
property that specifies whether to display the borders or not in the empty cells in a
table.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table.one {
border-collapse: separate;
border-spacing: 10px;
table.two {
border-collapse: separate;
border-spacing: 10px;
table,
td,
th {
border: 1.5px solid blue;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>empty cells hide:</h2>
<table class="one">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>empty cells show:</h2>
<table class="two">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
6. Table layout
The Table layout property is used to set up the layout algorithm used for the table.
Syntax:
table-layout:auto/fixed;
Example 6: This example describes the CSS Table by applying the table layout
property.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: left;
}
h1 {
color: green;
}
table.one {
width: 80px border-collapse: separate;
border-spacing: 10px;
table,
td,
th {
border: 1.5px solid blue;
width: 80px;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>auto table layout:</h2>
<table class="one">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C_D_E_F_G_H_I_J_K_L_M_N_O_P</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
<br>
<br>
<h2>fixed table layout:</h2>
<table class="two">
<tr>
<th>Roll Number</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A_B_C_D_E_F_G_H_I_J_K_L_M_N_O_P</td>
</tr>
<tr>
<td>2</td>
<td>X_Y_Z</td>
</tr>
</table>
</body>
</html>
CSS provides a range of properties to enhance the styling and functionality of HTML
tables. By understanding and applying properties like border, border-collapse,
border-spacing, caption-side, empty-cells, and table-layout, web developers can
create well-structured and visually appealing tables.
CSS Lists
The List in CSS specifies the listing of the contents or items in a particular manner
i.e., it can either be organized orderly or unorder way, which helps to make a clean
webpage. It can be used to arrange the huge with a variety of content as they are
flexible and easy to manage. The default style for the list is borderless.
Currently Active Property:
list-style-type: decimal;
Types of Lists in CSS
The list can be categorized into 2 types:
Unordered List: In unordered lists, the list items are marked with bullets i.e.
small black circles by default.
Ordered List: In ordered lists, the list items are marked with numbers and an
alphabet.
Properties of CSS List
We have the following CSS lists properties, which can be used to control the CSS
lists:
list-style-type: This property is used to specify the appearance (such as disc,
character, or custom counter style) of the list item marker.
list-style-image: This property is used to set the images that will be used as
the list item marker.
list-style-position: It specifies the position of the marker box with respect to
the principal block box.
list-style: This property is used to set the list style.
Now, we will learn more about these properties with examples.
List Item Marker
This property specifies the type of item marker i.e. unordered list or ordered.
The list-style-type property specifies the appearance of the list item marker (such
as a disc, character, or custom counter style) of a list item element. Its default value
is a disc.
Syntax:
list-style-type: value;
The following value can be used:
circle Displays a hollow circle as the marker, commonly used for unordered lists.
decimal Numbers the list items sequentially, starting from 1 (e.g., 1, 2, 3…).
decimal-leading-zero Similar to decimal, but adds leading zeroes (e.g., 01, 02, 03…).
lower-roman Uses lowercase Roman numerals for list markers (e.g., i, ii, iii…).
lower-alpha Assigns lowercase alphabet letters (e.g., a, b, c…) for ordered list markers.
upper-alpha Marks ordered list items with uppercase letters (e.g., A, B, C…).
<head>
<style>
ul.a {
list-style-type: square;
}
ol.c {
list-style-type: lower-alpha;
}
</style>
</head>
<body>
<h2>
GeeksforGeeks
</h2>
<p> Unordered lists </p>
<ul class="a">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<ul class="b">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<p> Ordered Lists </p>
<ol class="c">
<li>one</li>
<li>two</li>
<li>three</li>
</ol>
<ol class="d">
<li>one</li>
<li>two</li>
<li>three</li>
</ol>
</body>
</html>
Example 2: Using Custom Images for List Markers
This example illustrates how to use the list-style-image property to replace default
list markers with a custom image. In this case, the list items are styled with an
image URL, providing a unique visual representation for each bullet in an unordered
list.
HTML
<!DOCTYPE html>
<html>
<head>
<title> CSS list-style-image Property </title>
<style>
ul {
list-style-image:
url("https://contribute.geeksforgeeks.org/wp-content/uploads/listitem-
1.png");
}
</style>
</head>
<body>
<h1>
GeeksforGeeks
</h1>
<p> Unordered lists </p>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
ul.a {
list-style: square;
background: pink;
padding: 20px;
}
</style>
</head>
<body>
<h2>
GeeksforGeeks
</h2>
<p> Unordered lists </p>
<ul class="a">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</body>
</html>
CSS Colors
CSS Color Names
In CSS, a color can be specified by using a predefined color name.
Example
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
Example
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
Example
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
CSS grouping selector is used to select multiple elements and style them
together. This reduces the code and extra effort to declare common styles for
each element. To group selectors, each selector is separated by a space.
Example
Open Compiler
<!DOCTYPE html>
<html lang="en">
<head>
<title>Grouping Selector in CSS</title>
<style>
div, article {
background-color: #04af2f;
color: white;
text-align: center;
font-size: 20px;
}
#id1, #id2 {
padding: 5px;
border: 2px solid black;
}
</style>
</head>
<body>
<h2>
Grouping Selector in CSS
</h2>
<p><strong>
In this example, both div and article
element have same background-color, text
color and aligned to center using
grouping selector.
</strong></p>
<div>
This is a div element
</div>
<br>
<article>
This is an article element.
</article>
<p><strong>
In this example, both h4 and span
element have same padding, and
border properties using grouping
selector.
</strong></p>
<h4 id="id1">
This h4 element has padding and border
properties.
</h4>
<span id="id2">
This span element also has padding and
border properties.
</span>
</body>
</html>
The position property specifies the type of positioning method used for an
element (static, relative, fixed, absolute or sticky).
static
relative
fixed
absolute
sticky
Elements are then positioned using the top, bottom, left, and right properties.
However, these properties will not work unless the position property is set
first. They also work differently depending on the position value.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right
properties.
Example
div.static {
position: static;
border: 3px solid #73AD21;
}
position: relative;
An element with position: relative; is positioned relative to its normal
position.
Example
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
CSS Align
Last Updated : 23 Jul, 2024
<head>
<style>
.center {
margin: auto;
width: 60%;
border: 3px solid #73AD21;
padding: 10px;
}
</style>
</head>
<body>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h2>Center Align Elements</h2>
<div class="center">
This is div element on which
margin auto is used to horizontally
align it into center
</div>
</body>
</html>
<head>
<style>
.right {
position: absolute;
right: 0px;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
</style>
</head>
<body>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h2>Right Align</h2>
<div class="right">
<p>
Absolute positioned elements
can overlap other elements.
</p>
</div>
</body>
</html>
3. Text Align:
The text-align: center property centers text within its container
Example 3: This example describes the CSS align using the text-
align: center; property.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
border: 3px solid green;
}
</style>
</head>
<body>
<h1 style="color:green;
text-align: center;">
GeeksforGeeks
</h1>
<h2>BOTH TEXTS ARE AT CENTER</h2>
<div class="center">
<p>This text is centered.</p>
</div>
</body>
</html>
<body>
<h1 style="color:green;
text-align:center;">
GeeksforGeeks
</h1>
<h2>Center Vertically</h2>
<div class="center">
<p>This is vertically centered.</p>
</div>
</body>
</html>
<head>
<style>
.center {
padding: 70px 0;
border: 3px solid green;
text-align: center;
}
</style>
</head>
<body>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<p>
Here we use padding and
text-align to center the
div element vertically
and horizontally:
</p>
<div class="center">
<p>
This text is vertically
and horizontally centered.
</p>
</div>
</body>
</html>
Output:
CSS Pseudo-classes
selector:pseudo-class {
property: value;
}
Anchor Pseudo-classes
Links can be displayed in different ways:
Example
/* unvisited link */
a:link {
color: #FF0000;
}
/* visited link */
a:visited {
color: #00FF00;
}
/* selected link */
a:active {
color: #0000FF;
}
Navigation Bars
Having easy-to-use navigation is important for any web site.
With CSS you can transform boring HTML menus into good-looking navigation
bars.
In our examples we will build the navigation bar from a standard HTML list.
A navigation bar is basically a list of links, so using the <ul> and <li> elements
makes perfect sense:
Example
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
Image Sprites
An image sprite is a collection of images put into a single image.
A web page with many images can take a long time to load and generates
multiple server requests.
Using image sprites will reduce the number of server requests and save
bandwidth.
With CSS, we can show just the part of the image we need.
In the following example the CSS specifies which part of the "img_navsprites.gif"
image to show:
Example
#home {
width: 46px;
height: 44px;
background: url(img_navsprites.gif) 0 0;
}
Example explained:
This is the easiest way to use image sprites, now we want to expand it by using
links and hover effects.
Example
#navlist {
position: relative;
}
#navlist li {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
top: 0;
}
#home {
left: 0px;
width: 46px;
background: url('img_navsprites.gif') 0 0;
}
#prev {
left: 63px;
width: 43px;
background: url('img_navsprites.gif') -47px 0;
}
#next {
left: 129px;
width: 43px;
background: url('img_navsprites.gif') -91px 0;
}