Spin picture and optimize margins

I need help rotating an image by 90 degrees that is positioned between two boxes. The issue I am facing is that the rotated picture overlaps with the two boxes in this scenario.

Example 1: Incorrect formatting

CSS:

.box{
    height:50px;
    width:200px;
    background-color:blue;
}

HTML:

<div class='box'>Top</div>
    <img style='transform: rotate(90deg);' src='https://cdn.pixabay.com/photo/2017/12/10/20/56/feather-3010848_960_720.jpg' width='200px'>
<div class='box'>Bottom</div>

Example 2: Preferred formatting

There is a workaround available, but it cannot be used universally as "image-orientation" may not be supported by all browsers, especially on mobile devices:

<div class='box'>Top</div>
    <img style='image-orientation: 90deg;' src='https://cdn.pixabay.com/photo/2017/12/10/20/56/feather-3010848_960_720.jpg' width='200px'>
<div class='box'>Bottom</div>

Are there any other solutions to prevent a rotated image from overlapping other elements? Or is there an alternative for image-orientation that is universally supported by all browsers?

Answer №1

If you want to ensure that the image stays within a specific width while maintaining its aspect ratio, you can achieve this by surrounding the image with a div tag and using CSS to set up a 1:1 aspect ratio based on the maximum width of the container. By absolutely positioning the image inside the div, you can rotate it around a central axis point. Here is an example of the code:

<style type="text/css">
.box{
    height:50px;
    width:200px;
    background-color:blue;
}
.box--image {     
    position:relative;    
    max-width:200px; 
    outline:solid 1px red;
}

.box--image:before {
    content:"";
    display:block; 
    padding-top:100%;
}

.box--image img {    
    left:50%;
    position:absolute;          
    top:50%;
    transform:rotate(90deg) translate(-50%,-50%);
    transform-origin:top left;
    width:200px;
}

<div class="box">Top</div>
    <div class="box--image"><img src="https://cdn.pixabay.com/photo/2017/12/10/20/56/feather-3010848_960_720.jpg" /></div>
<div class="box">Bottom</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

What's the best way to ensure that my image remains perfectly centered at the point where the background colors of my

html <body> <header> <div class="indexHeaderWall" id="headerLeftWall"> </div> <img id="profilePic" src="icons/myPicture.jpg" alt="Picture of Daniel Campo ...

Adjust the color of the font within a div element when hovering over it

I've been attempting to modify the text color and add an underline when a user hovers over it. Despite trying various methods, I haven't been successful. I scoured the internet for a solution but couldn't find one that met my specific requi ...

Decrease the size of the "subscribe" button

Our "subscribe" button is currently displayed like this: https://i.sstatic.net/toiOb.png However, we are looking to reduce the extra width and have it appear more like this: https://i.sstatic.net/NaaYJ.png We are using the same code for both versions: ...

Creating an HTML element using jQuery isn't just about designing; it

When creating HTML elements using an Ajax call from the server-side, it can be challenging to align them properly on a responsive web page. Below is an example of how elements are generated with an Ajax call: var html = ""; $.ajax({ type: " ...

Executing a jQuery script on various elements of identical types containing different content (sizes)

I am currently working on a jQuery script that will center my images based on the text block next to them. Since I am using foundation 5, I have to use a jQuery script to override the CSS instead of using vertical-align: middle. The script looks like thi ...

Using bootstrap's center-block class, you can easily center

I'm attempting to center the content of a form. <div class="login-container"> <div class="center-block"> <form action="/joinChart" method="get"> <input name="username" id="username" type="text"><br><br> ...

"Addressing the issue of white space between the status bar and header in Cordova

I have tried numerous solutions but I am still unable to remove the white space between the header and status bar, as shown in the attached image. https://i.sstatic.net/a9xTG.jpg My current setup involves using Intel XDK version 3987 with Cordova plugin ...

Include various categories into the div containers of the listed items within the query outcomes

Is there a way to customize div styles based on query results? I want to differentiate the styles of divs in the result list based on their content. For example: I'd like bird names in the result list to have different div styles compared to other a ...

Styling the tab view in PrimeFaces

I am currently working with the tab view feature in primefaces and I have encountered a couple of issues. 1) When using Internet Explorer, the tabs are displayed vertically instead of horizontally. However, it works fine in Firefox. FireFox : Internet E ...

Bootstrap tab content getting shifted downwards

Having an issue with the Tab plugin in Bootstrap on a particular page. The tab body is being pushed down 400px below the actual tabs. This behavior is only occurring on this specific page, while most other pages using the same plugin are functioning fine. ...

Dynamic background image that fills the entire webpage, adjusts to different screen sizes, and changes randomly

Currently, I am working on designing a web page with a dynamic background image that adjusts responsively in the browser without distortion. The challenge is to randomly select an image from an array using jQuery. Unfortunately, my knowledge of jQuery is ...

Is there a way to disable a JavaScript hover effect when the mouse moves away?

I came across this JavaScript code that swaps the background of a parent div when clicking on a link in a child div. However, I noticed that the hover state remains even after moving the mouse out of the link. Can someone help me modify the code so that ...

Begin an unnumbered hierarchical list commencing at 1.2.1

I am trying to create a nested unordered list with specific numbering. My goal is for the list to start at "1.2.1, 1.2.2, etc." Please refer to the attached fiddle for reference. The desired outcome is shown in the following image: https://i.stack.imgur ...

Navigate to the top of the page using jQuery

Attempting to implement a simple "scroll jump to target" functionality. I've managed to set it up for all parts except the "scroll to top". The jumping works based on the tag's id, so it navigates well everywhere else, but not to the very top. He ...

SVG padding will only take effect if two or more sides are specified

What is the reason behind an SVG requiring at least 2 areas to be padded for padding to apply? For instance, applying padding-top: 50px to an svg element will have no effect. However, adding padding-top: 50px; padding-left: 1px will result in the padding ...

How to center multiple divs in a row using HTML and CSS

I've been attempting to achieve a pretty basic task, but unfortunately my efforts have fallen short thus far. I have an image and a title that need to be centered within their parent div element. However, I'm struggling to find the proper way to ...

How can I modify the color of a div when a validation error occurs?

I have recently completed a contact form with validation using the constraint validation api. Although the files are functioning as intended, I am curious if there is a method to make the error boxes turn red when an error occurs and white (or hidden) when ...

Is there a way to align text in the middle of a full-width jumbotron using bootstrap 4 while also restricting the maximum width of only the div or p container?

Trying to master the art of using Bootstrap 4 has been quite a journey. At first, everything seemed to work smoothly as I attempted to horizontally center some text within a full-sized jumbotron. However, things took an unexpected turn when I decided to im ...

What is the best way to adjust the z-index of a dropdown auto complete to appear above a background image?

Is there a way to adjust the z-index of the drop-down autocomplete box so that it appears above a background image? Take a look at this screenshot: The four images below are background images styled with sprite CSS. The area box represents the drop-down ...

I need to display 5 columns in a parent component, each with its own unique icon. How can I conditionally render them in a React component?

Creating a parent component to reuse multiple times can be very useful. In my case, I have included 5 different icons in each instance of the component, all taken from hero icons. I have set up an object with unique ids for each child component, but I am ...