What causes CSS boundaries to be accurately computed only during the initial page load?

I am currently working on a unique challenge where I aim to have a square element whose size is dependent on the height of the page. Basically, I want the width of the element to be linked to its height.

While I am aware that this can be achieved using the opposite technique, or by utilizing vh, I have chosen to tackle this as a learning exercise.

My solution involves leveraging the aspect-ratio maintenance capability of img tags for displayed images. Below is the HTML code snippet I came up with (the img tag contains a 1x1px gif):

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="styles.css"/>
    </head>
    <body>
        <div class="circle-tree">
            <div class="circle-tree-wrap">
                <!-- Experimenting with img tag to create an element that adjusts width based on height -->
                <img src="data:image/gif;base64,R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=">
                <div class="circle-tree-content"></div>
            </div>
        </div>
    </body>
</html>

In addition, here is the CSS code I wrote:

html, body {
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%; }

body {
  background-color: #64d0ac; }

.circle-tree {
  display: inline-block;
  height: 100%;
  width: 100%;
  transform: translateX(50%); }
  .circle-tree .circle-tree-wrap {
    position: relative;
    display: inline-block;
    transform-style: preserve-3d;
    height: 100%; }
    .circle-tree .circle-tree-wrap .circle-tree-content {
      position: absolute;
      width: 100%;
      height: 100%;
      background-color: #fff;
      top: 0;
      left: 0;
      transform: translateX(-50%); }
    .circle-tree .circle-tree-wrap img {
      height: 100%;
      width: auto;
      opacity: 0;
      transform: translateX(-50%); }

This method successfully creates a square .circle-tree-content upon initial page load. However, when resizing the browser window, the .circle-tree-wrap does not dynamically adjust its width to match the new width of the img element. A page reload corrects this issue temporarily. This behavior has been tested in both Chromium 43 and Firefox 38. While I understand that this approach may not be ideal for production purposes and I might opt for alternative solutions in the future (such as JS), I am intrigued by this particular quirk and would appreciate any insights from the community on why CSS calculations differ between resize events and page loads. Thank you for your help!

Answer №1

It seems to be a matter of how browsers handle the calculation of the "shrink-to-fit" or preferred width for inline elements without a specified width value. The issue may arise from the container div surrounding the image not adjusting properly when the browser window is resized, as the image does not create a line break like text does. To see this in action, check out the code example provided below or visit this interactive demo and gradually reduce the window width. You'll notice that the containers with the class ".circle-tree-wrap" adjust their width accordingly when text breaks to a new line, but not when it comes to the image.

html, body {
    margin: 0;
    width:100%; height:100%;
    background-color:black;
}
.circle-tree-wrap {
    display: inline-block;
    height: 100%;
    width:auto;
    background-color:grey;
}
img {
    vertical-align:top;
    height: 100%;
    width: auto;
    opacity:0.7;
}
em {
    vertical-align:top;
    height:100%;
    width:auto;
    color:green;
}
<div class="circle-tree-wrap">
    <!-- Adjust the img tag to observe an element adapting its width based on its height -->
    <img src="data:image/gif;base64,R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs="/>
</div>
<div class="circle-tree-wrap"><em>some text to fill the space</em>
<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

Vertically align header 3 text with a specific height

Recently, I encountered an issue with a h3 <h3>someText<h3> This particular h3 has a height and features a background image h3 { height:30px; background-image:url(); background-reat: repeat-x; } I am currently facing a challenge where the t ...

Is there a way to only display the current submenu in Angular while hiding all others?

I need help hiding other submenus when using the current submenu. Example: A a a B // using a A B b b // using b I tried to achieve this with mouseleave. <li id="admin" class="sidebar-list" ng-mouseenter="option=true" ng-mouseleave="option=fa ...

Including a Script Tag in an Angular 2 Application with a Source Attribute retrieved from a Web API Request

Situation: Within my Angular 2+ application, I am utilizing a web API to retrieve URLs for script tags created by a function called loadScript during the AfterViewInit lifecycle hook. The web API is providing the expected JsonResult data, which I have suc ...

Struggling to get Angular to properly load routes

I've been working on a local web page recently, following this helpful tutorial that shows how to integrate Django and Angular for user authentication. However, I've encountered an issue where the "register" button doesn't display anything e ...

How to Vertically Center a Span in a Mat-Header-Cell with Angular 5 Material

In my angular5 application, I am utilizing a material table to showcase some data. Within a mat-header-cell, I have a combination of a span and an img, and I'm attempting to align them correctly. This is how it currently appears: https://i.sstatic. ...

Iterating through every image displayed on a webpage

Two inquiries: What is the best way to efficiently loop through every image on a specific webpage and open each one in a new browser tab? Similar concept, but instead of opening in a new tab, I am interested in substituting different images for the ...

Error encountered while uploading image on django through jquery and ajax

Looking for help on posting a cropped image via Jquery and ajax. I've been trying to implement a solution from a question on cropping images using coordinates, but I'm facing issues with receiving the image on Django's end. The CSRF token an ...

Incorporate JSON and JavaScript into your website template

Currently, I am in the process of developing my online resume using a free template that I found and downloaded. The template incorporates bootstrap and showcases a progress bar indicating my skills. However, I want to take it a step further by dynamically ...

Sending the parameter with the URL and receiving the response in return

Can the result be retrieved by calling a URL and sending specific parameters with it? For instance, if I pass two numbers along with the URL, can I receive the sum in return? The addition operation is carried out on the page being called upon. ...

Guide on aligning a popup next to the button that activated it using CSS and JavaScript

I am currently generating a dynamic quantity of divs, each containing a button that triggers a popup when clicked. I am attempting to position the popup below the specific button that activated it, but it remains static and does not move accordingly. <d ...

Having trouble with my JQuery selection

In the code below, I have assigned an id of "song-pick." $("#archive").append("<div id=\"song-pick\" data-song=\""+song.rank+"\" class=\"box small back"+counter+"\"><span>"+song.title+"</span></div>"); ...

Tips on Getting Your Contact Form Operating Smoothly

Hey everyone, I'm exploring the world of web design and have a question about getting my contact form to work on my current theme. Below is the HTML code I am using: I would greatly appreciate it if someone could help me with coding the PHP file; doe ...

Deactivating the submit button after a single click without compromising the form's functionality

In the past, I have posed this question without receiving a satisfactory solution. On my quiz website, I have four radio buttons for answer options. Upon clicking the submit button, a PHP script determines if the answer is accurate. My goal is to disable t ...

Bootstrap the registration form to center it in the layout

https://i.sstatic.net/IWSHz.png Here is the code snippet: <div class="col-md-4 col-md-offset-3">. I am new to Bootstrap and trying to center a form that is currently on the left. I have experimented with different col-md and col-xl numbers, but have ...

What could be causing the font-weight property (CSS) to fail on certain elements?

I am facing an issue with the font-weight property in my HTML code when using spans and CSS. While some spans styled with a specific class are displaying the desired font-weight, others seem to ignore it completely. I'm unsure why this discrepancy is ...

Tips for maintaining an exposed dropdown menu in the following page?

I'm working with a dropdown that contains <a> tags and is initially set to be hidden. There's also a caret next to the dropdown that rotates down when the dropdown is open. What I'm trying to achieve is keeping the dropdown open (caret ...

How can the color of the <md-toolbar> be customized?

Here is a glimpse of how my code appears on CodePen: I am aiming to have the background color of "Sidenav Left" match that of "Menu Items", which is designated by the class .nav-theme { background-color: #34495E } I attempted to override it like this ...

Assistance with CSS Flexbox: How to align items in a grid format (images, titles, text)

I'm currently working on constructing the layout displayed in the image below. The aim is to have the image on the left with a fixed width, while the text on the right adjusts to the available width. Furthermore, the objective is for these items to s ...

What could be causing the CSS Selector to fail to click on an element even after waiting for it to become clickable?

Take a look at the code snippet below. The important part to focus on is the last line. from selenium import webdriver import os from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.su ...

Combining CSS, jQuery, and HTML into a single .html file would allow for seamless integration

I've been searching extensively, but I haven't been able to locate exactly what I need. My goal is to merge my jQuery and CSS into a single .html file, but I'm struggling to get the jQuery functionality to work. Although I have experience wi ...