DW Faisalabad New Version

DW Faisalabad New Version
Please Jump to New Version
Showing posts with label CSS3 Tutorials. Show all posts
Showing posts with label CSS3 Tutorials. Show all posts

Sunday, 27 August 2017

CSS3 Media Queries

CSS2 Introduced Media Types
The @media rule, introduced in CSS2, made it possible to define different style rules for different media types.



Examples: You could have one set of style rules for computer screens, one for printers, one for handheld devices, one for television-type devices, and so on.

Unfortunately these media types never got a lot of support by devices, other than the print media type.

CSS3 Introduces Media Queries
Media queries in CSS3 extend the CSS2 media types idea: Instead of looking for a type of device, they look at the capability of the device.

Media queries can be used to check many things, such as:

  • width and height of the viewport
  • width and height of the device
  • orientation (is the tablet/phone in landscape or portrait mode?)
  • resolution

Using media queries are a popular technique for delivering a tailored style sheet to tablets, iPhone, and Androids.

Media Query Syntax
A media query consists of a media type and can contain one or more expressions, which resolve to either true or false.

@media not|only mediatype and (expressions) {
    CSS-Code;
}
The result of the query is true if the specified media type matches the type of device the document is being displayed on and all expressions in the media query are true. When a media query is true, the corresponding style sheet or style rules are applied, following the normal cascading rules.

Unless you use the not or only operators, the media type is optional and the all type will be implied.

You can also have different stylesheets for different media:

<link rel="stylesheet" media="mediatype and|not|only (expressions)" href="print.css">

CSS3 Media Types

Values
Description
all Used for all media type devices
print Used for printers
screen Used for computer screens, tablets, smart-phones etc.
speech Used for screenreaders that "reads" the page out loud

Media Queries Simple ExamplesOne way to use media queries is to have an alternate CSS section right inside your style sheet.

The following example changes the background-color to lightgreen if the viewport is 480 pixels wide or wider (if the viewport is less than 480 pixels, the background-color will be pink):

Example
@media screen and (min-width: 480px) {
    body {
        background-color: lightgreen;
    }
}
Try it Yourself »

The following example shows a menu that will float to the left of the page if the viewport is 480 pixels wide or wider (if the viewport is less than 480 pixels, the menu will be on top of the content):

Example
@media screen and (min-width: 480px) {
    #leftsidebar {width: 200px; float: left;}
    #main {margin-left:216px;}
}
Try it Yourself »

CSS3 @media Reference
For a full overview of all the media types and features/expressions, please look at the @media rule in our CSS reference.
Read More »

CSS3 Flexbox

Flexible boxes, or flexbox, is a new layout mode in CSS3.



Use of flexbox ensures that elements behave predictably when the page layout must accommodate different screen sizes and different display devices.

For many applications, the flexible box model provides an improvement over the block model in that it does not use floats, nor do the flex container's margins collapse with the margins of its contents.

CSS3 Flexbox Concepts
Flexbox consists of flex containers and flex items.

A flex container is declared by setting the display property of an element to either flex (rendered as a block) or inline-flex (rendered as inline).

Inside a flex container there is one or more flex items.

Note: Everything outside a flex container and inside a flex item is rendered as usual. Flexbox defines how flex items are laid out inside a flex container.

Flex items are positioned inside a flex container along a flex line. By default there is only one flex line per flex container.

The following example shows three flex items. They are positioned by default: along the horizontal flex line, from left to right:

Example
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
    display: -webkit-flex;
    display: flex;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
.flex-item {
    background-color: cornflowerblue;
    width: 100px;
    height: 100px;
    margin: 10px;
}
</style>
</head>
<body>
<div class="flex-container">
  <div class="flex-item">flex item 1</div>
  <div class="flex-item">flex item 2</div>
  <div class="flex-item">flex item 3</div>
</div>
</body>
</html>
Try it Yourself »

It is also possible to change the direction of the flex line.

If we set the direction property to rtl (right-to-left), the text is drawn right to left, and also the flex line changes direction, which will change the page layout:

