HTML is a markup language used to create the structure of web pages. It consists of elements, which are represented by tags. For example, the <html>
tag is the root element of an HTML page, and it encloses the entire content. Other common tags include <head>
for metadata, <body>
for the visible content, <h1>
- <h6>
for headings, and <p>
for paragraphs.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>My HTML Page</title>
</head>
<body>
<h1>Welcome to my page</h1>
<p>This is a sample paragraph.</p>
</body>
</html>
CSS is used to style HTML elements. It can be applied in three ways: inline, internal, and external. Inline CSS is added directly to an HTML element using the style
attribute. Internal CSS is placed within the <style>
tag in the <head>
section of an HTML page. External CSS is stored in a separate .css
file and linked to the HTML page using the <link>
tag.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>My Styled Page</title>
<style>
h1 {
color: blue;
}
p {
font - size: 16px;
}
</style>
</head>
<body>
<h1>Styled Heading</h1>
<p>Styled paragraph.</p>
</body>
</html>
An iframe is an HTML element used to embed another HTML document within the current one. The <iframe>
tag has several attributes, such as src
to specify the source URL of the embedded document, width
and height
to set the dimensions of the iframe.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>Using Iframe</title>
</head>
<body>
<iframe src="https://www.example.com" width="600" height="400"></iframe>
</body>
</html>
When creating an HTML page, start with the <!DOCTYPE html>
declaration, followed by the <html>
tag. Inside the <html>
tag, include the <head>
and <body>
tags. The <head>
section contains metadata like the page title, character encoding, and links to external resources. The <body>
section holds the visible content of the page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>My Structured Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>Page Header</h1>
</header>
<main>
<p>Main content goes here.</p>
</main>
<footer>
<p>© 2024 My Website</p>
</footer>
</body>
</html>
To style an HTML page using CSS, first, select the HTML elements you want to style. You can use element selectors, class selectors, or ID selectors. Then, define the CSS properties and their values for those selectors.
/* styles.css */
header {
background - color: lightgray;
padding: 20px;
}
main {
margin: 20px;
}
footer {
background - color: lightgray;
text - align: center;
padding: 10px;
}
To embed an iframe, use the <iframe>
tag with the src
attribute set to the URL of the page you want to embed. You can also set the width and height attributes to control the size of the iframe.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>Embedding Iframe</title>
</head>
<body>
<iframe src="https://www.wikipedia.org" width="800" height="600"></iframe>
</body>
</html>
Semantic HTML elements like <header>
, <nav>
, <main>
, <article>
, <section>
, and <footer>
provide meaning to the structure of a web page. They make the code more readable for developers and also improve accessibility for screen readers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>Semantic HTML</title>
</head>
<body>
<header>
<h1>My Semantic Page</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
</ul>
</nav>
</header>
<main>
<article>
<h2>Article Title</h2>
<p>Article content goes here.</p>
</article>
</main>
<footer>
<p>© 2024 My Semantic Site</p>
</footer>
</body>
</html>
The CSS box model consists of content, padding, border, and margin. Understanding the box model is crucial for layout design. You can use the box - sizing
property to control how the width and height of an element are calculated.
div {
width: 300px;
padding: 20px;
border: 1px solid black;
margin: 10px;
box - sizing: border - box;
}
To make an iframe responsive, you can use CSS to set the width to a percentage value and use the aspect - ratio
property to maintain the correct proportions.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>Responsive Iframe</title>
<style>
.iframe - container {
position: relative;
width: 100%;
padding - bottom: 56.25%; /* 16:9 aspect ratio */
}
.iframe - container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div class="iframe - container">
<iframe src="https://www.youtube.com/embed/VIDEO_ID"></iframe>
</div>
</body>
</html>
To make an HTML page accessible, use semantic elements, provide alternative text for images using the alt
attribute, and ensure proper heading hierarchy. Also, use ARIA (Accessible Rich Internet Applications) attributes when needed.
<img src="image.jpg" alt="A beautiful landscape">
To improve CSS performance, minimize the use of inline CSS, use external CSS files, and compress your CSS code. Also, avoid using overly complex selectors and use CSS sprites for images.
When using iframes, be cautious about the source of the embedded content. Only embed content from trusted sources. You can also use the sandbox
attribute to restrict the permissions of the iframe, such as preventing it from executing scripts or submitting forms.
<iframe src="https://trusted - site.com" sandbox="allow - same - origin allow - scripts"></iframe>
HTML, CSS, and iframes are essential tools in web development. By understanding their fundamental concepts, usage methods, common practices, and best practices, you can create well - structured, visually appealing, and secure web pages. HTML provides the structure, CSS adds the style, and iframes allow you to embed external content seamlessly. Always keep accessibility and performance in mind when working with these technologies.