Review
Notes
#2 Selectors
- Running “document.createElement('main')” will allow IE8 (which doesn't support it) to understand that element exist so we can style it.
- [class|="btn"] class starts with "btn-" or is exactly "btn".
- [class^="btn"] starts with "btn".
- [title="foo"] matches foo in a space separated list of words.
- Attribute selectors tend to be case sensitive if the underlying document language is.
* HTML attribute key terms, which is not: input[type="CHECKBox"].
- CSS4 introduces a case-insensitivity identifier: img[src$=".pdf" i].
* Only works for values, may not work on attributes, be aware of: input[TYPE='tel'].
^ XHTML enforces case sensitivity, HTML5 does not.
- "li + li" selects all but the first.
- "div * span" selects span grandchildren.
- "h2 + ol" adjacent sibling.
* "h2 ~ ol" following sibling.
- "a:lang(de)" target german links.
- "p:empty" pseudo-class treats whitespace as not-empty.
* Be aware of some elements which are always empty, "img" equals "img:empty".
* Is the only selector that considers text nodes.
- ":only-child", ":only-of-type".
- Changing element size on :active or :hover could cause reflow by triggering layout on the browser.
- Empty inputs might match ":valid".
- ":not()" takes a single selector, no descendants, nesting or grouping.
- Pseudo-elements styles are limited to font properties.
#3 Specificity and the Cascade
- 0,0,0,0
* Inline styles get the first digit.
* ID.
* Class, attributes, pseudo-class.
* Elements, pseudo-elements.
- Style links in the order: :link, :visited, :focus, :hover, :active, since they have the same specificity.
#4 Values and Units
- Every property accepts: inherit, initial, unset.
* Initial: set back to starting value.
* Inherit: set back to initial or inherit, whichever was previously set.
* URLs in stylesheets are relative to themselves, not the document.
- Viewport units.
* 1vmin: lesser from n/100 viewport width or height.
*1vmax: greater from n/100 viewport width or height.
- "currentColor": value of the computed 'color' property.
* when 'color' is not set on the element, this holds the inherited 'color' value.
- "turn": is a unit handy for animations.
#5 Fonts
- Never quote generic font family names like serif, it makes the browser think it refers to a font-family: font-family: Arial, 'serif'.
- When using @font-face.
* Appending "format('woff')" to the "src" prevents downloading a font the browser won't understand.
* Add "local('my-font')" first so the browser doesn't download fonts it already has.
- unicode-range can allow using a font face for a single or group of characters.
- "font-weight: bolder;" resolves to a value bolder than the parent's element.
#6 Text Properties
- line-height: use without units to make it work as a multiplier of the font-size, cause values like 1.5em get calculated and inherited as an actual value by children elements.
- vertical-align only works on inline elements and table-cells (not all values apply for table-cells).
* Accepts keywords and values.
* "bottom" value aligns elements to the bottom edge of the line, which most times is below the baseline of the text.
- "word-break: break-all" don't show hyphens even if the "hyphen" property is set.
#7 Basic Visual Formatting
- The values of margin, border, padding and content must add up to the width of the element's containing block.
* Only margin and width can be set to "auto".
^ When "auto", the remaining width of the container is distributed among those auto values.
^ If none of them is set to "auto" and the total is less than the container's width, then, margin-right is set to auto by the user agent in order to make the element's box (including margin) match the container width.
- box-decoration-break: clone. Causes each fragment of multiline inline elements to be drawn as a standalone box. So padding left|right is applied on every line rather than only at start and end.
#8 Padding, Borders, Outlines, and Margins
- background-clip prevents background from extending into the padding and/or border outside edge.
- Percentage values on padding and margin are computed from the parent's width (except on most positioned elements, flex and grid, where they are calculated with respect to the
height of their formatting context).
* can be used for fluid fluid pages, where the padding on elements enlarges or reduces to match the actual size of the parent element.
- Border takes the foreground color if no border-color is defined.
- "border-radius: h / v" sets the horizontal and vertical rounding individually.
- border-image for using an image as a border, it needs border-style and width to be set.
- Outlines do not take up space and may be non-rectangular.
* Cannot have different styles for each side.
* Run along the margin, just outside the border.
- Negative margins can create a Mondrian effect.
- Margin collapse: when vertical margins meet, the smallest one is reset to zero according to the specification.
* For parent-child, adding padding to the parent prevents collapsing.
* Doesn't happen on floated elements.
#9 Colors, Backgrounds, and Gradients
- "background-clip: text" fills the text with the background color or image (will need "color: transparent" or with opacity "color: rgba()").
- background-position: can be set in pairs like: 'top left. Except for the keyword 'center', percentages or length values.
* '0% 0%' is top left, '100% 100%' is top right., '50% 50%' is center.
* Using lengths aligns the top left of the image to the specified value: '20px 50px'.
* Can use negatives.
- Can use 4 values too in order to change the edges from which offsets are calculated.
* "background-position: left 33% top 30px" from the left edge, have a horizontal offset of 33%;
from the top edge, have an offset of 30px.
* "background-position: right 33% bottom 30px" from the right edge, have a horizontal offset of
33%; from the bottom edge, have an offset of 30px.
- When 'background-origin: center;' and background-repeat is set, the bg will be placed in the middle and then repeated, rather than starting from top left as default, making a consisten look around the edges.
- "background-repeat: space;" creates a grid pattern.
* It may override 'background-position: center' if the image is too big, like if only two repetitions can fit, it'll draw them on the horizontal edges.
- "background-repeat: round;" tries to fit as many images in the space provided: if the element is 850 px and the image 300px, the browser will scale down the image to 283 to fit 3 of them changing the aspect ratio if needed.
* To make sure images are not cut and fit completely, the origin has to be set in a corner and not the centre.
- 'background-attachment' immunes the effects of scrolling.
- 'background-size: % %' percentages refer to the area defined by 'background-origin' and not 'background-clip'.
- Setting auto triggers a 3-step fallback process:
* "background-size: auto 100px"
* On a 300px by 200px image.
^ The image has a 3:1 aspect ratio so it'll be preserved, resulting in a size of 150px by 100px.
^ If that fails but the image has an intrinsic aspect ratio (like most raster formats JPEG, PNG, GIF and some SVG), that ration will be used, resulting in 300px by 100px.
^ If that fails (which can happen on SVG or CSS gradients), like the image doesn't have intrinsic ratio, the horizontal size of the positioning area will be used (background-origin).
- Nearly every background property can be comma-listed, allowing for multiple backgrounds.
* Even the shorthand 'background: ..., ..., ...;'
^ background-color can only be added to the last layer, otherwise the declarations are made invalid.
- Gradients are images just like raster or vector.
* And can be used in any context an imager can, e.g. 'list-style-image'.
* They've no intrinsic aspect ratio.
- Setting the same color stop for two colours causes a hard colour switch effect.
- Color-hints declare the midpoint colour of the gradient transition.
#10 Floating and Shapes
- Margin on floated elements do not collapse.
- If no width is specified, the width collapses to zero.
- Floated elements do not overlap, if two floats are wider than the container, the 2nd one will drop to the next row.
- Floats only move horizontally, elements won't float to the top or bottom of the container.
* And are aligned top.
- When other elements overlap with a float:
* Inline: Borders, big and content render on top of the float.
* Block: borders and bg behind, content on top.
- ‘shape-outside’: floats around a defined shape.
* For images, they should have an alpha channel, or the shape will be rectangular.
* Can use inset, circle, ellipse, polygon.
- ‘shape-image-threshold’: opacity of float shape.
- 'shape-margin': margin around shape.
#11 Positioning
- Containing block
* For relative and static: the parent's content edge.
* For absolute: content edge if inline parent, padding edge if block parent.
- You can absolute position elements inside absolutely positioned elements.
- When absolute, setting 'auto' for offset prop 'top' makes the top touch the upper edge line up with the place the element would've been if not positioned at all (static position).
* left and right also but not bottom.
- 'static' position is basically the place in the normal flow of the document.
- left + margin + width + right = containing block width.
* If they sum is less than the containing block width, margin-right takes the remaining space (reset to auto).
* On positioned elements with all values set (left, width, margin, right), 'right' is reset to auto.
* If any of the props is set to 'auto', that one takes the remaining space.
- 'position: absolute; height: 5em; top: 0; bottom: 0; margin: auto;': centres vertically.
- Replaced elements have an intrinsic width and height.
- The specification keeps children from being drawn behind their parents when using z-index.
* Each parent with z-index, establishes a stacking context for its children.
#12 Flexible Box Layout
- Flexbox was designed for single dimensional layouts, works best at arranging information along one axis, not grids.
* 'justify-content' won't necessarily apply the same spacing between elements on multiple cross-axis blocks if the element count or size on each block is different.
- When 'writing-mode: vertical-lr;', flex direction row means top to bottom.
* Similar for 'direction: rtl'.
- 'flex-wrap', controls the cross-axis direction.
- 'flex-flow', shorthand for flex-direction and flex-wrap.
- flex items won't shrink to smaller than their border, padding, margin combined.
- justify-content aligns items across the main axis.
* space-around gives each item the same margin, making the space between the ones in the middle being double of that at the start and end (no margin collapse).
* space-evenly splits white space evenly everywhere.
- 'align-items' aligns items across the cross axis.
* stretch: will make elements take 100% height unless margin, [min-|max-]height are set.
* baseline: aligns all items' baselines starting with the one having the biggest font-size.
- 'align-self' is applied to flex items to override 'align-items'.
- 'align-content': controls the space between items spanning multiple columns in the cross-axis.
* Like justify-content does for the main-axis.
* As opposed to align-items, who aligns vertically per row.
- On flex items: margin do not collapse, 'float' and 'clear' are ignored.
- Empty text nodes (including whitespace) are ignored.
* Non-whitespace ones are wrapped in anonymous flex items and become part of the layout.
- flex-basis takes precedence over width/height.
- 'flex: 3' = 'flex: 3 0 0' (flex-basis will grow at a factor of 3 from 0px).
- 'flex: none' = 'flex: 0 0 auto'.
#13 Grid Layout
- Grids will not slide behind floated elements.
- Column props, float, clear have not effect on grid containers.
- vertical-align has no effect on grid items.
- If an inline-grid container is floated or absolutely pos, changes to display:grid.
- Grid area: one or more cells.
- Grid line can have multiple names, cols and rows and share names.
- minmax(a, b): not shorter than a, not larger than b.
- Mixing fr with other cols of fixed width, gives the fr col the remaining space.
- min-content and max-content can by used for col/row size.