Example
body {
    direction: rtl;
}
.flex-container {
    display: -webkit-flex;
    display: flex;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
.flex-item {
    background-color: cornflowerblue;
    width: 100px;
    height: 100px;
    margin: 10px;
}
Try it Yourself »

Flex Direction
The flex-direction property specifies the direction of the flexible items inside the flex container. The default value of flex-direction is row (left-to-right, top-to-bottom).

The other values are as follows:
  • row-reverse - If the writing-mode (direction) is left to right, the flex items will be laid out right to left
  • column - If the writing system is horizontal, the flex items will be laid out vertically
  • column-reverse - Same as column, but reversed

The following example shows the result of using the row-reverse value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-direction: row-reverse;
    flex-direction: row-reverse;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The following example shows the result of using the column value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-direction: column;
    flex-direction: column;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The following example shows the result of using the column-reverse value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-direction: column-reverse;
    flex-direction: column-reverse;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The justify-content Property
The justify-content property horizontally aligns the flexible container's items when the items do not use all available space on the main-axis.

The possible values are as follows:

  • flex-start - Default value. Items are positioned at the beginning of the container
  • flex-end - Items are positioned at the end of the container
  • center - Items are positioned at the center of the container
  • space-between - Items are positioned with space between the lines
  • space-around - Items are positioned with space before, between, and after the lines

The following example shows the result of using the flex-end value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: flex-end;
    justify-content: flex-end;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »
The following example shows the result of using the center value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: center;
    justify-content: center;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »
The following example shows the result of using the space-between value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: space-between;
    justify-content: space-between;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »
The following example shows the result of using the space-around value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: space-around;
    justify-content: space-around;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The align-items Property
The align-items property vertically aligns the flexible container's items when the items do not use all available space on the cross-axis.

The possible values are as follows:

  • stretch - Default value. Items are stretched to fit the container
  • flex-start - Items are positioned at the top of the container
  • flex-end - Items are positioned at the bottom of the container
  • center - Items are positioned at the center of the container (vertically)
  • baseline - Items are positioned at the baseline of the container

The following example shows the result of using the stretch value (this is the default value):

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-align-items: stretch;
    align-items: stretch;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The following example shows the result of using the flex-start value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-align-items: flex-start;
    align-items: flex-start;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The following example shows the result of using the flex-end value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-align-items: flex-end;
    align-items: flex-end;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The following example shows the result of using the center value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-align-items: center;
    align-items: center;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The following example shows the result of using the baseline value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-align-items: baseline;
    align-items: baseline;
    width: 400px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The flex-wrap Property
The flex-wrap property specifies whether the flex items should wrap or not, if there is not enough room for them on one flex line.

The possible values are as follows:

  • nowrap - Default value. The flexible items will not wrap
  • wrap - The flexible items will wrap if necessary
  • wrap-reverse - The flexible items will wrap, if necessary, in reverse order

The following example shows the result of using the nowrap value (this is the default value):

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-wrap: nowrap;
    flex-wrap: nowrap;
    width: 300px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The following example shows the result of using the wrap value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-wrap: wrap;
    flex-wrap: wrap;
    width: 300px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The following example shows the result of using the wrap-reverse value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-wrap: wrap-reverse;
    flex-wrap: wrap-reverse;
    width: 300px;
    height: 250px;
    background-color: lightgrey;
}
Try it Yourself »

The align-content Property
The align-content property modifies the behavior of the flex-wrap property. It is similar to align-items, but instead of aligning flex items, it aligns flex lines.

The possible values are as follows:

  • stretch - Default value. Lines stretch to take up the remaining space
  • flex-start - Lines are packed toward the start of the flex container
  • flex-end - Lines are packed toward the end of the flex container
  • center - Lines are packed toward the center of the flex container
  • space-between - Lines are evenly distributed in the flex container
  • space-around - Lines are evenly distributed in the flex container, with half-size spaces on either end

The following example shows the result of using the center value:

Example
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-align-content: center;
    align-content: center;
    width: 300px;
    height: 300px;
    background-color: lightgrey;
}
Try it Yourself »

Flex Item Properties
Ordering
The order property specifies the order of a flexible item relative to the rest of the flexible items inside the same container:

Example
.flex-item {
    background-color: cornflowerblue;
    width: 100px;
    height: 100px;
    margin: 10px;
}
.first {
    -webkit-order: -1;
    order: -1;
}
Try it Yourself »

Margin
Setting margin: auto; will absorb extra space. It can be used to push flex items into different positions.

In the following example we set margin-right: auto; on the first flex item. This will cause all the extra space to be absorbed to the right of that element:

Example
.flex-item {
    background-color: cornflowerblue;
    width: 75px;
    height: 75px;
    margin: 10px;
}
.flex-item:first-child {
    margin-right: auto;
}
Try it Yourself »

Perfect Centering
In the following example we will solve an almost daily problem: perfect centering.

It is very easy with flexbox. Setting margin: auto; will make the item perfectly centered in both axis:

Example
.flex-item {
    background-color: cornflowerblue;
    width: 75px;
    height: 75px;
    margin: auto;
}
Try it Yourself »

align-self
The align-self property of flex items overrides the flex container's align-items property for that item. It has the same possible values as the align-items property.

The following example sets different align-self values to each flex item:

Example
.flex-item {
    background-color: cornflowerblue;
    width: 60px;
    min-height: 100px;
    margin: 10px;
}
.item1 {
    -webkit-align-self: flex-start;
    align-self: flex-start;
}
.item2 {
    -webkit-align-self: flex-end;
    align-self: flex-end;
}
.item3 {
    -webkit-align-self: center;
    align-self: center;
}
.item4 {
    -webkit-align-self: baseline;
    align-self: baseline;
}
.item5 {
    -webkit-align-self: stretch;
    align-self: stretch;
}
Try it Yourself »

