CSS wrap text around centred image

前端 未结 3 1769
春和景丽
春和景丽 2020-12-19 06:09

How to wrap a text around a centered (round) image like this:

\"http://i.stack.imgur.com/kgyCm.jpg\"

I

相关标签:
3条回答
  • 2020-12-19 06:24

    Question resolved :

    Actual situation : The CSS shape works for float, so it's not for centered images now. This property works only for Chrome and Opera at the moment. Maybe there will be a solution for non float elements in the future. Look at this W3C editor's draft : http://www.interoperabilitybridges.com/css3-floats/OriginalSubmition.html

    A hand made CSS solution : Basically, there are 2 columns (like in newspapers). The text begins in the left column and goes down. The text continues on the top of the right column and goes down. The columns are a bit higher than the image. The left column has a half invisible circle as well as the right column - on the position of the centered image. The two half circles are build by multiple boxes of different length, they are invisible. (The hight of the boxes is the height of the font.) The text must be justified. The text is now flowing around the half circles in each column. The image will be positioned over the 2 invisible half circles.

    Another, not very technical solution is to use Libre Office and Inkscape to produce an SVG file. Import the picture into Libre Office - wrap the text around the image - save as PDF - open Inkscape - save as SVG - import the SVG in your Webpage - done.

    Thanks everybody for helping me and for your inputs !

    0 讨论(0)
  • 2020-12-19 06:32

    I don't think it's possible since it relies on float and you can't float to the middle/center of the page.

    Here's what I came up with: [old fiddle]/5Lxc444p/8/

    If you put the width style on the actual circle it works better than on a parent div.

    Also, here's a good writeup on css shapes: http://www.html5rocks.com/en/tutorials/shapes/getting-started/

    EDIT:

    Here's an updated fiddle for the 2 column layout with absolute positioned circle. http://jsfiddle.net/5Lxc444p/11/

    0 讨论(0)
  • 2020-12-19 06:36

    As already noted, shape wrapping currently only works for floated elements, so this exact situation isn't do-able with CSS only, because only wrapping on one side of a shape is permitted (expected). Once the CSS Shapes 2 and/or CSS Exclusions specs) are adopted, we will be able to do this with not only shapes but also image transparency.

    I ran into this same problem while trying to figure out how shapes and CSS columns interact (spoiler: decent, but not organically). The problem seems to be that the layout algorithm looks for the farthest edge (ignoring the possiblity of multiple sides), then starts content layout from that coordinate. For elements in the middle, this means you get text only on one side. For CSS columns (which is how I figured this out), the layout again starts from the farthest edge, but then continues straight down instead of wrapping to the shape on each line (see fiddle), so protrusions on shapes (like a star polygon) can actually force wrapping content to end up below the entire shape instead of squished to one side or flowing down into the protrusion.

    (note there are 3 sets of 2 columns on 2nd example)


    However, there are a couple options that may work for similar situations. I have adapted the following from the other answers/comments, but had to make several changes to get them working (and several of the CSS attributes were experimental and are no longer valid), so I felt this was better as a new answer than as edits/comments:

    Wrap one side

    Use shape-outside on a left floated div to create a wrapping circle, then use margin-left to push it away from the left side. I added a circle inside the div for illustration (your image URL is 404), but had to tweak the location as Chrome did not calculate its position the way one would expect once margins were added.

    http://jsfiddle.net/brichins/50h20kxa/1/

    Columns and mirrored wrapping elements

    If columns are acceptable, manually (see above CSS column discussion) creating 2 containers for columns and placing a shaped element on the side of each gives the following:

    http://jsfiddle.net/brichins/gvhpfccu/

    The disadvantage here is columns where you may have wanted a single block (not necessarily bad for readability), as well has having to compute an appropriate split for your content.

    Reading

    • Intro and walkthrough on HTML5 Rocks: https://www.html5rocks.com/en/tutorials/shapes/getting-started/

      • References the amazing Alice in Wonderland example from 2013. It appears to not function completely anymore, but the entire talk is still interesting
    • Creating Non-Rectangular Layouts With CSS Shapes: https://sarasoueidan.com/blog/css-shapes/

    • CSS Tricks article: https://css-tricks.com/almanac/properties/s/shape-outside/

    • D3plus workaround plugin (for similar SVG solutions): https://d3plus.org/examples/utilities/a39f0c3fc52804ee859a/

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