Unit 1, 2 Notes
Unit 1, 2 Notes
Q) HTML
4. Lists
• Ordered list (numbered): <ol>, with <li> for each item.
html
CopyEdit
<ol>
<li>First item</li>
<li>Second item</li>
</ol>
• Unordered list (bulleted): <ul>, with <li> for each item.
html
CopyEdit
<ul>
<li>First item</li>
<li>Second item</li>
</ul>
5. Tables
Used to create tables:
html
CopyEdit
<table>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
6. Forms
Used to collect user inputs:
html
CopyEdit
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<input type="submit" value="Submit">
</form>
7. Semantic Tags
These improve the meaning of content:
• <header>: Defines a header.
• <footer>: Defines a footer.
• <section>: Defines sections of content.
• <article>: Represents an article.
• <nav>: Navigation links.
• <aside>: Sidebar or related content.
• <main>: Main content of the page.
class MyHandler(xml.sax.ContentHandler):
def startElement(self, name, attrs):
print(f"Start Element: {name}")
def characters(self, content):
print(f"Content: {content.strip()}")
def endElement(self, name):
print(f"End Element: {name}")
parser = xml.sax.make_parser()
parser.setContentHandler(MyHandler())
xml_data =
"""<person><name>Ayush</name><age>21</age><email>a
[email protected]</email></person>"""
import io
parser.parse(io.StringIO(xml_data))
Output:
mathematica
CopyEdit
Start Element: person
Start Element: name
Content: Ayush
End Element: name
Start Element: age
Content: 21
End Element: age
Start Element: email
Content: [email protected]
End Element: email
End Element: person
Hindi (WhatsApp style):
SAX ek event-driven, read-only model hai jo XML document
ko line-by-line (ya element-by-element) padhke process
karta hai. Yeh DOM se zyada fast aur memory-efficient hai,
lekin XML ko modify nahi kar sakta. SAX large XML files ke
liye accha hai.
SAX kaise kaam karta hai:
• Event-driven: Jab SAX XML ko padhta hai, to events
trigger hoti hain jaise startElement, endElement,
characters, etc., jinko aap handle kar sakte hain apne
code se.
• No tree structure: DOM ke unlike, SAX XML ko
sequentially process karta hai, bina tree banaye.
SAX Example (Python):
python
CopyEdit
import xml.sax
class MyHandler(xml.sax.ContentHandler):
def startElement(self, name, attrs):
print(f"Start Element: {name}")
def characters(self, content):
print(f"Content: {content.strip()}")
def endElement(self, name):
print(f"End Element: {name}")
parser = xml.sax.make_parser()
parser.setContentHandler(MyHandler())
xml_data =
"""<person><name>Ayush</name><age>21</age><email>a
[email protected]</email></person>"""
import io
parser.parse(io.StringIO(xml_data))
Output:
mathematica
CopyEdit
Start Element: person
Start Element: name
Content: Ayush
End Element: name
Start Element: age
Content: 21
End Element: age
Start Element: email
Content: [email protected]
End Element: email
End Element: person
UNIT -2 (CSS)
Q) What is CSS? (Cascading Style Sheets)
CSS is a language used to style and format the layout of web
pages. It controls the design, such as colors, fonts, spacing,
and positioning of elements on a webpage. Without CSS,
websites would look plain and unstyled.
Types of CSS
There are three types of CSS:
1. Inline CSS
o Written directly in an HTML element using the style
attribute.
o Example:
html
CopyEdit
<h1 style="color: blue;">Hello World</h1>
2. Internal CSS
o Defined in the <style> tag inside the <head>
section of an HTML document.
o Example:
html
CopyEdit
<html>
<head>
<style>
h1 {
color: red;
}
</style>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
3. External CSS
o Written in a separate .css file, linked to the HTML
file.
o Example:
html
CopyEdit
<link rel="stylesheet" href="styles.css">
Properties in CSS
CSS properties define how the elements should be styled.
Here are some common properties:
1. Color
o Defines the color of text or background.
o Example:
css
CopyEdit
p{
color: green;
}
2. Font-family
o Specifies the font of text.
o Example:
css
CopyEdit
h1 {
font-family: Arial, sans-serif;
}
3. Margin
o Defines the space outside the element.
o Example:
css
CopyEdit
div {
margin: 20px;
}
4. Padding
o Defines the space inside the element, between the
content and border.
o Example:
css
CopyEdit
div {
padding: 10px;
}
5. Background-color
o Sets the background color of an element.
o Example:
css
CopyEdit
body {
background-color: lightblue;
}
6. Border
o Adds a border around an element.
o Example:
css
CopyEdit
p{
border: 2px solid black;
}
Q) CSS Styling
1. Background Styling
In CSS, you can style the background of an element using
several properties, including color, images, gradients, and
more. Let’s look at the most commonly used background
properties:
• background-color
Sets the background color of an element.
css
CopyEdit
div {
background-color: lightblue;
}
• background-image
Sets an image as the background of an element. You
can use URLs for external images or local file paths.
css
CopyEdit
div {
background-image: url('background.jpg');
}
• background-size
Controls the size of the background image.
css
CopyEdit
div {
background-image: url('background.jpg');
background-size: cover; /* Makes sure the image covers the
whole area */
}
• background-repeat
Determines whether the background image should
repeat.
css
CopyEdit
div {
background-image: url('background.jpg');
background-repeat: no-repeat; /* Prevents image repetition
*/
}
• background-position
Positions the background image.
css
CopyEdit
div {
background-image: url('background.jpg');
background-position: center; /* Positions image at the
center */
}
• background
A shorthand property to combine all background-
related properties.
css
CopyEdit
div {
background: lightblue url('background.jpg') no-repeat
center/cover;
}
2. Text Formatting
CSS allows you to format the text in many ways, including
controlling font style, size, alignment, color, and more.
• font-family
Specifies the font type for text. You can list multiple
fonts as fallbacks.
css
CopyEdit
p{
font-family: 'Arial', sans-serif;
}
• font-size
Controls the size of the text.
css
CopyEdit
p{
font-size: 18px;
}
• font-weight
Controls the thickness of the text (bold, normal, etc.).
css
CopyEdit
p{
font-weight: bold;
}
• font-style
Specifies the style of the text (italic, normal, etc.).
css
CopyEdit
p{
font-style: italic;
}
• text-align
Aligns the text inside an element (left, right, center).
css
CopyEdit
p{
text-align: center;
}
• line-height
Sets the distance between lines of text.
css
CopyEdit
p{
line-height: 1.5;
}
• text-transform
Controls the capitalization of text (uppercase,
lowercase, capitalize).
css
CopyEdit
p{
text-transform: uppercase;
}
• color
Sets the color of the text.
css
CopyEdit
p{
color: red;
}
• letter-spacing
Sets the spacing between letters.
css
CopyEdit
p{
letter-spacing: 2px;
}
• text-decoration
Adds decorations like underline, line-through, or none.
css
CopyEdit
a{
text-decoration: underline;
}
</body>
</html>
/* ID */
#header {
font-size: 24px;
color: black;
}
html
CopyEdit
<!-- Applying class and ID -->
<button class="button">Click Me</button>
<div id="header">Welcome to the Page</div>
Key Differences:
• Classes can be used multiple times on a page, while
IDs should be used only once.
• Classes are selected using a dot (.) while IDs use a hash
(#).
Diagram:
First Diagram :
Second Diagram :
div {
display: block;
width: 100%;
background-color: lightblue;
}
o Behavior: The div will take up the full width of the
container and push the next element below it.
2. inline
o What it does: Makes the element an inline
element. Inline elements only take up as much
width as their content and do not start on a new
line.
o Examples of inline elements: <span>, <a>,
<strong>, etc.
Example:
css
CopyEdit
span {
display: inline;
color: red;
}
o Behavior: The span element will stay in the same
line as the surrounding text.
3. inline-block
o What it does: Makes the element behave like an
inline element, but allows you to set width and
height (unlike inline elements).
Example:
css
CopyEdit
.box {
display: inline-block;
width: 150px;
height: 100px;
background-color: lightgreen;
}
o Behavior: The element behaves like an inline
element but can still be sized (width and height).
4. none
o What it does: Hides the element entirely. The
element will not take up any space in the layout.
Example:
css
CopyEdit
.hidden {
display: none;
}
o Behavior: The element will not appear on the page,
and it will not affect the layout.
5. flex
o What it does: Makes the element a flex container.
This is used for creating flexible layouts where the
children of this element are flex items, and you can
control their alignment and distribution.
Example:
css
CopyEdit
.container {
display: flex;
justify-content: space-between;
}
o Behavior: The child elements inside .container will
be arranged in a row (by default) with space
between them.
Examples in Action:
Relative Positioning Example:
html
CopyEdit
<div class="relative">This is a relative element</div>
CSS:
css
CopyEdit
.relative {
position: relative;
top: 50px;
left: 50px;
background-color: lightblue;
}
• The .relative element will move 50px down and 50px to
the right, but it will still occupy the space it originally
would have in the document flow.
Fixed Positioning Example:
html
CopyEdit
<div class="fixed">I stay fixed at the bottom-right
corner!</div>
CSS:
css
CopyEdit
.fixed {
position: fixed;
bottom: 10px;
right: 10px;
background-color: lightgreen;
}
• The .fixed element will stay in the same position at the
bottom-right corner of the viewport, even when the
page is scrolled.
2. Align
The text-align property in CSS aligns the text horizontally
within its container.
Values of Text Alignment:
1. left: Aligns text to the left.
2. right: Aligns text to the right.
3. center: Centers the text.
4. justify: Stretches the text so that each line has equal
width.
Example:
html
CopyEdit
<p class="center">This is centered text.</p>
css
CopyEdit
.center {
text-align: center;
}
Vertical Alignment:
You can use the vertical-align property for inline elements or
table cells.
css
CopyEdit
.vertical {
vertical-align: middle;
}
3. Pseudo-class
A pseudo-class is a keyword added to selectors that
specifies a special state of the element.
Common Pseudo-classes:
1. :hover: Applies styles when the user hovers over an
element.
2. :focus: Applies styles when an element is focused.
3. :first-child: Targets the first child of an element.
4. :last-child: Targets the last child of an element.
5. :nth-child(n): Targets the nth child of an element.
Example:
html
CopyEdit
<a href="#" class="hover">Hover over me</a>
css
CopyEdit
.hover:hover {
color: red;
text-decoration: underline;
}
4. Navigation Bar
A navigation bar (navbar) is a section of a website that
contains links for navigation.
Example of a Simple Navbar:
html
CopyEdit
<nav>
<ul class="navbar">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
css
CopyEdit
.navbar {
list-style-type: none;
background-color: #333;
padding: 10px;
overflow: hidden;
}
.navbar li {
float: left;
}
.navbar a {
display: block;
color: white;
text-decoration: none;
padding: 10px 20px;
}
.navbar a:hover {
background-color: #575757;
}
5. Image Sprites
Image sprites combine multiple images into one image file,
reducing the number of HTTP requests for faster loading.
How to Use Image Sprites:
1. Use background-position to display different parts of
the sprite image.
2. Set the width and height of the element to match the
desired sprite.
Example:
html
CopyEdit
<div class="icon icon-home"></div>
<div class="icon icon-contact"></div>
css
CopyEdit
.icon {
background-image: url('sprites.png');
background-repeat: no-repeat;
display: inline-block;
}
.icon-home {
background-position: 0 0;
width: 50px;
height: 50px;
}
.icon-contact {
background-position: -50px 0;
width: 50px;
height: 50px;
}
6. Attribute Selector
Attribute selectors allow you to style elements based on
their attributes and attribute values.
Syntax:
1. [attribute]: Selects elements with the specified
attribute.
2. [attribute="value"]: Selects elements with the specified
attribute and value.
3. [attribute^="value"]: Selects elements with attributes
that start with the specified value.
4. [attribute$="value"]: Selects elements with attributes
that end with the specified value.
5. [attribute*="value"]: Selects elements with attributes
that contain the specified value.
Example:
html
CopyEdit
<input type="text" class="field">
<input type="password" class="field">
css
CopyEdit
input[type="text"] {
border: 2px solid blue;
}
input[type="password"] {
border: 2px solid red;
}
2. Align
Text alignment ka kaam hai text ko horizontally ya vertically
align karna.
Example:
css
CopyEdit
.center {
text-align: center; /* Center alignment */
}
3. Pseudo-class
Pseudo-class ek special state ko represent karta hai, jaise
:hover, :focus.
Example:
css
CopyEdit
a:hover {
color: red;
}
4. Navigation Bar
Navigation bar ek website ka section hota hai jo links provide
karta hai.
Example:
css
CopyEdit
.navbar {
background-color: #333;
list-style: none;
overflow: hidden;
}
5. Image Sprites
Multiple images ko ek hi file mein combine karke sprites
banaya jaata hai.
Example:
css
CopyEdit
.icon-home {
background-position: 0 0;
}
6. Attribute Selector
Attributes ke basis par elements ko style karne ke liye use
hota hai.
Example:
css
CopyEdit
input[type="password"] {
border: 2px solid red;
}