This work is produced by Johan Leitet for the course Introduction to web programming (1DV525) at Linnaeus University.
All content in this work excluding photographs, icons, picture of course litterature and Linnaeus University logotype and symbol, is licensied under a
Creative Commons Attribution 4.0 International License.
If you change the content do not use the photographs, icons, picture of the course literature or Linnaeus University logotype and symbol in your new work!
At all times you must give credit to: ”Linnaeus university – Introduction to Web Programming (1DV525)” with the link https://coursepress.lnu.se/kurs/introduction-to-web-programming/ and to the Creative Common-license above.
Inline stylesheets
<p style="color:#6C9; text-align:center">
Centered, blueish
</p>
Embedded stylesheets
<head>
...
<style type="text/css">
p { color: red; }
</style>
...
</head>
External stylesheets
<head>
...
<title>Page with style</title>
<link rel="stylesheet" href="mystyle.css" />
...
</head>
CSS Rules
selector { property: value; }
selector {
property: value;
property: value;
property: value;
}
Example
h1 {
color: #CDF;
background-color: #9FF;
}
Get all h1-element and change the color to #CDF and background-color to #9FF
CSS Rules
selector {
property: value;
property: value;
property: value;
}
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_Started/Selectors
Single selector
h1 { property: value; }
An em element that is a descendant of an h1 element
h1 em { property: value; }
Example:
p em strong { property: value; }
<p>
This is an <em><strong>example</strong></em>.
Another <strong>text</strong>.
</p>
Single selector
.avatar { property: value; }
<p class="avatar"> Selected </p>
...
<p class="avatar"> Selected </p>
...
<div class="active avatar"> Selected </div>
...
<p> Not selected </p>
Combined selector
p.avatar { property: value; }
<p class="avatar"> Selected </p>
...
<p class="avatar"> Selected </p>
...
<div class="active avatar"> Not selected </div>
...
<p> Not selected </p>
Single selector
#content { property: value; }
<div id="content">
Selected
</div>
<div>
Not selected
</div>
Important: The ID value (above "content") must be unique on the page!
All elements with the attribute disabled
[disabled] { propery: value; }
All
[type='button'] { propery: value; }
All secure links
a[href^="https://"] {propery: value;}]
All .png-images
img[src$=".png"] {propery: value;}]
...
Selector | Selects |
---|---|
A E | E that is decendant of A |
A > E | E that is a child of A |
E:first-child | E that is first child |
B + E | E that is next sibling of B |
h1,h2,h3 { propery: value; }
p.red strong.hidden, #content p:first-line { propery: value; }
Hovering links
a:link { propery: value; }
a:visited { propery: value; }
a:focus { propery: value; }
a:hover { propery: value; }
a:active { propery: value; }
Example
div p:first-child {propery: value;}
<div>
<p>I am first child</p>
<ul>
<li>I am first child as well!</li>
<li>Poor me... :(</li>
</ul>
<p>And me...</p>
</div>
Different selectors have higher specificity than others.
p#firstptag {color: yellow; } /* 101 */
p.content {color: green; } /* 11 */
p {color: red; } /* 1 */
p {color: blue; } /* 1 + last */
Some properties are inheritanced by decendents
p { color: red; }
<p>
This text will be red. <em>This text as well!</em>
</p>
Not all properties are inherited. Refer to the spec.
#content p { font-size: 1.1em; color: #4D83A4; margin-left: 10px; }
#content {
float: left;
font-size: 1.6em;
}
#content p {
color: #4D83A4;
font-size: 1.1em;
}
#content p:first-letter {
font-size: 1.7em;
}
#content
{
font-size : 1.6em;
float : left;
margin-right: left;
}
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_Started/Readable_CSS
Multiline comments
/* Layout
Using: Two column floating layout
Total width: 250px
*/
No single line // available
<head>
...
<link rel="stylesheet" href="reset.css" />
<link rel="stylesheet" href="main.css" />
</head>
Fonts and text | Sizes, margins, fontfaces | |
Graphics | Colors, borders, shadows, effects, backgrounds | |
Layout | Boxes, positioning, layout models | |
Animations | Transformations, transitions |
Responsive page?
<!-- CSS media query on a link element -->
<link rel="stylesheet" media="(max-width: 800px)" href="example.css" />
<!-- CSS media query within a stylesheet -->
<style>
@media (max-width: 600px) {
.facet_sidebar {
display: none;
}
}
</style>
@media tv and (min-width: 700px) and (orientation: landscape) { ... }
https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
A CSS Framework will give you a
solid foundation to build your
site upon. But beware, you do
not want to end up with yet
another Bootstrap-site.
If you have worked with CSS before we encourage
you to try a preprocessor in the first assignment.