Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I guess the solution is to think the problem like this:</p> <ol> <li>We have a circle of variable radius.</li> <li>We have a square inside the circle with maximum possible side length.</li> <li>We have to make sure the square fits inside the circle by adjusting it's side length.</li> </ol> <p>Obviously, the circle represents your outer div and the square represents the text inside. I wrote some jQuery function to do exactly what you want.</p> <p>Here is a <a href="http://jsfiddle.net/hQRrX/" rel="nofollow">JSfiddle</a>. Hope it helps.</p> <p>Now to explain this,</p> <ul> <li><p>First we put all our texts inside an inner div. This is to make sure we can calculate the height and width properly.</p> <pre><code>&lt;div class="circle"&gt; &lt;div class="text"&gt; Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta, quisquam, mollitia, dolores sit accusamus molestiae necessitatibus rerum nisi natus itaque amet maiores tempore veniam qui aut ullam odio magnam quidem! &lt;/div&gt; &lt;/div&gt; </code></pre></li> <li><p>We also make sure the text box padding doesn't get into way by setting the <code>box-sizing</code></p> <pre><code>.circle .text { border: 1px solid #bbb; background: #fff; font-size: 16px; /* This box sizing is necessary for inner calculations */ -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; padding: 5px; } </code></pre></li> <li><p>Finally we put our javascript to compute the font size property if the text div height is greater than the calculated square side length. Otherwise, we just adjust the margin to make it appear in a central position.</p> <pre><code>$('.circle').each(function() { //Get the container and inner text var circle = $(this), text = $(this).find('&gt;.text'); //Get some variables var height = circle.height(), width = circle.width(), radius = 0, sideLength = 0, margin = 0, fontSize = 0; //Fix it if necessary if(height !== width) { radius = Math.round(parseFloat(Math.max(height, width) / 2, 10)); circle.height(radius * 2).width(radius * 2); } else { radius = Math.round(height / 2); } //Now calculate a square box inside the circle to put the text into //This is pure maths //The diagonal of the square is basically two times of the radius //Which means side is approximately radius * 1.4142 sideLength = Math.round(radius * 1.4142); //Thus calculate the margin margin = ( height - sideLength ) / 2; //Assign the width text.css('width', sideLength); //Assign the margin text.css({ 'margin-left' : margin, 'margin-top' : margin }); //lets center the text if it's height is less than sideLenght if(text.height() &lt; sideLength) { var newMarginTop = (height - text.height()) / 2; text.css('margin-top', newMarginTop); } else if(text.height() &gt; sideLength) { //Or adjust the font //Now keep on decreasing the font until the height of the text becomes //less than or equal to the calculated size while(text.height() &gt; sideLength) { fontSize = parseInt(text.css('font-size'), 10); text.css('font-size', --fontSize); } } }); </code></pre></li> </ul> <p>Hope I'm able to explain. Let me know if you do not understand anything.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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