CSS. Deep Dive
This section will be useful for beginner front-end developers and anyone who wants to do a bit more than just showing an alert box on click.
On Formatting
No, I can't help myself, and I'll go ahead and show you the CSS formatting style I use:
/* Header */
header {
margin-bottom: 16px;
font-weight: 400;
}
header h1 {
color: #999;
}
header p {
font-size: 1.4em;
margin-top: 0;
}
/* /Header */Why this is good:
such CSS is easy to read
there's a block start marker (you can quickly find the right section even in a huge CSS file by searching for the label
* header)this formatting clearly indicates element nesting
and you can easily trace property inheritance
I'm not insisting on my approach, but I'd like you to adopt one of the many formatting standards and always stick with it.
When you become seasoned front-end developers, you'll discover the full power of CSS preprocessors. But for now, just listen and remember.
This visual complexity is meant for humans; browsers don't care about it at all, since they perform concatenation anyway when processing styles -- removing line breaks, indentation, comments, and non-critical spaces. Some types of web servers, for example those for banking thin clients, perform compression (and archiving) of files before sending them to where they're used, to save traffic and money.
Naming Classes and Identifiers
I already touched on this topic when talking about HTML markup, so here's the thing -- class names can even look like this: b-service-list__column b-service-list__column_right and that would be cool, and a "must have" -- but only within the scope of truly large projects. Actually, why am I going on about this? I'll just give you the starting point for further reading -- there's enough material there for a whole separate book ;)
About Colors
The web uses the RGB color model, named after the English names of the "carrier" color channels -- red, green, blue -- which relies on numerical notation for any shade. So the color red can be written not just as "red", but in several other ways too:
Now you should be able to name colors
#f00,#0f0,#00fwithout hesitation, and those who got an A in art class will also name#ff0,#0ffand#f0f;)
CSS 3 supports the enhanced RGBA model, where we can additionally set the alpha channel value, i.e. transparency:
Another CSS 3 goody is the ability to use the HSL (hue, saturation, lightness) and HSLA (HSL + alpha channel) color models:
There's a simple algorithm for converting from the HSL model to RGB, but don't burden yourself with it just yet.
Who even uses HSL?
Don't bother!HSL is becoming increasingly popular, and you should know about it, and even understand how it works!
For those of you stumped by the RGB channel mixing question, here's a visual guide:

Block and Inline Elements
You might not know this yet, but HTML tags are divided into block-type and inline-type. Block elements are those that render as a rectangle, take up the full available width inside their parent element, and their height is determined by their content. Block tags start and end on a new line by default -- these are <div>, <h1> and siblings, <p>, and others.
If you want your HTML to stay valid, make sure block elements are not placed inside inline elements. Inside inline tags, there can only be text or other inline elements.
One of the most common mistakes I see is people trying to stuff a list
<ul>or a button<button>inside a paragraph<p>. Don't do that.
Useful articles on the topic:
About Block Element Dimensions
I also want to specifically address the calculation of width and height for block elements, because there's a nuance here. By default, the height and width of elements are calculated without accounting for border thickness and padding, like this:

This box model is called "content-box", and CSS3 introduced the ability to change the box model by specifying the "box-sizing" property. Great, now we can choose between two values: "content-box" and "border-box". I already described the first one; the second calculates height and width including padding and border thickness:

Useful articles on the topic:
Floating Elements
I'd love to tell you more about the CSS float property, but I'm afraid the story would be long and tedious. In short: if you set the float property on an element, then:
our element will be shifted horizontally and "stick" to the specified edge of the parent element
if it was a block element, it will no longer take up the full width of the parent element, freeing up space
if block elements follow it, they will take its place
if inline elements follow it, they will wrap around our element on the free side
This is the "default" behavior, and you can see how it looks live in the example css.float.html.
The main thing here is to understand what's happening and be able to control it -- that is, if you want to learn at least a little bit about CSS layout :)
Positioning
I'll just give you a brief intro to position. It has five main values:
static-- the "default" state of affairs; elements are placed in the normal document flow, one after another from top to bottomabsolute-- the element is positioned relative to its nearest positioned ancestor (notstatic), according to specified coordinatesfixed-- the element is fixed at a certain position in the viewport, not moving when the page is scrolledrelative-- the element behaves likestatic, but can be offset from its normal position; it also serves as a positioning context for inner "absolute" elementssticky-- the element "sticks" to a certain position in the viewport when the page is scrolled past a given threshold, working as a combination ofrelativeandfixed
Last updated