Introduction of HTML and CSS
HTML:-
HTML (Hypertext Markup Language) is the standard markup language used to create and structure content on the web. It forms the backbone of web pages by defining elements like headings, paragraphs, links, images, and more. Here's a basic example of an HTML document:
example :-
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
CSS:-
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML or XML. It controls the layout, colors, fonts, and overall visual style of a webpage. CSS works by selecting HTML elements and applying styles to them.
example :-
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
html tags : -
About -:
HTML tags are the building blocks of web pages. They define the structure and content of a webpage by enclosing elements. Each tag has a specific purpose and can include attributes to provide additional information. Here’s a list of common HTML tags categorized by their use:
tags :-
Basic Structure Tags
<``!DOCTYPE html>
: Declares the document type and version (HTML5).<html>
: The root element of an HTML document.<head>
: Contains meta-information about the document (e.g., title, styles, scripts).<title>
: Sets the title of the webpage (displayed in the browser tab).<body>
: Contains the visible content of the webpage.<!DOCTYPE html> <html> <!-- head tag --> <head> <title>Welcome to Geeksforgeeks</title> </head> <!-- Body tag --> <body> <h2>Geeksforgeeks</h2> <p> A Computer Science Portal for Geeks </p> <p> Please change the code and click the Run the button to see the changes. </p> </body> </html>
Text Formatting Tags
<h1>
to<h6>
: Headings (h1 is the largest, h6 is the smallest).<p>
: Paragraph of text.<strong>
: Bold text (semantically indicates importance).<em>
: Italicized text (semantically indicates emphasis).<br>
: Line break.<hr>
: Horizontal rule (a thematic break).<span>
: Inline container for styling or scripting.
Example:
<h1>Main Heading</h1>
<p>This is a <strong>bold</strong> and <em>italic</em> text.</p>
<hr>
<p>This is another paragraph.<br>With a line break.</p>
Link and Image Tags
<a>
: Anchor tag for hyperlinks.<a href="https://example.com">Visit Example</a>
<img>
: Embeds an image.<img src="image.jpg" alt="Description of image">
List Tags
<ul>
: Unordered list (bullet points).<ol>
: Ordered list (numbered).<li>
: List item.
Example:
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<ol>
<li>First</li>
<li>Second</li>
</ol>
Table Tags
<table>
: Defines a table.<tr>
: Table row.<th>
: Table header cell.<td>
: Table data cell.
Example:
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
</table>
Form Tags
<form>
: Container for form elements.<input>
: Input field (text, password, checkbox, radio, etc.).<textarea>
: Multi-line text input.<button>
: Clickable button.<label>
: Label for form elements.<select>
: Dropdown list.<option>
: Option in a dropdown list.
Example:
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<br>
<label for="age">Age:</label>
<input type="number" id="age" name="age">
<br>
<button type="submit">Submit</button>
</form>
Semantic Tags
<header>
: Represents the header of a section or page.<nav>
: Navigation links.<main>
: Main content of the document.<section>
: A standalone section of content.<article>
: Self-contained content (e.g., blog post).<aside>
: Sidebar or tangential content.<footer>
: Footer of a section or page.
Example:
<header>
<h1>Website Title</h1>
<nav>
<a href="#">Home</a> |
<a href="#">About</a> |
<a href="#">Contact</a>
</nav>
</header>
<main>
<article>
<h2>Article Title</h2>
<p>Article content goes here.</p>
</article>
</main>
<footer>
<p>© 2023 My Website</p>
</footer>
Multimedia Tags
<audio>
: Embeds audio files.<audio controls> <source src="audio.mp3" type="audio/mpeg"> </audio>
<video>
: Embeds video files.<video controls width="320"> <source src="video.mp4" type="video/mp4"> </video>
Other Useful Tags
<div>
: Block-level container for grouping content.<span>
: Inline container for styling or scripting.<iframe>
: Embeds another webpage or content.<iframe src="https://example.com" width="600" height="400"></iframe>
<meta>
: Provides metadata (e.g., charset, viewport).<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
Self-Closing Tags
Some tags don’t require a closing tag:
<br>
(line break)<hr>
(horizontal rule)<img>
(image)<input>
(input field)<meta>
(metadata)
Example of a Complete HTML Page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sample Page</title>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>
<main>
<p>This is a sample paragraph.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</main>
<footer>
<p>© 2023 My Website</p>
</footer>
</body>
</html>
CSS selectors :-
CSS uses selectors to target HTML elements and apply styles. Below, I'll explain the key concepts and provide examples of how CSS works with HTML elements.
selectors :-
Element Selector*:*
Targets all instances of a specific HTML tag.p { color: blue; }
This styles all
<p>
elements with blue text.Class Selector*:*
Targets elements with a specific class attribute (prefixed with a.
)..highlight { background-color: yellow; }
This styles all elements with
class="highlight"
.ID Selector*:*
Targets a single element with a specific ID attribute (prefixed with a#
).#header { font-size: 24px; }
This styles the element with
id="header"
.Attribute Selector*:*
Targets elements with a specific attribute or attribute value.input[type="text"] { border: 1px solid #ccc; }
This styles all
<input>
elements withtype="text"
.Pseudo-class Selector*:*
Targets elements in a specific state (e.g., hover, focus).a:hover { color: red; }
This changes the color of links when hovered over.
Pseudo-element Selector*:*
Targets specific parts of an element (e.g., first line, first letter).p::first-letter { font-size: 2em; }
This styles the first letter of every
<p>
element.Combined Selectors*:*
Descendant Selector*:* Targets elements inside another element.
div p { color: green; }
This styles all
<p>
elements inside a<div>
.Child Selector*: * Targets direct children of an element.
ul > li { list-style-type: square; }
This styles only
<li>
elements that are direct children of<ul>
.Adjacent Sibling Selector: Targets an element immediately following another.
h1 + p { font-weight: bold; }
This styles the
<p>
element that directly follows an<h1>
.General Sibling Selecto**r**: Targets all siblings of an element.
h1 ~ p { color: purple; }
This styles all
<p>
elements that are siblings of an<h1>
.
Common CSS Properties
Here are some commonly used CSS properties:
Text and Font Properties*:*
color
: Sets text color.font-size
: Sets the size of the text.font-family
: Sets the font family.text-align
: Aligns text (left, right, center, justify).
Example:
h1 {
color: red;
font-size: 32px;
font-family: Arial, sans-serif;
text-align: center;
}
Box Model Properties*:*
width
andheight
: Set the size of an element.margin
: Sets the space outside an element.padding
: Sets the space inside an element.border
: Sets the border around an element.
Example:
div {
width: 300px;
height: 200px;
margin: 10px;
padding: 20px;
border: 2px solid black;
}
Background Properties*:*
background-color
: Sets the background color.background-image
: Sets a background image.background-size
: Controls the size of the background image.
Example:
body {
background-color: lightblue;
background-image: url('image.jpg');
background-size: cover;
}
Flexbox and Grid Properties
Flexbox:
display: flex
,justify-content
,align-items
.Grid:
display: grid
,grid-template-columns
,grid-gap
.
Example (Flexbox):
.container {
display: flex;
justify-content: center;
align-items: center;
}
Transitions and Animation
transition
: Smoothly changes property values.animation
: Creates complex animations using@keyframes
.
Example (Transition):
button {
background-color: blue;
transition: background-color 0.3s ease;
}
button:hover {
background-color: red;
}
Example (Animation):
@keyframes slide {
from { transform: translateX(0); }
to { transform: translateX(100px); }
}
.box {
animation: slide 2s infinite;
}
Example of CSS with HTML
Here’s how CSS can be applied to an HTML document:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Example</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
h1 {
color: #333;
text-align: center;
}
.container {
width: 80%;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.button {
display: inline-block;
padding: 10px 20px;
background-color: #007BFF;
color: #fff;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to My Website</h1>
<p>This is a paragraph with some styled text.</p>
<a href="#" class="button">Click Me</a>
</div>
</body>
</html>