Adaptable positioning determined by screen and window width exclusively

After searching through numerous resolved dynamic positioning questions, I have not found any solutions that are helpful to my situation. Here is a description of the page...

+-----------------------------------------------------------------------+
| +-----------+ +------------------+ +------------------+ +-----------+ |
| |   IMG 1   | |                  | |                  | |   IMG 4   | |
| | width:20% | | IMG 2, width:30% | | IMG 3, width:30% | | width:20% | |
| |  aspect   | | aspect ratio 4:3 | | aspect ratio 4:3 | |  aspect   | |
| |  ratio    | |                  | |                  | |  ratio    | |
| |   3:4     | +------------------+ +------------------+ |   3:4     | |
| |           |                                           |           | |
| +-----------+                                           +-----------+ |
| +-----------+                                           +-----------+ |
| |   IMG 5   |                                           |   IMG 6   | |
| | width:20% |                                           | width:20% | |
| |  aspect   |                                           |  aspect   | |
| |  ratio    |                                           |  ratio    | |
| |   3:4     |                                           |   3:4     | |
| |           |                                           |           | |
| +-----------+                                           +-----------+ |
| +-----------+                                           +-----------+ |
| |   IMG 7   |                                           |   IMG 8   | |
| | width:20% |                                           | width:20% | |
| |  aspect   |                                           |  aspect   | |
| |  ratio    |                                           |  ratio    | |
| |   3:4     |                                           |   3:4     | |
| |           |                                           |           | |
| +-----------+                                           +-----------+ |
+-----------------------------------------------------------------------+

(1) My aim is for the images to adjust in size based on the window width only. If the user resizes the window vertically, I do not want the images resized. (2) Additionally, I would like this resizing to continue until the window reaches 50% of the screen width, at which point they will remain constant in size. For example, IMG 1's width should stay at 10% of the screen width.

I can achieve (1) for the top row. The challenge lies with IMGs 5-8 as the 'top' value is calculated using the window height.

Is there a way I can dynamically accomplish what I need? I am unable to use calc() since it determines the result for the top position based on window height. Moreover, there seems to be no CSS property for screen width and JavaScript does not handle dynamic resizing.

Any assistance provided would be greatly appreciated. (additional images below may help clarify my request)

magic²

Full Screen

+-----------------------------------------------------------------------+
| +-----------+ +------------------+ +------------------+ +-----------+ |
| |   IMG 1   | |                  | |                  | |   IMG 4   | |
| | width:20% | | IMG 2, width:30% | | IMG 3, width:30% | | width:20% | |
| |  aspect   | | aspect ratio 4:3 | | aspect ratio 4:3 | |  aspect   | |
| |  ratio    | |                  | |                  | |  ratio    | |
| |   3:4     | +------------------+ +------------------+ |   3:4     | |
| |           |                                           |           | |
| +-----------+                                           +-----------+ |
| +-----------+                                           +-----------+ |
| |   IMG 5   |                                           |   IMG 6   | |
| | width:20% |                                           | width:20% | |
| |  aspect   |                                           |  aspect   | |
| |  ratio    |                                           |  ratio    | |
| |   3:4     |                                           |   3:4     | |
| |           |                                           |           | |
| +-----------+                                           +-----------+ |
| +-----------+                                           +-----------+ |
| |   IMG 7   |                                           |   IMG 8   | |
| | width:20% |                                           | width:20% | |
| |  aspect   |                                           |  aspect   | |
| |  ratio    |                                           |  ratio    | |
| |   3:4     |                                           |   3:4     | |
| |           |                                           |           | |
| +-----------+                                           +-----------+ |
+-----------------------------------------------------------------------+

width: 50% height:100%

