Is there some kind of a trick to make font size take up the same amount of width regardless of which browser is used? (I use pixels for font size, but I'm not sure how relevant that is to this question.)
Normally, if I have a line of text on my site that I want to only take up one line and not drop down to a second, I have to make sure the text that I write is short enough to fit as one line within the allotted space in all the main browsers. For instance, I might write some text within a div (that also has a specified width in pixels) that comes out as one line on firefox, safari, etc., but then when I go to check ie8, for some reason with the way it is displayed, it didn't have room for that last word and dropped that word down to the beginning of a second line. I was wondering if there was some way to make the text take up the same amount of room in terms of width no matter what. Is there some kind of simple solution here that I'm missing?
As I had said, I use pixels for font size, and I've been researching using other units of measurement, but to my understanding so far, using something else such as em's for font-size wouldn't really be the answer to this since they are basically an abstraction of pixels. Even if there was a way to do it with a different unit of measurement, I'd be interested to know if there is a a solution to this that would allow me to keep pixels, since that is just my preferred unit of measurement.
Different browsers and OSs have slightly different font handling, so it is really difficult to accurately anticipate these sorts of things. What I do is write a little jQuery function that increases the size of the font in a container until just before it wraps, fitting the container and not wrapping as a result. It generally flickers on load in IE but it is not noticeable on normal browsers. You can see it in action in the links attached (check out name of the market):
http://www.hdradioalliance.com/station_guides/widget.php?id=77
http://www.hdradioalliance.com/station_guides/widget.php?id=21
http://www.hdradioalliance.com/station_guides/widget.php?id=61
Feel free to recycle the code if it fits your needs.
Edit: actually, that particular bit of code shrinks the font until its container is one line tall—rather than what I said, which is the other way around. I've done it both ways tho. Here it is:
$(document).ready(function(){
var headingSize = 1;
while($('#headingSling h1 span').height() > 34 && parseInt($('#headingSling h1 span').css('font-size').replace(/\D\./g,'')) > 10){
headingSize -= 0.01;
$('#headingSling h1 span').css('font-size', headingSize+'em');
}
});
Related
One of the specs for Web accessibility level AA that is text can be resized to 200% without loss of content or function.
So if I zoom up to 200%, everything needs to look right.
How can I achieve that regarding the font size?
The issue is not so much a matter of font size or font size units, since most common browsers have a built-in zoom function that will zoom any text, regardless of the font size unit. The issue is that resizing may result in text containers that start to overlap, which then causes some content or functionality to become invisible (because it ends up behind something else). Success criterion 1.4.4 was written when zooming functionality was not yet as widespread as today. (In 2008, there were several browsers that supported zooming, but many people were still using older browsers that didn't support zooming, and text resizing—which is not quite the same as zooming—could cause overlap in web content.)
So, while using units such as em, %, rem etc. is a good idea, you need to make sure that text containers (such as section elements, nav, etc.) that are displayed next to each other don't overlap when you zoom in to 200% or resize text up to 200%. For example, you can describe column width using units such as % or em, or you can make sure that text containers that are next to each other at the default size end up below each other when zoomed in. If you use responsive design to make your web pages adapt to different screen sizes, you should also be able to handle zooming in desktop browsers.
You should try using em instead of px..
For example if you have div inside yourbody - suppose the font-size of the body of the page is set to 16px.
If the font-size you want in div section is 12px, then you should specify 0.75em (because 12/16 = 0.75).
You should try use "rem" (support most of the browsers).
Then you set the font-size of the root ( the html).and all the page reasize for example if you want the "h1" be always 2 times the body ,set it to 2rem;
I'd like to be able to increase the default font-size, but only within a certain DIV.
Obviously, this seems like exactly the sort of case that ems were meant for. My problem is I only want to increase the font-size, without affecting other things that are sized with em, like padding and margins.
This might seem like an odd use case, or perhaps I'm just "doing it wrong™". My use case here is essentially as follows:
I have a website that is displaying a chart containing tabular data. The chart can be viewed in two modes, summary mode and full mode. Summary mode shows a smaller version of the chart, with many data omitted. The "full" version shows everything, and takes up almost the width of the browser window.
I'd like to be able to use the same style sheet for both versions. But the "full" version should probably have a larger font-size. However, the "full" version (and just about every module aligned on the left side of the screen) has a certain left padding, which keeps all text aligned the same distance from the left edge of the screen.
If I wrap the "full" version chart in a DIV that simply increases the base em, it will also increase the padding, thus making the data in the chart not left-aligned with everything else on the screen.
So...
What are some solutions to increase the font-size without increasing padding?
Am I "doing this wrong" somehow - is it a mistake to use em units for things like padding or margins?
Use rem instead of em for paddings for example in your case, as it is not affected by the changes of other elements in the page, as it is based on the root font-size of your page (html if I remember well).
Good reading here : https://j.eremy.net/confused-about-rem-and-em/
Hopefully this is possible to do in just CSS and not javascript+css.
What I am trying to achieve is say I have a container which has a fixed width of 100px. It cannot be smaller nor larger than 100px. I have text in this container that I want to always be on one line (nowrap) and if the text is too long it will scale the font to fit the container.
Is this possible? I tried setting the width and setting font-size: 100% with whitespace:nowrap. It doesn't quite work tho. The font scales but not enough to stay in the box. Using overflow:hidden wont work because it will cut the text off which I do not want.
I know this is 4 years late,(This is for people searching this problem later on, as this was my number one hit on google) but I was able to do a small work around with {font-size: _vw}. I am not sure if this was back then. but for me personally, I set the vw of my text (with a little trial and error) to find the size I needed and stuck with it. It scales perfectly now. I guess you can come into issues with REALLY big screens and possibly mobile if the text becomes TOO small. But with just that one line of CSS it worked completely for me.
No it is not actually possible to scale a font size according to available space calculated on 'container width and how many characters are in container' with only .css
Let's say I have < img src="http://www.always-going-to-be-absolute.com/images/thisimage1.jpg" width= "some px" /> (only a .jpg or .gif). With only the width specified, are there any (any!) circumstances under which the height and width of the image may not scale accordingly, or may not scale at all, assuming there is no CSS or other code affecting it? I can't think of any circumstances (and research doesn't bring up any), but I'd like to be sure by running this by some people who have more experience than I do.
thanks!
No there are not. However, note that is it quicker to load an image of the correct size.
If any other css code or anything else is not affecting it, then I don't think there should be a problem. Try using images of correct sizes though. Helps reduce loading time and preserve quality of images as well. Try it out and if you get any scaling or other problems, you know where to ask, right? ^_^
It looks like you're scaling the images through width. From section 13.7 of the HTML 4 specification:
All IMG and OBJECT attributes that concern visual alignment and presentation have been deprecated in favor of style sheets.
I think the only two problems you may have here are:
Small images: If the image is 10x10px and is scaled to fit a 1000x1000px container, it will be pretty hard to make out what the image is supposed to be.
Thin images: If your container is 1000x1000px and the image is 250x25px, scaling the image to fit the container will result in a 1000x100px image.
OR, is there a way to at least determine the width of an image from a URL.jpg (if it's not specified in the html) without downloading the image?
In front-end JavaScript you can simply:
img.clientWidth;
img.clientHeight;
On the back-end, PHP has a getimagesize() function which returns height and width. I imagine other languages also have similar functions.
All major browsers today use page zoom by default, as opposed to "text size". "text size" options are still there, but they are a bit more hidden [especially in Chrome and Firefox]. Given this fact, is there still a reason why EMs are advantageous over PX or %? Why?
Most browsers indeed are capable of sizing pixels, and Zoom is now the more popular way which works well across browser - there is even a very simple fix the old IE6 behaviour of not being able to 'text size' (declare the original body size as a percentage - 100% will do - then fire away with pixels)
Remember too that if a user so desires, or needs to, they can set their own minimum pixel size so if they're basing their defaults on pixels why go through the nightmare of calculations..
IMHO pixels are perfect for text and borders (who needs to size a border lol..), always have been really.
em and percentage really are the same thing well they work identically, and their inheritance can be an absolute nightmare.. em's are great for widths and heights for a bit of fluidity
So no, I don't think we should still use them for text.. not that I ever did ;)
Even with the page-zoom feature turned off, most major browsers these days are capable of resizing text in px units. I think it was IE6 and under that was not capable of resizing px-based text.
I prefer specifying sizes in px just because I think it's easier than working off "relative" sizes.