Before To Read
- Check your browser and check property that is possible to use in your browser on caniuse.com.
Chapter 0 : About This Book
Javascript Coding type
-
return Array.prototype.slice.call(elements);
can be replaced byreturn [...elements];
. -
$$(".item")
returns Array, butdocument.querySelectorAll(".item")
returns NodeList.
Chapter 1: Introduction
Css Coding Tips
Minimize Code Duplication
- Minimizing the amount of edits necessary to make a change.
- When values depend on font size, try to reflect their relationship in the code.
- There are several issues with the maintainability of this code that we can fix.
- Now if I change the parent font size to 40px, the button will instantly become bigger. However, it will look quite different, because all other effects were designed for a smaller button and did not scale.
- To calculate
px
toem
, multiple 1/(2*10) to the original value. - Basic font size of
html
tag is 16px. - With this, you can make other buttons easily.
- Maintainability versus brevity
- When you want to except only one property, then, first, make it all same, and second, make an except.
- currentColor
- You can use
currentColor
like other colors, for example white, red or black.
- You can use
- inherit
-
inherit
follows parent’s property.
-
Trust your eyes, not numbers
- Optical illusions are very common in any form of visual design.
- Letterforms are much more straight on the sides than their top and bottom. We need to specify less padding for the top and bottom sides.
On Responsive Web Design
- Use percentages instead of fixed witdhs. When that’s not possible, use viewport-relative units(vm, vh, vmin, vmax), which resolve to a fraction of the viewport width or height.
- When you want a fixed width for larger resolutions, use
max-width
, not width, so it can still adapt to smaller ones without media queries.
- Don’t forget to set a max-width of 100% for replaced elements such as
img
,object
,video
andiframe
.
- In cases when a background image needs to cover an entire container,
background-size: cover
can help maintain that regardless of said container’s size. However, bear in mind that bandwidth is not unlimited, and it’s not always wise to include large images that are going to be scaled down via CSS in mobile designs. - When laying out images(or other elements) in a grid or rows and columns, let the number of columns be dictated by the viewport width. Flexible Box Layout(a.k.a Flexbox) or
display: inline-block
and regular text wrapping can help with that. - When using multi-column text, specify
column-width
instead ofcolumn-count
, so that you get one column only in small resolutions.
Use Shorthands Wisely
- It is good defensive coding and future-proofing to use them, unless we intentionally want to use cascaded properties.
- If only one valule is provided, it is expanded to apply to every item in the list, then we can take advantage of CSS list expansion rules.
Should I use a preprocessor?
- LESS, Sass or Stylus
- Debugging becomes harder.
- We are restricted in our choice of collaborators or need to spend extra time for training.
- They have their own bugs.
- Many preprocessor-inspired fetures have been making their way into pure CSS.
-
But, used properly, they can help keep code more flexible in a large project.
- variable and function in CSS
- Native CSS
calc()
has no trouble. -
--
sets a variable. Andvar()
returns a value of the variable.
- Native CSS
- Weird shorthand syntax
- Using a slash(/) to separate background-position on left and background-size on right.
Backgrounds & Borders
Translucent borders
- Background covers even border.
- But
background-clip
sets background area.
Chapter 8: Transitions & Animations
Elastic Transitions
- What is Transition?
-
transition
changes property in specific time.
-
- What is Animation?
-
animation
changes property with frames.
-
Bouncing Animations
-
This animation is so unnatural. The reason is that its
timing function
is the same across all these keyframes. -
timing function
-
ease-in
: slower -
ease-out
: faster -
ease-in-out
: slower and then faster -
linear
: same speed
-
- Moreover, with
cubic-bezier
, we can reverse any timing function by swapping the horizontal with the vertical coordinates for both its control points.
Elastic Transitions
- You can make elastic transition with animation, but honestly, transition is easier.
-
input:not(:focus) + .callout
, this menas, when we don’t focus on input box, set properties of callout class which is neighbor with input.
-
Additionaly, if you have color change, then add
transform
attransition
. -
Why duration is in original?
- Because, normally, we want to set same duration at fade-in and fade-out.
- So
input:hover { transform: scale(2); transition 1s; }
has only 1s fade-in, no fade-out.
Frame-By-Frame Animations
- Why should we stop to use gif?
- They cannot have alpha transparency.
- This is for portability, but not for experimentation.
-
steps
divides the whole animation in frames and abruptly swithes between them.
Blinking
-
animation-direction
changes animation process withnormal
,alternate
,reverse
,alternate-reverse
.
- Smoothly Blinking
- Use alternate, and make time to half and twice the loop.
- Classic Blinking
- Use step and make a frame on 50%.
Typing Animation
-
Please use this animation only one line!
- ch
- In monospace fonts, the width of the “0” glyph is the same as width of every glyph.
-
ch
is a degree of width of the “0” glyph.
- window.onload
- When you want to write javascript on header and need an elements of body, you have to wait until html is readed.
-
window.onload
is an event to wait reading all html document. - But, you don’t need to write this when you type code in the body.
- querySelector vs querySelectorAll
-
querySelector
gets only one element of selector andquerySelectorAll
gets all elements of selector.
-
Smooth State Animations
Animation Along A Circular Path
- When you change only base point of the animation.
- When image spin too with reverse direction.
- When you use only one element for image.