flex
The flex property specifies the length of the flex item, relative to the rest of the flex items inside the same container.

In the following example, the first flex item will consume 2/4 of the free space, and the other two flex items will consume 1/4 of the free space each:

Example
.flex-item {
    background-color: cornflowerblue;
    margin: 10px;
}
.item1 {
    -webkit-flex: 2;
    flex: 2;
}
.item2 {
    -webkit-flex: 1;
    flex: 1;
}
.item3 {
    -webkit-flex: 1;
    flex: 1;
}
Try it Yourself »

CSS3 Flexbox Properties
The following table lists the CSS properties used with flexbox:

Property
Description
display Specifies the type of box used for an HTML element
flex-direction Specifies the direction of the flexible items inside a flex container
justify-content Horizontally aligns the flex items when the items do not use all available space on the main-axis
align-items Vertically aligns the flex items when the items do not use all available space on the cross-axis
flex-wrap Specifies whether the flex items should wrap or not, if there is not enough room for them on one flex line
align-content Modifies the behavior of the flex-wrap property. It is similar to align-items, but instead of aligning flex items, it aligns flex lines
flex-flow A shorthand property for flex-direction and flex-wrap
order Specifies the order of a flexible item relative to the rest of the flex items inside the same container
align-self Used on flex items. Overrides the container's align-items property
flex Specifies the length of a flex item, relative to the rest of the flex items inside the same container

Read More »

CSS3 Box Sizing

The CSS3 box-sizing property allows us to include the padding and border in an element's total width and height.

Without the CSS3 box-sizing Property
By default, the width and height of an element is calculated like this:

width + padding + border = actual width of an element
height + padding + border = actual height of an element

This means: When you set the width/height of an element, the element often appear bigger than you have set (because the element's border and padding are added to the element's specified width/height).

The following illustration shows two <div> elements with the same specified width and height:





The two <div> elements above end up with different sizes in the result (because div2 has a padding specified):

Example
.div1 {
    width: 300px;
    height: 100px;
    border: 1px solid blue;
}
.div2 {
    width: 300px;
    height: 100px;
    padding: 50px;
    border: 1px solid red;
}
Try it Yourself »

So, for a long time web developers have specified a smaller width value than they wanted, because they had to subtract out the padding and borders.

With CSS3, the box-sizing property solves this problem.


With the CSS3 box-sizing Property
The CSS3 box-sizing property allows us to include the padding and border in an element's total width and height.

If you set box-sizing: border-box; on an element padding and border are included in the width and height:



Here is the same example as above, with box-sizing: border-box; added to both <div> elements:

Example
.div1 {
    width: 300px;
    height: 100px;
    border: 1px solid blue;
    box-sizing: border-box;
}
.div2 {
    width: 300px;
    height: 100px;
    padding: 50px;
    border: 1px solid red;
    box-sizing: border-box;
}
Try it Yourself »

Since the result of using the box-sizing: border-box; is so much better, many developers want all elements on their pages to work this way.

The code below ensures that all elements are sized in this more intuitive way. Many browsers already use box-sizing: border-box; for many form elements (but not all - which is why inputs and text areas look different at width: 100%;).

Applying this to all elements is safe and wise:

Example
* {
    box-sizing: border-box;
}
Try it Yourself »

Read More »

Wednesday, 16 August 2017

CSS3 User Interface

CSS3 has new user interface features such as resizing elements, outlines, and box sizing.

In this chapter you will learn about the following user interface properties:

  • resize
  • outline-offset

CSS3 Resizing
The resize property specifies whether or not an element should be resizable by the user.


The following example lets the user resize only the width of a <div> element:

Example
div {
    resize: horizontal;
    overflow: auto;
}
Try it Yourself »
The following example lets the user resize only the height of a <div> element:

Example
div {
    resize: vertical;
    overflow: auto;
}
Try it Yourself »
The following example lets the user resize both the height and the width of a <div> element:

Example
div {
    resize: both;
    overflow: auto;
}
Try it Yourself »

CSS3 Outline Offset
The outline-offset property adds space between an outline and the edge or border of an element.

Outlines differ from borders in three ways:
  • An outline is a line drawn around elements, outside the border edge
  • An outline does not take up space
  • An outline may be non-rectangular

The following example uses the outline-offset property to add a 15px space between the border and the outline:

Example
div {
    border: 1px solid black;
    outline: 1px solid red;
    outline-offset: 15px;
}
Try it Yourself »

CSS3 User Interface Properties
The following table lists all the user interface properties:

