CSS Layout - CSS position CSS
- CSS Introduction
- CSS Selectors
- How To Add CSS
- CSS Colors
- CSS Backgrounds
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Height and Width
- CSS Outline
- CSS Text
- CSS Fonts
- CSS Links
- CSS Lists
- CSS Tables
- CSS Layout - The display Property
- CSS Layout - CSS position
- CSS Layout - display: inline-block
- CSS Layout - Horizontal & Vertical Align
- CSS Combinators
- CSS Opacity / Transparency
- CSS Dropdowns
- Image Gallery
- CSS Attribute Selectors
- CSS Counters
- CSS Media Queries
CSS Layout - CSS position
CSS Layout - The position Property
The position
property specifies the type of positioning method used for an element (static, relative, fixed, absolute or sticky).
The position Property
The position
property specifies the type of positioning method used for an element.
There are five different position values:
static
relative
fixed
absolute
sticky
Elements are then positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position
property is set first. They also work differently depending on the position value.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right properties.
An element with position: static;
is not positioned in any special way; it is always positioned according to the normal flow of the page:
position: relative;
An element with position: relative;
is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.
position: fixed;
An element with position: fixed;
is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.
A fixed element does not leave a gap in the page where it would normally have been located.
Notice the fixed element in the lower-right corner of the page. Here is the CSS that is used:
Example
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
position: absolute;
An element with position: absolute;
is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.
Note: A "positioned" element is one whose position is anything except static
.
Example
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
position: sticky;
An element with position: sticky;
is positioned based on the user's scroll position.
A sticky element toggles between relative
and fixed
, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).
Note: Internet Explorer, Edge 15 and earlier versions do not support sticky positioning. Safari requires a -webkit- prefix (see example below). You must also specify at least one of top
, right
, bottom
or left
for sticky positioning to work.
In this example, the sticky element sticks to the top of the page (top: 0
), when you reach its scroll position.
Example
div.sticky {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;
}
CSS Layout - Overflow
The overflow
property specifies whether to clip the content or to add scrollbars when the content of an element is too big to fit in the specified area.
The overflow
property has the following values:
visible
- Default. The overflow is not clipped. The content renders outside the element's boxhidden
- The overflow is clipped, and the rest of the content will be invisiblescroll
- The overflow is clipped, and a scrollbar is added to see the rest of the contentauto
- Similar toscroll
, but it adds scrollbars only when necessary
Note: The overflow
property only works for block elements with a specified height.
Note: In OS X Lion (on Mac), scrollbars are hidden by default and only shown when being used (even though "overflow:scroll" is set).
overflow: visible
By default, the overflow is visible
, meaning that it is not clipped and it renders outside the element's box:
overflow: hidden
With the hidden
value, the overflow is clipped, and the rest of the content is hidden:
overflow: scroll
Setting the value to scroll
, the overflow is clipped and a scrollbar is added to scroll inside the box. Note that this will add a scrollbar both horizontally and vertically (even if you do not need it):
overflow: auto
The auto
value is similar to scroll
, but it adds scrollbars only when necessary:
overflow-x and overflow-y
The overflow-x
and overflow-y
properties specifies whether to change the overflow of content just horizontally or vertically (or both):
overflow-x
specifies what to do with the left/right edges of the content.
overflow-y
specifies what to do with the top/bottom edges of the content.
Example
div {
overflow-x: hidden; /* Hide horizontal scrollbar */
overflow-y: scroll; /* Add vertical scrollbar */
}
CSS Layout - float and clear
The float Property
The float
property is used for positioning and formatting content e.g. let an image float left to the text in a container.
The float
property can have one of the following values:
- left - The element floats to the left of its container
- right - The element floats to the right of its container
- none - The element does not float (will be displayed just where it occurs in the text). This is default
- inherit - The element inherits the float value of its parent
In its simplest use, the float
property can be used to wrap text around images.
Example - float: right;
The following example specifies that an image should float to the right in a text:
Example - float: left;
The following example specifies that an image should float to the left in a text:
Example - No float
In the following example the image will be displayed just where it occurs in the text (float: none;):
The clear Property
The clear
property specifies what elements can float beside the cleared element and on which side.
The clear
property can have one of the following values:
- none - Allows floating elements on both sides. This is default
- left - No floating elements allowed on the left side
- right- No floating elements allowed on the right side
- both - No floating elements allowed on either the left or the right side
- inherit - The element inherits the clear value of its parent
The most common way to use the clear
property is after you have used a float
property on an element.
When clearing floats, you should match the clear to the float: If an element is floated to the left, then you should clear to the left. Your floated element will continue to float, but the cleared element will appear below it on the web page.
The following example clears the float to the left. Means that no floating elements are allowed on the left side (of the div):
The clearfix Hack
If an element is taller than the element containing it, and it is floated, it will "overflow" outside of its container:
Then we can add overflow: auto;
to the containing element to fix this problem:
Grid of Boxes / Equal Width Boxes
With the float
property, it is easy to float boxes of content side by side:
Example
* {
box-sizing: border-box;
}
.box {
float: left;
width: 33.33%; /* three boxes (use 25% for four, and 50% for two, etc) */
padding: 50px; /* if you want space between the images */
}
Navigation Menu
Use float
with a list of hyperlinks to create a horizontal menu: