Unit-3 Web Technology
Unit-3 Web Technology
Unit-3
Page Designing with CSS
Introduction to designing approach
a. Table-based design
Traditionally, layouts were built using HTML with tables to layout the page structure and
content. Web design became more interesting with the introduction of table markups in
HTML. Web designers saw the opportunity to structure their design with the original
table markup. Sites were still text heavy, but at least they could separate the content
into different columns, rows, and other navigation elements. Designers could finally play
around with some graphic design elements as it grew rapidly in popularity, such as
having visit counters, animated GIFs, and so on. Texts and images were literally dancing
across websites everywhere.
We can see this in this website from 3drealms in 1996, which shows all the fancy
elements designers used to add to their websites:
The basic idea behind table based design is to partition our web page into various sections and
columns to give it a professional looks and to make it more user friendly. The easiest way to
divide our web page into subsections is to start by rendering it as one big table that spans the
entire width and at least the entire height (or more) of the web browser viewport. We can do
this by inserting a table in between the <body>...</body> tags of your HTML document and
then using the following code in the <table> start tag:
<table width="100%" style="height: 100%;">
Then everything on your web page goes inside this table.
Examples:
Slow to render as the browser needs to download most - if not all - of the table to
render it properly
They require more HTML than non-table layouts which means slower loading and
rendering, as well as an increased bandwidth usage
They can be a nightmare to maintain as they can quickly get complex
They were never intended to be used for page layouts
They are not as flexible as using proper semantic markup
b. Table-less designs
Tables are for tabular data, not for design. It is wrong to use tables to create our layout. We
should use other elements for layout (divs, lists, sections, articles, headers, footers, asides, etc.).
And we can achieve great effects with minimal HTML/CSS (leaving our code semantically
meaningful, lightweight, and easy to maintain). All along people used tables to create column
layouts and templates for their websites because they shied away from learning CSS and <div>s.
HTML tables have many problems which can be overcome using divs. Take an example of
changing the size of a column while using <table> tag, expanding a column also expands the
column next to it. Of course we can use colspan and rowspan attributes to correct this to an
extent but that makes it cumbersome and eats up precious bytes. With tableless design in CSS
we can overcome all these problems and have complete control over our design.
CSS vs CSS3
CSS CSS3
W3C developed the first version of CSS CSS3 is the latest version of CSS released
in 1996. in the year 2005.
The style attribute includes a series of CSS property and value pairs. Each "property: value" pair
is separated by a semicolon (;), just as you would write into an embedded or external style sheets.
But it needs to be all in one line i.e. no line break after the semicolon, as shown here:
Using the inline styles are generally considered as a bad practice. As style rules are embedded
directly inside the HTML tag, it causes the presentation to become mixed with the content of the
document; which makes the code hard to maintain and negates the purpose of using CSS.
Internal CSS
An internal CSS is used to define a style for a single HTML page. An internal CSS is defined in the
<head> section of an HTML page, within a <style> element. Comparatively, this looks neater,
simple, elegant and organized because of the separate styling and tagging. Here, Redundancy is
removed but the idea of separation of concerns still lost.
<html>
<head><title> Example of internal css</title>
<style>
body{
background-color: powderblue;
}
h1{
color: blue;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
</body>
</html>
External CSS
External stylesheets are used to allow people to format and recreate their web pages on an
entirely different document. This effectively means that you can have two or more workplaces,
as more than one stylesheet can be embedded inside the document, thereby providing you with
a much cleaner workspace. The stylesheet would be easily accessible in this case which is a huge
advantage, but on the other hand, any changes done in the external sheet would affect all the
parent sheets it is linked to.
An external style sheet can be linked to an HTML document using the <link> tag. The <link> tag
goes inside the <head> section, as you can see in the following example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My HTML Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph of text.</p>
</body>
</html>
CSS selectors
element Selector, id Selector and class Selector
element selector
The element selector selects HTML elements based on the element name.
p{
text-align: center;
color: red;
}
id Selector
The id selector uses the id attribute of an HTML element to select a specific element.To select an
element with a specific id, write a hash (#) character, followed by the id of the element.
#para1 {
text-align: center;
color: red;
}
class Selector
The class selector selects HTML elements with a specific class attribute. To select elements with
a specific class, write a period (.) character, followed by the class name.
. center {
text-align: center;
color: red;
}
CSS Properties
Text Properties
Property Description Values
color Sets the color of a text RGB, hex, keyword
line-height Sets the distance between lines normal, number, length, %
Increase or decrease the space between
letter-spacing normal, length
characters
text-align Aligns the text in an element left, right, center, justify
text- none, underline, overline, line-
Adds decoration to text
decoration through
Font Properties
background-image
The background-image property specifies an image to use as the background of an element.
Ex:
body {background-image: url("flower.gif");}
background-repeat
By default, the background-image property repeats an image both horizontally and vertically.
Ex:
body {background-image: url("rock.png"); background-repeat: no-repeat;}
Values:
no-repeat
repeat
repeat-x
repeat-y
space
background-attachment
The background-attachment property sets whether a background image scrolls with the rest of
the page, or is fixed.
Ex:
body {background-image: url("tree.gif");
background-repeat: no-repeat;
background-attachment: fixed;
}
background-position
The background-position property sets the starting position of a background image..
Ex:
body {
background-image: url(‘glass.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
background-size
It specifies the size of a background-image.
Ex:
div {
background: url(mountain.jpg);
background-repeat: no-repeat;
background-size: 300px 100px;
}
Color in css
In CSS, a color can be specified by using a predefined color name as well as using RGB values, HEX
values, HSL values, RGBA values, and HSLA values:
Ex.
<h1 style="border:2px solid Violet;">Hello World</h1>
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
It is used to develop the design and structure of a web page. It can be used as a set of tools to
personalize the layout of different components. According to the CSS box model, the web
browser supplies each element as a square prism.
Content
The content area, bounded by the content edge, contains the "real" content of the element, such
as text, an image, or a video player. Its dimensions are the content width (or content-box width)
and the content height (or content-box height). It often has a background color or background
image.
Padding
The padding area is the space around the content area and within the border-box. The thickness
of the padding is determined by the padding-top, padding-right, padding-bottom, padding-left,
and shorthand padding properties.
Border
The border area surrounds the padding and the content, and can be applied to all the sides of
the box or to selected side(s) - top, right, bottom, and/or left.
Margin
The margin area consists of space between the border and the margin. The margin does not
possess its own background color and is completely transparent.
<!DOCTYPE html>
<html>
<head><title>Demo</title>
<style>
div {
background-color: lightgrey;
width: 300px;
border: 25px solid green;
padding: 35px;
margin: 15px;
}
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<div>This text is the content of the box. We have added a 35px padding, 15px margin and a 25px
green border. </div>
</body>
</html>
CSS Position
The position property specifies the type of positioning method used for an element.
Possible values are: static, relative, fixed, absolute and sticky.
a. Static:
Every element has a static position by default, so the element will stick to the normal page flow.
So if there is a left/right/top/bottom/z-index set then there will be no effect on that element.
p{
position: static;
border: 3px solid green;
}
b. Relative:
An element’s original position remains in the flow of the document, just like the static value. But
now left/right/top/bottom/z-index will work. The positional properties “nudge” the element
from the original position in that direction.
h2 {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
c. Absolute:
The element is removed from the flow of the document and other elements will behave as if
it’s not even there whilst all the other positional properties will work on it. An element with
position: absolute; is positioned relative to the nearest positioned ancestor.
div {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
d. Fixed:
The element is removed from the flow of the document like absolutely positioned elements.
In fact they behave almost the same, only fixed positioned elements are always relative to
the document, not any particular parent, and are unaffected by scrolling.
div {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
e. Sticky:
the element is treated like a relative value until the scroll location of the viewport reaches a
specified threshold, at which point the element takes a fixed position where it is told to stick.
div{
position: sticky;
top: 0;
padding: 5px;
background-color: #cae8ca;
border: 2px solid #4CAF50;
}
CSS Display
Every element on a web page is a rectangular box. The display property in CSS determines just
how that rectangular box behaves.
Use of some different display values:
p.ex1 {display: none;}
p.ex2 {display: inline;}
p.ex3 {display: block;}
p.ex4 {display: inline-block;}
Value Description
Inline Displays an element as an inline element (like <span>). Any height and
width properties will have no effect
Block Displays an element as a block element (like <p>). It starts on a new line,
and takes up the whole width
contents Makes the container disappear, making the child elements children of the
element the next level up in the DOM
CSS List
Lists are very helpful in conveying a set of either numbered or bullet
points. We have the following five CSS properties, which can be used to
control lists −
The list-style-type allows you to control the shape or appearance
of the marker.
The list-style-position specifies whether a long point that wraps to
a second line should align with the first line or start underneath the
start of the marker.
The list-style-image specifies an image for the marker rather than
a bullet point or number.
The list-style serves as shorthand for the preceding properties.
The marker-offset specifies the distance between a marker and the
text in the list.
The list-style-type Property for unordered list
none-n/a
disc (default)- A filled-in circle
circle- An empty circle
square- A filled-in square
The list-style-type Property for ordered list
Value Description Example
CSS Tables
A table in CSS is used to apply the various styling properties to the HTML Table elements to
arrange the data in rows and columns, or possibly in a more complex structure in a properly
organized manner.
The look of an HTML table can be greatly improved with CSS:
CSS Table Style
Horizontal Dividers
we can add the border-bottom property to <th> and <td> for horizontal dividers:
th, td {
border-bottom: 1px solid #ddd;
}
Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:
tr:hover {background-color: coral;}
Striped Tables
For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or
odd) table rows:
tr:nth-child(even) {background-color: #f2f2f2;}
Table Color
The example below specifies the background color and text color of <th> elements:
th {
background-color: #04AA6D;
color: white;
}
Table Borders
Collapse Table Borders
The border-collapse property sets whether the table borders should be collapsed into a single
border:
table {
border-collapse: collapse;
}
If you only want a border around the table, only specify the border property for <table>:
table {
border: 1px solid;
}
Responsive Table
A responsive table will display a horizontal scroll bar if the screen is too small to display the full
content.
Add a container element (like <div>) with overflow-x:auto around the <table> element to make
it responsive:
<div style="overflow-x:auto;">
<table>
... table content ...
</table>
</div>
CSS Media queries are a way to target browser by certain characteristics, features, and user
preferences, then apply styles or run other code based on those things. Perhaps the most
common media queries in the world are those that target particular viewport ranges and apply
custom styles, which birthed the whole idea of responsive design.
Syntax:
@media [media-type] ([media-feature]) {
/* Styles! */
}
Media-type:
all
Suitable for all devices.
print
Intended for paged material and documents viewed on a screen in print preview mode.
screen
Intended primarily for screens.
Media features:
a. scripting
The scripting CSS media feature can be used to test whether scripting (such as JavaScript) is
available.
HTML:
<p class="script-enabled">You have scripting enabled! :-)</p>
CSS:
@media (scripting: enabled) {
.script-enabled {
color: red;
}
}
The scripting feature is specified as a keyword value chosen from the list below.
none
Scripting is completely unavailable on the current document.
initial-only
Scripting is enabled during the initial page load, but not afterwards.
enabled
Scripting is supported and active on the current document.
b. width
The width CSS media feature can be used to test the width of the viewport (or the page box, for
paged media). The width feature is specified as a <length> value representing the viewport width.
It is a range feature, meaning that you can also use the prefixed min-width and max-width
variants to query minimum and maximum values, respectively.
HTML:
<div>Watch this element as you resize your viewport's width. </div>
/* Exact width */
@media (width: 120px) {
div {
color: red;
}
}
CSS:
/* Minimum width */
@media (min-width: 50px) {
div {
background: yellow;
}
}
/* Maximum width */
@media (max-width: 300px) {
div {
border: 2px solid blue;
}
}
c. hover
The hover CSS media feature can be used to test whether the user's primary input mechanism
can hover over elements.
HTML:
<a href="#">Try hovering over me! </a>
CSS:
a: hover {/* default hover effect */
color: black;
background: yellow;
}
d. Resolution
The resolution CSS media feature can be used to test the pixel density of the output device.
HTML
<p>This is a test of your device's pixel density.</p>
CSS
/* Exact resolution */
@media (resolution: 150dpi) {
p{
color: red;
}
}
/* Minimum resolution */
@media (min-resolution: 72dpi) {
p{
text-decoration: underline;
}
}
/* Maximum resolution */
@media (max-resolution: 300dpi) {
p{
background: yellow;
}
}
There are lots of other things we can target. That might be screen resolution, device orientation,
operating system preference, or even more among a whole thing we can query and use to style
content.
Logical operators
The logical operators not, and, and only can be used to compose a complex media query. You can
also combine multiple media queries into a single rule by separating them with commas.
and
Used for combining multiple media features together into a single media query, requiring each
chained feature to return true for the query to be true. It is also used for joining media features
with media types.
not
Used to negate a media query, returning true if the query would otherwise return false. If present
in a comma-separated list of queries, it will only negate the specific query to which it is applied.
If you use the not operator, you must also specify a media type.
only
Applies a style only if an entire query matches. It is useful for preventing older browsers from
applying selected styles. When not using only, older browsers would interpret the query screen
and (max-width: 500px) as screen, ignoring the remainder of the query, and applying its styles on
all screens. If you use the only operator, you must also specify a media type.
, (comma)
Commas are used to combine multiple media queries into a single rule. Each query in a comma-
separated list is treated separately from the others Thus, if any of the queries in a list is true, the
entire media statement returns true. In other words, lists behave like a logical or operator.
The following are a few benefits of Image Design to HTML conversion that show why it is
necessary.