designates my notes. / designates important.
Very useful for understanding responsive design. The layout and flow of the book was very intuitive in leading from one topic to the next. Unlike many of the other programming books I read, this one I read for a practical purpose. Directly after reading it I built How to Eat an Elephant.xyz. About a year later I adapted what I already had there for this site.
Not much else to say, but the book allowed me to create what I wanted.
In terms of the HTML specification, the elements we will be looking at fall into one of three groups:
Sectioning elements, for the broadest strokes in a HTML page. These are the kind of elements to use for header, footer, and sidebar areas.
Grouping elements, which are used to wrap associated elements. Think of paragraphs, blockquotes, and content of that nature.
Text-level semantics, which are the elements we use to designate particulars, like a section of bold or italic text or code.
However, HTML5 introduced the ability for each sectioning container to have its own self-contained outline. That means it is not necessary to think about which level of heading tag you’re at in terms of the entire document. You could just concentrate on the sectioning container you are currently working in.
To illustrate why this might be preferable, within a blog, post titles could
be set to use <h1>
tags, while the title of the blog itself could also have
an <h1>
tag. For example, consider the following structure:
<h1>Ben's site</h1>
<section>
<h1>Ben's blog</h1>
<p>All about what I do</p>
</section>
<article>
<header>
<h1>A post about something</h1>
<p>Trust me this is a great read</p>
<p>No, not really</p>
<p>See. Told you.</p>
</header>
</article>
<h1>
headings, the outline still appears as follows:
<video src="myVideo.mp4" width="640" height="480"
controls
autoplay
preload="auto"
loop
poster="myVideoPoster.png">
If you're reading
this either the video didn't load or your browser is waaaayyyyyy old!
</video>
video {
max-width: 100%;
height: auto;
}
<iframe width="960" height="720" src="https://www.youtube.com/
embed/B1_N28DA3gY" frameborder="0" allowfullscreen></iframe>
However, if you add that to a page as is, even if adding that earlier CSS rule, if the viewport is less than 960px wide, things will start to get clipped.
The easiest way to solve this problem is with a little CSS trick pioneered by Gallic CSS maestro Thierry Koblentz here: http://alistapart.com/article/creating- intrinsic-ratios-for-video. Essentially, he is creating a box of the correct aspect ratio for the video it contains. I won’t spoil the magician’s own explanation, go take a read.
If you’re feeling lazy, you don’t even need to work out the aspect ratio and plug it in yourself; there’s an online service that can do it for you. Just head to http:// embedresponsively.com/ and paste your iframe URL in. It will spit you out a simple chunk of code you can paste into your page.
<style>
.embed-container {
position: relative;
padding-bottom: 56.25%;
height: 0;
overflow: hidden;
max-width: 100%;
height: auto;
}
.embed-container iframe,
.embed-container object,
.embed-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
<div class="embed-container">
<iframe
src="http://www.youtube.com/embed/B1_N28DA3gY"
frameborder="0"
allowfullscreen
></iframe>
</div>
<link rel="stylesheet" media="screen and (orientation: portrait)"
href="portrait-screen.css" />
@media screen and (orientation: portrait) {
/* styles here */
}
<link rel="stylesheet" media="not screen and (orientation:
portrait)" href="portrait-screen.css" />
width: The viewport width.
height: The viewport height.
DEPRICATED device-width: The rendering surface’s width (for our purposes, this is typically the screen width of a device).
DEPRICATED device-height: The rendering surface’s height (for our purposes, this is typically the screen height of a device).
orientation: This capability checks whether a device is portrait or landscape in orientation.
aspect-ratio: The ratio of width to height based upon the viewport width and height. A 16:9 widescreen display can be written as aspect-ratio: 16/9.
DEPRICATED device-aspect-ratio: This capability is similar to aspect-ratio but is based upon the width and height of the device rendering surface, rather than viewport.
color: The number of bits per color component. For example, min-color: 16 will check that the device has 16-bit color.
color-index: The number of entries in the color lookup table (the table is how a device changes one set of colors to another) of the device. Values must be numbers and cannot be negative.
monochrome: This capability tests how many bits per pixel are in a monochrome frame buffer. The value would be a number (integer), for example, monochrome: 2, and cannot be negative.
resolution: This capability can be used to test screen or print resolution; for example, min-resolution: 300dpi. It can also accept measurements in dots per centimeter; for example, min-resolution: 118dpcm.
scan: This can be either progressive or interlace features largely particular to TVs. For example, a 720p HDTV (the p part of 720p indicates “progressive”) could be targeted with scan: progressive, while a 1080i HDTV (the i part of 1080i indicates “interlaced”) could be targeted with scan: interlace.
grid: This capability indicates whether or not the device is grid- or itmap- based.
@import url("tiny.css") screen and (min-width:200px) and (max-
width:360px);
rwd-Nav {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
padding: 0 20px;
position: relative;
margin: 10px 0 0 0;
}
@media (min-width: 1000px) {
.rwd-Nav {
margin: 0;
padding: 0;
}
}
@media (pointer: coarse) {
/* styles for when coarse pointer is present */
}
@media (pointer: fine) {
/* styles for when fine pointer is present */
}
Browsers report whether the value of pointer is fine, coarse, or none, based on the “primary” pointing device. Therefore, consider that just because a device has the capability of a fine pointer, it doesn’t mean that will be the primary pointing device. Think of tablets where the primary pointer is a finger (coarse), but that has an attached stylus—a fine pointing device.
The safest bet is always to assume users are using touch-based input and to size user interface elements accordingly. That way, even if they are using a mouse they will have no difficulty using the interface with ease. If however you assume mouse input and don’t provide affordance for coarse pointers, it might make for a difficult user experience.
@media (hover: none) {
/* styles for when the user cannot hover */
}
@media (hover) {
/* styles for when user can hover */
}
Be aware that there are also any-pointer or any-hover media features. They are like the preceding hover and pointer but test the capabilities of any of the possible input devices.
That way, if you want to apply styles if any input device is capable of hover, regardless of whether that input device is the primary one:
@media (any-hover: hover) {
/* styles if any input device is capable of hover*/
}
@media (any-pointer: coarse) {
/* styles to be applied if any attached pointer is coarse */
}
body {
background-color: #e4e4e4;
color: #545454;
}
@media (prefers-color-scheme: dark) {
body {
background-color: #333;
color: #ddd;
}
}
For the sake of your sanity, to accurately and easily add vendor prefixes to CSS, use some form of automatic prefixing solution. Right now, I favor Autoprefixer (https://github.com/postcss/autoprefixer). It’s fast, easy to set up, and very accurate.
There are versions of Autoprefixer for most setups; you don’t necessarily need a command line-based build tool (such as Gulp or Grunt).
Flexbox has four key characteristics: direction, alignment, ordering, and flexibility.
<img src="https://placeimg.com/640/480/any" alt="an inquisitive cat">
And this CSS:
img[alt] {
border: 3px dashed #e15f5f;
}
[data-sausage] {
/* styles */
}
img[alt="Sausages cooking"] {
/* Styles */
}
<img src="img/sausages.png" alt="Sausages cooking" />
<li data-type="todo-chore">Empty the bins</li>
<li data-type="todo-exercise">Play football</li>
[data-type^="todo"] {
/* Styles */
}
[attribute*="value"] {
/* Styles */
}
[data-type^="todo"] {
/* Styles */
}
[attribute$="value"] {
/* Styles */
}
[data-activity-name="swimming"][data-location="indoor"] {
/* Styles */
}
div:first-child {
/* Styles */
}
div:last-child {
/* Styles */
}
.nav-Link:nth-child(odd) {
/* Styles */
}
.nav-Link:nth-child(even) {
/* Styles */
}
nth options: nth-child(n) nth-last-child(n) nth-of-type(n) nth-last-of-type(n)
:nth-child(3n+1) would start at the first element, and then select every third element.
For example, .parent .descendant {} would select any element that was a descendant of the .parent element with a class of .descendant, no matter how many levels deep.
We can select only the direct “child” of the parent element, like this:
.parent > .descendant {
/* Styles */
}
.one + .item {
border: 3px dashed #f90;
}
.item:nth-child(3) ~ .item {
border: 3px dashed #f90;
}
.a-div:not(.not-me) {
background-color: orange;
border-radius: 50%;
}
<div class="thing"></div>
.thing {
padding: 1rem;
background-color: violet;
}
.thing:empty {
display: none;
}
The vw unit, where each vw unit is 1% of the viewport width.
The vh unit, where each vh unit is 1% of the viewport height.
The vmin unit (for viewport minimum; equal to the smaller of either vw or vh).
The vmax (viewport maximum; equal to the larger of either vw or vh).
.Hero-text {
font-size: 25vw;
}
scaling font via viewport.
There is a new mathematical expression on the way to CSS called clamp(), which lets us specify a minimum, maximum, and variable size. For example, we might opt for a headline like this: .headline { font-size: clamp(20px, 40vw, 80px) }. And while our headline text might vary in size, depending upon the viewport, it would never be less than 20px or larger than 80px. You can read the specification for clamp() here: https://www. w3.org/TR/css-values-4/#calc-notation.
.thing {
width: calc(50% - 10px);
}
:root {
--MainFont: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
:root is a pseudo-element that references the top element (html tag in html page)
–MainFont: is like a variable that can be passed to javascript or in the css file.
.Title {
font-family: var(--MainFont);
}
.my-Item {
background-color: var(--backgroundColor, #555);
}
@supports (display: grid) {
.Item {
display: inline-grid;
}
}
@supports not (display: grid) {
.Item {
display: inline-flex;
}
}
Here, we are defining one block of code for when the browser supports a feature, and another lot for when it doesn’t. This pattern is fine if the browser supports @supports (yes, I realize that is confusing), but if it doesn’t, it won’t apply any of those styles.
If you want to cover off devices that don’t support @supports, you’re better off writing your default declarations first and then your @supports-specific one after. That way, the prior rule will be overruled if support for @supports exists, and the @supports block will be ignored if the browser doesn’t support it. Our prior example could, therefore, be reworked to:
.Item {
display: inline-flex;
}
@supports (display: grid) {
.Item {
display: inline-grid;
}
}
@supports ((display: flex) and (pointer: coarse)) {
.Item {
display: inline-flex;
}
}
chaining conditionals in @supports.
And that really is all there is to feature queries. As with media queries, put your “default” styles first, and then your enhancement inside a feature query @supports at-rule. Remember, you can combine queries and also offer differing possibilities in which the enclosed code can apply.
.style {
/*...(more styles)...*/
transition-property: all;
transition-duration: 1s;
transition-timing-function: ease;
transition-delay: 0s;
}
transition: all 1s ease 0s;
transition: background-color 2s;
If no timing function is provided, the default timing function of ease is applied.
multiple transitions:
.style {
/* ...(more styles)... */
transition-property: border, color, text-shadow;
transition-duration: 2s, 3s, 8s;
}
.style {
transition: border 2s, color 3s, text-shadow 8s;
}
scale: Scale an element (larger or smaller)
translate: Move an element on the screen (up, down, left, and right)
rotate: Rotate the element by a specified amount (defined in degrees or turns)
skew: Skew an element with its X and Y coordinates
matrix: Allows you to move and shape transformations in multiple ways