Change size of specific font - javascript

Assume there are two languages (English and Chinese) in a web page. I need to increase size of Chinese font on my web page. i don't need to change size of English fonts. only need to increase size of Chinese fonts. How could i do this ? are there any way to do this using java script or any other technology ?

Expanding on #MichaelRobinson's comment:
You should have the text marked up by using the lang attribute and then you can use the CSS attribute selector (particularly the hyphen-separated variant) or the :lang pseudo-class to select those texts, e.g.:
[lang|=en] {
font-size: 12px;
}
:lang(zh) {
font-size: 15px;
}
<h1 lang="en">English</h1>
<p lang="zh">汉语</lang>

I have not test it, but I suspect the following will work if the font families are different. Using query you can retrieve css values of an element. If you retrieve the font-family value, you should have the font name. Based on that you can determine if this element has a chinese or an english font. If the font-family is the chinese fonts, you can set font-size to be bigger.

Related

How to increase space width of text using css on condition of language used?

I am using "Nafees Nastaliq" font on a wordpress website. This font provides English and Urdu characters to write in both languages. Urdu language doesn't need that width of space that english language needs. So, the font contains a narrow width of "space" character. On the website, I have to type English and Urdu both sometimes in the same div. So, is their anyway to increase the width of "space" character only when their is a space character between two characters which are both in English? Here is an example screenshot, how it look likes:
In the above example, system should check that if their is English words like "No Categories", so the space between "no" and "Categories" should be maximized in width just like it should be in English. But for the urdu side "اصنافِ سخن" it should not increase the width of space.
Here is CSS:
li {
font-family: 'Nafees Nastaliq' !important;
font-weight: normal !important;
}
Pseduo Code I have : But how to implement?
Select a "space"
check if left side character is in (A,B,C.. a,b,c...) AND check if right side character is in (A,B,C.. a,b,c...)
If yes, then increase_space_width()
else use_space_as_it_is() or do_nothing();
Put all your characters or words that you want to write in Urdu language, in a span with a specific class name. For example:
HTML CODE:
<p>
This paragraph contains multiple languages such as Englsih and <span class="urdu">اردو</span> at the same time.
</p>
CSS CODE:
p {
font-familly: "English Font", "Urdu Font";
font-weight: bBold;
}
p .urdu {
font-weight: normal;
/* Your custome style for Urdu language only */
}

Customizing google custom search engine. Especially with changing the title font

I have a custom google search engine on my website. The problem is that I am facing an issue where I just can't change the font of the search result titles. So when I try to resize my webpage the results looks too large and jumbled... I did managed to change the snippet size by:
.gs-snippet{
font-size: 3vw;
}
However, when I tried to change the title like this
.gs-title {
font-size: 3vw;
}
it just doesn't budge.... I do not know why it works with .gs-snippet but not with .gs-title. If you can figure out my issue of what I am doing wrong or if you can propose a different way to change the title then that would be great!
Thanks a bunch,
Archie
If you're overwriting a CSS style from Google, then !important is your answer. Read about it here. !important overrides other styles that don't contain !important.
.gs-title {
font-size: 3vw !important;
}
Also, are you sure you want to use 'vw' as your unit for font size? I would suggest using either 'px' or 'em'. That may be the issue, or at least it's worth a shot.

CSS for browser default font

