Is there any way to set the width of ` ` or `&nnbsp;` to zero? [duplicate] - html

Is it possible to change the size of a specific character using CSS?
For example, in a paragraph I am using font-face: Arial; font-size: 12pt;. I would like that only the letter "a" would appear in size 10.
Is there a way to do that?
Thanks.

No. Except for :first-letter and other pseudo-classes, you can't target single characters using CSS. You'd need to wrap the character into an element (e.g. a <span>) to specify a style for it.
You can work around this using Javascript - there are jQuery based solutions for this here on SO. But it's kludgy.

I don't believe you can, consider using a text editor to do a find/replace 'a' with <span class='a-xxx'>a</span> and then you can use css to do .a-xxx { font-size: 10px; }

Sorry for digging up this 11 year old thread, but I just now ran into this problem as well.
My use case was to make Tibetan characters bigger on a specific website, because they were barely readable compared to latin characters of the same font size.
As I understand, all the answers here are outdated, as I found the #font-face css at-rule that covers this. It accepts a Unicode range, so should work for a single character as well. Supported by all modern browsers.
So all I needed to do is add the following to my css, which will define a new font called 'Yangpo Tibetan Uni' (of course, modify the url parameter to your liking):
#font-face {
font-family: 'Yangpo Tibetan Uni';
src: url("./util/fonts/YagpoTibetanUni-x3jnj.ttf") format("truetype");
unicode-range: U+0F00-0FFF;
}
And then use your newly defined font like so:
body {
font-family: /* main font */ 'Raleway', /* and then your override */ 'Yangpo Tibetan Uni';
}
OK, replacing the whole font is one thing, but how to make one character bigger? #font-face also accepts size-adjust parameter (BEWARE!!! This one parameter will not work in Safari, but there are others like font-stretch - take a look what fits your needs):
The size-adjust CSS descriptor defines a multiplier for glyph outlines and metrics associated with this font. This makes it easier to harmonize the designs of various fonts when rendered at the same font size.
So make the #font-face url point to the original and manipulate the size-adjust value (or other parameters, as per docs).

No. You can only target elements (such as a span that contains a single letter) and pseudo-elements (such as :first-letter).

You can't do this in a cross-browser-consistent and simple way without javascript.
I recommend 'captify' for jquery.

Also for accessibility and compatibility and all that it is best not to define specific fonts (try font-family) and sizes in terms of large, larger then use % ontop of that, and define them all as custom span/div styles
e.g
bigletter (font-size:150%);

Related

Defining different font size for same element

