- Explain the float property
- Describe how to use calc
Imagine we had an image, and we wanted to have text wrap around it, like in a newspaper. The float property was originally created to handle exactly this.
The float
property has two values
- left
- right
If you want to force text to begin on the line below the floated element (i.e. stop floating), you can use the clear:left
, clear:right
, clear:both
- Create a page with an image on it
- Add some placeholder text
- Have the placeholder text flow around the right side of the image (image is on the left side of the page)
- Reverse it, so the image is on the right side of the page, with the text flowing around the image's left side
You can use float
to line up block
elements, one next to another.
WE DO: Nav Bars!
- All elements in the line must have widths that do not add up to more than their container, or they will wrap.
- If you have a container with nothing but floated elements for content, the height of the container will be 0, even though the floated elements take up space
- This is problematic if the container has a background color/image
- To fix this, you have many options:
- put an empty div with
clear:both
after all the floated elements - add
overflow:auto
to the container - many others...
- put an empty div with
- Create four div tags (with content), each 25% of the screen. Use
float
to have them line up on the same line as each other - Surround these
div
tags with asection
tag that has a background color and a padding of 1em - Fix it so you can see the background color of the
section
tag
Inline-block elements are often preferable to a line of floated block elements, but inline-block elements have a problem too:
If you have inline-block elements next to each other, there is often space between each element. This is caused by the browser rendering the line breaks between each tag as a single space.
To fix this:
- reformat code so that there is no space between each tag
- Set the
font-size
of the container to be 0 and then reset it for each element - https://css-tricks.com/fighting-the-space-between-inline-block-elements/
- Create four div tags (with content), each 25% of the screen. Use
display:inline-block;
and some css hackery to have them line up on the same line as each other.
The calc
property is used when you want to combine two units of measure together. This is especially useful when the units of measure are different.
Example: If you have a fixed width element (e.g. a logo) in line with an element that has a % width
- Create a nav with a logo on the left and four links to the right of the logo
- As you resize the window, the logo should remain a fixed width, but the space for the four links should grow and shrink appropriately.