If web designer has a full control over the entire code, it is easy to use browser default font - just don't change any font style and you got it.
However if there is not a full control over it and for example there is some font-related style defined on html or body element, or font-related CSS style for * { ... }, then there is a need to redefine font style to not inherit modified styling.
Is there any way, CSS, pure JavaScript or jQuery solution that would allow explicitly set browser default font for specific element?
Unfortunately, there is no simple "initial value" for font-family. It is, as you know, user-agent dependent.
Perhaps the closest you can come is by using a font keyword. font-family:serif; will use whatever the browser considers to be the default serif font. font-family:sans-serif; is the same, for sans-serif.
This is the closest I can suggest, sorry!
Yes. Do
.my-selector {
font: initial;
}
Well, I understand your requirement sounds simple but UNFORTUNATELY this is just a limitation of web technologies. We can not read browser's / system's font using either CSS or jQuery in any way.
The fonts rendered either by browser or system can not be read by web application. It is not designed that way. All we can do is apply font-family:serif or something similar to that.
I tried couple of POC for you but I could not retrieve the browser default settings.
All we can do is create a new ticket in W3C for font-family reset and let's keep hoping that they include something like that in future..
I would still do some digging for you and get back to you if I find something. This definitely is one very important requirement..
I dealt with such situations before but the other way. Just like overriding with the required font.. That's it!
However,
There is an alternative for this. I won't say it is the best way but we can try this :
Prpeare a JavaScript that reads the user agent, something like this..
var setDefaultFont = {
Android: function () {
$('*').css( "font-family", "//FONT USED IN ANDROID BROWSERS" );
},
BlackBerry: function () {
$('*').css( "font-family", "//FONT USED IN BLACKBERRY BROWSERS" );
},
iOS: function () {
$('*').css( "font-family", "//FONT USED IN iOS BROWSERS" );
},
Opera: function () {
$('*').css( "font-family", "//FONT USED IN OPERA BROWSERS" );
},
Windows: function () {
$('*').css( "font-family", "//FONT USED IN WINDOWS BROWSERS" );
}
};
We can be specific to Windows browsers if we need..
I know this is not the best solution but it should work
This has already been answered, but I found a solution that may be helpful:
*, html, body {
font-family: inherit !important;
}
This tells the root elements of the HTML document to inherit their font from the parent. In this case, the parent would be the browser's user agent styles, so your fonts will inherit from the default.
I've been coming across your questions on a somewhat regular basis it seems, and they appear largely theoretical cases. In any case, the others have more or less pointed you in the right direction, but I'm going to sound off just to re-enforce some points.
Explicit Means
There is no existing built-in functionality in Javascript nor CSS to explicitly assign the browser default font. This is outright impossible with the present CSS specification, however, you could, theoretically, utilize Javascript to get the name of the font for explicit use. I will not attempt to write the Javascript code because I predict it would be time consuming to accurately portray, but I will at least try to provide light on how it may be possible. Additionally, I under no circumstances am implying the following idea will work, but I believe it could work.
Conceptual Structure
I'd suggest the following:
Create 5 span nodes as following:
<span style="font-family: monospace;">Monospace Text</span>
<span style="font-family: sans-serif;">Sans-Serif Text</span>
<span style="font-family: serif;">Serif Text</span>
<span style="font-family: fantasy;">Fantasy Text</span>
<span style="font-family: cursive;">Cursive Text</span>
The five generic font families are the browser defaults, however this does not immediately resolve your question because a) it does not give us an explicit font-name that a person is using and b) the default font for a given element is one of these, not all of them.
Pay respect to the words "a person is using"; what the browser would have used by default versus what the user has set the default font to and subsequently sees by default is very different; this is why Rahul Patil's structure would be imperfect, despite I'll admit this was my original assumption and in most cases he would be absolutely correct.
Note
Certain elements default to different generic font families, and are not consistent from browser to browser. Code blocks, for example, are typically rendered as monospace by default. For consistency, you will need to utilize a CSS Reset file.
Use something like this to detect the font:
http://www.lalit.org/lab/javascript-css-font-detect/
This font detection is not ready for what you need out of the box, you need to modify it to parse a list of fonts and match it to the default. This script only shows the theory behind what may work for you; character dimension comparison, as typically all fonts are different in size in height and width, and that difference is easier to detect when the font size is large (hence why it uses 72 pixels).
Caveat
One major caveat I can think of is that this will typically only be able to detect ASCII / Romanized fonts and may not apply to Unicode sets; I'm sure you could modify it, but it may computationally intensive to accurately detect the font. You would likely retain a database of fonts applicable to each language, and you would need a consistent way to detect the language settings for the browser (and not the system); this is not easy, or even possible to my knowledge in a consistent way.
You can obtain an idea of the language settings using the "navigator.language" and "navigator.userLanguage" variables in Javascript, but these, to my knowledge reflect the system language and not the language used by the browser. Server side code would likely be necessary for this to be achieved with any degree of accuracy.
Build a Javascript routine to parse a list of fonts, and compare said fonts against the dimensions of the elements created in Item 1 of this list. You can get a list of fonts necessary to parse by using Flash. I'd suspect a Java applet would be able to do this as well. This may be helpful (JS+Flash):
https://github.com/gabriel/font-detect-js
http://font-detect.s3.amazonaws.com/index.html
This should, theoretically, give you the explicit font name used by the person. You could extend upon this and also get the default generic fonts for each individual element type (e.g. sans-serif, serif, monospace, fantasy and cursive), but I suspect this would be CPU intensive and would be best used with some of type of system level application; e.g. caching the given conditions for a given member, and create the CSS catered to that specific user.
Implicit Means
A number of people suggested some other means, Daniel Lisik in particular highlighted what I may do, with respect that is "serif" declaration is invalid in it's context, since declaring "sans-serif !important" would assign "sans-serif"; you can actually build up using "inherit !important" and make strict font-family changes with the given "font-family !important". For example:
<html>
<head>
<style>
div { font-family: Cursive; }
p { font-family: Inherit !important; }
.serif { font-family: Serif; }
.sans-serif { font-family: Sans-Serif; }
.serif-important { font-family: Serif !important; }
.sans-serif-important { font-family: Sans-Serif !important; }
</style>
</head>
<body>
<div>
Normal Div Text
<p>Default P Text</p>
<p class="serif">Serif</p>
<p class="sans-serif">Sans-Serif</p>
<p class="serif-important ">Important, Serif</p>
<p class="sans-serif-important ">Important, Sans-Serif</p>
</div>
</body>
</html>
"Normal Div Text" would be in "Cursive"
"Default P Text" would be in "Cursive"
"Serif" would be in "Cursive"
"Sans-Serif" would be in "Cursive"
"Important, Serif" would be in "Serif"
"Important, Sans-Serif" would be in "Sans-Serif"
If you need any clarifications, just ask and I'll edit as necessary.
References
http://www.w3.org/TR/CSS21/fonts.html#value-def-generic-family
http://www.w3.org/TR/CSS2/cascade.html#value-def-inherit
Resources
http://www.lalit.org/lab/javascript-css-font-detect/
https://github.com/gabriel/font-detect-js
http://font-detect.s3.amazonaws.com/index.html
Your question isn't very clear. I don't know if I've misunderstood but I will try to help anyway.
If you're really determined to respect the default browser fonts and font sizes set in the browser then this should do the trick:
html, body {
font-size: 100% !important;
}
body {
font-family: serif !important; /* Or sans-serif, monospace or whatever is appropriate. */
}
Then the inheritance should kick in, as long as you don't set font stacks or sizes on everything else.
Bear in mind, the cascade order comes into play here.
This would override all types of stylesheets except for user stylesheets containing !important declarations set on relevant elements. In this particular case, you just need to respect the user's choices.
Personally, I would just write this:
html, body {
font-size: 100%;
}
body {
font-family: serif; /* Or sans-serif, monospace or whatever is appropriate. */
}
Then I would refrain from setting font stacks on everything else simply because, if an user was really determined to have his own way, he's likely to have set !important declarations on the elements that matter to him, like fonts, etc in his own user stylesheet - and this will override a normal author stylesheet (which omits !important declarations).
The others are right, the initial keyword is a viable method as long as you're not worried about IE because, no version of IE supports it.
So I think your best bet is to look for a polyfill of some sort for IE, or create your own. Modernizer might be worth a look or if you want to create your own, there's polyfill.js. I don't know anything about creating polyfills, sorry!
I know it's not a perfect answer (it's 6am and I've been up all night), but I hope it helps you in some way.
P.S. I've just realised you might have been referring to hijacking a browser's settings to set the default font you want set, regardless of how the user has it set up. It would be pointless doing that. In any case, I don't think it would work if an user has his own user stylesheet especially if it has !important declarations set on the font properties.
You can try it in this way:
.element {
font-family: -webkit-body;
font-family: -moz-body
font-family: -o-body
font-family: body
}
And also try the following which is also working fine:
.element {
font-family: none;
}
Are you simply talking about element selectors?
div {
color: red;
}
input {
font-family: sans-serif;
}
As per my understanding, font-family: none; works fine for me in chrome and firefox.
.element {
font-family: none;
}
This code will inherit to default browser font.
Use a generic font-family. The browser will choose which font is "best" for him :)
font-family: serif;