I would like to use an embedded font along side Arial as a substitute. The embedded font requires a much larger font-size.
How can I make sure that Arial displays at 15pt, and Bebas displays at 20pt, for the same element. (For the same piece of text)
Thanks!
*Let me explain further:
I have a string of text. I want it to display as Bebas or Arial. When Arial is loaded as a substitute, it needs to have a different font-size and weight, as sharing the font-size doesn't work well for these fonts (Bebas is small).
You could use a script like FontChecker to check if a font is available. It relies on MooTools and gets called like this:
window.addEvent('domready',function() {
var fc = new FontChecker();
if (!fc.check('Bebas')) {
$$('.someclass').setStyles({'font-size': '15pt'});
}
});
If Bebas isn't available, it sets the font size for all elements with class someclass to 15pt.
Your CSS file:
.someclass {
font-family:Bebas,Arial,sans-serif;
font-size:20pt;
}
If you don't use MooTools, maybe there's a similar script for other libraries or vanilla JS (=plain JS without libraries). Or just rewrite it, it's quite short.
edit:
Some other scripts (I don't know them, I only use FontChecker):
jQuery: http://remysharp.com/2008/07/08/how-to-detect-if-a-font-is-installed-only-using-javascript/
Vanilla: http://www.samclarke.com/2013/06/javascript-is-font-available/
Another vanilla: http://www.lalit.org/lab/javascript-css-font-detect/

Unicode down triangle doesn't show up in IE9

I'd like to include a down triangle in my CSS dropdown menu. It works correctly in every browser, except for IE9.
In normal browsers (Firefox, Opera, Chrome, etc.) it looks like this way:
However in IE9 it shows up like this:
The CSS code:
.parent-down > em:after,
.parent-down > a > em:after {
content: "\25be";
}
Any idea what can be the problem and how could I fix it?
You need to declare the font family for the (pseudo-)element containing the special character so that you use only fonts that are known to contain that character. For example, add the following rule into your declaration:
font-family: Arial Unicode MS, Lucida Sans Unicode, sans-serif;
When you declare Helvetica, as you say (in a comment) you do, Windows treats it as Arial by its own special internal rules (if Helvetica is not available, and it usually isn’t). Since Arial does not contain the character, the browser should check the different fonts in the system in some order set by its settings to find one that contain the character. Browsers may fail in doing so, and IE often does.
In this case, it is probably better to use another, more visible character, which happens to have better font coverage. The basic principle is still that special characters need special attention when declaring fonts. For generalities on this, see my Guide to using special characters in HTML.
I came to the solution by accident.
However IE9 doesn't show the "\25be" (▾) character, it shows the "\25bc" (▼).
It's the same down triangle, however somewhat bigger. So now I use this one and change its size with CSS, so it looks identical with the original version.
Don't ask why, IE9 is quite strange.
My new code:
.parent-down > em:after,
.parent-down > a > em:after {
content: "\25bc";
font-size: 8px;
vertical-align: 2px;
margin-left: 2px;
}
There is a bug in IE9 that causes the font of a container to be set to the font of the first element of that container. Try to set a :before selector like this:
.parent-down >em:before,
.parent-down> a > em:before {
content: '';
}
This will force IE9 to recognize that the font assigned to the container is the font meant to be used
See this site, which describes a similar issue and gives this workaround, which solved this very tricky problem for me.

Using CSS, is there any way to display different characters using different font?

My HTML text is just like this:
<p>abcdefghijklmnopqrstuvwxyz</p>
What I want is to display a-n using "Times New Roman", and display o-z using "Courier New", and this should be done using CSS, say, with no change to the HTML text.
Simply stated, I want CSS to automatically choose the specified font corresponding to which character it is.
a-n should be displayed using "Times New Roman";
o-z shoule be displayed using "Courier New".
Is there any way to accomplish this?
If this problem can be solved, another problem can be solved: display different language using different font.
Yes you can, using something called unicode-range It works in all modern web browsers: https://developer.mozilla.org/en-US/docs/Web/CSS/#font-face
BTW, more info about this from http://24ways.org/2011/unicode-range
Live example: http://jsfiddle.net/jfcox/3LQyr/
<style>
#font-face {
font-family: Foobar;
src: local('Times New Roman');
unicode-range: U+61-6E;
}
#font-face {
font-family: Foobar;
src: local('Arial');
unicode-range: U+6F-7A;
}
body{
font-family:Foobar;
}
</style>
<p>abcdefghijklmnopqrstuvwxyz</p>​
If the characters belong to different writing systems, such as Latin and Hebrew, or Cyrillic and Greek, browsers often automatically use different fonts for them. However, this only happens to the extent that the page does not specify fonts, i.e. this relates to default fonts only, and the fonts used are determined by browser defaults and browser settings controlled by the user.
Although the technique described in JayC’s answer gives a partial solution, you get much better browser coverage by distinguishing the texts in different languages in markup. In a bilingual document, it suffices to use markup for texts in one of them (the one used less, for practical reasons). Using class as in gutch’s answer gives best coverage, but nowadays support to language selectors in CSS is so widespread that you might consider using the more logical lang attribute instead, e.g.
<h1>Hello − <a lang=ru>Привет</а></h1>
Then you would use rules like
[lang=ru] { font-family: ...; }
(My example uses an <a> element effectively as a shorter variant of <span>. Formally, this is possible only when the text is not inside an outer <a> element.)
However, for visual style, just the opposite of font selection by language would be needed. It really looks odd if the “e” in “Hello” is different from the Cyrillic “е” in “Привет” on the same line. It is almost always better to use the same font for all languages in a document, if possible. This means selecting a font that works for all of them.
You can't use CSS to change the font of particular characters as you describe, because the CSS selectors don't select individual characters — they select HTML elements.
So you would need to create elements around the blocks of text that need specific fonts. Ideally you would do that in server-side code, though I don't know whether that's practical for you. Your server would need to output HTML like this:
<p><span class="languageOne">abcdefghijklmn</span><span class="languageTwo">opqrstuvwxyz</span></p>
Then you apply the fonts as appropriate in your CSS:
.languageOne { font-family: "Times New Roman", serif; }
.languageTwo { font-family: "Courier New", monospace; }

Applying a single font to an entire website with CSS

