Utilizing absolute positioning with a rotated div element

The situation I am facing involves a parent div and child div. The child div is positioned absolutely with respect to the parent div and has been rotated by an angle of 90 degrees. Due to the fact that the origin is located at the center of the child div (where the two diagonals meet), the child div appears to be shifted outside the container.

To provide further clarification on this issue, I have included two sets of parent-child outputs in the fiddle link provided below. Fiddle Link

Within the fiddle, there is an event added which resets the position of the child elements to the top-left corner of their respective parents. Here is the code snippet for resetting:

JQuery Code:

        $("#link").click(function(){
    
            //Child 1 requires top=45 and left=-45
            $("#c1").css({"top":"45px", "left":"-45px"});
    
            //However, Child 2 needs top=30 and left=-30
            $("#c2").css({"top":"25px", "left":"-25px"});
    
            //As shown above, different values are assigned to top and left positions for child divs of varying dimensions. Is there a specific relation or formula to determine these values?
        });
    

I am seeking to understand the relationship between the rotation angle, width, height, and coordinate system of the child div. If such information exists, kindly share it with me. Thank you.

Answer №1

To achieve the desired alignment without relying on Javascript, try utilizing the following style:

transform: rotate(90deg) translate(0,-100%);
transform-origin: top left;

This approach will help you attain the requested visual positioning.

Answer №2

For your convenience, you can utilize the subsequent equation:

top = (child_div.width - child_div.height)/2
left = (child_div.height - child_div.width)/2 = -top

Let's analyze the formula for top. The midpoint of the child div is (child_div.height/2) units below the top of the parent's div. Following rotation of the child div, its top becomes (child_div.width/2) units above its center. Consequently, its top position relative to the parent's top is

(child_div.width/2) - (child_div.width/2)
units. By simplifying this expression, we arrive at the aforementioned formula. Similarly, the formula for left can be determined.

If you plan on adjusting their positions using Javascript, you can calculate these values dynamically and apply them accordingly.

Answer №3

When dealing with a child div within a parent container whose position is not static, it has its own coordinate system. If you need to rotate the div around a specific point that is not the origin of its coordinate system, follow these steps:

  1. Subtract the vector of the rotation point from the position vector of the div (to ensure the rotation center becomes the origin).

  2. Rotate the div.

  3. Undo step one by adding the "rotation-center-vector" to the div's position.

These steps are a general approach and a fundamental concept applicable in transformation matrices. In essence, rotating around a particular point always involves these three essential steps (similarly for scaling objects).

Best wishes...

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

Using Vue.js to pass an image URL as a prop for CSS animation by converting it to a CSS variable

