How do I determine which html elements are using a font? - html

I'm working on a website that downloads about a dozen fonts and I'm trying to figure out where they're being used. I know that the page will only download a font once it's being applied to actual rendered text - I just can't tell where that text is on the page. Does anyone know any methods or tools for figuring out where a font is being applied on a large web page?
Chrome dev tools were helpful in tracking down the fonts being downloaded by the page - but how do I track down which elements are using these fonts?

Getting all elements grouped by computed font-family style
If you're interested in an extremely rudimentary solution, try this:
var fonts = {};
document.querySelectorAll('*').forEach(function(element) {
var fontFamily = window.getComputedStyle(element).getPropertyValue('font-family');
fonts[fontFamily] = fonts[fontFamily] || [];
fonts[fontFamily].push(element);
});
console.log(fonts);
It'll group the page's elements by computed font-family and then log them to the console.
At least on Chrome Dev Tools, hovering the logged element will highlight it in the page.
Note that "computed font-family" doesn't mean the element was actually rendered using that font-family, only that it's the font computed from the CSS (or lack thereof, in the case of default fonts).
Getting all elements whose computed font-family includes font X
Here's a similar approach, targeting a specific font:
function getElementsUsingFont(fontName) {
var elements = [];
document.querySelectorAll('*').forEach(function(element) {
var fontFamily = window.getComputedStyle(element).getPropertyValue('font-family');
if (fontFamily.includes(fontName)) {
elements.push(element);
}
});
return elements;
}
console.log(getElementsUsingFont('monospace'));
<span style="font-family: monospace">Dragons!</span>
Unfortunately, this will capture elements that include the given font name anywhere in its list of possible fonts. For example"
span {
font-family: "Times New Roman", Georgia, ChuckNorris, monospace, 'Comic Sans';
}
The above snippet would capture any element with that style because "monospace" is in the list. You could tinker with the conditions to push the element to try to choose more carefully (regex maybe?).

Related

How to overwrite style sheets for embedded Tweets

I am trying to overwrite CSS for embedded Tweets. For example I just tried to change font-size, but it doesn't work.
jsfiddle
css
twitterwidget, blockquote, .Tweet, blockquote.Tweet, .twitter-tweet, twitterwidget.twitter-tweet, p, blockquote p, blockquote.Tweet p, twitterwidget.twitter-tweet p{
font-size: 26px !important;
}
From the documentation you linked to, it seems you can change the styling of the fallback presentation, but not the fully styled regular version. The regular version is shown using an iframe to which, if I recall correctly, you can't apply styles from the parent page. This is probably by design -- Twitter don't want you to mess with the branding of the embedded content.
Edit: For example, if you disable JavaScript on the linked documentation page, the tweet indeed renders using the fallback markup, and styles such as yours apply.
In case someone else is searching
I have implemented a simple solution in my project recently, you can customize all kinds of styles to have it fit in seamlessly with your website branding. Hope it helps.
jQuery('.twitter-block').delegate('#twitter-widget-0','DOMSubtreeModified propertychange', function() {
//function call to override the base twitter styles
customizeTweetMedia();
});
var customizeTweetMedia = function() {
overrides font styles and removes the profile picture and media from twitter feed
jQuery('.twitter-block').find('.twitter-timeline').contents().find('.timeline-Tweet-media').css('display', 'none');
jQuery('.twitter-block').find('.twitter-timeline').contents().find('img.Avatar').css('display', 'none');
jQuery('.twitter-block').find('.twitter-timeline').contents().find('span.TweetAuthor-avatar.Identity-avatar').remove();
jQuery('.twitter-block').find('.twitter-timeline').contents().find('.timeline-Tweet-text').css('font-size', '12px');
jQuery('.twitter-block').find('.twitter-timeline').contents().find('.timeline-Tweet-text').css('font-family', "'Proxima Nova', lato, sans-serif");
jQuery('.twitter-block').find('.twitter-timeline').contents().find('span.TweetAuthor-screenName').css('font-size', '12px');
jQuery('.twitter-block').find('.twitter-timeline').contents().find('span.TweetAuthor-screenName').css('font-family', "'Proxima Nova', lato, sans-serif");
//also call the function on dynamic updates in addition to page load
jQuery('.twitter-block').find('.twitter-timeline').contents().find('.timeline-TweetList').bind('DOMSubtreeModified propertychange', function() {
customizeTweetMedia(this);
});
}
Link to my complete example

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/

CSS Change text if font-face does not exist?

Is there a way to change what text is displayed in a HTML file based on if the user has a certain font-face (preferrably without javascript)?
For example if there is a font-face 'AlphaIcons' I want to display:
<span>A</span>
Else I want to display:
<span><img src="apple.png">Apple</span>
(Giving the font to users without it is not an option in this case).
EDITED***
Check out this post - it may lead you in the right direction:
Changing Body Font-Size based on Font-Family with jQuery
In the first answer, it gives a new library that can detect fonts. If you can give it a true/false boolean, then you may be able to write in an image swap.
I believe CSS can do this already for you, using font-family prioritizes the fonts you want to use. If it can't find the first font on the user's system, it goes to the next.
http://www.w3schools.com/css/css_font.asp
Just use css like so:
span {
font-family:"AlphaIcons", "Times New Roman", Times, serif;
}
or am I missing something???
If you want to do some fancier fonts using javascript, check out Google's webfont library:
http://www.google.com/webfonts
You can't check this with pure HTML or CSS. You need Javascript to handle this problem.
Go through the following steps:
Embed the font files via font-face
Detect if font-face is avaiable in the clients browser with javascript. e.g. modernizr can do the trick
When font-face isn't available, insert the image into the span with the following code:
HTML
<span data-image="apple.png">A</span>
Javascipt
// check font face compatibility
if (!Modernizr.fontface) {
// replace each span content with the right image
$('span').each(function(){
// get the image
var image = $(this).data('image');
// insert this image into the span tag
$(this).html('<img src="'+image+'" />');
});
}
data-attributes are only one of many possible solutions. Just a little hint.
In general, there a no methods to check the availability of fonts without Javascript.

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 !