Maintain the aspect ratio of DIV or image

I am looking to maintain the aspect ratio of a DIV element, similar to how it's done on Google Maps.

<div id="map" style="background-color: grey;width:100%;"> Map here </div>

The current setup adjusts the width based on the window size or col-* for bootstrap.

However, I now want to ensure that the width-to-height ratio remains 1:1.

How can I achieve this?

A similar issue arises with images where I would like to maintain a square shape.

<img class="img-fluid" style="width:100%;">

Answer №1

If you want to maintain equal width and height using CSS, you can use aspect ratio CSS. Check out the code snippet below and refer to Aspect Ratio for more detailed information.

.div {
   position: relative;
   width: 100%;
   padding-top: 100%; }
<div id="map" class="div" style="background-color: grey;width:100%;"> Map here </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the possibilities of combining AngularJS with a Bootstrap modal for a

Hello, I am currently attempting to update a modal select box using Angular and Bootstrap modals. Unfortunately, when I click the button, I am having trouble getting it to update the related select box value in the Bootstrap modal. I've tried differ ...

What strategies exist for leveraging jQuery with predefined events?

Is there a set of guidelines for incorporating predefined events into a jQuery plugin? Take for instance the unique features of the Zebra Accordion plugin (an efficient accordion plugin for jQuery) or any other plugin that defines specific events like bel ...

Having an issue with jQuery where trying to append a remove button to a list results in the

Looking to create a dynamic list where users can easily add new items by clicking a button. As each item is added, a corresponding remove button should also be displayed. The issue I'm facing is that every time a new item is added, an extra close but ...

Having trouble getting v-validate to work on dynamically generated fields in Vue.js?

I am facing an issue with validating dynamic fields using v-validate. It seems to work fine for static fields, but the same code does not seem to validate dynamically generated fields: <div v-if="condition=='true'> <input ...

Choosing links with a particular hash code

My goal is to apply an active class to links that have previously been selected with a hash. For example, if I click on: <a href="example.com/#about"> The page will display the #about section, so all links with the #about hash should now have the ...

I seem to be having trouble getting innerHTML to function properly

Whenever I attempt to showcase HTML code from a DIV element, the innerHTML function fails to retrieve its contents: <div id="container"> <tr> <td style="background-color: #ffffff;">TEST</td> </tr> </div> ...

The JQuery datepicker fails to retain the date that is selected

I have a project built with Cakephp 3.6 running locally on my localhost and also deployed on a server. In this project, I am utilizing a datepicker widget as shown below: <?php echo $this->Form->control('created', ['type' ...

Tips for positioning Material Ui buttons beside a list of images

I have a list of images and I would like to display buttons beneath each image Here is my current code: export const Media = (stuff) => { const { medias } = stuff; console.log(medias); const classes = useStyles(); return ( <div classNam ...

Do you know of any helpful resources for learning JSON with PHP through video tutorials?

I've been browsing the internet trying to learn about JSON, but a lot of tutorials are just too complicated for me. I'm specifically interested in learning how to use JSON with jQuery and PHP. If anyone has any recommendations for videos or websi ...

Creating a dynamic form where input fields and their values update based on user input in jQuery

In my form, I have an input field where users will enter an ISBN number. Based on the input number, I need to populate two other input fields: one for book title and one for author name. I am currently calling a JavaScript function on the onblur event of ...

Steps to create a styled text area in your code

Hello everyone, I am currently working on an application where the admin can input text into a text area, which will then be displayed to the logged-in users. My issue is that if the admin copies code from editors like Eclipse or IntelliJ IDE, I want t ...

Using jQuery to create a Select All Checkbox that toggles a specific class

I have come across similar examples in the past, but I have been unable to make it work as intended. The examples I found only use standard checkboxes. I attempted to customize the checkbox using a sprite sheet by applying a class, but I am struggling to a ...

A simple method to obtain the ID of the element that has been clicked and save it in a variable to be utilized in a function responsible for

Seeking assistance in optimizing the code below by removing the specific #static id and allowing for dynamic IDs such as #dynamic within the one click function. This would eliminate the need to repeatedly copy and paste the same function with different ID ...

jquery ajax function that returns an object when successful

Below is a brief example of an AJAX call wrapped in a function. MyNS.GetStrings = function (successCallback, errorCallback) { var url = serverUrl + "/GetStrings"; $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", dataType: ...

Ways to extract value and delete a text box using jQuery

I added a text box to my div using the following code: $('<input />').appendTo("#box") Now I am trying to retrieve the value entered by the user. Additionally, I want to remove the text box afterwards. I attempted this method: $ ...

Repair the voting system

In my Ruby on Rails app, I have successfully set up an up/down voting system that utilizes ajax. When a user clicks the buttons, it triggers the create method to insert a vote into the database and calculate the total sum of votes. Currently, users can fr ...

incapable of executing a rest request

Hello everyone, I am trying to make a REST call and redirect the output to a text area in HTML. Below is the code I have written for this purpose. Please review it and let me know if there are any mistakes. Additionally, could you please explain how to a ...

Troubleshooting responsive navigation bar in VueJs: Why are elements not appearing when ToggleButton is clicked?

I'm developing a VueJs single page application and I'm struggling to implement a responsive NavBar. No matter what I try, it just doesn't seem to work as expected. I've experimented with several solutions, and the closest I have come t ...

A step-by-step guide on leveraging jQuery .get() function within a Windows Gadget

I am attempting to extract information from a website and display it in my sidebar gadget using the following code: $.get("http://localhost/index.php", function(data) { $("#result").html("Data Loaded: " + data); }); While this works when I access it ...

No links were detected in the page source

I am attempting to locate URL links within the page source of this website. http://data2.7m.cn/database/index_en.htm However, I have been unable to find any links in the page source or even after trying Ajax calls with Firebug. There are no links for cou ...