CSS breaks the web page into boxes that are laid out based on the display property and sizing properties. This gets a bit more complicated when you place containers within other containers, which tends to be how you build out a design.
The block is your basic element. They take up the entire width of whatever element they are placed within as well as their height. Even if another block could fit beside a block, it will not and instead the next block element will be placed beneath the first one. While it is possible to put blocks inside of blocks, the embedded block will still try to take whatever width is available in the new block even if it is not using it, such as below where we have a block with a 50% width and one with a 25% width. Even taking padding into account, the blocks could fit beside each other but won’t.
In order to have boxes beside each other, we need to set the display type to be inline. The idea here is that this is for contents that appear inside a block as part of the text. Even though we use div tags to be blocks, nothing is stopping us from changing the div tag to use display:inline, which will make the div tag act just like a span.
The problem with inline blocks is that they fit within the line, which makes sense as they are in-line elements. This means that the width and height of the object is ignored. Any padding is put in the line with it overflowing above and below the line.
The problem is they take the size of their content.
As you can see, this means that the margins overlap.
Here is some text with spans of Green and Yellow
Notice the top/bottom margins are collapsed and padding goes outside of span box.
The width and height isn't used
Green width/height 50% Yellow width/height 50%It would be great if we could have both the block’s solid layout properties as well as the ability to be placed in-line with other elements. As it happens, we have the inline-block display option. It has the features of a block but if there is room, it will allow other elements on the same line.
While this sounds like it should solve our problems, it doesn’t as it, like most of CSS, is very finicky. If you are using a mix of measurement types, such as % and Ems, then there will be a point where the different padding and margins and display sizes end up working out to over 100 percent. This can be an advantage as you may want the display to become a single column. Working out the numbers to do this is a bit tricky, so to handle the work for you there are additional advanced display methods. The first one we will be looking at is the flex box.
This chapter's summary cheat sheet.
Why layout is complicated for web pages.
The basic display types.
A box that contains other components and sizes them to fit the available space.
A flexible grid where you can lay out your design.
Using templates to make laying out components in a grid easy.
Using the columns attributes to easily allow for multiple columns.
Having images (or other blocks) have text wrap around them.
There are other ways of placing elements by using the position attribute.
Altering CSS based on the properties of the device the page is being rendered on.
The project for this chapter.
Solution for this chapter's project