Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit a paragraph inside a css polygon

Tags:

html

css

I have been able to successfully use shape-outside: polygon(...) and clip-path: polygon(...) to create a desired polygon shape.

I have seen many pages referencing use of shape-inside, and yet I have read that shape-inside had been deprecated and has no replacement. They were also written in 2014, so I am hoping that CSS3 has changed since then.

After looking on the web, I was able to piece together something that almost works. I like the shape, but now I need the text wrap inside the shape with the overflow hidden.

I have seen hints about ::before, but I still don't understand how that would help. Testing it didn't seem to produce any results.

Whether it's simple or complicated, how can I use CSS to wrap the text within the polygon? Or does the solution lie outside of CSS? Do I need to use another approach, like jQuery?

This is the polygon with text clipped off.

CSS

/*
For reference:    
@vertex1: 120px;
@vertex2: @vertex1*2;
*/

.diamondContainer {
    display: block;    
    position: absolute;
    text-align: center;
    width: @vertex2;
    height: @vertex2;
    overflow: hidden; /* hide anything longer than allowed string length */

    /* This is a diamond shape */    
    shape-outside: polygon(@vertex1 0, @vertex2 @vertex1, @vertex1 @vertex2, 0 @vertex1);    
    clip-path: polygon(@vertex1 0, @vertex2 @vertex1, @vertex1 @vertex2, 0 @vertex1);
}

HTML

<div class="diamondOuter">
    <div class="diamondWrapper">
        <div class="diamondContainer diamondCell2">
            <span><strong>Title Text</strong><br />Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</span>
        </div>
    </div>
</div>
like image 995
RandomHandle Avatar asked Jan 06 '17 19:01

RandomHandle


People also ask

How to position clip-path in CSS?

The path() value allows us to use an SVG path to clip a specific area. For now, the browser support is inconsistent. To make it work across different browsers, we need to use an inline SVG, and then use the url() as a value for clip-path . In CSS, we need to append the path using url() value.

How to use clip-path polygon CSS?

CSS clip-path EditorStart by selecting a polygon() — ellipse() or url() -preset. To move the selected point, use mouse, touch or Arrow -keys. When using Arrow -keys, hold Ctrl to move point in smaller intervals.

How do you make a polygon shape in CSS?

The polygon() function is an inbuilt function in CSS which is used with the filter property to create a polygon of images or text. Syntax: polygon( percentage | length); Parameter: This function accepts two parameter percentage or length which is used to hold the value of polygon size.


1 Answers

Look again at shape-outside.

You don't set it on the text container, but other elements around the text.

In your case, you may need to have at least 2 div's for the text to flow around.

There is an example of text flowing between 2 polygons on this page.

https://www.html5rocks.com/en/tutorials/shapes/getting-started/

enter image description here

like image 109
Phil Poore Avatar answered Sep 19 '22 01:09

Phil Poore