Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to accurately resize nested elements with ems and font-size percentage?
    primarykey
    data
    text
    <p>I have a carousel with textboxes for each image, and my client (who knows nothing about HTML) edits the textboxes using a WYSIWYG text editor. The resulting output is akin to your worst nightmares; something like:</p> <pre><code>&lt;div class="carousel-text-container"&gt; &lt;span style="font-size:18pt;"&gt; &lt;span style="font-weight:bold;"&gt; Lorem &lt;span style="font-size:15pt:"&gt;Dolor&lt;/span&gt; &lt;/span&gt; Ipsum &lt;span style="font-size:19pt;"&gt; &lt;span&gt;&amp;nbsp;&lt;/span&gt; &lt;span&gt;Sit&lt;span&gt;Amet&lt;/span&gt;&lt;/span&gt; &lt;/span&gt; &lt;/span&gt; &lt;/div&gt; </code></pre> <p>This site has to be displayed at 3 different sizes to accomodate smaller monitors, so I have been using CSS media queries to resize the site. </p> <p>Now I am having trouble resizing the text inside the textbox correctly. I have tried using <code>jQuery.fn.css</code> to get the font size of each element in <code>px</code>, and then convert it to <code>em</code>. Then, by setting a <code>font-size:x%</code> sort of declaration on <code>.carousel-text-container</code>, I hoped that that would resize everything properly. </p> <p>Unfortunately, there seems to be a recursive nature with how font-size is applied in ems. That is, <code>.example</code> is not resized properly in the following because its parent is also influencing it</p> <pre><code>&lt;span style="font-size:2em;"&gt; Something &lt;span class="example" style="font-size:1.5em;"&gt;Else&lt;/span&gt; &lt;/span&gt; </code></pre> <p>How can I resize everything reliably and precisely such I can achieve a true percentage of my original font size, margin, padding, line-height, etc. for all the children of <code>.carousel-text-container</code>? </p> <p>Right now, I'm at the point of imagining up some sort of recursive JavaScript function:</p> <pre><code>var carouselTextBoxEl; function resizeCarouselTextBox () { // some example numbers thrown in var newWidthRatio = .8, function resizeCarouselTextBoxEl (el, nestedLevelNum) { nestedLevelNum = nestedLevelNum || 1; var childElFontSizePxStr = parseFloat(el.css('font-size')), // 16.2984392, for example // calculate new font size based on // width ratio of original size site to new size, // and do something with nestedLevelNum... what's the math with that? newChildElFontSizePxStr = childElFontSizePxStr * newWidthRatio, // get children of el, if any elChildrenEls = el.children(), i = elChildrenEls.length; el.css('font-size', newChildElFontSizePxStr + 'em'); while (--i &gt;= 0) { resizeCarouselTextBoxEl(elChildrenEls[i], nestedLevelNum + 1); } } carouselTextBoxEl.children().each(function () { resizeCarouselTextBoxEl($(this) }); } onSmallerScreen(resizeCarouselTextBox); </code></pre> <p>UPDATE: I am letting the client take advantage of resizing certain words to use as headings and such, so I cannot simply just set everything to a single font size.</p> <p>Just a thought: would it be possible to place inside an HTML5 canvas and take a snapshot and/or resize that way? </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.
 

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