Why does the order of the link elements in <head> matter? - html

From Code Academy's Make a Website: CSS Styling:
<head>
<link href="font.css" rel="stylesheet">
<link href="main.css" rel="stylesheet">
</head>
"Inside the head element are two link elements. The order of these
link elements matters.
The browser first sees the link for the custom font font.css, and
makes the font available to use in the page.
Next the browser sees the link for main.css. Since the browser now
knows about the custom font, we can use it from main.css to style
elements."
I want to have an intuition/answer as to why does the order matter (link to fonts before the main CSS stylesheet)?
I tried to do it in the other order (link to main stylesheet before link to fonts) and it still worked.

Order matters when you're overriding CSS definitions. This is part of the "cascade" of cascading style sheets. If main.css does not contain any font definitions, then the order doesn't matter.
For example: you were given a default CSS file from a designer, but you need to tweak it a little. Instead of editing the default.css file, you create a custom.css file and change only the handful of definitions that you wanted to tweak. Now the order matters.
<head>
<link href="default.css" rel="stylesheet">
<link href="custom.css" rel="stylesheet">
</head>
If you were to put custom.css first, then your changes would never appear.
Here's an article that gets into various levels of further detail. Warning, it can make your head spin.

Related

CSS Loaded but not being applied

I know it's already been asked but that person had a typo, and the answer mentioned firebug so it's not current (and is closed).
In WordPress, I noticed that main file, style.css is not being applied, specifically:
The file IS loaded correctly
Its styles are NOT being applied
When I inspect element for an element I know is being targeted by style.css, nothing from style.css is there (styles tab of inspect element) at all. I've inspected several elements.
It's loaded through WordPress as so (an unchanged call generated with _underscores):
wp_enqueue_style( 'themexzz-style', get_stylesheet_uri(), array(), _S_VERSION );
This is the produced link (and clicking on it works like it's supposed to):
<link rel='stylesheet' id='themexzz-style-css' href='http://localhost:8012/themexzz/wp-content/themes/themexzz/style.css?ver=1.0.0' media='all' />
I'm using
Xampp with php 7.2.3
with wordpress 5.5.3
meta charset="UTF-8"
Database collation is utf8mb4_unicode_ci
It's an underscores generated theme
Thanks in advance.
#Xhynk - You were right. It was the:
you may have invalid CSS way at the beginning of that file, blocking
all the rest of the CSS being applied
As it is an underscores theme and I apply my own CSS from another file, it didn't even cross my mind that there might be invalid CSS in style.css
Thank you #Xhynk
The position of CSS files in head tag also matters,
Assume that you have loaded 2 CSS file in the head tag
<link rel='stylesheet' href="..../style.css">
<!--(have property of background color blue and text white)-->
<link rel='stylesheet' href="..../mycss.css">
<!--(have property of background color white and text black)-->
if you place tags in this order
<head>
<link rel='stylesheet' href="..../style.css">
<link rel='stylesheet' href="..../mycss.css">
</head>
Both the files will be loaded but CSS will be applied of the myss.css file,
this happens because of CSS overwrite
You can change the position of the link tags to get your desired output

<link rel="stylesheet" type="text/css" href="css2/css2.css"> not working

I've been trying to connect html with the CSS.
I've checked that:
The stylesheet path of the css is correct, and it is: css2/css2.css
The <link rel="stylesheet" type="text/css" href="css2/css2.css" /> code is well written and I think it is.
I also tried to try several code editors in case it was a preview problem, I've already tried Atom and brackets and the two do not show that the CSS gets connected.
HTML code :
The html close tag is written too at the bottom.
CSS
here is where the html and CSS file is placed
As you have mentioned in your statement that css files is css/css2.css
so it means you should link css file by this code.
<link rel="stylesheet" type="text/css" href="css/css2.css" />
You added css2 instead of css as folder name
This code will 100% work, Just make sure your HTML file and CSS2 folder need to be on same level (in same folder).
otherwise this CSS file not link to your HTML.

Applying single Font to Entire web documents

I want to apply single font for each html pages through out my website.
So far I tried this:
* { font-family:Nyala; }
but this works for only one page.
You should include this CSS style to each html page you wish it to take affect in.
Each HTML page should include this:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
And the mystyle.css is:
* { font-family:Nyala; }
Note: see #Raptor comments in order to improve your code.
Move * { font-family:Nyala; } in to a external css file like style.css and call it to the head section of the all html file you want like below.
<link type="text/css" rel="stylesheet" href="style.css" />
Don't forget to give exact file path where you keep your .css file.
Add Below css
body{font-family:Nyala;}
To make a css global you can place it in one common css file and make it's reference on every page.
Another point is if you are using master page in your website then you can place the reference of that css file once in the master page and it will automatically inherited on every content page which have master page applied.

Force CSS Style

I have a wordpress theme which have its default styling, I developed a new page template and wrote some styles specifically for it but for some reason the styles aren't being applied.
The styles include table cell-padding.
When inspecting the code, the css I applied is being stroked out. How can I force that CSS to apply itself. I have already tried using the !important but still no success
usually if you are developing your own theme for wordpress that there are no problems with the application of styles possible presence of an error in the connection of style, even when finished remaking theme personally I have no problems
Sometimes you have to use all classes and ids of div to declare style for it. Anyway you shouldn't use !important, but try to describe your div precisely.
Write CSS (stylesheet) at the end of the all css files in header, Write css in Hierarchy level. select parent class or id and written down css
CSS Declaration in Header
<head>
<link rel="stylesheet" type="text/css" href="defaultwordpress.css">
<link rel="stylesheet" type="text/css" href="own.css">
</head>
Can you make sure your own CSS code is called after the main CSS code ?
Maybe by changing the order of CSS calls in your head section like this :
<head>
<link rel="stylesheet" type="text/css" href="defaultWordpress.css">
<link rel="stylesheet" type="text/css" href="myOwn.css">
</head>
edit : default Wordpress css first :-)

