Web Technology Unit-3
Web Technology Unit-3
Web Technology
Unit-3 - (Style Sheets)
CSS :
Cascading Style Sheets, fondly referred to as CSS, is a simply designed language intended to
simplify the process of making web pages presentable. CSS allows you to apply styles to web
pages. More importantly, CSS enables you to do this independently of the HTML that makes up
each web page. It describes how a webpage should look: it prescribes colours, fonts, spacing,
and much more. In short, you can make your website look however you want. CSS lets
developers and designers define how it behaves, including how elements are positioned in the
browser.
o CSS stands for Cascading Style Sheet.
o CSS is used to design HTML tags.
o CSS is a widely used language on the web.
o HTML, CSS and JavaScript are used for web designing. It helps the web designers to
apply style on HTML tags.
Why CSS ?
CSS saves time: You can write CSS once and reuse the same sheet in multiple HTML
pages.
Easy Maintenance: To make a global change simply change the style, and all elements in
all the webpages will be updated automatically.
Search Engines: CSS is considered a clean coding technique, which means search
engines won’t have to struggle to “read” its content.
Superior styles to HTML: CSS has a much wider array of attributes than HTML, so you
can give a far better look to your HTML page in comparison to HTML attributes.
Offline Browsing: CSS can store web applications locally with the help of an offline
cache. Using this we can view offline websites.
CSS Syntax
A CSS rule set contains a selector and a
declaration block.
2
Selector: Selector indicates the HTML element you want to style. It could be any tag like <h1>,
<title> etc.
Declaration Block: The declaration block can contain one or more declarations separated by a
semicolon. For the above example, there are two declarations:
1. color: yellow;
2. font-size: 11 px;
Each declaration contains a property name and value, separated by a colon.
Property: A Property is a type of attribute of HTML element. It could be color, border etc.
Value: Values are assigned to CSS properties. In the above example, value "yellow" is assigned
to color property.
Example : In this example all <p> elements will be center-aligned, with a red text color:
p{
color: red;
text-align: center;
}
Example Explained
p is a selector in CSS (it points to the HTML element you want to style: <p>).
color is a property, and red is the property value
text-align is a property, and center is the property value
Types of CSS :
1. Inline CSS
We can apply CSS in a single element by inline CSS technique.
The inline CSS is also a method to insert style sheets in HTML document. This method mitigates
some advantages of style sheets so it is advised to use this method sparingly.
If you want to use inline CSS, you should use the style attribute to the relevant tag.
Syntax:
<htmltag style="cssproperty1:value; cssproperty2:value;"> </htmltag>
Example:
3
2. Internal CSS
The internal style sheet is used to add a unique style for a single document. It is defined in
<head> section of the HTML page inside the <style> tag.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: red;
margin-left: 80px;
}
</style>
</head>
<body>
<h1>The internal style sheet is applied on this heading.</h1>
<p>This paragraph will not be affected.</p>
</body>
</html>
3. External CSS
The external style sheet is generally used when you want to make changes on multiple pages. It
is ideal for this condition because it facilitates you to change the look of the entire web site by
changing just one file.
4
It uses the <link> tag on every pages and the <link> tag should be put inside the head section.
Example:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
The external style sheet may be written in any text editor but must be saved with a .css
extension. This file should not contain HTML elements.
Let's take an example of a style sheet file named "mystyle.css".
File: mystyle.css
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
CSS Selectors:
CSS selectors are used to select the content you want to style. Selectors are the part of CSS rule
set. CSS selectors select HTML elements according to its id, class, type, attribute etc.
There are several different types of selectors in CSS.
1. CSS Element Selector
2. CSS Id Selector
3. CSS Class Selector
4. CSS Universal Selector
<style>
p{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p>This style will be applied on every paragraph.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
2) CSS Id Selector
The id selector selects the id attribute of an HTML element to select a specific element. An id is
always unique within the page so it is chosen to select a single, unique element.
It is written with the hash character (#), followed by the id of the element.
Let’s take an example with the id "para1".
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. #para1 {
6. text-align: center;
7. color: blue;
8. }
9. </style>
6
10. </head>
11. <body>
12. <p id="para1">Hello Javatpoint.com</p>
13. <p>This paragraph will not be affected.</p>
14. </body>
15. </html>
CSS Properties
align-content Specifies the alignment between the lines inside a flexible
container when the items do not use all available space
border-image-outset Specifies the amount by which the border image area extends
beyond the border box
border-inline-color Sets the color of the borders at start and end in the inline
direction
bottom Sets the elements position, from the bottom of its parent
element
flex A shorthand property for the flex-grow, flex-shrink, and the flex-
basis properties
flex-grow Specifies how much the item will grow relative to the rest
flex-shrink Specifies how the item will shrink relative to the rest
float Specifies whether an element should float to the left, right, or not at all
@font-face A rule that allows websites to download and use fonts other than the
"web-safe" fonts
gap A shorthand property for the row-gap and the column-gap properties
grid-area Either specifies a name for the grid item, or this property is a
shorthand property for the grid-row-start, grid-column-start, grid-row-
end, and grid-column-end properties
gap
justify-content Specifies the alignment between the items inside a flexible container
when the items do not use all available space
justify-items Is set on the grid container. Specifies the alignment of grid items in the
inline direction
justify-self Is set on the grid item. Specifies the alignment of the grid item in the
inline direction
mix-blend- Specifies how an element's content should blend with its direct parent
mode background
order Sets the order of the flexible item, relative to the rest
overflow-wrap Specifies whether or not the browser can break lines with long words,
if they overflow the container
overflow-x Specifies whether or not to clip the left/right edges of the content, if it
overflows the element's content area
overflow-y Specifies whether or not to clip the top/bottom edges of the content, if
it overflows the element's content area
17
position Specifies the type of positioning method used for an element (static,
relative, absolute or fixed)
table-layout Defines the algorithm used to lay out table cells, rows, and
columns
text-align-last Describes how the last line of a block or a line right before a
forced line break is aligned when text-align is "justify"
text-underline-position Specifies the position of the underline which is set using the
text-decoration property
word-break Specifies how words should break when reaching the end of a
line
CSS Box-Model
The CSS box model is a container that contains multiple properties including borders, margins,
padding, and the content itself. It is used to create the design and layout of web pages. It can
be used as a toolkit for customizing the layout of different elements. The web browser renders
every element as a rectangular box according to the CSS box model.
The CSS box model is essentially a box that wraps around every HTML element. It consists of:
margins, borders, padding, and the actual content. The image below illustrates the box model:
20
CSS 3 - CSS3 is a latest standard of css earlier versions(CSS2). The main difference between
css2 and css3 is follows −
Media Queries
Namespaces
Selectors Level 3
Color
CSS3 modules
CSS3 is collaboration of CSS2 specifications and new specifications, we can called this
collaboration is module. Some of the modules are shown below −
21
Selectors
Box Model
Backgrounds
Image Values and Replaced Content
Text Effects
2D Transformations
3D Transformations
Animations
Multiple Column Layout
User Interface
CSS3 features
CSS3 new features are used in designing better layouts and attractive web pages easily.
It makes the html elements look more appealing to the eyes. Now, let us study the
application of each feature in detail.
New Selectors: More ways to target HTML Elements
CSS3 has introduced new selectors to target HTML elements to create more efficient
stylesheets. Examples of new Selectores are:
Attribute Selectors: These selectors help you to target elements based on attributes like
class or id.
Adjacent Sibling Selectors: The adjacent Sibling Selectors allow you to target a
component that is the immediate successor of another element. To target the first
paragraph after a heading, we can use the “h1+p” selector.
Child Selectors: These target elements that are direct children of another element. For
example, to target the lists of an ordered list, we use the selector “ol>li.”
Nth-child selectors: These are used depending on their position within a parent
element. For example, “li: nth-child (even).”
Rounded Corners
With the earlier versions of CSS, it was pretty challenging to have rounded corners for
images and elements. The CSS3 feature of “border-radius” to create rounded corners
has simplified this issue. We can have more appealing HTML components with rounded
22
corners by specifying a value greater than 0 to the border-radius. Today, we can see
rounded corners to most of the web content, all thanks to CSS3.
Example:
{
border-radius: 12px;
}
2D and 3D transformations for shapes and text
CSS3 comes with a critical transform property that can be applied to rotate, translate,
skew, flip, zoom, shrink, and scale HTML elements.
For 2D transformations CSS3 provides rotate(), scale(), skew() and translate() functions.
While for 3D transformations, we have rotateX(), rotateY(), and rotateZ(). These
functions can be used to create more interactive and stunning web designs.
rotate(): To rotate an element clockwise and counterclockwise around the z axis, we use
the rotate() function in CSS3.
scale(): To resize any HTML element on a 2D plane, we use the scale() function inCSS3.
skew(): To give a shear transformation to a specific angle, we use the skew() function in
CSS3.
translate(): To change the position of any HTML element along the horizontal or vertical
direction, we use the translate() function in CSS3.
Shadow effect
The CSS3 has an important box-shadow property to add shadows to text and shapes,
thus adding a sense of depth to them. Along with the box shadow, we can add a
horizontal and vertical offset, blur amount, shadow color, shadow spread, and inset.
This makes the web pages visually more engaging.
Example:
.container{
box-shadow : 1.5px 1.5px 5px #312577;
}
Transitions and Animations: Smooth and Dynamic Visual Effects
CSS3 transitions and animations are an advanced styling feature that allows web
developers to add smooth and dynamic visual effects to their web pages. While the
23
transition property is used to create gradual changes in CSS property, with animations,
you can create more complex effects by specifying the keyframes, which in turn target
the values of CSS properties at specific time stamps. Both transitions and animations can
be used to create interactive web pages.
Example:
HTML
<div class=”container”></div>
CSS
.container {
width: 50px;
height: 50px;
background-color: yellow;
transition: width 1s ease-in-out;
animation: rotate 4s linear infinite;
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
Setting Opacity
Using rgba colors, the CSS3 opacity property can be applied to alter the opacity of HTML
elements. Instead of editing the picture with the required opacity and adding it to the
HTML file, we can simply write a single line of code in CSS.
Example:
24
div{
opacity: 0.7;
}
Flexbox: Powerful Layout Capabilities for Responsive Design
CSS3 comes with a very versatile and powerful flexbox property to organize the contents
within an HTML element in an efficient way to create a responsive layout. The features
of Flexbox include the following:
Flexible container: The HTML component containing the flex items can be adjusted to
fill the entire space or shrink as per the requirement.
Flexible items: The flex items can also be positioned as needed.
Alignment: Flexbox provides alignment options to align its items center, spaced, or
wrapped.
Responsive design: This property enables us to create flexible layouts for different
screen sizes.
Direction control: We can change the direction of the Flexbox from column to row and
vice versa using the direction control feature of the Flexbox.
Media queries: Conditional styling for different screen sizes
The contribution of media query properties toward the responsive design of a webpage
is worth mentioning. This CSS3 feature allows conditional styling to create designs and
layouts for different screen sizes like laptops, desktops, tablets, mobiles, etc. Thus a
consistent user experience across different screens is achieved through media queries.
Word Wrapping
CSS3 word wrapping feature allows developers to control and manage long words when
they exceed the container's width.
We can have two values with the word-wrap property: normal and break-word. While
the normal value allows lengthy words to extend beyond the container's width, the
word-break value breaks long words and wraps them to the following line.
Example:
.box {
width: 150px;
border: 1px solid red;
25
word-wrap: break-word;
}
Font Flexibility
CSS3 has introduced many new font-related features, including font flexibility. This
feature allows us to specify the range of font styles, thus giving us absolute control over
how the text appears on the web page. We can also add our own variations to the font
styles using the font-stretch property.