Is there a way to maintain image proportions when resizing the window?

I'm in the process of developing a classroom-themed website with interactive overlaying images. The goal is to have various pictures that users can click on to access different resources from different websites. My main challenge right now is getting the sizing and proportions of all the images just right. I envision a background image that spans the entire page, with carefully positioned images and text layered on top. However, when I resize the JSFiddle window, everything appears out of proportion.

For a clearer example of what I'm aiming for: You'll notice the text over the "chalkboard" image. I want it to give the impression that the text is actually being written on the chalkboard, so it should stay fixed on the blackboard without shifting when the window is resized or viewed at different aspect ratios. I plan to do this with numerous images, so a detailed explanation would be greatly appreciated.

JSFiddle

.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: rgb(71, 71, 71);
  color: white;
  text-align: center;
  opacity: 0.75;
}

a {
  color: lightgray;
}

p {
  font-family: 'Schoolbell', arial, serif;
  color: white;
}

.container {
  position: fixed;
  top: 0%;
  left: 0%;
  height: 100%;
  width: 100%;
  background-image: url("https://images.unsplash.com/photo-1630699376059-b781970715b1?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80");
  background-repeat: no-repeat;
  background-size: 100%;
}

#whiteboard img {
  width: 40%;
  position: absolute;
  top: 20%;
  left: 35%;
}

#chalkboard img {
  width: 20%;
  height: 40%;
  position: absolute;
  top: 2%;
  left: 78%;
}

#chalkboard p {
  position: absolute;
  top: 10%;
  left: 80%;
  color: white;
  font-size: 140%;
  text-align: center;
}
<body>
  <div class="container">
    <div id="whiteboard"><img src="https://images.unsplash.com/photo-1497409988347-cbfaac2f0b12?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" /></div>
    <div id="chalkboard">
      <img src="https://images.unsplash.com/photo-1614292253351-4deb4913c142?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" />
      <p>
        Hi all!
      </p>
    </div>
  </div>
  <div class="footer">
    <p>Footer Stuff</p>
  </div>
</body>

// Side Note: These aren't the images I'm trying to use, just stock photo examples.

Appreciate your help!

Answer №1

After completing the positioning based on a percentage of the actual image, ensuring that the picture's container maintains the same aspect ratio regardless of the viewport's aspect ratio is crucial.

I conducted a preliminary assessment of your webp image by converting it to a png and determining the dimensions - further refinement may be necessary for accuracy.

Here is a sample code snippet:

<style>
  .footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    background-color: rgb(71, 71, 71);
    color: white;
    text-align: center;
    opacity: 0.75;
  }
  
  a {
    color: lightgray;
  }

  // Additional CSS styles here...

</style>
<html>

<body>
  <div class="container">
    <div id="whiteboard"><img src="https://images.unsplash.com/photo-1497409988347-cbfaac2f0b12?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" /></div>
    <div id="chalkboard">
      <img src="https://images.unsplash.com/photo-1614292253351-4deb4913c142?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" />
      <p>
        Hi all!
      </p>
    </div>
  </div>
  <div class="footer">
    <p>Footer Stuff</p>
  </div>
</body>

</html>

Please note: font size and handling text layout may require further adjustments in relative terms for proper scaling.

Additional information: The cropping issue with maintaining a constant 100vw width for the image has been highlighted.

The following code snippet dynamically adjusts either the width or height of the classroom to maximum capacity (100vw or 100vh) while preserving the aspect ratio:

<!doctype html>
<html>

<head>
  <style>
    // CSS styles for fluid image resizing...
  </style>
</head>

<body>
  <div class="container">
    <div id="whiteboard"><img src="https://images.unsplash.com/photo-1497409988347-cbfaac2f0b12?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" /></div>
    <div id="chalkboard">
      <img src="https://images.unsplash.com/photo-1614292253351-4deb4913c142?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" />
      <p>
        Hi all!
      </p>
    </div>
  </div>
  <div class="footer">
    <p>Footer Stuff</p>
  </div>
</body>

</html>

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

Disabling a chosen option within a dropdown menu