+-------------------------------------------+
| +------+ +---------+ +---------+ +------+ |
| |IMG 1 | |  IMG 2  | |  IMG 3  | |IMG 4 | |
| |      | |         | |         | |      | |
| |      | +---------+ +---------+ |      | |
| +------+                         +------+ |
| +------+                         +------+ |
| |IMG 5 |                         |IMG 6 | |
| |      |                         |      | |
| |      |                         |      | |
| +------+                         +------+ |
| +------+                         +------+ |
| |IMG 7 |                         |IMG 8 | |
| |      |                         |      | |
| |      |                         |      | |
| +------+                         +------+ |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
+-------------------------------------------+

width: 50% height: 50%

+-------------------------------------------+
| +------+ +---------+ +---------+ +------+ |
| |IMG 1 | |  IMG 2  | |  IMG 3  | |IMG 4 | |
| |      | |         | |         | |      | |
| |      | +---------+ +---------+ |      | |
| +------+                         +------+ |
| +------+                         +------+ |
| |IMG 5 |                         |IMG 6 | |
| |      |                         |      | |
| |      |                         |      | |
| +------+                         +------+ |
| +------+                         +------+ |
| |IMG 7 |                         |IMG 8 | |
+-------------------------------------------+

width: 33% height: 50%

+-------------------------+
| +------+ +---------+ +--|
| |IMG 1 | |  IMG 2  | |  |
| |      | |         | |  |
| |      | +---------+ +--|
| +------+                |
| +------+                |
| |IMG 5 |                |
| |      |                |
| |      |                |
| +------+                |
| +------+                |
| |IMG 7 |                |
+-------------------------+

Answer №1

Although not flawless, one way to achieve this is by using a value that signifies the screen size without relying on percentages. In CSS, there are two options available: vw and vh. For your case, using vw for the viewport width would be ideal.

Essentially, vw represents the viewport width divided by 100, whereas vh stands for viewport height divided by 100.

These values adjust dynamically when the window is resized, ensuring consistent proportions. To illustrate, consider the following example:

.square
{
  width: 2vw;
  height: 2vw;
  background-color: lightblue;
  position: relative;
  top: 5vh;
  left: 5vw;
}
<div class="square">
</div>

While it's possible to perform more complex calculations by determining the screen's aspect ratio, the principle remains the same.

Answer №2

I found the solution I was looking for in the javascript onresize event.