Placing CSS information on an HTML page

Where are all of the places you can put CSS style information on an HTML page?
I know you can place CSS style info in the head of an HTML page, where else is it valid to put CSS elements?
I would like to place my CSS someplace else on the page due to inheritance, e.g:
<style type="text/css">
...
</style>
You can use
<link rel="stylesheet" type="text/css" href="style.css" />
in the head to link to an external stylesheet.
You can also have inline style attributes, such as
Hello
And you can also set the styles in your scripts, e.g. using jQuery (which can go where ever your script is):
$('textBox').css("font-weight", "bold");
However, it is good practise to try to keep all the style information in one standard spot, i.e. the head of the document - it makes it easier for others to maintain your work for you.
Note that if you really want to override a particular attribute, the best way to do it is to use the !important option, such as
color: red !important;
You can use this with any of the methods listed above, and it will override any later settings that conflict.
You can link external stylesheets in the <head> block. You can use more than one stylesheet, and they are loaded in order (in this example, both screen.css and print.css override some elements of style.css.
<link rel="stylesheet" href="style.css" type="text/css" media="all" />
<link rel="stylesheet" href="screen.css" type="text/css" media="screen" />
<link rel="stylesheet" href="print.css" type="text/css" media="print" />
You can import it from an external stylesheet in the <head> block:
<style>
#import url(style.css)
</style>
Or import it using another method:
<style type="text/css" media="all">
#import "style.css";
</style>
You can put the CSS in the <head> block:
<style type="text/css">
p {font-face:Arial;}
</style>
You can put the CSS inline into the html:
<ul> <li style="list-style:none">Item 1</li></ul>
You can add the CSS to the DOM via javascript:
function addCss(cssCode) {
var styleElement = document.createElement("style");
styleElement.type = "text/css";
if (styleElement.styleSheet) {
styleElement.styleSheet.cssText = cssCode;
} else {
styleElement.appendChild(document.createTextNode(cssCode));
}
document.getElementsByTagName("head")[0].appendChild(styleElement);
}
You can specify it "inline"
<div style="border: 1px solid red" />
Other than that I'm not used to place it anywhere else than separate files / <head>
Off hand:
In other documents. Include CSS files with the LINK element:
<link href="style.css" type="text/css" rel="stylesheet">
Inline with HTML elements:
<h1 style="color: red">Title</h1>
That is usually the only two other places you will put CSS. You can also apply CSS to documents via JavaScript.
As the others have said, non-inline CSS belongs in the head, if you want to write syntactically-correct code. Check the schema if you're not sure.
If you need to generate the CSS during dynamic page creation, you can easily inject it into the DOM, at the bottom of the head, using javascript:
document.getElementsByTags('head')[0].appendChild( -- css here --);
Be forewarned that this will cause your page rendering to slow down and "blink," as the browser must restyle the page when your new CSS is inserted. The same thing will happen if you ignore the schema and place your CSS in the body.
This leads to a poor user experience.
If you're concerned with inheritance in css then you need to remember this general rule. 
Inline styles > everything else. 
Tag > id
Id > class
Then you have combinations of these rules. 
Tag + id (div#main) > id
Parent tag + tag+id > tag+id 
Remember these are just general rules but they should take care of 99% of your situations. Placing styles in your scripts are generally a bad idea because it affects perfoance as others have noted and it adds another place for you to update when you need to change  the styles. 
So, you can specify it inline as an attribute on the element you want to style...
<p style=" font-weight:bold; ">
Or, you could add it in a style block in the page body or header
<style>
p {font-weight:bold;}
</style>
And lastly, you could include it from a linked CSS file by importing...
<style>
#import url(css/bold.css)
</style>
Or by linking it...
<link rel="stylesheet" href="css/bold.css" />
Honestly, linking is, 99.9999% of the time, is the best way to include stylesheets on a page as it neatly separates your CSS from your code, making updates to either much faster.