Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set height of an iframe element using HTML?
The iframe element is a powerful tool in HTML that allows web developers to embed external content into a web page. It creates a container that displays other HTML documents within a page. The default height of an iframe element does not always fit with the webpage's layout, so we need to ensure that it's sized correctly to fit seamlessly into the page layout.
Understanding the Iframe Element
Iframe stands for inline frame. It allows developers to embed content such as videos, maps, or social media feeds from another website or web page into the current page. The iframe element creates a rectangular box on the page that displays the embedded content, and it has several attributes that can be set to customize its behavior, including the width and height attributes.
Syntax
Following is the basic syntax for the iframe element with height attribute
<iframe src="URL" height="value"></iframe>
The height value can be specified in pixels (e.g., 300) or as a percentage (e.g., 50%).
Importance of Setting Iframe Height
Setting the height of an iframe element is important for several reasons
Layout Control It ensures that the iframe content fits into the page layout without causing unwanted scrolling or excessive white space.
Performance It helps improve the loading time of the page by allowing the browser to allocate the correct space for the iframe content.
User Experience It improves the accessibility and readability of the web page by displaying content in an appropriately sized container.
Setting Iframe Height Using HTML Attributes
The most common way to set the height of an iframe element is by using the height HTML attribute. This attribute accepts values in pixels or percentages.
Example Fixed Height in Pixels
Following example sets the iframe height to 300 pixels using the height attribute
<!DOCTYPE html> <html> <head> <title>Iframe Height with HTML Attribute</title> </head> <body style="font-family: Arial, sans-serif; text-align: center; padding: 20px;"> <h1>Welcome to TutorialsPoint</h1> <h3>Setting the height of an iframe element using HTML</h3> <iframe src="https://www.tutorialspoint.com/index.htm" height="300" width="600"></iframe> </body> </html>
The iframe displays the embedded content in a 300-pixel tall container
Welcome to TutorialsPoint Setting the height of an iframe element using HTML [Iframe content displayed in 300px height container]
Example Percentage Height
Following example sets the iframe height to 50% of the viewport height
<!DOCTYPE html> <html> <head> <title>Iframe Percentage Height</title> </head> <body style="font-family: Arial, sans-serif; text-align: center; padding: 20px;"> <h2>Responsive Iframe with Percentage Height</h2> <iframe src="https://www.tutorialspoint.com/market/index.asp" height="50%" width="80%"></iframe> </body> </html>
The iframe adjusts its height to 50% of the browser window height, making it responsive to different screen sizes.
Setting Iframe Height Using CSS
The second method to set the height of an iframe element is using CSS. This approach provides more flexibility and helps keep styling separate from HTML structure, making the code more organized and maintainable.
Example CSS Height Property
Following example sets the iframe height to 250 pixels using CSS
<!DOCTYPE html>
<html>
<head>
<title>Iframe Height with CSS</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 20px;
}
iframe {
height: 250px;
width: 450px;
border: 2px solid #ccc;
}
</style>
</head>
<body>
<h1>Welcome to TutorialsPoint</h1>
<h3>Setting the height of an iframe element using CSS</h3>
<iframe src="https://www.tutorialspoint.com/market/index.asp"></iframe>
</body>
</html>
The CSS approach also allows additional styling such as borders, making the iframe more visually integrated with the page design
Welcome to TutorialsPoint Setting the height of an iframe element using CSS [Iframe content displayed in 250px height container with border]
Example Responsive Iframe with CSS
Following example creates a responsive iframe using CSS viewport units
<!DOCTYPE html>
<html>
<head>
<title>Responsive Iframe</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.iframe-container {
width: 100%;
max-width: 800px;
margin: 0 auto;
}
.responsive-iframe {
width: 100%;
height: 60vh;
border: 1px solid #ddd;
border-radius: 8px;
}
</style>
</head>
<body>
<h2>Responsive Iframe Design</h2>
<div class="iframe-container">
<iframe class="responsive-iframe" src="https://www.tutorialspoint.com/index.htm"></iframe>
</div>
</body>
</html>
This creates an iframe that takes 60% of the viewport height (60vh) and adapts to different screen sizes.
Comparison of Methods
Following table compares the HTML attribute and CSS approaches for setting iframe height
| HTML Attribute Method | CSS Method |
|---|---|
Uses height="value" directly in the iframe tag. |
Uses height: value; in CSS rules. |
| Quick and simple for basic implementations. | More flexible with advanced styling options. |
| Styling is mixed with HTML structure. | Keeps styling separate from HTML structure. |
| Limited responsive design capabilities. | Better support for responsive designs with units like vh, %. |
| Good for static, fixed-size iframes. | Ideal for dynamic and responsive layouts. |
Common Height Units
When setting iframe height, you can use various units
Pixels (px) Fixed height, e.g.,
height="300"orheight: 300px;Percentage (%) Relative to parent container, e.g.,
height="50%"Viewport Height (vh) Relative to browser window height, e.g.,
height: 50vh;Em/Rem units Relative to font size, e.g.,
height: 20em;
Conclusion
Setting the height of an iframe element can be accomplished using either HTML attributes or CSS properties. The HTML attribute method is simple and direct, while CSS provides more flexibility and better separation of concerns. Choose the method that best fits your project's requirements and design approach for optimal integration of embedded content into your web pages.
