Adjusting the height and width of the carousel based on whether the photo is in landscape or portrait orientation

I have been using the code snippet below to make my landscape (4:3) photos fit into a carousel. However, I am now looking to dynamically change the width and height of the .carousel based on whether the photo is landscape or portrait orientation. How can I achieve this?

html {
  height: 100%;
  width: 100%;
}

body {
  height: 100%;
  width: 100%;
  display: block;
}

.carousel {
  /* The percentages specified here are for a 4:3 landscape photo (1600x1200) */
  height: 60%;
  width: 70%;
}

/* For portrait photos, I need to set height: 70%; and width: 60% */

Should I consider adding a class to the carousel-item indicating whether it's a landscape or portrait photo?

Answer №1

Create two classes, one for portrait orientation and one for landscape orientation. When an image is loaded or the size of the image is determined, check if it is in portrait or landscape mode, then add the corresponding class to the image or carousel container.

// An array containing a list of images
// You can store this list in a separate js file 
var imagesArray = ["https://lorempixel.com/300/500/animals/1", "https://lorempixel.com/300/500/animals/2", "https://lorempixel.com/500/300/animals/1","https://lorempixel.com/500/300/animals/2","https://lorempixel.com/500/300/city/1","https://lorempixel.com/300/500/city/2"];

// Function to display image based on user action 
function displayImage(direction, isURL) {
  // Implementation omitted for brevity    
}

// Event handler for image load
function imageLoadHandler(event) {
  // Implementation omitted for brevity   
}

function updateNavigationLabel() {
   // Implementation omitted for brevity  
}

window.addEventListener("DOMContentLoaded", function() {
  var element = document.getElementById("myImage");
  var button = document.getElementById("button");
  var carousel = document.getElementById("myCarousel");
  
  // Listen for when an image loads
  element.addEventListener("load", imageLoadHandler);
  // Listen for when the user clicks on the random button
  button.addEventListener("click", function() {
    displayImage('random')
  });

  displayImage("https://lorempixel.com/300/500/animals/2", true);

});
.landscape {
  height: 60%;
  width: 70%;
  outline:2px solid blue;
}

.portrait {
  height: 70%;
  width: 60%;
  outline:2px solid purple;
}

#myCarousel {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

#myImage {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  outline: 1px dashed red;
  height: 100%;
  width: 100%;
  object-fit: contain;
}

#button {
  position: fixed;
  right: 10px;
  top: 50px;
}

#loadingLabel {
  position: absolute;
  bottom: -20px;
  left: 50%;
  transform: translateX(-50%);
  font: 10px sans-serif;
  white-space: nowrap;
}

#navigationLabel {
  font: 10px sans-serif;
}

#navigation {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  font: 10px sans-serif;
}
<script src="myimages.js"></script>

<div id="myCarousel" class="landscape">
    <img id="myImage">
<label id="loadingLabel"></label>
</div>

<button id="button">random</button>

<div id="navigation">
  <button id="prev" onclick="displayImage('previous')">prev</button>
  <label id="navigationLabel"></label>
  <button id="next" onclick="displayImage('next')">next</button>
</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

The responsiveness of CSS isn't functioning as expected

