iOS 14.7.1 causing issues with CSS aspect ratio feature

I created an application that utilizes the aspect ratio CSS property to manage the width of an element by only specifying its height. Unfortunately, when testing it on my Iphone 11 running iOS 14.7.1, the aspect ratio doesn't seem to function properly.

Here is a minimal working example (MWE):

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    body {
      height: 100vh;
      width: 100vw;
      background: darkgray;
    }
    div {
      height: 20vh;
      aspect-ratio: 1;
      background: blue;
    }
  </style>
</head>
<body>
  <div></div>
</body>
</html>

The output differs between my development machine (left) and the Iphone 11 (right):

https://i.sstatic.net/Tjvhwm.png https://i.sstatic.net/igX89m.png

The width of the div on the Iphone becomes distorted due to the aspect ratio not functioning correctly. This issue occurs in both chrome and safari. Is there any workaround available?

Answer №1

In the absence of aspect-ratio support in Safari on IOS 14 and earlier versions, we need to explore alternative methods.

One workaround is to use a square image within a div, where the div's height is set as needed in percentage and the width defaults to auto. By setting the image's height to 100% and leaving the width as auto, it fills the space appropriately, creating a square of the desired dimensions.

Although not ideal due to the necessity of modifying the HTML for proper styling, this method proves effective. The square image can be an inline SVG, eliminating the need for an additional file.

<!doctype html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    body {
      height: 100vh;
      width: 100vw;
      background: darkgray;
    }
    
    div {
      height: 20%;
      background: blue;
      display: inline-block;
    }
    
    img {
      height: 100%;
    }
  </style>
</head>

<body>
  <div></div>
  <div>
    <img src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect fill='rgba(0,0,0,0)' x='0' y='0' width='100%' height='100%'/></svg>" />
  </div>
</body>

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

Is it possible to refresh a table that is currently displayed in a model popup?

My code to reload a table or div inside a model popup continuously is not working as expected. Can you help me with this issue? I attempted to utilize ajax for this purpose but unfortunately, the code below is not updating chats (I am working on a chat ta ...

Can you explain the function of the * CSS operator? Are there any additional CSS operators that serve similar purposes?

While researching how to create a nested table using list elements in CSS, I came across this interesting page: . Upon examining the stylesheet, I noticed unfamiliar symbols being used that appear to be CSS operators, such as the > and * symbols. For ...

What could be causing Safari to alter the heights of my div element?

After completing a challenge for FrontEnd Mentor, I excitedly texted a friend my demo link to showcase my work. However, upon opening the link on my phone, I discovered that the formatting was all over the place. Switching to Safari on my desktop only made ...

Encountering a JSON error while attempting to login through an API on the Ionic platform

While implementing the login functionality through API in Ionic, I encountered the following error: Error: Property 'json' does not exist on type '{}'. Below is my loginpage.html code: <ion-header> <ion-navbar> & ...

Styling Tables with Bootstrap CSS and Integrating Database Data

Currently, I am utilizing the code snippet below to retrieve and display data stored in a database. Although everything functions correctly, I am encountering an issue with displaying it using Bootstrap CSS. Strangely, when I try copying an example of a ta ...

AlignmentPositioning Customize Check Boxes with Progress Bars in Bootstrap 4

I have the code snippet below: <div class="row"> <div class="col-md-2"> <div class="custom-control custom-checkbox my-auto" style="display: inline; align-items: center;"> <input class="custom-control ...

When using AngularJS with DateRangePicker, I am encountering an issue where my ng-model does not capture the input text value when I select a date

Trying to establish the selected date range in an ng-model called dateRange in the following HTML. The input text field displays the selected value correctly, but dateRange remains null. What steps can I take to address this issue? <!DOCTYPE html> ...

How can you add a border before and after text as well as around the entire box

Is there a way to add borders to the image below? I would like to include a border that looks similar to this: text added before and after the entire box How can I achieve a border using css either with before, after, or background? ...

Transferring HTML documents using the Express framework

I'm currently developing a small website that includes a subscription list for sending emails using Node and Express. One issue I've encountered is with providing users an unsubscribe link. app.get('/unsubscribe', ... ...); My goal i ...

Tips for positioning two divs side by side in block formation

I'm attempting to display two distinct div elements as blocks, one for the name and the other for the names. Here is the Fiddle The names block should be displayed as a separate block next to the name div, regardless of screen responsiveness. How ca ...

Problem arising from implementing background and relative positioning in html and css

Encountering an issue with html and css. Today, attempted to create a page with a large background image for the first time. Utilizing the foundation framework, but facing a challenge. Foundation is an adaptive framework, and when resizing the browser, a ...

What is the process for embedding HTML and CSS content into an Outlook email?

Has anyone experienced trouble adding an HTML and CSS web page to an Outlook email? I attempted to use the "Insert as text" option, but the CSS file is not loading correctly in Outlook. Any suggestions on how to fix this? The error message states that the ...

What could be causing the issue with HTML not being printed upon button click in ReactJS?

My goal is to display the word "Hello" on the screen when the add button is clicked. However, I am encountering an issue where it is not showing up. Any insights or solutions would be greatly appreciated! import React, { Component } from 'react'; ...

Ways to rearrange items in the navigation bar?

I am working with a Bootstrap template and I am trying to adjust the layout to be right-to-left (RTL). Currently, when I set the site title to RTL in the navbar, the buttons in the navbar also switch to RTL alignment. This is not the desired layout I want. ...

Displaying a pop-up window upon clicking on an item in the list view

After creating a list where clicking an item triggers a popup, issues arose when testing on small mobile screens. Many users found themselves accidentally tapping buttons in the popup while trying to view it. What is the most effective way to temporarily ...

Issue with text not truncating in nested flexbox, and min-width is ineffective

I've been struggling for hours to implement text truncation in a flexbox layout (mainly with Bootstrap 5) but have had no success. I've come across suggestions to use min-with: 0 on the parent div, but despite trying various combinations, I can&a ...

IE6 is not displaying the tag styles correctly

My website has a section that shows two links next to each other. While it's displaying correctly on most browsers, it seems to have issues specifically in IE6. You can view the issue on this fiddle. Can anyone shed some light on why this is happenin ...

Include the url of the html file in your JavaScript code

I have a piece of code that I want to include in my JavaScript instead of HTML. The code is as follows: <script async src="https://www.googletagmanager.com/gtag/js?id=ID"></script> Since all my functions are written in JavaScript, I ...

When a cursor hovers over an image, a dark line appears

I wanted to create a hover effect where the image would enlarge when hovered over. I achieved this using a JavaScript function that applies the transform property with a scale of 1.2 to the picture. However, during the animation, a black line appears at th ...

Show a 1920x1170 / 183KB image as the background using CSS styling

I need help with displaying a background image that is 1920x1170 pixels and has a file size of 183KB. Here is the code I am using: .bground{ height:auto; left:0; position:fixed; top:0; width:100%; } <div class="bgrou ...