Need help with a component that features a hover animation displaying 4 rotating images: animation: changeImg-1 2.5s linear infinite; @keyframes changeImg-1 { 0%, 100% { background-image: url('images/wel1.png'); } 25% { background-image: ur ...

Error message: "Unassigned value in knockout.js"

Here is my code snippet for binding to a textbox: var CategoryViewModel = { categoryModel: ko.observable({ categoryId: ko.observable(), categoryName: ko.observable(), active: ko.observable() }), GetCategoryById: functio ...

Sending emails to multiple recipients with different content using Nodemailer

I have been working on a method to send emails to multiple recipients while also passing the user attribute, which contains the name of the recipient, to the html template. (I AM UTILIZING NODEMAILER as a nodejs module) My current code looks like this: S ...

Utilize Jquery to insert new text into a textbox and then organize the contents of the textbox using Jquery

Currently, I am utilizing the ASP.NET asp:checkboxlist control which consists of 36 listitems, displaying 36 checkboxes. The HTML representation resembles a table format. My goal is to dynamically add checked items in this div in a sorted manner. Additiona ...

Tips on identifying the method to import a module

Currently, I am including modules into my Node project like this: import * as name from "moduleName"; Instead of the old way: var name = require("moduleName"); In the past, we used require in Node projects. My question is, is there a difference in ...

What could be causing the error "SyntaxError: Expected token ']' to appear" to pop up?

Recently, I have been working on implementing a 'night mode' feature for a website. However, every time I attempt to execute the script, an error message pops up: "SyntaxError: Expected token ']'" on line 9. The problematic line in ...

Maintaining a sticky footer that remains visible throughout the content as the screen is resized

For more information, please visit my website at I attempted to implement a "sticky footer" technique following the steps outlined in this tutorial (http://ryanfait.com/sticky-footer/) Unfortunately, the sticky footer does not function properly when resi ...

A guide to modifying the color of the Menu component in material-ui

Currently, I am utilizing the Menu component from material-ui and facing an issue while trying to modify the background color. The problem arises when I attempt to apply a color to the Menu, as it ends up altering the entire page's background once the ...

Navigate through information within designated boundaries

In order to achieve vertical scrolling of a sidebar div and its content (3 links) between two points, I have utilized the CSS property position:fixed; top: 100px. While this setup works well initially by positioning the sidebar 100px down from the top and ...

Webpack bundling only a singular Typescript file rather than all of its dependencies

I'm currently facing a challenge while attempting to consolidate all the files in my Typescript project, along with their dependencies from node_modules, into a single file using Webpack. Despite trying multiple options, it seems that only the entry f ...

Tips for dynamically loading images in React with a Collage component

It appears that all the examples I've come across are static in terms of loading images. While the code works, it does not display the divider <div> tag as expected. Instead, the results (images) are shown stacked in the first item of the Collag ...

The information in the table is spilling out beyond the boundaries

I'm facing an issue with my bootstrap table where if a field value is too long, the table extends past the container length. I tried using responsive-table to prevent this, but then a scroll bar appears at the bottom, making it difficult to view the d ...

What is the best way to increment the stock number based on the quantity of items in the cart using Next.js

I am in the process of developing a shop system where customers can order items and save them to the database. I have encountered an issue where I need to calculate the remaining stock after deducting the quantity of items ordered. My API is responsible f ...

Different method for adding child elements to the DOM

When creating a DOM element, I am following this process: var imgEle = document.createElement('img');     imgEle.src = imgURL;             x.appendChild(imgEle); Instead of appending the last line which creates multiple img elements ev ...

Is there a way to manipulate a website's HTML on my local machine using code?

I am currently working on a project to create a program that can scan through a website and censor inappropriate language. While I have been able to manually edit the text using Chrome Dev tools, I am unsure of how to automate this process with code. I ha ...

Encountering net::ERR_SSL_PROTOCOL_ERROR while trying to access the Nextjs dev server using an IP address

Upon running my Nextjs project with npm run dev, I encountered an issue while accessing the app via http://localhost:3000. The resources were loaded using the HTTP protocol, such as http://localhost:3000/js/dmak_normal.js. However, when attempting to acce ...

Enhancing Angular Directives with Dynamic Templates upon Data Loading

I am facing an issue with a directive that is receiving data from an api call. While the directive itself functions properly, the problem seems to be occurring because the directive loads before the api call is complete. As a result, instead of the expecte ...

Changing the size of icons in an Alert using Material UI with React

Recently, Material UI unveiled the new 'Alert' component. Everything seems to be working well, except for the fact that I can't find a way to adjust the size of the icon. Here is my code snippet: <Snackbar open={true}> <Alert ...

extract specific data from JSON using JavaScript

Dealing with JSON data can be tricky, especially when working with multiple sets of information to choose from. When I inspect the data in my JavaScript code using console.log(contacts.data["all"]);, I can see the returned objects clearly. Here's a ...

Classic ASP offers a feature that allows users to select all checkboxes at once

I'm looking to create a functionality where there is a 'Select all' checkbox along with individual checkboxes for each database record. Is it possible to use JavaScript to ensure that when the 'Select all' checkbox is checked, all ...