how to use clip-path with text inside the div?

前端 未结 1 1749
轻奢々
轻奢々 2021-02-20 04:14

i tried the following:

http://codepen.io/anon/pen/YXePBY

lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor sit amet lor
1条回答
  •  死守一世寂寞
    2021-02-20 04:42

    You can't do this with clip path alone.

    Clip path is applied to the element regardless of it's contents. It's a definition of the outer bounds of an element but a definition of the how the element is clipped within it's bounds, hence the text not behaving as you might expect. It still requires the element to be a box, because currently in css, all things are boxes.

    It is however possible. These two articles here and here go into the required detail. First of all you define an element using a css shape declaration. You then use a clip-path, with the same definition as the polygon definition, to clip away any of the background that affects your shape

    Taken from the first article (and conveniently close to what you want):

    .shaped{
        /*...*/
        shape-outside: polygon(0 0, 100% 0, 100% 100%, 30% 100%);
        shape-margin: 20px;
        clip-path: polygon(0 0, 100% 0, 100% 100%, 30% 100%);
    }
    

    We can see it's pretty simple, the shape is defined as a polygon and it will behave like any other block, except it has polygon edge rather than a block edge. Floating the element will then cause the text to flow around it.

    Here is an example at JSFiddle* (-webkit- prefixes are required, as is a WebKit browser such as Google Chrome). By looking at this JSFiddle*, before clip-path is applied, you can see why it is necessary. In my example, both .shaped and .text are held within the same container but as siblings. The text flows around it's sibling rather than expanding to fill it's container. The container itself and use of background colours achieve the effect in the mock-up. (you may need to adjust the JSFiddle panel size to squish or stretch things and see the effect clearly).

    Unfortunately, css shapes are very fresh. A quick glance at caniuse.com shows barely half of current browsers support these. Even Firefox has no implementation at all in the current version. As for IE ...

    What I would suggest is that you use css shapes as your default and let the new browsers handle it (Firefox will catch up soon I'm sure) but provide some sort of browser feature detection method like Modernizr to decide whether or not to apply a fallback that will display the two sections as blocks without the slant.

    0 讨论(0)
提交回复
热议问题