Unit-2 - CSS
Unit-2 - CSS
HTML Forms
An HTML form is used to collect user input. The user input is most often
sent to a server for processing.
The HTML <form> element is used to create an HTML form
Form contains different types of input elements, such as: text fields,
checkboxes, radio buttons, submit buttons, etc.
Type Description
Example:
<html>
<head>
</head>
<body>
District : <select>
<option> Srikakulam</option>
<option> Vizag</option>
<option> Krishna</option>
</select> <br/>
</body>
</html>
Syntax:
navigator.geolocation.getCurrentPosition()
2) Drag and Drop API
Ex:
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
3) localStorage Object
The localStorage object stores the data with no expiration date. The data
will not be deleted when the browser is closed, and will be available the
next day, week, or year.
Example
localStorage.setItem("lastname", "Smith");
4. SSE API
Server-Sent Events (SSE) allow a web page to get updates from a server.
This was also possible before, but the web page would have to ask if any
updates were available. With server-sent events, the updates come
automatically.
OUTPUT:
An easier solution is to let YouTube play the videos in your web page.
To play your video on a web page, do the following:
Example :
<iframe width="420" height="315"
src="https://www.youtube.com/embed/tgbNymZ7vqY">
</iframe>
CSS Introduction :
CSS Syntax :
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 example,
color: yellow;
font-size: 10px;
Colours :
Color : this property sets text colour.
Background-color : it sets background colour.
CSS Background:
The border-color property is used to set the colour of the four borders.
Example :
border-style: dotted;
border-width: 5px;
border-color: green;
border-radius: 5px;
CSS margins :
The CSS margin properties are used to create space around elements,
outside of any defined borders.
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
Example :
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
CSS padding :
The CSS padding properties are used to generate space around an element's
content, inside of any defined borders.
CSS has properties for specifying the padding for each side of an element:
padding-top
padding-right
padding-bottom
padding-left
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
CSS Font:
Example :
font-weight: bold;
font-style: italic;
font-variant: small-caps;
font-variant: small-caps;
CSS – Text :
The color property is used to set the colour of a text.
The direction property is used to set the text direction.
The letter-spacing property is used to add or subtract space between
the letters that make up a word.
The word-spacing property is used to add or subtract space between
the words of a sentence.
The text-shadow property adds shadow to text.
Example:
color: blue;
text-decoration-line: line-through;
letter-spacing: 5px;
word-spacing: 10px;
text-shadow: 2px 2px;
CSS Lists :
There are various CSS properties that can be used to control lists.
Example:
.One{
list-style-image: url(‘ img.png ’);
}
</style>
Overflow :
The CSS overflow property controls what happens to content that is too big to fit into
an area. The overflow property specifies whether to clip the content or to add
scrollbars when the content of an element is too big to fit in the specified area.
visible - Default. The overflow is not clipped. The content renders outside the
element's box
hidden - The overflow is clipped, and the rest of the content will be invisible
scroll - The overflow is clipped, and a scrollbar is added to see the rest of the
content
auto - Similar to scroll, but it adds scrollbars only when necessary
div {
overflow: scroll;
}
CSS Opacity
The opacity property specifies the opacity/transparency of an element.
<html>
<head>
<style>
img {
opacity: 0.5;
}
</style>
</head>
<body>
<img src="forest.jpg" width="170" height="100">
</body>
</html>
CSS Selector :
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.
p{
text-align: center;
color: blue;
}
2) CSS Id Selector
It is written with the hash character (#), followed by the id of the element.
#One {
text-align: center;
color: blue;
}
The class selector selects HTML elements with a specific class attribute. It is used
with a period character( . - full stop symbol) followed by the class name.
.Two {
text-align: center;
color: blue;
}
The universal selector is used as a wildcard( * ) character. It selects all the elements
on the pages.
AD* {
color: green;
font-size: 20px;
}
5) CSS Group Selector
The grouping selector is used to select all the elements with the same style
definitions.
Grouping selector is used to minimize the code. Commas are used to separate each
selector in grouping.
1. h1,h2,p {
2. text-align: center;
3. color: blue;
4. }
CSS is added to HTML pages to format the document according to information in the
style sheet. There are three ways to insert CSS in HTML documents.
1. Inline CSS
2. Internal CSS
3. External CSS
1) Inline CSS
The inline CSS is also a method to insert style sheets in HTML document. If you
want to use inline CSS, you should use the style attribute to the relevant tag.
Syntax:
Example:
<h2 style="color:red;margin-
left:40px;">Inline CSS is applied on this heading.</h2>
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.
<html>
<head>
<style>
h1 {
color: red;
margin-left: 80px;
}
</style>
</head>
<body>
<h1>The internal style sheet is applied on this heading.</h1>
</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.
It uses the <link> tag on every pages and the <link> tag should be put inside the
head section.
Example:
1. <head>
2. <link rel="stylesheet" type="text/css" href="mystyle.css">
3. </head>
The external style sheet may be written in any text editor but must be saved with
.css extension.
File: mystyle.css
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
CSS Combinators
A combinator is something that explains the relationship between the selectors.
There are four different combinators in CSS:
1.descendant combinator (space)
2.child combinator (>)
3.adjacent sibling combinator (+)
4.general sibling combinator (~)
Descendant Combinators
The descendant selector matches all elements that are descendants of a specified
element.
The following example selects all <p> elements inside <div> elements:
div p {
background-color: yellow;
}
pseudo-class:
A pseudo-class is used to define a special state of an element.
Syntax:
selector:pseudo-class {
property: value;
}
Example :
<html>
<head>
<style>
a:hover {
color: pink;
font-size: 22px;
}
</style>
</head>
<body>
<a href="page1.html">page1</a>
</body>
</html>
pseudo-element:
Syntax:
selector::pseudo-element {
property: value;
}
Example:
p::first-letter
{
color: red;
}
::selection {
color: red;
background: yellow;
}