Subject of this article are CSS layout properties, what they cause and how they interact. It is a supplement for my passed Blog about the position
property.
You might have noticed that web page layout is not done by assigning something like BorderLayout, GridLayout, FlowLayout, VerticalLayout, TableLayout, ... to some HTML container. Rather you need to set some CSS properties on each element taking part in the layout. These properties are not always that intuitive as expected, especially the position
property.
Let's try out by "click and play", not by writing source code.
Below the following 5 setting panels you find 5 HTML <div>
elements, visible as coloured bars, spanning the whole width. You can change the CSS layout properties of each of them by using the setting panel of same color.
If you want to reset, press browser Reload, or Shift-Reload in Firefox.
Mind that
Thus these fields are disabled initially.
|
|
|
|
|
Here is the playground's HTML:
<div>
<div id="box-1">Box 1</div>
<div id="box-2">Box 2</div>
<div id="box-3">Box 3</div>
<div id="box-4">Box 4</div>
<div id="box-5">Box 5</div>
</div>
You will find a lot of information and jsfiddle try-out pages on the internet about these CSS properties. Following is a rough description of how they should be understood.
static
causes the browser to locate the element according to the document flow, all others need at least one of top, left, right or bottom
. Mind that absolute
positions relative to the next non-static
parent element. See also my Blog about this property. block
) or horizontally (inline
). The value inline-block
is the same as inline
, but the dimension of the element is respected. The value none
would make the element invisible, not even taking any place in the layout. Mind that there are many more values for display
, but they are not all supported by the different browsers. float
element affects its parent's layout. none
would denote a "line break" for horizontally arranged elements. position
is static
. They serve to locate the element relatively to their next non-static parent in any other positioning-mode. Give the yellow box a height of 4 em. Then make it inline
. You see that height is ignored now. Then change to inline-block
. The height is respected again.
Make box 1 float: left
and box 2 float: right
. You see that the order of elements is different now, box 3 is between box 1 and box2.
Make box 1 float: left
. Then increase its height. You see that it pushes more and more of the other boxes to the right.
Try to arrange all boxes horizontally, by either assigning them all float: left
or position: inline-block
. Then give them all a width of 15 em. You see that they won't fit into the page and will break into the next layout line.
Feel free to visit my homepage to see the latest state of this page.
ɔ⃝ Fritz Ritzberger, 2015-11-30