Different fonts inside a label

If I have labels such as "1234B", "5678M"... How can I change this label so the letter at the end is smaller size than the size of numbers?
<p>1234<span class="smaller">B</span></p>
.smaller {
font-size: 5px;
}
The simplest way is to use the small element (which is still allowed in HTML5, though with contrived “semantics”, but to browsers it still means just smaller font size):
1234<small>B</small>
You can then use CSS to tune the font size reduction, e.g. with
small { font-size: 80%; }
However, this produces typographically bad results, since different font size implies different stroke width, so the letters will look thinner, too, in addition to being smaller. In typography, one would probably use small-caps glyphs of the font instead (though in typography, one would normally rather try and make digits and letters match in size, rather than unmatch!). This is in principle possible on web pages too (using font-feature-settings: "smcp", with prefixes), though still rare, and it requires a font that has such glyphs available (like Calibri, Cambria, or Palatino Linotype).
watson has it.............. but let me think CMS thinking......if you just want to lowercase the only letter and the numbers stay put you can do this:
HTML:
<div style="text-transform: lowercase;">3529M</div>
or
<div class="lowMe">3529M</div>
<div class="lowMe">5546D</div>
CSS:
.lowMe {text-transform: lowercase}
else I would just do span as it was mentioned...

Set a font specifically for all numbers on the page

