Different CSS Transform values are required based on the length of the text

I have created a demo that showcases 5 floating <div>s with rotated text of various lengths. Is there a way to implement a CSS class that can centrally align all text, regardless of length? Currently, I find myself creating a new class for each character length in my stylesheet, which could become cumbersome. Additionally, adjusting the size of the enclosing <div> disrupts the offsets.

Although I intend to apply these classes to divs through jQuery, ensuring cross-browser compatibility still requires setting up individual classes.

.transform3 {
    -webkit-transform-origin: 65% 100%;
    -moz-transform-origin: 65% 100%;
    -ms-transform-origin: 65% 100%;
    -o-transform-origin: 65% 100%;
    transform-origin: 65% 100%;    
}
.transform4 {
    -webkit-transform-origin: 70% 110%;
    -moz-transform-origin: 70% 110%;
    -ms-transform-origin: 70% 110%;
    -o-transform-origin: 70% 110%;
    transform-origin: 70% 110%;
}
.transform5 {
    -webkit-transform-origin: 80% 120%;
    -moz-transform-origin: 80% 120%;
    -ms-transform-origin: 80% 120%;
    -o-transform-origin: 80% 120%;
    transform-origin: 80% 120%;
}
.transform6 {
    -webkit-transform-origin: 85% 136%;
    -moz-transform-origin: 85% 136%;
    -ms-transform-origin: 85% 136%;
    -o-transform-origin: 85% 136%;
    transform-origin: 85% 136%;
}
.transform7 {
    -webkit-transform-origin: 90% 150%;
    -moz-transform-origin: 90% 150%;
    -ms-transform-origin: 90% 150%;
    -o-transform-origin: 90% 150%;
    transform-origin: 90% 150%;
}

Note: The offset values were estimated visually.


Update

While I initially hoped to handle this with a stylesheet, it seems I may need to calculate the CSS transformations in JavaScript instead.

I have now developed a different demo to illustrate dynamic transformations. In this demo, users can adjust the font-size within the .v_text class, and if the text length remains within the height of the .v_text_wrapper, the text should align vertically at the center. However, please be aware that adjustments to the magicOffset value may be necessary.

Interestingly, this method doesn't seem to work on iOS... Thank you @Dinesh Kumar DJ

Answer №1

Check out this link for a solution: http://codepen.io/teodragovic/pen/fgekh

#output1,
#output2{
  margin: 50px;
  display: inline-block;
}

.rotate {
  //delete this line if using javascript
  transform: translateY(150px) rotate(-90deg);
}

.v_text_wrapper {
  float: left;
  width: 100px;
  height: 150px;
  background: #AAA;
  border: solid #222 1px;
  padding: 0;
  margin: 0;
  position: relative;
}

#output2 .v_text_wrapper {
  height: 170px;
}

.v_text {
  color: #444;
  font-family: monospace;
  font-weight: bold;
  font-size: 1em;

  width: 150px; //delete this line if using javascript
  height: 100px;
  transform-origin: top left;
  text-align: center;
  position: absolute;
  display: table;
}

p {
  display: table-cell;
  vertical-align: middle;
}

