Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Transform offset varies with text length

I have set up a demo that has 5 floating <div>s with rotated text of varying length. I am wondering if there is a way to have a CSS class that can handle centering of all text regardless of length. At the moment I have to create a class for each length of characters in my stylesheet. This could get too messy. I have also noticed that the offsets get screwd up is I increase or decrease the size of the wrapping <div>.

I will be adding these classes to divs through jQuery, but I still have to set up each class for cross-browser compatibility.

.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 I set were eye-balled.


Update

Although I would like this handled with a stylesheet, I believe that I will have to calculate the transformations of the CSS in JavaScript.

I have created the following demo to demonstrate dynamic transformations. In this demo, the user can adjust the font-size of the .v_text class and as long as the length of the text does not exceed the .v_text_wrapper height, the text should be vertically aligned in the center, but be aware that I have to adjust the magicOffset value.

Well, I just noticed that this does not work in iOS... Thanks @Dinesh Kumar DJ

like image 557
Mr. Polywhirl Avatar asked Nov 02 '22 12:11

Mr. Polywhirl


1 Answers

Here you go: http://codepen.io/teodragovic/pen/fgekh

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

.rotate {
  //remove this line if using js
  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; //remove this line if using js
  height: 100px;
  transform-origin: top left;
  text-align: center;
  position: absolute;
  display: table;
}

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

No need for JS and dynamic offseting. You can set all <div>'s containing text at same width and height as wrappers, rotate them around top left corner (could be any corner) and then use translateY to position them back inside wrapper (this assumes that wrapper dimensions are always the same and known value).

I added extra <p> element around text and did vertical centering using this method: http://css-tricks.com/vertically-center-multi-lined-text/

Edit: I updated pen in case wrapper <div> changes size. Since <div>'s are rotated, their witdh becames height and vice versa so if wrapper has dimension 100x150 then child has to be 150x100.

$('.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});
});
like image 113
Teo Dragovic Avatar answered Nov 09 '22 05:11

Teo Dragovic