For my webpage, I chose a font that works well for all the letters. However, for all numbers I'd like to use a different font.
Is there a way that I can set a CSS rule to target all the numbers on the page?
If I can't do it strictly with CSS, my first thought is to use regular expressions to surround all numbers with a span with a "numbers" class and apply a font for that class. Is there a better way to do this?
You can do it in JavaScript relatively simply by traversing the document so that you wrap any sequence of digits in a span element with a class attribute and declare font-family for it in CSS.
It’s possible in principle in pure CSS, too, though only WebKit browsers currently support this:
#font-face {
font-family: myArial;
src: local("Courier New");
unicode-range: U+30-39;
}
#font-face {
font-family: myArial;
src: local("Arial");
unicode-range: U+0-2f, U+40-10FFFF;
}
body { font-family: myArial; }
Finally, and most importantly, don’t do it. If you are dissatisfied with digits in a font, don’t use that font.
Surrounding your numbers with a <span class="number"> sounds like a good, semantically sound approach.
In fact, CSS3 doesn't offer an alternative, and I don't see anything forthcoming in CSS4. If you look at the latest CSS3 recommendation, it doesn't specify any content selector, but the old 2001 candidate recommendation was the last version to provide the :contains() pseudoclass.
This would let you match an element that contained numbers:
/* Deprecated CSS3 */
p:contains(0), p:contains("1"), p:contains("2") {
background-color: red;
}
Even if this were actually available, it matches the p, not the number, so the whole p would be styled...not what you wanted. The CSSWG is kicking around these ideas for formatting numerical content...still not what you wanted.
To apply CSS to the numbers, there is no avoiding some additional markup.

Categories

Resources