I want to use a single font named "Algerian" across my whole website. So, I need to change all HTML tags and I don't want to write different code for different tags like:
button{font-family:Algerian;}
div{font-family:Algerian;}
The method written below is also highly discouraged:
div,button,span,strong{font-family:Algerian;}
Put the font-family declaration into a body selector:
body {
font-family: Algerian;
}
All the elements on your page will inherit this font-family then (unless, of course you override it later).
*{font-family:Algerian;}
better solution below
Applying a single font to an entire website with CSS
The universal selector * refers to all elements,
this css will do it for you:
*{
font-family:Algerian;
}
But unfortunately if you are using FontAwesome icons, or any Icons that require their own font family, this will simply destroy the icons and they will not show the required view.
To avoid this you can use the :not selector, a sample of fontawesome icon is <i class="fa fa-bluetooth"></i>, so simply you can use:
*:not(i){
font-family:Algerian;
}
this will apply this family to all elements in the document except the elements with the tag name <i>, you can also do it for classes:
*:not(.fa){
font-family:Algerian;
}
this will apply this family to all elements in the document except the elements with the class "fa" which refers to fontawesome default class,
you can also target more than one class like this:
*:not(i):not(.fa):not(.YourClassName){
font-family:Algerian;
}
* { font-family: Algerian; }
The universal selector * refers to any element.
Ensure that mobile devices won't change the font with their default font by using important along with the universal selector * :
* { font-family: Algerian !important;}
As a different font is likely to be already defined by the browser for form elements, here are 2 ways to use this font everywhere:
body, input, textarea {
font-family: Algerian;
}
body {
font-family: Algerian !important;
}
There'll still have a monospace font on elements like pre/code, kbd, etc but, in case you use these elements, you'd better use a monospace font there.
Important note: if very few people has this font installed on their OS, then the second font in the list will be used. Here you defined no second font so the default serif font will be used, and it'll be Times, Times New Roman except maybe on Linux.
Two options there: use #font-face if your font is free of use as a downloadable font or add fallback(s): a second, a third, etc and finally a default family (sans-serif, cursive (*), monospace or serif). The first of the list that exists on the OS of the user will be used.
(*) default cursive on Windows is Comic Sans. Except if you want to troll Windows users, don't do that :) This font is terrible except for your children birthdays where it's welcome.
Please place this in the head of your Page(s) if the "body" needs the use of 1 and the same font:
<style type="text/css">
body {font-family:FONT-NAME ;
}
</style>
Everything between the tags <body> and </body>will have the same font
Ok so I was having this issue where I tried several different options.
The font i'm using is Ubuntu-LI , I created a font folder in my working directory. under the folder fonts
I was able to apply it... eventually here is my working code
I wanted this to apply to my entire website so I put it at the top of the css doc. above all of the Div tags (not that it matters, just know that any individual fonts you assign post your script will take precedence)
#font-face{
font-family: "Ubuntu-LI";
src: url("/fonts/Ubuntu/(Ubuntu-LI.ttf"),
url("../fonts/Ubuntu/Ubuntu-LI.ttf");
}
*{
font-family:"Ubuntu-LI";
}
If i then wanted all of my H1 tags to be something else lets say sans sarif I would do something like
h1{
font-family: Sans-sarif;
}
From which case only my H1 tags would be the sans-sarif font and the rest of my page would be the Ubuntu-LI font
in Bootstrap,
web inspector says the Headings are set to 'inherit'
all i needed to set my page to the new font was
div, p {font-family: Algerian}
that's in .scss
*{font-family:Algerian;}
this html worked for me. Added to canvas settings in wordpress.
Looks cool - thanks !

how decrease font size?

i am using this css.
.text_style3{
font:normal 8px Helvetica;
color:#f7922c;
}
i want to it more small but after 10px this is not working. i have used 7px, 6px, 5px etc. but this is not working.
so how can i decrease size. this css is not working in mozilla.
That is a rule specified by the browser, usually 10px is the minimum font size allowed in a default Firefox installation.
Try it by going to Preferences -> Content -> Fonts & Colors -> Advanced -> Minimum font size.
Any font size smaller than 10px will be almost non-readable. The rule is there to ensure better accessibility.
Hope that helps.
You most likely have a CSS Specificity issue, where another style is overriding the style you were expeceting to see.
You can use tools like Firebug for Firefox to see what style the browser is using and where in your code it has come from.
You may need to make your font size declaration more specific, by changing the selector, or even methods like using the !important operator or making the style inline in your HTML.
See these links for more information on ways to handle this:
http://htmldog.com/guides/cssadvanced/specificity/
http://www.w3.org/TR/CSS2/cascade.html
are you sure you arent resetting text_style3 after this to have a normal style? it may be that you are styling div p or span (or any other containing tag) after you do this one.
if you are not, break the font style down to the following:
.text_style3{
font-weight:normal;
font-size: 8px;
font-family: Helvetica;
color:#f7922c;
}
You have some really good answers here, and they are probably correct (min-font size, use !important to override other CSS). I would add to try to use em's, once you get use to them, they seem to work alot better than straight px, and they resize better (my opinion) for users who need to increase the font size for readability.
Also worth noting is that Cascading Style Sheets are just that: Cascading
Levels:
stylesheet
style tag in file
style in code
1 will be overwritten by definitions in 2.
Both 1 and 2 will be overwritten by definitions in 3.
The closer the CSS is to the actuall item/text being displaied, the more important it is.
As Mauro wrote, if the tag you are doing class="text_style3" on has some other definition of text size this may also affect the display.
It may be that you have a minimum font size set in your browser, check Tools > Options > Content and choose Advanced in the fonts and colours section and change the minimum font size to None.
It is likely your minimum font size is set to 10px (smaller sizes are unreadable)