Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align text on slanted line

Tags:

css

css-shapes

Is it posible to make text left aligned on a slanted line? it's alignement should follow the slanted slanted image with required support for IE9+?

Text left aligned against slanted shape

My example code :

img {
  display: block;
  float: left;
  transform: rotate(-5deg);
  margin: 0 15px;
}
<div>
  <img src="http://placehold.it/150x250&text=img" alt="image" />
  <p>Lorem ipsum dolor sit amet. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum dapibus, mauris nec malesuada fames ac turpis velit, rhoncus eu,luctus et interdum adipiscing wisi. Aliquam erat ac ipsum. Integer aliquam purus. Quisque lorem tortor fringilla sed, vestibulum id, eleifend justo vel bibendum sapien massa ac turpis faucibus orci luctus non, consectetuer lobortis quis, varius in, paragraph.</p>
</div>
like image 442
Tymek Avatar asked Jan 10 '15 13:01

Tymek


People also ask

How do I align text diagonally?

On the sheet, select the text of row(s)/column(s) that is to be aligned diagonally. Upon selection, in the Alignment section of the ribbon, click the Orientation button. From the drop-down that appears, you can select either Rotate Text UP or Rotate Text Down.

How do I align text diagonally in Indesign?

Take your angled text frame, then with the Direct Selection Tool, pull down the bottom right point so it creates a polygon with a flat bottom. (use Y axis number or a guide to be precise.) You could also make this shape using Pathfinder or the Polygon tool. You got it!

How do you vertically align text in Word?

1 Select the text you want to center between the top and bottom margins. 2 On the Page Layout tab, click the Page Setup Dialog Box Launcher. 3 Select the Layout tab. 4 In the Vertical alignment box, click Center 5 In the Apply to box, click Selected text, and then click OK.

How do I manually align text?

To align the text left, press Ctrl+L. To align the text right, press Ctrl+R. To center the text, press Ctrl+E.


2 Answers

Using LESS

You guys made me think a bit more outside of the box, so I came out with my own ugly solution. My idea is to add a bunch of extra square elements and calculate its size:

.loop(@i) when (@i > 0){
  .loop((@i - 1));
  .space@{i}{
    width: floor(@i*@hSize/(1/tan(5deg)));
  }
}
@hSize: 15px;
.space {
  float: left;
  clear: left;
  width: @hSize;
  height: @hSize;
}

HTML:

<p>
  <span class="space space1"></span>
  <span class="space space2"></span>
  <!-- (...) -->
  <span class="space space11"></span>
  Lorem ipsum dolor sit amet. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum dapibus, mauris nec malesuada fames ac turpis velit, rhoncus eu, luctus et interdum adipiscing wisi. Aliquam erat ac ipsum. Integer aliquam purus. Quisque lorem tortor fringilla sed, vestibulum id, eleifend justo vel bibendum sapien massa ac turpis faucibus orci luctus non, consectetuer lobortis quis, varius in, paragraph.
</p>

Proof of concept: http://codepen.io/Tymek/pen/jEypOX?editors=110

@chipChocolate.py, it was just a matter of principle for me NOT to use JavaScript for this. If anyone wants to write JS/jQuery code based on my solution, you're welcome. Please share it here afterwards.

like image 187
Tymek Avatar answered Oct 25 '22 02:10

Tymek


WARNING: The shape-outside property should not be used in live projects1. This answer is here just to show how the desired output can be achieved with this property.

Here is an example using the shape-outside property (modern webkit browsers only) :

DEMO

img {
  display: block;
  float: left;
  transform: rotate(-5deg);
  margin: 0 20px;
  -webkit-shape-outside: polygon(0 3%, 85% -3%, 100% 97%, 15% 103%);
  shape-outside: polygon(0 3%, 85% -3%, 100% 97%, 15% 103%);
}
<div>
  <img src="http://placehold.it/150x250&text=img" alt="image" />
  <p>Lorem ipsum dolor sit amet. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum dapibus, mauris nec malesuada fames ac turpis velit, rhoncus eu,
    luctus et interdum adipiscing wisi. Aliquam erat ac ipsum. Integer aliquam purus. Quisque lorem tortor fringilla sed, vestibulum id, eleifend justo vel bibendum sapien massa ac turpis faucibus orci luctus non, consectetuer lobortis quis, varius in,
    paragraph.</p>
</div>

1The CSS Shapes Module Level 1 actually (mai 2016) has the status of "Candidate Recommendation". As this means it is a work in progress, it may change at any moment and therefore should not be used other than for testing.

The same layout could be achieved with the shape-inside property and specify a containing box for the text but no browser I know of supports this property today.


For a cross browser approach please see Tymek's answer.

like image 20
web-tiki Avatar answered Oct 25 '22 02:10

web-tiki