Property
Description
box-sizing Allows you to include the padding and border in an element's total width and height
nav-down Specifies where to navigate when using the arrow-down navigation key
nav-index Specifies the tabbing order for an element
nav-left Specifies where to navigate when using the arrow-left navigation key
nav-right Specifies where to navigate when using the arrow-right navigation key
nav-up Specifies where to navigate when using the arrow-up navigation key
outline-offset Adds space between an outline and the edge or border of an element
resize Specifies whether or not an element is resizable by the user

Read More »

CSS3 Multiple Columns

The CSS3 multi-column layout allows easy definition of multiple columns of text - just like in newspapers:



CSS3 Multi-column Properties
In this chapter you will learn about the following multi-column properties:

  • column-count
  • column-gap
  • column-rule-style
  • column-rule-width
  • column-rule-color
  • column-rule
  • column-span
  • column-width

CSS3 Create Multiple Columns
The column-count property specifies the number of columns an element should be divided into.

The following example will divide the text in the <div> element into 3 columns:

Example
div {
    -webkit-column-count: 3; /* Chrome, Safari, Opera */
    -moz-column-count: 3; /* Firefox */
    column-count: 3;
}
Try it Yourself »

CSS3 Specify the Gap Between Columns
The column-gap property specifies the gap between the columns.

The following example specifies a 40 pixels gap between the columns:

Example
div {
    -webkit-column-gap: 40px; /* Chrome, Safari, Opera */
    -moz-column-gap: 40px; /* Firefox */
    column-gap: 40px;
}
Try it Yourself »

CSS3 Column Rules
The column-rule-style property specifies the style of the rule between columns:

Example
div {
    -webkit-column-rule-style: solid; /* Chrome, Safari, Opera */
    -moz-column-rule-style: solid; /* Firefox */
    column-rule-style: solid;
}
Try it Yourself »

The column-rule-width property specifies the width of the rule between columns:

Example
div {
    -webkit-column-rule-width: 1px; /* Chrome, Safari, Opera */
    -moz-column-rule-width: 1px; /* Firefox */
    column-rule-width: 1px;
}
Try it Yourself »
The column-rule-color property specifies the color of the rule between columns:

Example
div {
    -webkit-column-rule-color: lightblue; /* Chrome, Safari, Opera */
    -moz-column-rule-color: lightblue; /* Firefox */
    column-rule-color: lightblue;
}
Try it Yourself »

The column-rule property is a shorthand property for setting all the column-rule-* properties above.

The following example sets the width, style, and color of the rule between columns:

Example
div {
    -webkit-column-rule: 1px solid lightblue; /* Chrome, Safari, Opera */
    -moz-column-rule: 1px solid lightblue; /* Firefox */
    column-rule: 1px solid lightblue;
}
Try it Yourself »

Specify How Many Columns an Element Should Span
The column-span property specifies how many columns an element should span across.

The following example specifies that the <h2> element should span across all columns:

Example
h2 {
    -webkit-column-span: all; /* Chrome, Safari, Opera */
    column-span: all;
}
Try it Yourself »

Specify The Column Width
The column-width property specifies a suggested, optimal width for the columns.

The following example specifies that the suggested, optimal width for the columns should be 100px:

Example
div {
    -webkit-column-width: 100px; /* Chrome, Safari, Opera */
    -moz-column-width: 100px; /* Firefox */
    column-width: 100px;
}
Try it Yourself »

CSS3 Multi-columns Properties
The following table lists all the multi-columns properties:

Property
Description
column-count Specifies the number of columns an element should be divided into
column-fill Specifies how to fill columns
column-gap Specifies the gap between the columns
column-rule A shorthand property for setting all the column-rule-* properties
column-rule-color Specifies the color of the rule between columns
column-rule-style Specifies the style of the rule between columns
column-rule-width Specifies the width of the rule between columns
column-span Specifies how many columns an element should span across
column-width Specifies a suggested, optimal width for the columns
columns A shorthand property for setting column-width and column-count

Read More »

CSS Pagination Examples

Learn how to create a responsive pagination using CSS.

Simple Pagination
If you have a website with lots of pages, you may wish to add some sort of pagination to each page:



Example
.pagination {
    display: inline-block;
}
.pagination a {
    color: black;
    float: left;
    padding: 8px 16px;
    text-decoration: none;
}
Try it Yourself »

Active and Hoverable Pagination



Highlight the current page with an .active class, and use the :hover selector to change the color of each page link when moving the mouse over them:

