- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to maintain the aspect ratio of an element with CSS?
To maintain the aspect ration of an element with CSS, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body{ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } .aspectContainer { background-color: rgb(191, 66, 216); position: relative; width: 100%; padding-top: 75%; } .aspectText { position: absolute; text-align: center; top: 0; left: 0; bottom: 0; right: 0; Text-align: center; font-size: 20px; color: white; } </style> </head> <body> <h1>Aspect ratio example</h1> <div class="aspectContainer"> <div class="aspectText">4:3 Aspect ratio</div> </div> <h2>Resize the window to see 4:3 aspect ratio in action</h2> </body> </html>
Output
The above code will produce the following output −
On resizing the window the element will shrink in the ratio 4:3 −
- Related Articles
- How to change the aspect ratio of an image in JavaFX?
- How to compute the aspect ratio of an object in an image using OpenCV Python?
- How to scale an Image in ImageView to keep the aspect ratio in Android?
- How to change the aspect ratio of a plot in ggplot2 in R?
- How to style an hr element with CSS?
- Maintaining Aspect Ratios for HTML Videos with CSS
- Setting the aspect ratio of a 3D plot in Matplotlib
- How to display an element on hover with CSS?
- How to remove the border from an editable element with CSS?
- How to zoom/scale an element on hover with CSS?
- How to center an element vertically and horizontally with CSS?
- Set the background color of an element with CSS
- Set the background image of an element with CSS
- Set the font stretch of an element with CSS
- Set the right margin of an element with CSS

Advertisements