4

I would think this would be a common issue, but my search for a solution leads me to believe it may not be possible. I simply want to place a div in the lower right corner of its parent div, yet have the text in the parent div flow around it.

Although I've found many posts addressing this question, I have not found one that appears to work. Most of these posts were several years old, which gives me a sliver of hope that there may be a way to do it after all with HTML 5?

I should have mentioned that this is in pursuit of a responsive design, so a static solution will not work.

Here's what I'm attempting to do: http://test.scoe.net/rfox/usalResponsive6/indexTeacher.html

I have a background image in the lower right of the div. I would like the text within this div to not flow across the image, but around it instead. I thought I'd be able to place an empty div (represented by the purple rectangle in the referenced page) in the lower corner to prevent text from flowing across the background image, but I can't seem to find a way to accomplish this.

RJF
  • 39
  • 7

1 Answers1

5

Didn't see a demo there - or anyone doing it with a pseudo element (which would be a bit more semantically correct because it's styling and not content) so let me just post that then :

Demo

<div id="parent">
  <div></div>
  <span>text</span>
</div>

#parent:before {
  content: '';
  height: 35%;
  float: right;
}

#parent div {
  width: 130px;
  height: 65%;
  float: right;
  clear: right;
}

When it comes to responsiveness in this particular case there are two aspects. First would be the background but since that isn't responsive itself for the most part and positioned at the bottom right, some width and height may have to be set along with the break points in the media queries.

Another form of responsiveness, automatic adjustment to the amount of text, is a tricky one that doesn't seem to be solvable without JavaScript. When height is left to auto, the floated elements will not inherit any height. This causes for the effect to not render. And because children cannot refer up the tree to relate to their parent's unknown height there isn't a pure CSS approach available.

So the example still has a fixed height and a minor bit of JS that's commented out but which should come close to making it adapt. It's a workaround but it's all current browser support will allow.


And now what can be used in the future!

Caniuse

The image itself could be cropped and saved as png, leaving transparent space around it. Then we can apply shape-outside and shape-image-threshold rules to it. With the current spec any text will then wrap when it's floated. Browser support it still limited at this point but it's very promising. The great thing I noticed about it is that when the floated element is given top margin, the text will start to flow above it! This does not occur in the example at the top of this post, it will only make the block appear longer (and empty as well). Because of this, a minimal bit of vanilla JS can make it fully responsive by only setting margin and without using an additional pusher element :

Example

<img id="image" src="image.png" alt="">

#image {
  shape-outside: url(image.png);
  shape-image-threshold: 0.5;
  float: right;
}

window.onload = placeBottom;
window.onresize = placeBottom;

function placeBottom() {
var parent = document.getElementById('parent'),
image = document.getElementById('image');
image.style.marginTop = 0;
var space = parent.clientHeight-image.clientHeight;
image.style.marginTop = space + 'px';
}

It's actually very straightforward :

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

Credit for the latter part to Paulie_D for putting me on the track of CSS shapes and later recognising that images used in this way are subject to same domain policy. Meaning they have to be hosted by the site itself or when linked externally, CORS restrictions will need to be relaxed.

Shikkediel
  • 5,195
  • 16
  • 45
  • 77
  • nope: http://codepen.io/anon/pen/gaMKrg?editors=110 adding text to the parent simply pushes the child down, doesnt wrap – Destination Designs Sep 22 '15 at 01:00
  • You were right, first draft didn't cut it at all. – Shikkediel Sep 22 '15 at 02:45
  • This answer seems to be the way to go and does meet the specifications: having an element in the lower right corner around which the text wraps. However, I would update the demo to have match the one from the question (the background shouldn't be on the "pusher" block, but on the parent block, the "pusher" is just there to avoid the text flowing over the person in the background). – saeraphin Sep 22 '15 at 19:19
  • Thanks for the feedback, I'll have a good look at what you suggested. Admitting I was a bit preoccupied with getting it right in general and that I may have neglected to match it with the question well. And then later decided so add Sam Jackson (it was an empty div before), lol. – Shikkediel Sep 22 '15 at 19:40
  • Thanks for all your attention to this question, Shikkediel. I'll explore the CSS shapes and see how far I get. – RJF Sep 23 '15 at 17:24
  • No problem,too bad support is very much lacking at the moment but it looks very promising and is likely the only true solution to the particular question. – Shikkediel Sep 23 '15 at 17:39
  • I noticed, a bit late, the demo you updated, thankyouverymuch. Looked very promising until I added some real-world text to the area. For some reason, as soon as I added H2 tags, or an unordered list, the text stopped showing up. I tried changing the

    tag with the ID of "content" to a

    , as a

    won't take an H2 or an unordered list, but that didn't seem to work. So close, but yet...

    – RJF Sep 23 '15 at 18:11
  • 1
    Caused by the CSS not being very specific that way yet maybe? It's also important to keep the empty div as the first child of the parent. http://tinyurl.com/omhevm7 – Shikkediel Sep 23 '15 at 19:07
  • Wow, Shikkediel, this comes very close to what I need it to do. Nice work! The only fly in the ointment here is that, since this is a responsive layout, the enclosing box needs to be able to get taller when the viewport is narrowed. Right now there's a set height to the "parent" div and the text flows past the bottom of the div. I've tried removing the height specification, as well as using "height: 100%" but it didn't work. Any suggestions to allow the div to get taller as the window is narrowed? Here's where it stands right now: http://test.scoe.net/rfox/usalResponsive6/indexTeacher6.html – RJF Sep 28 '15 at 23:18
  • 1
    That's looking pretty good already. Unfortunately there's no pure CSS approach to address the height issue. For the children to inherit any, it must set on the parent. But I see the site is using jQuery. With that it would be very easy to make it adapt, simply by reading the height of the text and explicitly setting it on the parent. Here's some code that should do just that : http://codepen.io/anon/pen/BoQEpb?editors=001. Maybe a tip... the empty div can also have a fixed height (since the image isn't responsive), then on the pseudo you could use height: `calc(100% - 250px)` for example. – Shikkediel Sep 29 '15 at 00:16
  • Shikkediel, I can't tell you how much I appreciate your time and expertise. With your help, I think I've got the layout working pretty darn well. It ain't perfect, but I'm not sure there is such a thing with responsive design. That last bit of javascript put me over the top, so many thanks. Here's where the page stands now, if you'd care to take a peek: http://test.scoe.net/rfox/usalResponsive6/indexTeacher6.html – RJF Sep 29 '15 at 20:59
  • Very nice work. I think with the current possibilities of responsiveness, it's as good as it gets. – Shikkediel Sep 30 '15 at 12:14
  • This is a brilliant solution and can also be modified to have the `space` used for the text's `top-padding`. Unfortunately, Shikkediel's solution does not work when you want to vertically align the text itself. Especially when you do not know the height of the text. I too came up with a similar solution, but I need to vertically position the text. All of my attempts to vertically align the text have failed. – Xavier Sep 23 '19 at 20:42
  • I take that back. By adding the `space` to the `top-margin` of the text as well, I get results! – Xavier Sep 23 '19 at 20:45
  • However, in my specific case, I wanted to position the text to the bottom of the parent div. Because of this, I cannot get the text to position to the bottom without knowing the height. And that my friends, takes me down the rabbit hole; as soon as you add a margin or padding, the height changes! duh. All the Javascript in the world will run you into a neverending battle of knowing the height of the text. – Xavier Sep 23 '19 at 20:49