tags:

views:

82

answers:

5

I need to make a control that has three T's of varying size that are linked. By clicking on each T the article text will resize to either a small, medium, or large font appropriately.

Does anyone know how I can do this? Also, do you know of a site that uses this kind of text resize feature?

Thanks in advance.

UPDATE: Thanks for all of your responses. I went digging through Google a little further and found that this has potential: http://mirificampress.com/permalink/daynamically%5Fresizing%5Ftext%5Fwith%5Fcss%5Fand%5Fjavascript It's using JS to dynamically resize the font and this is exactly what I want to do. I'd much rather do this in CSS if possible still though - anyone?

A: 

You could attach different CSS files depending on which 't' the person has clicked (changing the paragraph text size). I'm sure there's a better way, but that'll get the job done!

mr1989foster
Thanks mr1989. I don't think I want to attach different files though :)
Cindy
A: 

Using jQuery you could do something like this:

$(function(){
  $('#smallT').click(setTextToSmall);
  $('#mediumT').click(setTextToMedium);
  $('#largeT').click(setTextToLarge);
});

function setTextToSmall(evt)
{
  $('.text-to-resize').addClass('small').removeClass('medium').removeClass('large');
}

// Have similar setTextTo.. functions for Medium and Large here

To clarify, this actually does use CSS to change the sizes. You would have three CSS classes named 'small', 'medium', and 'large':

.small { font-size: 0.5em; }
.medium { font-size: 1em; }
.large { font-size: 1.5em; }

The setTextToSmall() function is called when the user clicks on the small "T". It adds the class 'small' to all elements that already have a class of 'text-to-resize' and removes the 'medium' and large classes. So where you might have this before the click:

<div class="text-to-resize">Some sample text</div>

You would have this after the click:

<div class="text-to-resize small">Some sample text</div>

If you wanted to apply this to every element on the page then you would simply change setTextToSmall() to the following:

function setTextToSmall(evt)
{
  $().addClass('small').removeClass('medium').removeClass('large');
}

The benefit of jQuery (or other frameworks for that matter) is that it abstracts out the DOM which is very tricky across different browsers. For example, to do this in straight Javascript you might instinctively want to use document.getElementsByClassName(). However, that method doesn't exist in any version of IE.

Darrell Brogdon
OP never said or tagged JQuery, it is prevalent, doesn't mean there aren't better frameworks out there. How about plain Javascript?
Murali VP
I don't know any JQuery at all. Unless this is a copy/paste type of code, this won't help me. Thanks for the response.
Cindy
Yeah don't use jQuery, its a deviation from JavaScript code, I highly advise anyone to not use something like jQuery unless you really can't find your way around Javascript, the DOM and cross-browser bugs.
drlouie - louierd
While I'll concede that if she doesn't know it or doesn't want to use jQuery then my answer isn't helpful, jQuery is perfect for the type of thing @Cindy is trying to do. Suggesting she not use jQuery just because its a framework is like saying one shouldn't use Windows because a command-line will suffice.
Darrell Brogdon
@LouieRd - DrLouie: uh, yeah most people can't find their way around the DOM, that's why jQuery (and other libraries) are such a godsend. @Darrell: although a valiant effort this answer was, you wrote *nothing* in the way of an explanation! Do you realize how foreign your code looks to someone who doesn't know the jQuery API? A little explanation to *how* you accomplished the OPs request may help.
Crescent Fresh
@Crescent Fresh: Understood. Thanks :)
Darrell Brogdon
@Darrell thank you for explaining this further. I understand this more now :)
Cindy
+3  A: 

You can do this with CSS - if all of your fonts are percentages, then you can set one font size for the document and all children will be a percentage of that.

Gabriel McAdams
I'd love to do this in CSS. Can you explain this further? My fonts are in .em, what do I link the T's to?
Cindy
First of all, you need to understand that the em actually a percentage based font size (1.5em is 1.5 times the size of the font set on the parent element).Now, let me explain further: for the text that you want to be effected by the change in T size, set their font-size to a percentage (or use em). they should all have the same parent, so this should be easy. When the user clicks a T of a given size, change the font-size of the parent and you're done.
Gabriel McAdams
Please remember to accept this answer if you found it to be useful.
Gabriel McAdams
A: 

You can use

("fontSize",+=3px)

If you don't want to have limit.

Axiol
+1  A: 

The approach will not be able to be implemented using CSS only. You will need to use CSS in conjunction with JavaScript.

The best approach would be to set your page's default body size using either percentages or ems. You would create two extra classes for the larger and smaller font size for the page's container or <body> tag. These classes could use larger and smaller percentages / ems or you could use the keywords: xx-small, x-small, small, larger, x-large, xx-large. (NOTE: I left out smaller and larger since they seem not to work sometimes).

Then using JavaScript you could attach an onclick event to your three T's which would dynamically add the desired class to the page container (or <body> tag). If they clicked on the middle T then the applied large/small class would be removed returning the page to it's default font-size.

A few things to keep in mind:

  • A user can set a minimum font size for their browser so if you set your "small" size below that that setting, that user will never see your smallest font setting.
  • You will need to experiment with how your layout acts if a user has a larger default font-size setting.

Hope this helps and good luck!

Lark
Thank you, Lark!
Cindy