What strategies can be used to accurately position a rotated image within a rotated div?

Below is the code snippet I am using:

<div class="container">
   <img class="img"/>
   <div class="underlay"></div>
</div>

The image is a picture of some rotated text by 10 degrees, although the actual image is a standard rectangle. You can view the image here: https://i.sstatic.net/tI1Sg.png

Here is the accompanying CSS:

.container {
   position: relative;
   width: 100%;
   height: 100%;
}

.img {
   height: 20vh;
   width: auto;
   position: absolute;
   top: 5vh;
   left: 5vh;
}

.underlay {
  position: absolute;
  top: 5vh;
  left: 5vh;
  background-color: rgba(0, 0, 0, .5);
  width: 100%;
  height: 15vh;
  transform: rotate(-10deg);
}

Here is what the layout looks like with the above code:

https://i.sstatic.net/VVpmn.png

I am facing an issue in positioning the underlay element perfectly beneath the image. I believe there might be a trigonometry solution for this, but I am uncertain of where to begin. I can manually adjust the underlay to align at certain widths, but resizing the viewport causes it to fall out of alignment, which is puzzling since it is all defined using vh. I suspect the issue may lie in the left positioning, but I am unsure.

Answer №1

In case someone is seeking a solution, I recently discovered that adding transform-origin: top left; to the underlay did the trick.

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

Attempting to use tabs within a tab does not function correctly

Visit the CodePen link here This is the codepen example. <div class="row"> <div class="col s12"> <ul class="tabs2"> <li class="tab col s3"><a href="#test1">Test 1</a></li> <li class=" ...

Calculating values within dynamically generated elements requires utilizing JavaScript to target and extract the

I am working on creating input fields inside an HTML table using Vue.js. On click of a button, I want to perform some calculations based on the input values. However, it seems that the calculations are not happening as desired. What I have attempted so fa ...

Adjust the visual presentation based on the chosen option at the top of a column JQchart

Can anyone offer assistance with creating three radio button fields that will display yearly, monthly, or weekly data on a column chart? I have searched through numerous examples in JQchart but haven't found one for this specific scenario. Are there o ...

Is there a way to disable auto rotation on a website when accessed from a mobile phone?

My current solution involves the following code: @media (max-height: 480px) and (min-width: 480px) and (max-width: 600px) { html{ -webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg); -ms-transform: rotate(- ...

Updating contact list to a database table structure

Currently, my code displays the data in address books, but I would like it to be shown in a table instead. I attempted to use document.write to create the table, but I'm unsure how to populate the table with the data rather than the address book forma ...

Why is it that the LESS compiler fails to recognize classes that are created by a zero iterator

When working with my CSS sheet, I found that I needed to reset some Bootstrap 3 preset values in order to achieve my desired style. To do this, I started using utility classes like .m-b-0 which sets margin-bottom:0px. However, I encountered an issue with ...

Is it possible to have the cursor rotate or animate by 45 degrees when clicked

Do you know how to create a unique custom cursor using CSS? Say, for example, we have this code: cursor: url(images/cursor.png) 15 15, auto; Now, what if we wanted to take it up a notch and make the cursor rotate -45 degrees when clicked, and then revert ...

Adding or removing a class on hover in Tailwind: The simple way!

Is there a way to change the class upon hovering an object using tailwind classes? <span className='group inline-block relative'> Hello <span className='hidden absolute -top-16 left-16 group-hover:inline-block'>&#45; ...

"What is the best way to access and extract data from a nested json file on an

I've been struggling with this issue for weeks, scouring the Internet for a solution without success. How can I extract and display the year name and course name from my .json file? Do I need to link career.id and year.id to display career year cours ...

Removing CSS from an HTML document using Gulp

My Web App is built using Angular and I encountered an issue with Gulp. Every time I add a new custom CSS line to my HTML file and run Gulp, it automatically removes that line from the file. <!--MATERILIZE CORE CSS--> <link h ...

Bootstrap Tags Input - the tagsinput does not clear values when removed

I am attempting to manually remove the input value from bootstrap-tags-input when the x button is clicked, but the values are not changing in either the array or the inputs. This is the code I have tried: $('input').tagsinput({ allowDuplica ...

How can I make my iPad switch to landscape mode in my specific situation?

I'm attempting to trigger the landscape mode on an iPad web browser, including the address bar and tabs, when a user clicks a link. Here's what I currently have: <div> <a ng-click="click me()" href="www.cnn.com">Click me</a&g ...

Create a stunning wave CSS effect using the :after and :before pseudo-elements

Hey there! I'm currently experimenting with creating a wave effect using CSS, specifically utilizing 'before' and 'after'. However, I've encountered an issue where I can't seem to position my waves correctly at the top a ...

Obtain array buffer from NodeJS to capture frames from a video file

My current goal is to extract frames from a video in node without relying on a video tag. I am utilizing openvg-canvas for this task, which allows me to print images and use the canvas API functionalities successfully. The challenge lies in the fact that ...

Enhancing an element by incorporating animation into its active state

For my web assignment, I have received specific instructions regarding a section of the webpage involving five dice: There are five dice in play. When the webpage loads, a three-dimensional image of each dice must be displayed. Upon clicking a dice, it ...

"Submitting an HTML form and displaying the response without redirecting to a

I created a basic portlet for Liferay that includes a form for inputting parameters to send to an external webservice. However, when I submit the form, I am redirected to the URL of the webservice. Is there a method to prevent this redirection and instead ...

Issue with Bootstrap jQuery dynamic table edit/delete/view button functionality not functioning as expected

Could really use some assistance with this issue. I have a DataTable that includes a button in the last column which offers options like Edit/Delete/View. When clicked, it should delete the entire row. However, I'm struggling to access the current ele ...

What is the best way to implement a modal that can toggle dark mode using the Konami code, with the added functionality of a close button?

Recently, I attempted to create a Modal window that would activate when the Konami code (↑↑↓↓←→←→BA) is typed. As someone new to JavaScript, I'm still learning and open to feedback. While I have the coding part figured out, I need assi ...

Navigating to a different page in the app following a document update: a step-by-step guide

I am facing an issue with a page where I am trying to print a specific DIV using the script below... function printReceiptDiv() { var divElements; if (vm.isDLR) { divElements = document.getElementById("DLRreportCont ...

I'm new to React and curious - why do we need to use ReactDOM.render() to update HTML instead of just writing the HTML directly?

I am new to learning react and am struggling to grasp why we need to utilize reactDOM.render() instead of just writing plain HTML code. ...