Skip to content Skip to sidebar Skip to footer

Trying To Set The Size Of A Span As A Percentage Of Size Of Contents

I think I've got what sounds like a simple request but is probably actually stupid hard to do, but I haven't worked in HTML in a long time. First, I only have to support IE. I und

Solution 1:

Span has no child as David said, also, all you have to do is set span to a block (display:block) and 50% and it'll take the width of 50% of the parent (if thats what you mean) and then do overflow:hidden which will snip it off at 50% and wont show the rest of whats inside.

-- UPDATE 2 --

Here is the raw JS. Let me know if you have any questions. This does the same thing as i had in jQuery, but with just JS. plop it in a script tag and you should be good to go.

//Setting up vars
$ = document;
$parent = $.getElementById('parent');
$child = $.getElementById('child');

//Set the styles for the child$child.style.width = $child.offsetWidth+'px'$child.style.display = 'block';

//Set the styles for the parent$parent.style.width = ($child.offsetWidth*0.5)+'px'$parent.style.overflow = 'hidden';
$parent.style.display = 'block';

Post a Comment for "Trying To Set The Size Of A Span As A Percentage Of Size Of Contents"