CSS References CSS Tools: Reset CSS What is CSS? Backgrounds and Borders
What is the purpose of CSS?
Using CSS, you can control exactly how HTML elements look in the browser, presenting your markup using whatever design you like.
What are the three ways to insert CSS into your project?
Write an example of a CSS rule that would give all < p > elements red text.
Example: ‘< p style=”color:red;” >’ This is a paragraph.’< /p >’
Flexbox is a one-dimensional layout method for arranging items in rows or columns. Items flex (expand) to fill additional space or shrink to fit into smaller spaces.
To start with, we need to select which elements are to be laid out as flexible boxes. To do this, we set a special value of display on the parent element of the elements you want to affect. In this case we want to lay out the < article > elements, so we set this on the < section >:
section {
display: flex;
}
This causes the < section > element to become a flex container and its children to become flex items.
When elements are laid out as flex items, they are laid out along two axes:
The parent element that has “display: flex” set on it (the < section > in our example) is called the flex container. The items laid out as flexible boxes inside the flex container are called flex items (the < article > elements in our example).
Flexbox provides a property called flex-direction that specifies which direction the main axis runs (which direction the flexbox children are laid out in). By default this is set to row, which causes them to be laid out in a row in the direction your browser’s default language works in (left to right, in the case of an English browser). flex-direction: column;
One issue that arises when you have a fixed width or height in your layout is that eventually your flexbox children will overflow their container, breaking the layout.
At this point it’s worth noting that a shorthand exists for flex-direction and flex-wrap: flex-flow.
flex is a shorthand property that can specify up to three different values:
Let’s now return to our first example and look at how we can control what proportion of space flex items take up compared to the other flex items. First, add the following rule to the bottom of your CSS:
article {
flex: 1;
}
This is a unitless proportion value that dictates how much available space along the main axis each flex item will take up compared to other flex items. In this case, we’re giving each < article > element the same value (a value of 1), which means they’ll all take up an equal amount of the spare space left after properties like padding and margin have been set. This value is proportionally shared among the flex items: giving each flex item a value of 400000 would have exactly the same effect. You can also specify a minimum size value within the flex value. Try updating your existing article rules like so:
article {
flex: 1 200px;
}
article:nth-of-type(3) {
flex: 2 200px;
}
This basically states, “Each flex item will first be given 200px of the available space. After that, the rest of the available space will be shared according to the proportion units.” Horizontal and vertical alignment You can also use flexbox features to align flex items along the main or cross axis.
div {
display: flex;
align-items: center;
justify-content: space-around;
}
centered horizontally and vertically. We’ve done this via two new properties.
align-items controls where the flex items sit on the cross axis.
By default, the value is stretch, which stretches all flex items to fill the parent in the direction of the cross axis. If the parent doesn’t have a fixed height in the cross axis direction, then all flex items will become as tall as the tallest flex item. This is how our first example had columns of equal height by default. The center value that we used in our above code causes the items to maintain their intrinsic dimensions, but be centered along the cross axis. This is why our current example’s buttons are centered vertically. You can also have values like flex-start and flex-end, which will align all items at the start and end of the cross axis respectively. See align-items for the full details. You can override the align-items behavior for individual flex items by applying the align-self property to them. For example, try adding the following to your CSS:
button:first-child {
align-self: flex-end;
}
justify-content controls where the flex items sit on the main axis.
Ordering flex items Flexbox also has a feature for changing the layout order of flex items without affecting the source order. This is another thing that is impossible to do with traditional layout methods.
button:first-child {
order: 1;
}
button:last-child {
order: -1;
}
It’s possible to create some pretty complex layouts with flexbox. It’s perfectly OK to set a flex item to also be a flex container, so that its children are also laid out like flexible boxes. The HTML for this is fairly simple. We’ve got a < section > element containing three < article >s. The third < article > contains three < div >s, and the first < div > contains five < button >s:
section - article
article
article - div - button
div button
div button
button
Button
First of all, we set the children of the < section > to be laid out as flexible boxes.
section {
display: flex;
}
Finally, we set some sizing on the button. This time by giving it a flex value of 1 auto. This has a very interesting effect, which you’ll see if you try resizing your browser window width. The buttons will take up as much space as they can. As many will fit on a line as is comfortable; beyond that, they’ll drop to a new line. button { flex: 1 auto; margin: 5px; font-size: 18px; line-height: 1.5; }
Items will be laid out following either the main axis (from main-start to main-end) or the cross axis (from cross-start to cross-end).
| main-start | main-end – The flex items are placed within the container starting from main-start and going to main-end. |
| cross-start | cross-end – Flex lines are filled with items and placed into the container starting on the cross-start side of the flex container and going toward the cross-end side. |
A grid is a collection of horizontal and vertical lines creating a pattern against which we can line up our design elements. They help us to create layouts in which our elements won’t jump around or change width as we move from page to page, providing greater consistency on our websites.
A grid will typically have columns, rows, and then gaps between each row and column. The gaps are commonly referred to as gutters.