Struggling to achieve responsiveness on my page, facing issues with a white border at the top and image fitting problems. Any insights on what might be causing this? CSS: .cb-slideshow, .cb-slideshow:after { position: fixed; width: 100%; hei ...

CSS- The ultimate guide to perfectly centering a webpage without the need for extra margins!

Our SharePoint 2013 generated web page has a width of 1024. The main content div is styled as follows: #container_master { width:1024px !important; margin-left: auto !important; margin-right: auto !important; background-color:#FFF !impor ...

Position the Button in the Center between Borders using CSS and MUI

I'm trying to center a button just like the one shown in the image below. It needs to be perfectly aligned with the lines. What is the best way to achieve this? Check out the example on Codesandbox https://i.sstatic.net/dnhKx.png <MainContainer&g ...

Loop through a vector or data frame in RCurl's postForm() function to input values into a text field form

My objective is to transmit each individual "finput" observation into an HTML form under the name "text". The process works smoothly when dealing with a single observation. However, I am encountering difficulties while attempting to iterate through the ve ...

Switching images between mobile and desktop view in responsive mode is a useful feature for optimizing

Within a specific folder structure, I have a collection of images designated for both desktop and mobile displays: img: img-1.png img-2.png img-3.png img-4.png img-5.png img-6.png img/mobile: img-1.png img-2.png ...

Styling a Razor view using inline CSS and responsive media queries

I need to add some simple inline styling for print purposes to an .cshtml page I'm working on. I initially tried using the @media print media query, but it is causing issues with my cshtml page. Is there a workaround for this problem? The culprit see ...

Collapse the columns and set them to float on the left side

My current setup might not be the most visually appealing, but it gets the job done. I have several col-md-4 tags in place. The issue arises when the height of the first tag is larger than the ones next to it, causing the subsequent tag to appear in the mi ...

Passing both the event and a local Vue variable to a bound function in a idiomatic manner

I had been grappling with the problem of accessing both a v-for member and the original event data within a bound function on an event in vue.js. Despite my efforts, I couldn't seem to find a solution in the documentation. Here is what I was aiming f ...

The issue of uploading images with Django is causing a problem for users

I am developing a user personal picture uploading system using the Django framework. The project is named SGV and includes an app called Success to manage user requests. Upon logging in, users are directed to a webpage titled userhome.html where they can u ...

Problem with Silverstripe: Replicating Site configuration or settings menu

I have duplicated the Settings menu (siteConfig) folder in order to create a new CMS menu for inputting company information. The functionality of the menu is working correctly, however, the CSS styling is not loading properly. Image: https://i.stack.imgur ...

Steps for showcasing the value received from the server on the browser console

I'm having some trouble retrieving a value from a server using the httpclient get method in Angular. Despite my efforts, I can't seem to see it displayed on either the console or the webpage. Can anyone help me figure out what's going wrong? ...

I am facing an issue with my navbar image not aligning properly when using the

I have created a navbar using Bootstrap with the following code: <!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Complete Bootstrap 4 Webs ...

The hover effect's color isn't functioning as intended

Something very peculiar... I'm looking to modify the text color and background color of the links when they are hovered over. Here's the code snippet: CSS: #link-menu a { color:white; display:block; height:100%; width: 100%; ...

When a button is triggered, it should initiate a click event on an input in TypeScript

I have created a color picker that is visible on a page. When clicked, it displays a dropdown menu of colors for selection. However, my objective is to hide the color picker initially and only reveal it when a specific button is clicked. This way, the dro ...

Is it feasible to send a GET form to two separate views using two buttons?

On one view, I have a form that functions perfectly. http://myurl.com/myapp/filter_objects/?risk__worktask=1&craft=3 In another view, I need to export the filtered list to a PDF. Currently, I store the results in session and access the list from ther ...

Utilizing the <style scoped> feature within my Angular template

When adding CSS styles in the specified htm file's templateUrl while loading a directive, I wonder if this is a bad idea. Will it be repeated every time the template is instantiated on the rendered page, or does Angular handle this differently? By usi ...

Manipulating dynamic form inputs: utilizing JQuery to assign and update values with Materialize autocomplete

In the process of creating an invoice page that allows users to search for a product, I have implemented auto-fill functionality where details are automatically populated. Values retrieved from the database are used to populate the Materialize dropdown an ...

In the Bootstrap grid, the first two rows are tightly packed with no empty space between them

Currently working on coding a footer with a grid/table using Bootstrap 4. However, encountering an issue where there's no spacing between the first and second rows in the grid. Oddly enough, there is spacing between all other columns. Can anyone provi ...

Using JavaScript to only update the CSS background image when the source image changes

Is it feasible to dynamically update the CSS image referenced by a provided URL in "background-image: \"<some-url>\" using JavaScript, but only when the source image is modified on the server? The idea is to cache the image a ...

Designing websites and Creating Visual Content

I have been working in IT for quite some time now, but recently I have started to delve more into web development. My main focus is on HTML 5, CSS 3, JavaScript, jQuery, and responsive design. However, one aspect that I always struggle with is images. I am ...