Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This type of layout question has been <a href="https://stackoverflow.com/questions/499829/css-wrap-text-around-a-bottom-right-div">asked before</a> quite a few times :-)</p> <p>From a quick read of the links on that thread, it looks like it is not easy to do.</p> <p><strong>Edit:</strong> Right, this one has been bugging me. I figured it was possible with a <strike>small amount</strike> <strong>very large amount</strong> of complex JavaScript/jQuery and set about <strong><a href="http://jsfiddle.net/Kn74j/6/" rel="nofollow noreferrer">making a demo</a></strong>.</p> <p><strike>There is still some work to be done, since the <code>&lt;p&gt;</code> that contains the image has been left in a bad way (i.e. full of <code>&lt;span&gt;</code> elements). I also make no promises on the performance of this solution. It seems to work for me OK. Oh and also the <code>Font +</code> link will only work once as the actual size of the font it increases the text to is fixed.</strike></p> <p>That said, I would really try and find an alternate solution to your problem or even question why you need to fix an image position in this way. It is going against the normal browser re-flow and rules.</p> <p><strong>Edit: <a href="http://jsfiddle.net/Kn74j/11/" rel="nofollow noreferrer">Improved demo</a></strong> that addresses the issues in the comments.</p> <p><strong>Edit 2:</strong> Wow, I did not know how complex the CSS3 column layout must be for browsers! Height is calculated in order to make the columns more or less equal and that means that when I detach the <code>&lt;img&gt;</code> the height is adjusted. This adjusted height will be different if the <code>font-size</code> increases and the <code>&lt;img&gt;</code> is <strong>not</strong> removed. It's just not possible to determine the height of the result before the <code>&lt;img&gt;</code> is removed and added.</p> <p>That said, I have made it work with one major condition - that the column has a hard-coded height. I tried to make it work with a browser calculated height (as normal flow rules would dictate) but I think it might be very difficult without a lot more work. The only way I think this might work is moving the <code>&lt;img&gt;</code> element forwards one <code>&lt;span&gt;</code> (i.e. word) at a time until the correct <code>position().top</code> is reached. But that would be incredibly inefficient and probably slow the rendering down so much it will be unusable. </p> <p>So, here is <strong><a href="http://jsfiddle.net/Kn74j/12/" rel="nofollow noreferrer">my final demo</a></strong> which is working in Chrome 11 for me. Sorry I could not reach a complete solution with variable height but as I, and others, have said it really is breaking all the browser layout rules!</p> <p><strong>Edit 3:</strong> When I said "final" demo I clearly didn't mean it. I accept your challenge to actually make this work properly with multiple images and I <strong>really</strong> hope that this time, I have got it. Please have a look at this <strong><a href="http://jsfiddle.net/Kn74j/14/" rel="nofollow noreferrer">new demo</a></strong> (working in Chrome12 for me) which works with the HTML you posted. I must admit that the JavaScript you posted was very complex. So I have re-factored everything to work with multiple images based on my original JavaScript.</p> <p><strong>Edit 4:</strong> Quite a lot of edits now&hellip; but I found the bug. For each font increment or decrement the images are moved in the flow and potentially a <code>margin-top</code> added to move the image back to it's original position. But I was not clearing this CSS before re-calculating the position on a subsequent font size change.</p> <p>The key code added was:</p> <pre><code>// clear any existing marginTop added on the images images.each(function() { $(this).css('marginTop', 0); }); </code></pre> <p>I also tidied up the JavaScript a bit more whilst I was there and fixed another bug with removing the <code>&lt;span&gt;</code> elements :-)</p> <p><strong><a href="http://jsfiddle.net/Kn74j/16/" rel="nofollow noreferrer">Updated jsFiddle demo</a></strong></p> <p>And just in case jsFiddle ever disappears, here is the complete solution as one HTML file:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; //&lt;![CDATA[ $(function(){ var fontSize = 15; // cannot store img.position().top or .left here because it is // before the browser has re-flowed the columns, therefore the // positions will be incorrect var imageTops = new Array; var imageLefts = new Array; $('#fontUp').click(function() { reflow(1); }); $('#fontDown').click(function() { reflow(-1); }); function reflow(fontSizeStep) { storeImagePositions(); var fontLimitReached = changeFont(fontSizeStep); if (!fontLimitReached) { moveImages(); } return false; } function changeFont(step) { fontSize += step; var fontSizeLimitReached = true; if (fontSize &gt; 30) { fontSize = 30; } else if (fontSize &lt; 15) { fontSize = 15; } else { fontSizeLimitReached = false; } if (!fontSizeLimitReached) { $('p').css({fontSize: fontSize + 'px'}); } return fontSizeLimitReached; } // initialize store of img top and left positions function storeImagePositions() { if (imageTops.length == 0) { // only do it once $('img').each(function() { var imgPosition = $(this).position(); imageTops.push(imgPosition.top); imageLefts.push(imgPosition.left); }); } } function moveImages() { // bye bye images var images = $('img').detach(); // clear any existing marginTop added on the images images.each(function() { $(this).css('marginTop', 0); }); // spanify paragraphs $('#column &gt; p').each(function() { $(this).html('&lt;span&gt;' + $(this).html().replace(/\s\s+/g).replace(/(\s)/g,'&lt;/span&gt;$1&lt;span&gt;') + '&lt;/span&gt;'); }); var imageIndex = 0; // iterate words, working out where we can move the img to in the flow and if // we find a match, increment the index so that as we continue the each() // the next image is evaluated for replacement $('#column &gt; p span').each(function() { var wordPosition = $(this).position(); var wordLeft = wordPosition.left; if (wordLeft &gt;= imageLefts[imageIndex]) { var wordBottom = wordPosition.top + $(this).height(); if (wordBottom &gt; imageTops[imageIndex]) { $(this).before(images[imageIndex]); // move img before this word var newImgTop = $(images[imageIndex]).position().top; $(images[imageIndex]).css({marginTop: imageTops[imageIndex] - newImgTop + 'px'}); imageIndex++; // increment index so remainder spans are being evaluated against the next image } } }); // reverse the "spanification" $('#column &gt; p').each(function() { $(this).html($(this).html().replace(/&lt;\\?span&gt;/g, '').trim()); }); } }); //]]&gt; &lt;/script&gt; &lt;style type="text/css"&gt; div#column { -moz-column-count:6; -webkit-column-count:3; column-count:3; -webkit-column-width:100px; -moz-column-width:100px; column-width:100px; height:500px; } p { margin:0; clear:left; font-size:15px; text-align:justify; } img { float:left; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div&gt;&lt;a href="#" id="fontUp" style="margin-right:10px"&gt;Font +&lt;/a&gt;&lt;a href="#" id="fontDown"&gt;Font -&lt;/a&gt;&lt;/div&gt; &lt;div id="column"&gt; &lt;p&gt;Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. &lt;img src="http://www.avatarblast.com/avatars/cool/yoda.jpg" title="yoda" alt="yoda"/&gt;It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.&lt;/p&gt; &lt;p&gt;The change of name from &lt;img src="http://www.avatarblast.com/avatars/cool/yoda.jpg" title="yoda" alt="yoda"/&gt; LiveScript to JavaScript roughly coincided with Netscape adding support for Java technology in its Netscape Navigator web browser. The final choice of name caused confusion, giving the impression that the language was a spin-off of the Java programming language, and the choice has been characterized by many as a marketing ploy by Netscape to give JavaScript the cachet of what was then the hot new web-programming language. It has also been claimed that the language's name is the result of a co-marketing deal between Netscape and Sun, in exchange for Netscape bundling Sun's Java runtime with their then-dominant browser. Vivamus scelerisque ipsum ut justo. Pellentesque et ligula eu massa sagittis rutrum. In urna nibh, eleifend vel, suscipit ut, sagittis id, nunc.&lt;/p&gt; &lt;p&gt;Nam ut sapien sed pede pulvinar rutrum. Nunc eu elit sed augue aliquet tincidunt. Morbi rutrum. Vestibulum dui turpis, lobortis quis, euismod sed, consectetuer sit amet, nunc. Nam mi. Fusce at nisl eu tortor bibendum eleifend. Sed ac metus. Phasellus nec elit. Morbi tortor nulla, tristique a, adipiscing at, consectetuer et, nisi. Nunc vel sapien sed risus hendrerit egestas. Vivamus turpis arcu, placerat eu, congue vel, commodo ut, nisl.&lt;/p&gt; &lt;p&gt;Java EE includes several API specifications, such as JDBC, RMI, e-mail, JMS, web services, XML, etc., and defines how to coordinate them. Java EE also features some specifications unique to Java EE for components. These include Enterprise JavaBeans, Connectors, servlets, portlets (following the Java Portlet specification), JavaServer Pages and several web service technologies.&lt;img src="http://www.avatarblast.com/avatars/cool/yoda.jpg" title="yoda" alt="yoda"/&gt; This allows developers to create enterprise applications that are portable and scalable, and that integrate with legacy technologies. A Java EE application server can handle transactions, security, scalability, concurrency and management of the components that are deployed to it, in order to enable developers to concentrate more on the business logic of the components rather than on infrastructure and integration tasks.&lt;/p&gt; &lt;p&gt;Java (Indonesian: Jawa) is an island of Indonesia. With a population of 136 million, it is the world's most populous island, and one of the most densely populated regions in the world. It is home to 60% of Indonesia's population. The Indonesian capital city, Jakarta, is in west Java. Much of Indonesian history took place on Java; it was the centre of powerful Hindu-Buddhist empires, Islamic sultanates, the core of the colonial Dutch East Indies, and was at the centre of Indonesia's campaign for independence. The island dominates Indonesian social, political and economic life.&lt;/p&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload