tools = require('../js/tools'); tools.init()3 ways to apply CSS¶
located in a separate CSS file - via its own URL
embedded in HTML within a
<style>taghard-attached to an element itself with
style=
method 1 : a separate CSS¶
this is the preferred method
write your CSS in a separate file, e.g.
mystyle.csswhich, assuming it is in the same directory as your
hello.htmlcan be kind-of included in
hello.htmlby inserting the following line
in the
<head>part of your html
URLs¶
where
https://is a indication of the protocol to use; it is very oftenhttps://indeedwhat does the s stand for ?the final
sinhttpsstands for secure; indeedhttpsis mostlyhttp, but withan encryption layer (to avoid man-in-the-middle attacks),
and also certificates to make sure that you really talk to whom you intend to
hostname.iois a hostname; before the browser can actually send a packet to that host, it first needs to find its IP address - and for that purpose it asks the DNS service (Domain Name System)/the/path/to/contentis a path relative to the web server root it may represent an actual path in the server’s file system (for static pages)
but most often it is relative to virtual namespace, i.e. it is interpreted by the server as a way to identify what is requested; for example,/book/52could be a way to ask information about book number 52
lots of variants
there are many variants in the way to build a URL, and we’ll touch on that during the course, but for example
mailto:someone@example.comwould use another protocol entirely - here it just opens a mail messagehttps://hostname.io:999/the/path/to/contentwould mean to use an alternative port number 999https://username:password@hostname.io/the/path/to/contentwould specify an authentication methodhttps://hostname.io/the/path/to/content#some-sectionwould allow to point to the named anchorsome-sectionin the page, i.e. a ` element, instead of the top of the page
relative URLs¶
it is possible to omit some parts of the URL;
and that’s exactly what we’ve done when we wrote href="hello.css" in our <head> above
imagine if you have loaded a document as, say https://hostname.io/the/path/to/content
then from within that document:
href="to.css"is interpreted ashref="http://hostname.io/the/path/to.css"href="/to.css"is interpreted ashref="http://hostname.io/to.css"href="/other/path/to.css"is interpreted ashref="http://hostname.io/other/path/to.css"href="other/path/to.css"is interpreted ashref="http://hostname.io/the/path/other/path/to.css"
and the same goes with the file:/// URL scheme
method 2 : inline in html¶
back to the topic of injecting CSS in the page
you can also insert a
<style>tag in your HTMLand mention the CSS code there directly
it is less recommended as it kind of ruins the
separation between contents and presentation
method 3: hardwired with style=¶
attach a
style=attribute on a HTML tagthis method is by far the worst
and should be used in last resort
practice¶
we recommend you use a local git repo all along
i.e. create a new folder andgit initcopy
hello.htmlintomycv.htmlcreate a more realistic skeleton for a résumé
with 4 sections Experience, Education, Skills and Languages
keep it simple for now, nothing too elaborate
make sure all the text gets attached to adapted tags like
<div>or<li>and not directly under
<body>- like it was done inhello.htmlmake sure to insert at least one
<a href=...>hyperlink
practice (continued)¶
create a CSS file
mycv.csswith some settings that should apply to
mycv.html
add a
<link>tag in the html<head>areaso the css is loaded by the html
load
mycv.htmlin a browserchange the CSS and reload the browser page
to see the effect of your changes
the browser cache¶
for performance reasons primarily :
fetching a file may be slow in poor network conditions
so, once a file has been loaded
it may be cached inside the browser
so that future references do not fetch it again
beware of that during development
reloading the html file
may not reload the css because it is cached
how to deal with it¶
a couple hints and workarounds
reload with the
Shiftmodifier pressed
either with a mouse-click (↻),
or keyboard shortcut (⌘-r on e.g. chrome/mac)
double-check that with the browser you actually usedevel tools have a Sources tab that let you check
the content of each individual loaded fileoften browsers have more advanced tools to manage cache
e.g. Chrome :⠸menu → More Tools → Clear Browsing Data