Example
.pagination a.active {
    background-color: #4CAF50;
    color: white;
}
.pagination a:hover:not(.active) {background-color: #ddd;}
Try it Yourself »

Rounded Active and Hoverable Buttons



Add the border-radius property if you want a rounded "active" and "hover" button:

Example
.pagination a {
    border-radius: 5px;
}
.pagination a.active {
    border-radius: 5px;
}
Try it Yourself »

More Examples

  • Hoverable Transition Effect: Add the transition property to the page links to create a transition effect on hover:

Try it Yourself »


  • Bordered Pagination:Use the border property to add borders to the pagination:

Try it Yourself »


  • Rounded Borders: Tip: Add rounded borders to your first and last link in the pagination:

Try it Yourself »


  • Space Between Links: Tip: Add the margin property if you do not want to group the page links:

Try it Yourself »


  • Pagination Size: Change the size of the pagination with the font-size property:

Try it Yourself »


  • Centered Pagination: To center the pagination, wrap a container element (like <div>) around it with text-align:center

Try it Yourself »

Breadcrumbs



Another variation of pagination is so-called "breadcrumbs":

Example
ul.breadcrumb {
    padding: 8px 16px;
    list-style: none;
    background-color: #eee;
}
ul.breadcrumb li {display: inline;}
ul.breadcrumb li+li:before {
    padding: 8px;
    color: black;
    content: "/\00a0";
}
Try it Yourself »

Read More »

Tuesday, 15 August 2017

CSS Buttons

Learn how to style buttons using CSS.

Basic Button Styling



Example
.button {
    background-color: #4CAF50; /* Green */
    border: none;
    color: white;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
}
Try it Yourself »

Button Colors



Use the background-color property to change the background color of a button:

Example
.button1 {background-color: #4CAF50;} /* Green */
.button2 {background-color: #008CBA;} /* Blue */
.button3 {background-color: #f44336;} /* Red */
.button4 {background-color: #e7e7e7; color: black;} /* Gray */
.button5 {background-color: #555555;} /* Black */
Try it Yourself »

Button Sizes



Use the font-size property to change the font size of a button:

Example
.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 20px;}
.button5 {font-size: 24px;}
Try it Yourself »

Use the padding property to change the padding of a button:



Example
.button1 {padding: 10px 24px;}
.button2 {padding: 12px 28px;}
.button3 {padding: 14px 40px;}
.button4 {padding: 32px 16px;}
.button5 {padding: 16px;}
Try it Yourself »

Rounded Buttons



Use the border-radius property to add rounded corners to a button:

Example
.button1 {border-radius: 2px;}
.button2 {border-radius: 4px;}
.button3 {border-radius: 8px;}
.button4 {border-radius: 12px;}
.button5 {border-radius: 50%;}
Try it Yourself »

Colored Button Borders



Use the border property to add a colored border to a button:

Example
.button1 {
    background-color: white;
    color: black;
    border: 2px solid #4CAF50; /* Green */
}
...
Try it Yourself »

Hoverable Buttons
Use the :hover selector to change the style of a button when you move the mouse over it.



Tip: Use the transition-duration property to determine the speed of the "hover" effect:

Example
.button {
    -webkit-transition-duration: 0.4s; /* Safari */
    transition-duration: 0.4s;
}
.button:hover {
    background-color: #4CAF50; /* Green */
    color: white;
}
...
Try it Yourself »

Shadow Buttons



Use the box-shadow property to add shadows to a button:

Example
.button1 {
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
.button2:hover {
    box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}
Try it Yourself »

Disabled Buttons
Use the opacity property to add transparency to a button (creates a "disabled" look).

Tip: You can also add the cursor property with a value of "not-allowed", which will display a "no parking sign" when you mouse over the button:

Example
.disabled {
    opacity: 0.6;
    cursor: not-allowed;
}
Try it Yourself »

Button Width

By default, the size of the button is determined by its text content (as wide as its content). Use the width property to change the width of a button:

Example
.button1 {width: 250px;}
.button2 {width: 50%;}
.button3 {width: 100%;}
Try it Yourself »

Button Groups



Remove margins and add float:left to each button to create a button group:

Example
.button {
    float: left;
}
Try it Yourself »

Animated Buttons

Example
Add an arrow on hover:

   

Try it Yourself »

Example
Add a "pressed" effect on click:

  

Try it Yourself »

Example
Add a "ripple" effect on click: Flash Effect



Try it Yourself »

Read More »

CSS Images

Learn how to style images using CSS.

Rounded Images
Use the border-radius property to create rounded images:


Example
Rounded Image:
img {
    border-radius: 8px;
}
Try it Yourself »
Example
Circled Image:
img {
    border-radius: 50%;
}
Try it Yourself »

Thumbnail Images
Use the border property to create thumbnail images.

Example
img {
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 5px;
    width: 150px;
}
<img src="paris.jpg" alt="Paris">
Try it Yourself »

Example
img {
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 5px;
    width: 150px;
}
img:hover {
    box-shadow: 0 0 2px 1px rgba(0, 140, 186, 0.5);
}
<a href="paris.jpg">
  <img src="paris.jpg" alt="Paris">
</a>
Try it Yourself »
Responsive Images
Responsive images will automatically adjust to fit the size of the screen.

Resize the browser window to see the effect:


If you want an image to scale down if it has to, but never scale up to be larger than its original size, add the following:

Example
img {
    max-width: 100%;
    height: auto;
}
Try it Yourself »

Tip: Read more about Responsive Web Design in our CSS RWD Tutorial.

Center an Image
To center an image within the page, use margin: auto; and make it into a block element:

Example
img {
    display: block;
    margin: auto;
    width: 50%;
}
Try it Yourself »

Polaroid Images / Cards


Example
div.polaroid {
    width: 80%;
    background-color: white;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
img {width: 100%}
div.container {
    text-align: center;
    padding: 10px 20px;
}
Try it Yourself »

Transparent Image
The opacity property can take a value from 0.0 - 1.0. The lower value, the more transparent:


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 */
}
Try it Yourself »

Image Filters
The CSS filter property adds visual effects (like blur and saturation) to an element.

Note: The filter property is not supported in Internet Explorer, Edge 12, or Safari 5.1 and earlier.


Example
Change the color of all images to black and white (100% gray):
img {
    -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */
    filter: grayscale(100%);
}
Try it Yourself »

Tip: Go to our CSS filter Reference to learn more about CSS filters.

Image Hover Overlay
Create an overlay effect on hover:

Example
Fade in text:
Try it Yourself »

Example
Fade in a box:
Try it Yourself »

Example
Slide in (top):
Try it Yourself »

Example
Slide in (bottom):
Try it Yourself »

Example
Slide in (left):
Try it Yourself »

Example
Slide in (right):
Try it Yourself »

Responsive Image Gallery
CSS can be used to create image galleries. This example use media queries to re-arrange the images on different screen sizes. Resize the browser window to see the effect:

Example
.responsive {
    padding: 0 6px;
    float: left;
    width: 24.99999%;
}
@media only screen and (max-width: 700px){
    .responsive {
        width: 49.99999%;
        margin: 6px 0;
    }
}
@media only screen and (max-width: 500px){
    .responsive {
        width: 100%;
    }
}
Try it Yourself »

Tip: Read more about Responsive Web Design in our CSS RWD Tutorial.

Image Modal (Advanced)
This is an example to demonstrate how CSS and JavaScript can work together.

First, use CSS to create a modal window (dialog box), and hide it by default.

Then, use a JavaScript to show the modal window and to display the image inside the modal, when a user clicks on the image:

Example
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}
Try it Yourself »
Read More »

Friday, 11 August 2017

CSS3 Animations

CSS3 animations allows animation of most HTML elements without using JavaScript or Flash!

Browser Support for Animations

The numbers in the table specify the first browser version that fully supports the property. Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.



What are CSS3 Animations?
An animation lets an element gradually change from one style to another.

You can change as many CSS properties you want, as many times you want.

To use CSS3 animation, you must first specify some keyframes for the animation.

Keyframes hold what styles the element will have at certain times.

The @keyframes Rule
When you specify CSS styles inside the @keyframes rule, the animation will gradually change from the current style to the new style at certain times.

To get an animation to work, you must bind the animation to an element.

The following example binds the "example" animation to the <div> element. The animation will last for 4 seconds, and it will gradually change the background-color of the <div> element from "red" to "yellow":

Example
/* The animation code */
@keyframes example {
    from {background-color: red;}
    to {background-color: yellow;}
}
/* The element to apply the animation to */
div {
    width: 100px;
    height: 100px;
    background-color: red;
    animation-name: example;
    animation-duration: 4s;
}
Try it Yourself »

Note: If the animation-duration property is not specified, the animation will have no effect, because the default value is 0.

In the example above we have specified when the style will change by using the keywords "from" and "to" (which represents 0% (start) and 100% (complete)).

It is also possible to use percent. By using percent, you can add as many style changes as you like.

The following example will change the background-color of the <div> element when the animation is 25% complete, 50% complete, and again when the animation is 100% complete:

Example
/* The animation code */
@keyframes example {
    0%   {background-color: red;}
    25%  {background-color: yellow;}
    50%  {background-color: blue;}
    100% {background-color: green;}
}
/* The element to apply the animation to */
div {
    width: 100px;
    height: 100px;
    background-color: red;
    animation-name: example;
    animation-duration: 4s;
}
Try it Yourself »

The following example will change both the background-color and the position of the <div> element when the animation is 25% complete, 50% complete, and again when the animation is 100% complete:

Example
/* The animation code */
@keyframes example {
    0%   {background-color:red; left:0px; top:0px;}
    25%  {background-color:yellow; left:200px; top:0px;}
    50%  {background-color:blue; left:200px; top:200px;}
    75%  {background-color:green; left:0px; top:200px;}
    100% {background-color:red; left:0px; top:0px;}
}
/* The element to apply the animation to */
div {
    width: 100px;
    height: 100px;
    position: relative;
    background-color: red;
    animation-name: example;
    animation-duration: 4s;
}
Try it Yourself »

Delay an Animation
The animation-delay property specifies a delay for the start of an animation.

The following example has a 2 seconds delay before starting the animation:

Example
div {
    width: 100px;
    height: 100px;
    position: relative;
    background-color: red;
    animation-name: example;
    animation-duration: 4s;
    animation-delay: 2s;
}
Try it Yourself »

Set How Many Times an Animation Should Run
The animation-iteration-count property specifies the number of times an animation should run.

The following example will run the animation 3 times before it stops:

Example
div {
    width: 100px;
    height: 100px;
    position: relative;
    background-color: red;
    animation-name: example;
    animation-duration: 4s;
    animation-iteration-count: 3;
}
Try it Yourself »

The following example uses the value "infinite" to make the animation continue for ever:

Example
div {
    width: 100px;
    height: 100px;
    position: relative;
    background-color: red;
    animation-name: example;
    animation-duration: 4s;
    animation-iteration-count: infinite;
}
Try it Yourself »

Run Animation in Reverse Direction or Alternate Cycles
The animation-direction property is used to let an animation run in reverse direction or alternate cycles.

The following example will run the animation in reverse direction:

Example
div {
    width: 100px;
    height: 100px;
    position: relative;
    background-color: red;
    animation-name: example;
    animation-duration: 4s;
    animation-iteration-count: 3;
    animation-direction: reverse;
}
Try it Yourself »

The following example uses the value "alternate" to make the animation first run forward, then backward, then forward:

Example
div {
    width: 100px;
    height: 100px;
    position: relative;
    background-color: red;
    animation-name: example;
    animation-duration: 4s;
    animation-iteration-count: 3;
    animation-direction: alternate;
}
Try it Yourself »

Specify the Speed Curve of the Animation
The animation-timing-function property specifies the speed curve of the animation.

The animation-timing-function property can have the following values:

  • ease - specifies an animation with a slow start, then fast, then end slowly (this is default)
  • linear - specifies an animation with the same speed from start to end
  • ease-in - specifies an animation with a slow start
  • ease-out - specifies an animation with a slow end
  • ease-in-out - specifies an animation with a slow start and end
  • cubic-bezier(n,n,n,n) - lets you define your own values in a cubic-bezier function

The following example shows the some of the different speed curves that can be used:

Example
#div1 {animation-timing-function: linear;}
#div2 {animation-timing-function: ease;}
#div3 {animation-timing-function: ease-in;}
#div4 {animation-timing-function: ease-out;}
#div5 {animation-timing-function: ease-in-out;}
Try it Yourself »

Animation Shorthand Property
The example below uses six of the animation properties:

Example
div {
    animation-name: example;
    animation-duration: 5s;
    animation-timing-function: linear;
    animation-delay: 2s;
    animation-iteration-count: infinite;
    animation-direction: alternate;
}
Try it Yourself »
The same animation effect as above can be achieved by using the shorthand animation property:

Example
div {
    animation: example 5s linear 2s infinite alternate;
}
Try it Yourself »

CSS3 Animation Properties

Property
Description
@keyframes Specifies the animation code
animation A shorthand property for setting all the animation properties
animation-delay Specifies a delay for the start of an animation
animation-direction Specifies whether an animation should play in reverse direction or alternate cycles
animation-duration Specifies how many seconds or milliseconds an animation takes to complete one cycle
animation-fill-mode Specifies a style for the element when the animation is not playing (when it is finished, or when it has a delay)
animation-iteration-count Specifies the number of times an animation should be played
animation-name Specifies the name of the @keyframes animation
animation-play-state Specifies whether the animation is running or paused
animation-timing-function Specifies the speed curve of the animation

Read More »

Tuesday, 8 August 2017

CSS3 3D Transforms

CSS3 allows you to format your elements using 3D transformations. Mouse over the elements below to see the difference between a 2D and a 3D transformation:

Original                                                                             after roration
                                           

Browser Support for 3D Transforms
The numbers in the table specify the first browser version that fully supports the property. Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.



CSS3 3D Transforms
In this chapter you will learn about the following 3D transformation methods:

  • rotateX()
  • rotateY()
  • rotateZ()

The rotateX() Method



The rotateX() method rotates an element around its X-axis at a given degree:

Example
div {
    -webkit-transform: rotateX(150deg); /* Safari */
    transform: rotateX(150deg);
}
Try it Yourself »

The rotateY() Method



The rotateY() method rotates an element around its Y-axis at a given degree:

Example
div {
    -webkit-transform: rotateY(130deg); /* Safari */
    transform: rotateY(130deg);
}
Try it Yourself »

The rotateZ() Method
The rotateZ() method rotates an element around its Z-axis at a given degree:



Example
div {
    -webkit-transform: rotateZ(90deg); /* Safari */
    transform: rotateZ(90deg);
}
Try it Yourself »
CSS3 Transform Properties

Property
Description
transform Applies a 2D or 3D transformation to an element
transform-origin Allows you to change the position on transformed elements
transform-style Specifies how nested elements are rendered in 3D space
perspective Specifies the perspective on how 3D elements are viewed
perspective-origin Specifies the bottom position of 3D elements
backface-visibility Defines whether or not an element should be visible when not facing the screen

3D Transform Methods

unction Description
matrix3d

(n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n)
Defines a 3D transformation, using a 4x4 matrix of 16 values
translate3d(x,y,z) Defines a 3D translation
translateX(x) Defines a 3D translation, using only the value for the X-axis
translateY(y) Defines a 3D translation, using only the value for the Y-axis
translateZ(z) Defines a 3D translation, using only the value for the Z-axis
scale3d(x,y,z) Defines a 3D scale transformation
scaleX(x) Defines a 3D scale transformation by giving a value for the X-axis
scaleY(y) Defines a 3D scale transformation by giving a value for the Y-axis
scaleZ(z) Defines a 3D scale transformation by giving a value for the Z-axis
rotate3d(x,y,z,angle) Defines a 3D rotation
rotateX(angle) Defines a 3D rotation along the X-axis
rotateY(angle) Defines a 3D rotation along the Y-axis
rotateZ(angle) Defines a 3D rotation along the Z-axis
perspective(n) Defines a perspective view for a 3D transformed element

Read More »

CSS3 Web Fonts



CSS3 Web Fonts - The @font-face Rule
Web fonts allow Web designers to use fonts that are not installed on the user's computer.

When you have found/bought the font you wish to use, just include the font file on your web server, and it will be automatically downloaded to the user when needed.

Your "own" fonts are defined within the CSS3 @font-face rule.

Browser Support
The numbers in the table specify the first browser version that fully supports the property.



Different Font Formats
TrueType Fonts (TTF)

TrueType is a font standard developed in the late 1980s, by Apple and Microsoft. TrueType is the most common font format for both the Mac OS and Microsoft Windows operating systems.

OpenType Fonts (OTF)

OpenType is a format for scalable computer fonts. It was built on TrueType, and is a registered trademark of Microsoft. OpenType fonts are used commonly today on the major computer platforms.

The Web Open Font Format (WOFF)

WOFF is a font format for use in web pages. It was developed in 2009, and is now a W3C Recommendation. WOFF is essentially OpenType or TrueType with compression and additional metadata. The goal is to support font distribution from a server to a client over a network with bandwidth constraints.

The Web Open Font Format (WOFF 2.0)

TrueType/OpenType font that provides better compression than WOFF 1.0.

SVG Fonts/Shapes

SVG fonts allow SVG to be used as glyphs when displaying text. The SVG 1.1 specification define a font module that allows the creation of fonts within an SVG document. You can also apply CSS to SVG documents, and the @font-face rule can be applied to text in SVG documents.

Embedded OpenType Fonts (EOT)

EOT fonts are a compact form of OpenType fonts designed by Microsoft for use as embedded fonts on web pages.

Browser Support for Font Formats
The numbers in the table specifies the first browser version that fully supports the font format.



*IE: The font format only works when set to be "installable".

*Firefox: Not supported by default, but can be enabled (need to set a flag to "true" to use WOFF2).

Using The Font You Want
In the CSS3 @font-face rule you must first define a name for the font (e.g. myFirstFont), and then point to the font file.



To use the font for an HTML element, refer to the name of the font (myFirstFont) through the font-family property:

Example
@font-face {
    font-family: myFirstFont;
    src: url(sansation_light.woff);
}
div {
    font-family: myFirstFont;
}
Try it Yourself »

Using Bold Text
You must add another @font-face rule containing descriptors for bold text:

Example
@font-face {
    font-family: myFirstFont;
    src: url(sansation_bold.woff);
    font-weight: bold;
}
Try it Yourself »
The file "sansation_bold.woff" is another font file, that contains the bold characters for the Sansation font.

Browsers will use this whenever a piece of text with the font-family "myFirstFont" should render as bold.

This way you can have many @font-face rules for the same font.

CSS3 Font Descriptors

Descriptor Values Description
font-family name Required. Defines a name for the font
src URL Required. Defines the URL of the font file
font-stretch normal

condensed

ultra-condensed

extra-condensed

semi-condensed

expanded

semi-expanded

extra-expanded

ultra-expanded
Optional. Defines how the font should be stretched. Default is "normal"
font-style normal

italic

oblique
Optional. Defines how the font should be styled. Default is "normal"
font-weight normal

bold

100

200

300

400

500

600

700

800

900
Optional. Defines the boldness of the font. Default is "normal"
unicode-range unicode-range Optional. Defines the range of UNICODE characters the font supports. Default is "U+0-10FFFF"

Read More »