CSS Layout – The position Property
❮ Previous Next ❯
The position property specifies the type of positioning method used for an element (static, relative, fixed or absolute).
The position Property
The position property specifies the type of positioning method used for an element.
There are four different position values:
static
relative
fixed
absolute
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:
This
Here is the CSS that is used:
Example
div.static {
position: static;
border: 3px solid #73AD21;
}
»
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.
This
Here is the CSS that is used:
Example
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
»
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;
}
»
This
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.
Here is a simple example:
This
This
Here is the CSS that is used:
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;
}
»
Overlapping Elements
When elements are positioned, they can overlap other elements.
The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).
An element can have a positive or negative stack order:
This is a heading
Because the image has a z-index of -1, it will be placed behind the text.
Example
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
»
An element with greater stack order is always in front of an element with a lower stack order.
Note: If two positioned elements overlap without a z-index specified, the element positioned last in the HTML code will be shown on top.
Positioning Text In an Image
How to position text over an image:
Example
Bottom Left
Top Left
Top Right
Bottom Right
Centered
:
Top Left » Top Right » Bottom Left » Bottom Right » Centered »
More Examples
Set the shape of an element
This example demonstrates how to set the shape of an element. The element is clipped into this shape, and displayed.
How to show overflow in an element using scroll
This example demonstrates how to set the overflow property to create a scroll bar when an element’s content is too big to fit in a specified area.
How to set the browser to automatically handle overflow
This example demonstrates how to set the browser to automatically handle overflow.
Change the cursor
This example demonstrates how to change the cursor.
Test Yourself with Exercises!
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 » Exercise 5 »
All CSS Positioning Properties
Property Description
bottom Sets the bottom margin edge for a positioned box
clip Clips an absolutely positioned element
cursor Specifies the type of cursor to be displayed
left Sets the left margin edge for a positioned box
overflow Specifies what happens if content overflows an element’s box
overflow-x Specifies what to do with the left/right edges of the content if it overflows the element’s content area
overflow-y Specifies what to do with the top/bottom edges of the content if it overflows the element’s content area
position Specifies the type of positioning for an element
right Sets the right margin edge for a positioned box
top Sets the top margin edge for a positioned box
z-index Sets the stack order of an element
❮ Previous Next ❯
CSS Layout – Overflow
TUTORIAL HOME
CSS Layout – Overflow
❮ Previous Next ❯
CSS Overflow
The CSS overflow property specifies whether to clip content or to add scrollbars when the content of an element is too big to fit in a specified area.
This text is really long and the height of its container is only 100 pixels. Therefore, a scrollbar is added to help the reader to scroll the content. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem.
The overflow property has the following values:
visible – Default. The overflow is not clipped. It renders outside the element’s box
hidden – The overflow is clipped, and the rest of the content will be invisible
scroll – The overflow is clipped, but a scrollbar is added to see the rest of the content
auto – If overflow is clipped, a scrollbar should be added to see the rest of the content
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).
Visible
By default, the overflow is visible, meaning that it is not clipped and it renders outside the element’s box:
You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element’s box.
Example
div {
width: 200px;
height: 50px;
background-color: #eee;
overflow: visible;
}
»
Hidden
With the hidden value, the overflow is clipped, and the rest of the content is hidden:
Example
div {
overflow: hidden;
}
»
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):
You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element’s box.
Example
div {
overflow: scroll;
}
»
Auto
The auto value is similar to scroll, only it add scrollbars when necessary:
You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element’s box.
Example
div {
overflow: auto;
}
»
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.
You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element’s box.
Example
div {
overflow-x: hidden; /* Hide horizontal scrollbar */
overflow-y: scroll; /* Add vertical scrollbar */
}
»
Test Yourself with Exercises!
Exercise 1 » Exercise 2 » Exercise 3 »
❮ Previous Next ❯
CSS Layout – inline-block
❮ Previous Next ❯
The inline-block Value
It has been possible for a long time to create a grid of boxes that fills the browser width and wraps nicely (when the browser is resized), by using the float property.
However, the inline-block value of the display property makes this even easier.
inline-block elements are like inline elements but they can have a width and a height.
Examples
The old way – using float (notice that we also need to specify a clear property for the element after the floating boxes):
Example
.floating-box {
float: left;
width: 150px;
height: 75px;
margin: 10px;
border: 3px solid #73AD21;
}
.after-box {
clear: left;
}
»
The same effect can be achieved by using the inline-block value of the display property (notice that no clear property is needed):
Example
.floating-box {
display: inline-block;
width: 150px;
height: 75px;
margin: 10px;
border: 3px solid #73AD21;
}
»
❮ Previous Next ❯
CSS Combinators
TUTORIAL HOME
CSS Combinators
❮ Previous Next ❯
CSS Combinators
A combinator is something that explains the relationship between the selectors.
A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator.
There are four different combinators in CSS3:
descendant selector (space)
child selector (>)
adjacent sibling selector (+)
general sibling selector (~)
Descendant Selector
The descendant selector matches all elements that are descendants of a specified element.
The following example selects all
elements inside
Example
div p {
background-color: yellow;
}
»
Child Selector
The child selector selects all elements that are the immediate children of a specified element.
The following example selects all
elements that are immediate children of a
Example
div > p {
background-color: yellow;
}
»
Adjacent Sibling Selector
The adjacent sibling selector selects all elements that are the adjacent siblings of a specified element.
Sibling elements must have the same parent element, and “adjacent” means “immediately following”.
The following example selects all
elements that are placed immediately after
Example
div + p {
background-color: yellow;
}
»
General Sibling Selector
The general sibling selector selects all elements that are siblings of a specified element.
The following example selects all
elements that are siblings of
Example
div ~ p {
background-color: yellow;
}
»
Test Yourself with Exercises!
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 »
❮ Previous Next ❯
CSS Layout – Horizontal & Vertical Align
TUTORIAL HOME
CSS Layout – Horizontal & Vertical Align
❮ Previous Next ❯
▲ ▼
◀ ►
Center elements
horizontally and vertically
Center Align Elements
To horizontally center a block element (like
Setting the width of the element will prevent it from stretching out to the edges of its container.
The element will then take up the specified width, and the remaining space will be split equally between the two margins:
This div element is centered.
Example
.center {
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
»
Note: Center aligning has no effect if the width property is not set (or set to 100%).
Center Align Text
To just center the text inside an element, use text-align: center;
This text is centered.
Example
.center {
text-align: center;
border: 3px solid green;
}
»
Tip: For more examples on how to align text, see the CSS Text chapter.
Center an Image
To center an image, use margin: auto; and make it into a block element:
Example
img {
display: block;
margin: auto;
width: 40%;
}
»
Left and Right Align – Using position
One method for aligning elements is to use position: absolute;:
In my younger and more vulnerable years my father gave me some advice that I’ve been turning over in my mind ever since.
Example
.right {
position: absolute;
right: 0px;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
»
Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.
Tip: When aligning elements with position, always define margin and padding for the element. This is to avoid visual differences in different browsers.
There is also a problem with IE8 and earlier, when using position. If a container element (in our case
Example
body {
margin: 0;
padding: 0;
}
.container {
position: relative;
width: 100%;
}
.right {
position: absolute;
right: 0px;
width: 300px;
background-color: #b0e0e6;
}
»
Left and Right Align – Using float
Another method for aligning elements is to use the float property:
Example
.right {
float: right;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
»
Tip: When aligning elements with float, always define margin and padding for the element. This is to avoid visual differences in different browsers.
There is also a problem with IE8 and earlier, when using float. If the !DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be space reserved for a scrollbar. So, always set the !DOCTYPE declaration when using float:
Example
body {
margin: 0;
padding: 0;
}
.right {
float: right;
width: 300px;
background-color: #b0e0e6;
}
»
Center Vertically – Using padding
There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding:
I am vertically centered.
Example
.center {
padding: 70px 0;
border: 3px solid green;
}
»
To center both vertically and horizontally, use padding and text-align: center:
I am vertically and horizontally centered.
Example
.center {
padding: 70px 0;
border: 3px solid green;
text-align: center;
}
»
Center Vertically – Using line-height
Another trick is to use the line-height property with a value that is equal to the height property.
I am vertically and horizontally centered.
Example
.center {
line-height: 200px;
height: 200px;
border: 3px solid green;
text-align: center;
}
/* If the text has multiple lines, add the following: */
.center p {
line-height: 1.5;
display: inline-block;
vertical-align: middle;
}
»
Center Vertically – Using position & transform
If padding and line-height is not an option, a third solution is to use positioning and the transform property:
I am vertically and horizontally centered.
Example
.center {
height: 200px;
position: relative;
border: 3px solid green;
}
.center p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
»
Tip: You will learn more about the transform property in in our 2D Transforms Chapter.
Test Yourself with Exercises!
Exercise 1 » Exercise 2 »
❮ Previous Next ❯
CSS Pseudo-elements
TUTORIAL HOME
CSS Pseudo-elements
❮ Previous Next ❯
What are Pseudo-Elements?
A CSS pseudo-element is used to style specified parts of an element.
For example, it can be used to:
Style the first letter, or line, of an element
Insert content before, or after, the content of an element
Syntax
The syntax of pseudo-elements:
selector::pseudo-element {
property:value;
}
Notice the double colon notation – ::first-line versus :first-line
The double colon replaced the single-colon notation for pseudo-elements in CSS3. This was an attempt from W3C to distinguish between pseudo-classes and pseudo-elements.
The single-colon syntax was used for both pseudo-classes and pseudo-elements in CSS2 and CSS1.
For backward compatibility, the single-colon syntax is acceptable for CSS2 and CSS1 pseudo-elements.
The ::first-line Pseudo-element
The ::first-line pseudo-element is used to add a special style to the first line of a text.
The following example formats the first line of the text in all
elements:
Example
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
»
Note: The ::first-line pseudo-element can only be applied to block-level elements.
The following properties apply to the ::first-line pseudo-element:
font properties
color properties
background properties
word-spacing
letter-spacing
text-decoration
vertical-align
text-transform
line-height
clear
The ::first-letter Pseudo-element
The ::first-letter pseudo-element is used to add a special style to the first letter of a text.
The following example formats the first letter of the text in all
elements:
Example
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
»
Note: The ::first-letter pseudo-element can only be applied to block-level elements.
The following properties apply to the ::first-letter pseudo- element:
font properties
color properties
background properties
margin properties
padding properties
border properties
text-decoration
vertical-align (only if “float” is “none”)
text-transform
line-height
float
clear
Pseudo-elements and CSS Classes
Pseudo-elements can be combined with CSS classes:
Example
p.intro::first-letter {
color: #ff0000;
font-size:200%;
}
»
The example above will display the first letter of paragraphs with class=”intro”, in red and in a larger size.
Multiple Pseudo-elements
Several pseudo-elements can also be combined.
In the following example, the first letter of a paragraph will be red, in an xx-large font size. The rest of the first line will be blue, and in small-caps. The rest of the paragraph will be the default font size and color:
Example
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
p::first-line {
color: #0000ff;
font-variant: small-caps;
}
»
CSS – The ::before Pseudo-element
The ::before pseudo-element can be used to insert some content before the content of an element.
The following example inserts an image before the content of each
element:
Example
h1::before {
content: url(smiley.gif);
}
»
CSS – The ::after Pseudo-element
The ::after pseudo-element can be used to insert some content after the content of an element.
The following example inserts an image after the content of each
element:
Example
h1::after {
content: url(smiley.gif);
}
»
CSS – The ::selection Pseudo-element
The ::selection pseudo-element matches the portion of an element that is selected by a user.
The following CSS properties can be applied to ::selection: color, background, cursor, and outline.
The following example makes the selected text red on a yellow background:
Example
::selection {
color: red;
background: yellow;
}
»
Test Yourself with Exercises!
Exercise 1 » Exercise 2 » Exercise 3 »
All CSS Pseudo Elements
Selector Example Example description
::after p::after Insert something after the content of each
element
::before p::before Insert something before the content of each
element
::first-letter p::first-letter Selects the first letter of each
element
::first-line p::first-line Selects the first line of each
element
::selection p::selection Selects the portion of an element that is selected by a user
All CSS Pseudo Classes
Selector Example Example description
:active a:active Selects the active link
:checked input:checked Selects every checked element
:disabled input:disabled Selects every disabled element
:empty p:empty Selects every
element that has no children
:enabled input:enabled Selects every enabled element
:first-child p:first-child Selects every
elements that is the first child of its parent
:first-of-type p:first-of-type Selects every
element that is the first
element of its parent
:focus input:focus Selects the element that has focus
:hover a:hover Selects links on mouse over
:in-range input:in-range Selects elements with a value within a specified range
:invalid input:invalid Selects all elements with an invalid value
:lang(language) p:lang(it) Selects every
element with a lang attribute value starting with “it”
:last-child p:last-child Selects every
elements that is the last child of its parent
:last-of-type p:last-of-type Selects every
element that is the last
element of its parent
:link a:link Selects all unvisited links
:not(selector) :not(p) Selects every element that is not a
element
:nth-child(n) p:nth-child(2) Selects every
element that is the second child of its parent
:nth-last-child(n) p:nth-last-child(2) Selects every
element that is the second child of its parent, counting from the last child
:nth-last-of-type(n) p:nth-last-of-type(2) Selects every
element that is the second
element of its parent, counting from the last child
:nth-of-type(n) p:nth-of-type(2) Selects every
element that is the second
element of its parent
:only-of-type p:only-of-type Selects every
element that is the only
element of its parent
:only-child p:only-child Selects every
element that is the only child of its parent
:optional input:optional Selects elements with no “required” attribute
:out-of-range input:out-of-range Selects elements with a value outside a specified range
:read-only input:read-only Selects elements with a “readonly” attribute specified
:read-write input:read-write Selects elements with no “readonly” attribute
:required input:required Selects elements with a “required” attribute specified
:root root Selects the document’s root element
:target #news:target Selects the current active #news element (clicked on a URL containing that anchor name)
:valid input:valid Selects all elements with a valid value
:visited a:visited Selects all visited links
❮ Previous Next ❯
CSS Navigation Bar
CSS Navigation Bar
❮ Previous Next ❯
Demo: Navigation Bars
Vertical
Home
News
Contact
About
Horizontal
Home
News
Contact
About
Home
News
Contact
About
Navigation Bars
Having easy-to-use navigation is important for any web site.
With CSS you can transform boring HTML menus into good-looking navigation bars.
Navigation Bar = List of Links
A navigation bar needs standard HTML as a base.
In our examples we will build the navigation bar from a standard HTML list.
A navigation bar is basically a list of links, so using the
- and
- elements makes perfect sense:
Example
- Home</li>
- News
- Contact</a>
- About</li>
»
Now let’s remove the bullets and the margins and padding from the list:Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
»
Example explained:list-style-type: none; – Removes the bullets. A navigation bar does not need list markers
Set margin: 0; and padding: 0; to remove browser default settings
The code in the example above is the standard code used in both vertical, and horizontal navigation bars.Vertical Navigation Bar
To build a vertical navigation bar, you can style the elements inside the list, in addition to the code above:Example
li a {
display: block;
width: 60px;
}
»
Example explained:display: block; – Displaying the links as block elements makes the whole link area clickable (not just the text), and it allows us to specify the width (and padding, margin, height, etc. if you want)
width: 60px; – Block elements take up the full width available by default. We want to specify a 60 pixels width
You can also set the width of- , and remove the width of , as they will take up the full width available when displayed as block elements. This will produce the same result as our previous example:
- or to center the links.
Add the border property to
- add a border around the navbar. If you also want borders inside the navbar, add a border-bottom to all
- elements, except for the last one:
Home
News
Contact
About
Example
ul {
border: 1px solid #555;
}li {
text-align: center;
border-bottom: 1px solid #555;
}li:last-child {
border-bottom: none;
}
»
Full-height Fixed Vertical Navbar
Create a full-height, “sticky” side navigation:Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
height: 100%; /* Full height */
position: fixed; /* Make it stick, even on scroll */
overflow: auto; /* Enable scrolling if the sidenav has too much content */
}
»
Note: This example might not work properly on mobile devices.Horizontal Navigation Bar
There are two ways to create a horizontal navigation bar. Using inline or floating list items.Inline List Items
One way to build a horizontal navigation bar is to specify the - elements as inline, in addition to the “standard” code above:
Example
li {
display: inline;
}
»
Example explained:display: inline; – By default,
- elements are block elements. Here, we remove the line breaks before and after each list item, to display them on one line
Floating List Items
Another way of creating a horizontal navigation bar is to float the - elements, and specify a layout for the navigation links:
Example
li {
float: left;
}a {
display: block;
padding: 8px;
background-color: #dddddd;
}
»
Example explained:float: left; – use float to get block elements to slide next to each other
display: block; – Displaying the links as block elements makes the whole link area clickable (not just the text), and it allows us to specify padding (and height, width, margins, etc. if you want)
padding: 8px; – Since block elements take up the full width available, they cannot float next to each other. Therefore, specify some padding to make them look good
background-color: #dddddd; – Add a gray background-color to each a element
Tip: Add the background-color to
- elements, except for the last one:
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 60px;
}li a {
display: block;
}
»
Vertical Navigation Bar Examples
Create a basic vertical navigation bar with a gray background color and change the background color of the links when the user moves the mouse over them:Home
News
Contact
About
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
}li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}/* Change the link color on hover */
li a:hover {
background-color: #555;
color: white;
}
»
Active/Current Navigation Link
Add an “active” class to the current link to let the user know which page he/she is on:Home
News
Contact
About
Example
.active {
background-color: #4CAF50;
color: white;
}
»
Center Links & Add Borders
Add text-align:center to - Home</li>
CSS Opacity / Transparency
TUTORIAL HOME
CSS Opacity / Transparency
❮ Previous Next ❯
The opacity property specifies the opacity/transparency of an element.
Transparent Image
The opacity property can take a value from 0.0 – 1.0. The lower value, the more transparent:
opacity 0.2
opacity 0.5
opacity 1
(default)
Note: IE8 and earlier use filter:alpha(opacity=x). The x can take a value from 0 – 100. A lower value makes the element more transparent.
Example
img {
opacity: 0.5;
filter: alpha(opacity=50); /* For IE8 and earlier */
}
»
Transparent Hover Effect
The opacity property is often used together with the :hover selector to change the opacity on mouse-over:
Example
img {
opacity: 0.5;
filter: alpha(opacity=50); /* For IE8 and earlier */
}
img:hover {
opacity: 1.0;
filter: alpha(opacity=100); /* For IE8 and earlier */
}
»
Example explained
The first CSS block is similar to the code in Example 1. In addition, we have added what should happen when a user hovers over one of the images. In this case we want the image to NOT be transparent when the user hovers over it. The CSS for this is opacity:1;.
When the mouse pointer moves away from the image, the image will be transparent again.
An example of reversed hover effect:
Example
img:hover {
opacity: 0.5;
filter: alpha(opacity=50); /* For IE8 and earlier */
}
»
Transparent Box
When using the opacity property to add transparency to the background of an element, all of its child elements inherit the same transparency. This can make the text inside a fully transparent element hard to read:
opacity 1
opacity 0.6
opacity 0.3
opacity 0.1
Example
div {
opacity: 0.3;
filter: alpha(opacity=30); /* For IE8 and earlier */
}
»
Transparency using RGBA
If you do not want to apply opacity to child elements, like in our example above, use RGBA color values. The following example sets the opacity for the background color and not the text:
100% opacity
60% opacity
30% opacity
10% opacity
You learned from our CSS Colors Chapter, that you can use RGB as a color value. In addition to RGB, CSS3 introduced an RGB color value with an alpha channel (RGBA) – which specifies the opacity for a color.
An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).
Tip: You will learn more about RGBA Colors in our CSS3 Colors Chapter.
Example
div {
background: rgba(76, 175, 80, 0.3) /* Green background with 30% opacity */
}
»
Text in Transparent Box
This is some text that is placed in the transparent box.
Example
div.background {
background: url(klematis.jpg) repeat;
border: 2px solid black;
}
div.transbox {
margin: 30px;
background-color: #ffffff;
border: 1px solid black;
opacity: 0.6;
filter: alpha(opacity=60); /* For IE8 and earlier */
}
div.transbox p {
margin: 5%;
font-weight: bold;
color: #000000;
}
This is some text that is placed in the transparent box.
»
First, we create a
element.
Test Yourself with Exercises!
Exercise 1 » Exercise 2 »
❮ Previous Next ❯
CSS Dropdowns
CSS Dropdowns
❮ Previous Next ❯
Create a hoverable dropdown with CSS.
Demo: Dropdown Examples
Move the mouse over the examples below:
Dropdown Text
Hello World!
Dropdown Menu
Link 1 Link 2 Link 3
Other:
Beautiful Trolltunga, Norway
Basic Dropdown
Create a dropdown box that appears when the user moves the mouse over an element.
Example
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
padding: 12px 16px;
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
Hello World!
»
Example Explained
HTML) Use any element to open the dropdown content, e.g. a , or a element.
Use a container element (like
Wrap a
CSS) The .dropdown class use position:relative, which is needed when we want the dropdown content to be placed right below the dropdown button (using position:absolute).
The .dropdown-content class holds the actual dropdown content. It is hidden by default, and will be displayed on hover (see below). Note the min-width is set to 160px. Feel free to change this. Tip: If you want the width of the dropdown content to be as wide as the dropdown button, set the width to 100% (and overflow:auto to enable scroll on small screens).
Instead of using a border, we have used the CSS3 box-shadow property to make the dropdown menu look like a “card”.
The :hover selector is used to show the dropdown menu when the user moves the mouse over the dropdown button.
Dropdown Menu
Create a dropdown menu that allows the user to choose an option from a list:
Dropdown Menu
Link 1 Link 2 Link 3
This example is similar to the previous one, except that we add links inside the dropdown box and style them to fit a styled dropdown button:
Example
/* Style The Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* The container
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
»
Right-aligned Dropdown Content
Left
Link 1 Link 2 Link 3
Right
Link 1 Link 2 Link 3
If you want the dropdown menu to go from right to left, instead of left to right, add right: 0;
Example
.dropdown-content {
right: 0;
}
»
More Examples
Dropdown Image
How to add an image and other content inside the dropdown box.
Hover over the image:
Beautiful Trolltunga, Norway
»
Dropdown Navbar
How to add a dropdown menu inside a navigation bar.
»
❮ Previous Next ❯
CSS Tooltip
TUTORIAL HOME
CSS Tooltip
❮ Previous Next ❯
Create tooltips with CSS.
Demo: Tooltip Examples
A tooltip is often used to specify extra information about something when the user moves the mouse pointer over an element:
Top Tooltip text
Right Tooltip text
Bottom Tooltip text
Left Tooltip text
Basic Tooltip
Create a tooltip that appears when the user moves the mouse over an element:
Example
/* Tooltip container */
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black; /* If you want dots under the hoverable text */
}
/* Tooltip text */
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
/* Position the tooltip text – see examples below! */
position: absolute;
z-index: 1;
}
/* Show the tooltip text when you mouse over the tooltip container */
.tooltip:hover .tooltiptext {
visibility: visible;
}
Tooltip text
»
Example Explained
HTML) Use a container element (like
The tooltip text is placed inside an inline element (like ) with class=”tooltiptext”.
CSS) The tooltip class use position:relative, which is needed to position the tooltip text (position:absolute). Note: See examples below on how to position the tooltip.
The tooltiptext class holds the actual tooltip text. It is hidden by default, and will be visible on hover (see below). We have also added some basic styles to it: 120px width, black background color, white text color, centered text, and 5px top and bottom padding.
The CSS3 border-radius property is used to add rounded corners to the tooltip text.
The :hover selector is used to show the tooltip text when the user moves the mouse over the
Positioning Tooltips
In this example, the tooltip is placed to the right (left:105%) of the “hoverable” text (
Right Tooltip
.tooltip .tooltiptext {
top: -5px;
left: 105%;
}
Result:
Hover over me Tooltip text »
Left Tooltip
.tooltip .tooltiptext {
top: -5px;
right: 105%;
}
Result:
Hover over me Tooltip text »
If you want the tooltip to appear on top or on the bottom, see examples below. Note that we use the margin-left property with a value of minus 60 pixels. This is to center the tooltip above/below the hoverable text. It is set to the half of the tooltip’s width (120/2 = 60).
Top Tooltip
.tooltip .tooltiptext {
width: 120px;
bottom: 100%;
left: 50%;
margin-left: -60px; /* Use half of the width (120/2 = 60), to center the tooltip */
}
Result:
Hover over me Tooltip text »
Bottom Tooltip
.tooltip .tooltiptext {
width: 120px;
top: 100%;
left: 50%;
margin-left: -60px; /* Use half of the width (120/2 = 60), to center the tooltip */
}
Result:
Hover over me Tooltip text »
Tooltip Arrows
To create an arrow that should appear from a specific side of the tooltip, add “empty” content after tooltip, with the pseudo-element class ::after together with the content property. The arrow itself is created using borders. This will make the tooltip look like a speech bubble.
This example demonstrates how to add an arrow to the bottom of the tooltip:
Bottom Arrow
.tooltip .tooltiptext::after {
content: ” “;
position: absolute;
top: 100%; /* At the bottom of the tooltip */
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: black transparent transparent transparent;
}
Result:
Hover over me Tooltip text »
Example Explained
Position the arrow inside the tooltip: top: 100% will place the arrow at the bottom of the tooltip. left: 50% will center the arrow.
Note: The border-width property specifies the size of the arrow. If you change this, also change the margin-left value to the same. This will keep the arrow centered.
The border-color is used to transform the content into an arrow. We set the top border to black, and the rest to transparent. If all sides were black, you would end up with a black square box.
This example demonstrates how to add an arrow to the top of the tooltip. Notice that we set the bottom border color this time:
Top Arrow
.tooltip .tooltiptext::after {
content: ” “;
position: absolute;
bottom: 100%; /* At the top of the tooltip */
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent black transparent;
}
Result:
Hover over me Tooltip text »
This example demonstrates how to add an arrow to the left of the tooltip:
Left Arrow
.tooltip .tooltiptext::after {
content: ” “;
position: absolute;
top: 50%;
right: 100%; /* To the left of the tooltip */
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent black transparent transparent;
}
Result:
Hover over me Tooltip text »
This example demonstrates how to add an arrow to the right of the tooltip:
Right Arrow
.tooltip .tooltiptext::after {
content: ” “;
position: absolute;
top: 50%;
left: 100%; /* To the right of the tooltip */
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent transparent black;
}
Result:
Hover over me Tooltip text »
Fade In Tooltips (Animation)
If you want to fade in the tooltip text when it is about to be visible, you can use the CSS3 transition property together with the opacity property, and go from being completely invisible to 100% visible, in a number of specified seconds (1 second in our example):
Example
.tooltip .tooltiptext {
opacity: 0;
transition: opacity 1s;
}
.tooltip:hover .tooltiptext {
opacity: 1;
}
»
Note: You will learn more about transitions and animations later in our tutorial.
❮ Previous Next ❯