No need for JavaScript or dynamic offsetting. You can match the dimensions of the text-containing divs to those of the wrappers, rotate them around any corner you prefer, and then position them back inside the wrapper using translateY (assuming the wrapper's dimensions are fixed).

To vertically center multi-lined text, I included an additional <p> element and used the method outlined here: http://css-tricks.com/vertically-center-multi-lined-text/

Edit: I made updates to the code pen in case the wrapper div changes size. Since the divs are rotated, their width becomes height and vice versa, so the child div must have dimensions 150x100 if the wrapper is 100x150.

$('.v_text_wrapper').each(function(){
  var x = $(this).css("height");
  var text = $(this).children('.v_text');
  text.css({"transform":"translateY("+x+") rotate(-90deg)", "width":x});
});

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

Getting the data stored in variables from a JavaScript file into Python

Currently, I am facing a challenge with a JavaScript file where I need to extract data in a specific format. My goal is to make the data readable and subscriptable similar to a Python dictionary (like dict["key/value"]). When I try opening the file, it re ...

The current URL in the browser does not change

My HTML form has a JavaScript function that involves handling the window.location.href. I successfully remove two unwanted query string parameters from the source using this function, as shown in the first and second alert screenshots. However, I encounte ...

Calendar control failing to trigger the OnTextChanged event in the textbox

I have integrated a calendar control from the internet for phone use, where users can scroll up and down to select a date and time before clicking 'Confirm' or 'Cancel'. However, I am not familiar with JavaScript. The relevant JavaScrip ...

Encountered an error when creating my own AngularJS module: Unable to instantiate

Attempting to dive into TypeScript and AngularJS, I encountered a perplexing error after following a tutorial for just a few lines. It appears that there may be an issue with my mydModule? angular.js:68 Uncaught Error: [$injector:modulerr] Failed to inst ...

Struggling to display Three.js ply file on screen

I'm having trouble displaying my ply file using the three.js webgl_loader_ply example. Even though I can view the object in MeshLab when I open the ply file, it doesn't show up in the three.js example. I've tried various adjustments like zoo ...

Exploring the dynamic loading of components within routes

Just starting out with Vue and experimenting with vue-router, I'm trying my hand at dynamically loading components without relying on additional libraries like webpack. So far, I've set up an index page and a router. Upon initial page load, I not ...

Utilizing media queries in AMP for optimizing mobile website performance

Recently, I delved into the realm of utilizing AMP for my blog. After perusing the documentation and observing examples of its application, I must say that anything enhancing performance is a welcome addition to me. Therefore, I am keen on incorporating i ...

Endless loop in React Native with an array of objects using the useEffect hook

For the current project I am working on, I am facing the challenge of retrieving selected items from a Flatlist and passing them to the parent component. To tackle this issue, I have initialized a local state as follows: const [myState, setMyState] = useS ...

Choose all the elements that have both of these particular classes

Let's say I have buttons that have multiple classes: <button class="template-1-box template-1 t-premium active-template"></button> <button class="template-1-box template-2 t-premium"></button> <button cla ...

Pause before submitting another request using JQuery

I'm currently working with the Spotify Web API and my goal is to update the HTML display with the name of the song once I click the next song button. However, the issue I'm facing is that when I click the button, it updates the song title to the ...

Navigating through the various child objects within a JSON structure

As I traverse through a moderately complex JSON object, I gather and store all the values once I reach the end of the recursive loop Here is an example of the object: "if": { "and": { "or": { "compare": [ { ...

Automatically calculate the quantity of pie charts to showcase and present each one individually on a CanvasJS-generated page

I am looking to create multiple pie charts dynamically and display them together on a webpage. The number of pie charts needed will be determined by the elements in a JSON object. Here is an example of how the data will be structured: [ { "id" : "12 ...

Tips for converting an entire column along the vertical axis to a different language

Looking for a solution with my grid view where I want to translate the items of the second column on the y axis, as shown in the image. I attempted to use the translateY() function, but it resulted in the need to scroll in order to see the translated items ...

Having trouble accessing the data I'm setting in a separate component using the Context API

Currently working on my own project and I've encountered a problem while using the Context API. It's my first time using it. The issue I'm facing is that I can't seem to console.log the data I'm setting. I'm trying to create ...

JavaScript not populating data in ASP TextBox or Input field

<asp:TextBox ID="DataBus" runat="server"></asp:TextBox> This is a text box element that is currently not being populated with any data from JavaScript. $("#DataBus").val('hi'); Even after trying the HTML code below, the issue persi ...

The scripts do not allow the image to be resized

Struggling to resize an image while implementing a hover effect with css and javascript. The code I have so far is: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equ ...

Looking to enhance this JavaScript code for updating an object's description - any tips on optimization?

I have a list of group items, with 7 items per column. Each item has an id, such as #a1, #b2, etc. I am using the Simple Slider plugin to display two columns on each slide: one with the list and one with the description #descr. I am looking for a more effi ...

Tips for sending information from a controller to jQuery (Ajax) in CodeIgniter

Code snippet in controller: $rates['poor'] = 10; $rates['fair'] = 20; $this->load->view('search_result2', $rates); //Although I have attempted different ways, the only successful method is using the code above. Other ...

The object in question does not have the capability to support the "live" property or method

I've encountered an issue in IE related to a script error with the live method. The problem arises when testing on the website URL @ Despite trying multiple solutions and various fixes, I haven't been able to resolve the issue yet. Any assistanc ...

Troubleshooting the Missing Border Issue in Tailwind Form Fieldset [Code Snippet Included]

I am developing a basic react application with the help of Tailwind css. Scenario 1: Functioning correctly without tailwind: https://codesandbox.io/s/bold-agnesi-y70ue In this example, the tailwind library is not utilized. The simple react app displays ...