<html>
<head>
</head>
<body onresize="myFunction()">
<span id="demo"><img src="img1.jpg" width="20%"></span>
<script>
function myFunction() {
    if (window.innerWidth > (screen.width/2))
    {
        var txt = "<img src='img1.jpg' width='20%'>";
    } else {
        var txt = "<img src='img1.jpg' width='";
        txt = txt + screen.width/10;
        txt = txt + "'>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>

This code will adjust the image size to 20% of the window's inner width until it reaches or exceeds the screen width, at which point the image size will stay constant at 10% of the screen width.

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

Uncovering specific content within an html document with the help of BeautifulSoup

I am currently working with a code that involves using BeautifulSoup to scrape text from elements with the class 'product'. However, I am only interested in extracting the 2nd and 4th values ('Product 2' and 'Product 4') to be ...

Reposition the CSS content

In order to achieve the desired effect of having the text in the middle of the div both vertically and horizontally, you can try using the following code: .servicecircle { width: 204px; height: 204px; background-image: url('secret.png&a ...

Tips for preventing a bootstrap modal from being dragged beyond its parent container

I need help figuring out how to prevent my bootstrap modal from being dragged outside of its parent container. Is there a way to constrain the modal within its parent? $(document).ready(function() { ShowValidationResult(); }); function ShowValidation ...

Ionic Troubleshoot: Issue with Reading Property

Encountering an error: A TypeError occurs: Cannot Read Property of "username" of Undefined The HTML code responsible for the error is as follows: <ion-content padding style="text-align: center; margin-top: 35px"> <form (ngSubmit)="logFor ...

If a popup is present on the webpage, be sure to close it

I'm facing the challenge of scraping data from a website. Sometimes, the web page displays a security dialog box before redirecting to the login page. To handle this scenario, I attempted to close the popup dialog using this code: Dr.FindElementByCss ...

A Python program utilizing Selenium webdriver for Chrome which attempts to retrieve and print out the elements inside a listbox, but encounters difficulties and is unable to

I'm currently executing the script shown below: #import necessary packages from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdrive ...

Blazor Dropdown Menus using <NavLink>

I'm currently working with the Blazor template that utilizes the NavLink component. It essentially incorporates Bootstrap menu functionalities under the hood. Although I've come across a few articles that discuss certain features like Matching, ...

Can you explain the significance of this HTML code?

While examining some source code, I came across the following: <div class="classname {height: 300px; width: 200px}"></div> I am aware that element styling can be done using the style="" attribute. Could you explain what this code snippet sig ...

The Bootstrap 4 Carousel Indicators are missing from the page

I am currently working on creating a carousel slider with indicators at the bottom using Bootstrap 4.1.3. However, I have run into an issue where the indicators are not visible, although they still function when clicked on. I have attempted solutions such ...

The Drop-down menu with carousel in Bootstrap is experiencing difficulties displaying its sub-menu items

I am currently facing an issue where the sub-menu items are hidden behind the content in a bootstrap carousel. Despite everything else working fine, this particular problem is causing a headache. JSFiddle: https://jsfiddle.net/Saneesh/pwmyvsw6/65/ Is th ...

Is it possible to continuously increase the value of a CSS property with each click?

I am trying to implement a feature where the value of an element decreases each time a different element is clicked. Currently, I have the following code: $("#trigger_heritage").click(function () { $(".heritage_content ul").css("margin-left", + -880); ...

Creating a 2 by 2 division grid using the float property and setting a minimum height

I currently have a 2 x 2 grid of divs, each set to float:left; with a width: 400px; inside a parent div with width:800px;. In order for the divs to expand as new content is added or removed, I've added min-height: 150px;. The issue arises when a div ...

The spacing within the HTML table appears to be sporadic and

My tables seem to be experiencing large gaps between the rows, despite setting padding and spacing to 0... The table has attributes like cellspacing="0", cellpadding="0", width="460" and border="0" However, there are still significant gaps between the ...

I am encountering difficulty loading a .gltf file for use with Three.js on my React website

I uploaded my .gltf file of a 3D model to the public folder of my project and then ran the command "npx gltfjsx filename" to convert it into a .js React component. Within that .js file, I included the following line: const { nodes, materials } = useGLTF(&a ...

Creating a new table based on an if statement from an HTML table index value of x

Hey everyone, I could really use your assistance. I've been attempting to create an if statement based on the values in my HTML table's index-x (where x represents the column index). I've successfully managed to convert the table into an arr ...

There are three dropdown menus in my form, and the options available in each one are based on the choices made in the

I need to create a form with three dropdown menus. Each menu should display options based on the selection made in the previous one. For example, there should be dropdowns for categories, car brands, and car models. The car brand dropdown should only sh ...

The positioning of inline block elements is not aligned correctly

Can anyone shed light on why my text is causing the block element to move downward? Removing the text aligns the block elements perfectly. <div class="item-0" align="center"> <div class="row"> <div class="col-lg-12"> ...

Use jQuery to change the background color when clicked

Below is the HTML code with UL and LI elements: <UL> <LI><span id='select1'>Text</span></LI> <LI><span id='select2'>Text</span></LI> <LI><span id='select3'>Tex ...

Object vanishing beneath layers despite z-index: 4 (topmost layer)

Despite using z-index: 4 as the highest layer, the element keeps disappearing from the screen. I can confirm that it is correctly applied through the browser console. Any suggestions are appreciated. Please refer to the links below for images (I can' ...

What are the options for transferring information between HTMX and Flask using JSON or another format aside from HTML?

So I am currently working on an app using Flask and HTMX. While I understand that htmx requires data in HTML format to swap with a specific target element, I am wondering if there is a way to update the current page using different formats such as dictiona ...