Placing elements using float, position and display





What is it all about?

Under normal circumstances, the coded elements are placed on the page where the code is. Intuitively this makes sense, and the element moves along with the page when scrolling, so it isn't visible all the time. But, there may be instances where you want some part of the page to be visible at all times, e.g. a header or footer, because it gives the best UI or you just like the design that way. If you have images that are fairly small, you may get a visually better layout having the text float around the image, instead of having a break in the text.

As may be expected, you have a number of options for placement, and as always, some solutions makes more sense than others.

Placement using float

Float is, as the name suggests, to have the element floating between the other elements, should you choose so. Float can have the following values:

ValueEffect
noneThe content does not float around the element (default).
leftThe element is placed to the left, so the rest of the content floats on the element's right side.
rightThe element is placed to the right, so the rest of the content floats on the element's left side.
initialThe element's float value is set to default value.
inheritThe element inherit the float value from the parent element.


For example float:left
Field containing e.g. an image or a fact box.
Surrounding text/ content that has to float around the field floating to the left side. As can be seen, the text follows the left margin, where there is room for it.


In the example shown here, the box is right next to the left margin. Visually this isn't always optimal, and you can of course move the element a bit in by using margin, in this case margin-left. Likewise you may want to consider having some distance between the floating element to the surroundings. In the shown example, margin-right is set to 5px, just to push the surrounding text free of the element's right side.

One thing lacking with float, is the ability to make the content float on both sides, equivalent to having a float:middle option. Beware that if you also apply an absolute position by using position (see further down), the float value is ignored.


Placement using position

One way of placing elements on the page, which is used quite a lot for optimizing the UI, is position. Where you write the code for an element in the HTML file isn't necessarily the same place as where you want the element to be shown. It might be something like a footer which is always visible, or an icon for "goto top" that doesn't appear until you have scrolled so far down the page, that having the function makes sense.

Position can have the following values:

ValueEffect
staticThe element is shown as placed in the code (default).
absoluteThe element is placed in a fixed position relative to the code.
Note: Here you also need to use the four variable for the sides left, right, top and bottom + a distance from the specified side to the element. In this particular case, bottom does not mean the bottom of the page but the bottom of the previous element in the code.
fixedThe element is placed in a fixed position relative to the browser window (viewport).
Note: Here you also need to use the four variable for the sides left, right, top and bottom + a distance from the specified side to the element.
relativeThe element is placed relative to the code, i.e. for fine tuning of the element's placement.
Note: Here you also need to use the four variable for the sides left, right, top and bottom + a distance for how much the element should be moved.
stickyThe element is placed relative to the user's scroll position, i.e. a shift between relative and fixed position, when scrolling.
Note: Not supportet by IE/Edge ver. 15 and before. Not supporten by Safari ver. 6.1 and before, but, if you need to support Safari 6.1 and before, you add "position: -webkit-sticky;" before "position:sticky". Here you also need to use the the four variable for the sides left, right, top and bottom + a distance to the side at which the element must stop.
initialThe element's position value is set to default value.
inheritThe element inherit the position value from the parent element.


position:static


position:absolute


position:fixed


position:relative


position:sticky



Placement using display

Display is a setting for how the showing of an element should be handled. You have in tags like SPAN, DIV and TABLE a number of implicit properties in regards to how they are displayed, which you might want to adjust, e.g. a DIV behaving like an inline element, because it can do something that SPAN can't that you need. It could also be parts of a page you want hide until, for instance, an area is clicked on, as done on this site's drop-down menus.

ValueEffect
inlineThe element is shown as an inline element. The value cancels some of other variables, e.g. the block element's height and width.
blockThe element is shown as a block element.
inline-blockThe element is shown as an inline element, but the variables from block element such as height and width still work.
noneThe element is removed entirely. Examples shown here
run-inThe element is shown as a block or an inline element, depending on the following element.
Note: The value only worked on a few old versions of some of the browsers, and is useless today.
list-itemThe element is shown and treated as an LI tag.
inline-tableThe element is shown and treated as a table, only inline.
tableThe element is shown and treated as a TABLE tag.
table-captionThe element is shown and treated as a CAPTION tag.
table-column-groupThe element is shown and treated as a COLGROUP tag.
table-header-groupThe element is shown and treated as a THEAD tag.
table-footer-groupThe element is shown and treated as a TFOOT tag.
table-row-groupThe element is shown and treated as a TBODY tag.
table-cellThe element is shown and treated as a TR tag.
table-columnThe element is shown and treated as a COL tag.
table-rowThe element is shown and treated as a TR tag.
flex
inline-flex
Used for flexbox for adaptive design, as shown in details here.
grid
inline-grid
Used for grid for adaptive design, as shown in details here.
initialThe element's display value is set to default value.
inheritThe element inherit the display value from the parent element.


A couple of examples on the practical application:

display:inline-table
Showing something as an inline-table is particularly practical for formulas and other typographical challenges, where you need to have numbers, letters and other symbols stading on top of each other, which HTML is completely unable to do without workarounds. It could be something like isotopes, e.g.
2
1
H. If we take a look at the code:

Deuterium is written like this: <TABLE STYLE="display:inline-table; font-size:65%; border-collapse:collapse; vertical-align:middle"><TR><TD STYLE="padding:0px">2</TD></TR> <TR><TD STYLE="padding:0px;">1</TD></TR></TABLE>H, meaning that it is a hydrogen atom containing two nucleons.


It looks like this on the page:

Deuterium is written like this:
2
1
H, meaning that it is a hydrogen atom containing two nucleons.


display:list-item
You can make a tag like DIV behave like a list item, i.e. the LI tag. The practical application of this will only be relevant on the more advanced sites, but the effect is interesting from a technical point of view. If you have a DIV with display:list-item in an UL tag, you will, as could be expected, get a bullet:

    A bullet list using DIV and display:list-item


The same DIV in an ordered list (OL tag):

    An ordered list using DIV and display:list-item


What makes it really interesting for the ordered lists is that the numbering also works, when mixed up with LI tags:

  1. First list item done using an LI tag.
  2. First DIV with display:list-item
  3. Second list item done using an LI tag.
  4. Second DIV with display:list-item