`Hello there, I am just starting out with JavaScript and jQuery. Currently, I am developing a web application for ordering food and drinks. I have a dropdown menu for selecting breakfast items and the quantity. When the "add" button is clicked, a dynamic ...

Combining hover and mousedown event listeners in jQuery: Tips and tricks

htmlCODE: <div class="original_circle" style="border-radius: 50%; background-color: blue; width: 40px; height:40px; z-index: 0"> <div class="another_circle" style="position:absolute; border-radius: 50%; background-color: coral; width: 40px; h ...

Change the classes of the body prior to the initial rendering

I know this may seem like a difficult task, and I understand that what I want to achieve might be nearly impossible. My goal is to incorporate a dark/light mode switch on my website. The challenge lies in the fact that the site consists of static files on ...

I require assistance in developing a mouseover image link that conceals the top image and unveils a bottom image that scrolls vertically in the exact same area as the top

I am trying to achieve a similar effect as shown on: this website I require assistance in creating a mouseover image link that hides the top image and reveals a bottom image that scrolls vertically within the same space as the top image. The long vertica ...

The issue with the smooth scrolling feature in next/link has not been resolved

I am currently facing an issue where smooth scrolling is not working when using next/Link, but it works perfectly fine with anchor tags. However, the downside of using anchor tags is that the page reloads each time, whereas next/link does not reload the pa ...

Django CSS graphical interface for selecting styles

I am looking to implement a feature that allows users to select an "interface theme": To enable users to change the theme across all templates, I have created a dropdown in my base.html. For all my HTML templates, I utilize a base.html file by extending ...

How can I use AJAX to remove a record from a MySQL table?

I am looking to create a button with an image X that, when clicked, will delete a row from the database at that exact moment. teste-checkbox.php <style> table { font-family: verdana; font-size: 12px; } table th { text-align: left; backgrou ...

Select a hyperlink to access the corresponding tab

I've been playing around with bootstrap and AngularJS. I placed nav-tabs inside a modal-window and have it open when clicking on a link. However, I want the appropriate tab to open directly upon click. For example, if I click on "travel", I want the t ...

Sending values from buttons to different Django templates/views

Initially, the user provides a CSV file. (Template File:) <form method="post" action="{% url 'rowcol' %}" enctype="multipart/form-data"> {% csrf_token %} <input type="file" name="file" accept=".csv"> <button type="sub ...

LFT Etica font showcases digits with varying heights

In the project I am working on, we are required to use the font LFT Etica. However, there is an issue with the height of certain digits when displayed. Specifically, some numbers appear taller than others. Is there a way to make all digits have equal heigh ...

"Implementing a dynamic image thumbnail list with adjustable opacity effects and the ability to add or remove classes

I found a script on another post, but it's not working correctly in my implementation. Everything is functioning properly except that the "selected" class is not being stripped, causing the thumbnails to remain highlighted after being clicked. Here is ...

-=:Heading with a decorative vertical line=:-

Currently, I am working on a CSS project that requires page titles (headings) to be centered with horizontal lines positioned vertically on both sides. Additionally, there is a background image on the page which means the title's background must be tr ...

At times, Firefox may display HTML incorrectly due to rendering issues

I recently created a CSS menu that worked perfectly on all browsers during my testing with pure HTML/CSS. However, when we integrated the code into our development environment running on cakePHP, we encountered a peculiar bug in Firefox (3.5.2) specificall ...

The parent DIV has a height of 0px, yet the child element is still visible

As I explored options for creating a navbar drop down menu with an accordion effect, I stumbled upon this informative YouTube video. The tutorial demonstrated the use of a custom component and manipulation of the max-height property of the card-content ele ...

Activate the saturation toggle when a key is pressed in JavaScript

I am trying to modify a script that currently toggles the value of a variable when a key is pressed and then lifted. Instead of changing the variable value, I would like to adjust the saturation of the screen based on key presses and releases. How can I ac ...

Ensure the video frame stretches to fit the full width

I am struggling to make my video frame fill the entire width of the screen. Even though I have tried using CSS, the video container does not stretch to the full width as expected. https://i.sstatic.net/kxyE0.png How can I ensure that the video plays acro ...

Avoid clicking in areas outside the perimeter of the blue circle in the SVG

Is there a way to restrict clicking outside the blue circle? The goal is to only allow opening by clicking on the svg itself, This includes everything, even white space inside the svg. How can this be achieved in the code? The current behavior is that ...

Develop an onclick function with an href attribute

I am interested in transforming links into AJAX versions whenever possible. To achieve this, I plan to implement a function called replaceLinks that will add an onClick handler to each link on the page and trigger ajaxPageWSM(href). This is what I currentl ...

Using a PHP variable to trigger the jQuery .show() function

I'm attempting to trigger jQuery .show() events based on PHP variables. Below is my PHP code (retrieved from a form submission on another page): $last_pic_displayed = trim($_POST["last_pic_displayed"]); if (strlen($last_pic_displayed) <= ...

Is it possible to arrange elements in CSS based on their attribute values?

I need to arrange a list of images in ascending order based on the index attribute using CSS. The challenge is that the index values will change dynamically and new images will be added through Ajax, so I want them to automatically update in the correct or ...