CSS Tutorial - CSS Outline








Outlines are an alternative to borders.

An outline is a line drawn around elements outside the borders.

+-------------------------------------+
|   Outline                           |  
|   +-----------------------------+   |
|   |Border                       |   |
|   |   +--------------------+    |   |
|   |   |Padding             |    |   |
|   |   |  +--------------+  |    |   |
|   |   |  |Content       |  |    |   |
|   |   |  |              |  |    |   |
|   |   |  +--------------+  |    |   |
|   |   |                    |    |   |
|   |   +--------------------+    |   |
|   |                             |   |
|   +-----------------------------+   |
|                                     |
+-------------------------------------+

We can style outline with style, color, and width.

The outline property is different from the border property: outline is not a part of an element's dimensions, the element's total width and height is not affected by the width of the outline.

The outlines are not considered to be part of the page, and so do not cause the page layout to be adjusted when you apply them.

The following list describes the elements that relate to outlines.

  • outline-color
    Sets the color out the outline.
    Value: color
  • outline-offset
    Sets the offset of the outline.
    Value:length
  • outline-style
    Sets the style of the outline.
    This value is the same as for the border-style property.
  • outline-width
    Sets the width of the outline.
    Value:thin or medium or thick or length
  • outline
    Shorthand property sets the outline in a single declaration.
    Value: color style width




Outline Style

The outline-style property specifies the style of an outline.

The following table lists the possible values for outline-style.

ValueDescription
noneno outline. This is default
hiddena hidden outline
dotteda dotted outline
dasheda dashed outline
solida solid outline
doublea double outliner
groovea 3D grooved outline. The effect depends on the outline-color value
ridgea 3D ridged outline. The effect depends on the outline-color value
inseta 3D inset outline. The effect depends on the outline-color value
outseta 3D outset outline. The effect depends on the outline-color value
initialSets this property to its default value.
inheritInherits this property from its parent element.

IE8 supports the outline properties only if a !DOCTYPE is specified.

The following code shows how to use the outline-style.

p.dotted {outline-style: dotted;}
p.dashed {outline-style: dashed;}
p.solid {outline-style: solid;}
p.double {outline-style: double;}
p.groove {outline-style: groove;}
p.ridge {outline-style: ridge;}
p.inset {outline-style: inset;}
p.outset {outline-style: outset;}




Outline Color

The outline-color property sets the color of an outline.

p {
    outline-style: dotted;
    outline-color: #00ff00;
}

Outline Width

The outline-width sets the width of an outline.

ValueDescription
mediuma medium outline. This is default
thina thin outline
thicka thick outline
lengthAllows you to define the thickness of the outline
initialSets this property to its default value.
inheritInherits this property from its parent element.

The following code sets the outline width.

p {
    outline-style: dotted;
    outline-width: 5px;
}

Outline shortcut

The outline shorthand property sets all the outline properties in one declaration.

The shorthand property has the following syntax:

outline: outline-color, outline-style, outline-width
<!DOCTYPE html>
<html>
<head>
<style>
p {<!--from  www. j a  v a  2  s  .c o  m-->
    border: 1px solid red;
    outline: green dotted thick;
}
</style>
</head>
<body>
<p>This is a test</p>
</body>
</html>

Click to view the demo

Property Description CSS
outline-color Set the outline color3
outline-offset Set the outline offset3
outline-style Set the outline style3
outline-width Set outline width3
outline Shorthand property for outline3