Author: Saim Khalid

  • CSS Pseudo-classes

    What is Pseudo-class

    The CSS pseudo-classes allow you to style the dynamic states of an element such as hover, active and focus state, as well as elements that are existing in the document tree but can’t be targeted via the use of other selectors without adding any IDs or classes to them, for example, targeting the first or last child elements.

    A pseudo-class starts with a colon (:). Its syntax can be given with:

    selector:pseudo-class { property: value; }

    The following section describes the most commonly used pseudo-classes.

    Anchor Pseudo-classes

    Using anchor pseudo-classes links can be displayed in different ways:

    These pseudo-classes let you style unvisited links differently from visited ones. The most common styling technique is to remove underlines from visited links.

    Example

    a:link {
    
    color: blue;
    } a:visited {
    text-decoration: none;
    }

    Some anchor pseudo-classes are dynamic — they’re applied as a result of user interaction with the document like on hover, or on focus etc.

    Example

    a:hover {
    
    color: red;
    } a:active {
    color: gray;
    } a:focus {
    color: yellow;
    }

    These pseudo-classes change how the links are rendered in response to user actions.

    • :hover applies when a user places cursor over the element, but does not select it.
    • :active applies when the element is activated or clicked.
    • :focus applies when the element has keyboard focus.

    Note: To make these pseudo-classes work perfectly, you must define them in the exact order — :link:visited:hover:active:focus.


    The :first-child Pseudo-class

    The :first-child pseudo-class matches an element that is the first child element of some other element. The selector ol li:first-child in the example below select the first list item of an ordered list and removes the top border form it.

    Example

    ol li:first-child {
    
    border-top: none;
    }

    Note: To make :first-child to work in Internet Explorer 8 and earlier versions, a <!DOCTYPE> must be declared at the top of document.


    The :last-child Pseudo-class

    The :last-child pseudo-class matches an element that is the last child element of some other element. The selector ul li:last-child in the example below select the last list item from an unordered list and removes the right border from it.

    Example

    ul li:last-child {
    
    border-right: none;
    }

    Note: The CSS :last-child selector does not work in Internet Explorer 8 and earlier versions. Supports in Internet Explorer 9 and above.


    The :nth-child Pseudo-class

    The CSS3 introduces a new :nth-child pseudo-class that allows you to target one or more specific children of a given parent element. The basic syntax of this selector can be given with :nth-child(N), where N is an argument, which can be a number, a keyword (even or odd), or an expression of the form xn+y where x and y are integers (e.g. 1n2n2n+1, …).

    Example

    table tr:nth-child(2n) td {
    
    background: #eee;
    }

    The style rules in the example above simply highlight the alternate table row, without adding any IDs or classes to the <table> elements.

    Tip: The CSS :nth-child(N) selector is very useful in the situations where you have to select the elements that appears inside the document tree in a specific interval or pattern like at even or odd places, etc.


    The :lang Pseudo-class

    The language pseudo-class :lang allows constructing selectors based on the language setting for specific tags. The :lang pseudo-class in the example below defines the quotation marks for <q> elements that are explicitly given a language value of no.

    Example

    q:lang(no) {
    
    quotes: "~" "~";
    } /* HTML code snippet */ <p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p>

    Note: Internet Explorer up to version 7 does not support the :lang pseudo-class. IE8 supports only if a <!DOCTYPE> is specified.


    Pseudo-classes and CSS Classes

    Pseudo-classes can be combined with CSS classes.

    The link with class="red", in the example below will be displayed in red.

    Example

    a.red:link {    /* style rule */
    
    color: #ff0000;
    } <a class="red" href="#">Click me</a> /* HTML code snippet */
  • CSS Alignment

    Text Alignment

    Text inside the block-level elements can aligned by setting the text-align properly.

    Example

    h1 {
    
    text-align: center;
    } p {
    text-align: left;
    }

    See tutorial on CSS Text to learn more about text formatting.


    Center Alignment Using the margin Property

    Center alignment of a block-level element is one of the most important implications of the CSS margin property. For example, the <div> container can be aligned horizontally center by setting the left and right margins to auto.

    Example

    div {
    
    width: 50%;
    margin: 0 auto;
    }

    The style rules in the above example center align the <div> element horizontally.

    Note: The value auto for the margin property will not work in Internet Explorer 8 and earlier versions, unless a <!DOCTYPE> is specified.


    Aligning Elements Using the position Property

    The CSS position in conjunction with the leftrighttop and bottom property can be used to align elements with respect to the document’s viewport or containing parent element.

    Example

    .up {
    
    position: absolute;
    top: 0;
    } .down {
    position: absolute;
    bottom: 0;
    }

    To learn more about positioning elements, see the tutorial on CSS positioning.


    Left and Right Alignment Using the float Property

    The float CSS property can be used to align an element to the left or right of its containing block in such a way that other content may flow along its side.

    Hence, if an element is floated to the left, content will flow along its right side. Conversely, if the element is floated to the right, content will flow along its left side.

    Example

    div {
    
    width: 200px;
    float: left;
    }

    Clearing Floats

    One of the most confusing things about working with the float-based layouts is the collapsing parent. The parent element doesn’t stretch up automatically to accommodate the floated elements. Though, this isn’t always obvious if the parent doesn’t contain any visually noticeable background or borders, but it is important to be aware of and must dealt with to prevent strange layout and cross-browser problem. See the illustration below:

    CSS Collapsed Parent

    You can see the <div> element doesn’t stretch up automatically to accommodate the floated images. This problem can be fixed by clearing the float after the floated elements in the container but before the closing tag of the container element.


    Fixing the Collapsed Parent

    There are several ways to fix the CSS collapsing parent issue. The following section will describe you these solutions along with the live examples.

    Solution 1: Float the Container

    The easiest way to fix this problem is to float the containing parent element.

    Example

    .container {
    
    float: left;
    background: #f2f2f2;
    }

    Warning: This fix will only work in a limited number of circumstances, since floating the parent may produce unexpected results.

    Solution 2: Using the Empty Div

    This is an old fashioned way but is an easy solution and works across every browser.

    Example

    .clearfix {
    
    clear: both;
    } /* html code snippet */ <div class="clearfix"> </div>

    You could also do this by means of a <br> tag. But this method is not recommended since it adds nonsemantic code to your markup.

    Solution 3: Using the :after Pseudo-Element

    The :after pseudo-element in conjunction with the content property has been used quite extensively to resolve float-clearing issues.

    Example

    .clearfix:after {
    
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
    }

    The class .clearfix is applied to any container that has floating children.

  • CSS Float

    Floating Elements with CSS

    You can float elements to the left or right, but only applies to the elements that generate boxes that are not absolutely positioned. Any element that follows the floated element will flow around the floated element on the other side.

    The float property may have one of the three values:

    ValueDescription
    leftThe element floats on the left side of its containing block.
    rightThe element floats on the right side of its containing block.
    noneRemoves the float property from an element.

    How Elements Float

    A floated element is taken out of the normal flow and shifted to the left or right as far as possible in the space available of the containing element.

    Other elements normally flow around the floated items, unless they are prevented from doing so by their clear property. Elements are floated horizontally, which means that an element can only be floated left or right, not up or down.

    Example

    img {
    
    float: left;
    }

    If several floating elements are placed adjacently, they will float next to each other if there is horizontal room. If there is not enough room for the float, it is shifted downward until either it fits or there are no more floating elements present.

    Example

    .thumbnail {
    
    float: left;
    width: 125px;
    height: 125px;
    margin: 10px;
    }

    Turning off Float Using Clear Property

    Elements that comes after the floating element will flow around it. The clear property specifies which sides of an element’s box other floating elements are not allowed.

    Example

    .clear {
    
    clear: left;
    }

    Note: This property can clear an element only from floated boxes within the same block. It doesn’t clear the element from floated child boxes within the element itself. To learn more about clearing float see tutorial on CSS Alignment.

  • CSS Layers

    Stacking Elements in Layers Using z-index Property

    Usually HTML pages are considered two-dimensional, because text, images and other elements are arranged on the page without overlapping. However, in addition to their horizontal and vertical positions, boxes can be stacked along the z-axis as well i.e. one on top of the other by using the CSS z-index property. This property specifies the stack level of a box whose position value is one of absolutefixed, or relative.

    The z-axis position of each layer is expressed as an integer representing the stacking order for rendering. An element with a larger z-index overlaps an element with a lower one.

    z-index property can help you to create more complex webpage layouts. Following is the example which shows how to create layers in CSS.

    Example

    .box {
    
    position: absolute;
    left: 10px;
    top: 20px;
    z-index: 2;
    }

  • CSS Visibility

    Controlling the Visibility of Elements

    You can use the visibility property to control whether an element is visible or not. This property can take one of the following values listed in the table below:

    ValueDescription
    visibleDefault value. The box and its contents are visible.
    hiddenThe box and its content are invisible, but still affect the layout of the page.
    collapseThis value causes the entire row or column to be removed from the display. This value is used for row, row group, column, and column group elements.
    inheritSpecifies that the value of the visibility property should be inherited from the parent element i.e. takes the same visibility value as specified for its parent.

    The style rule visibility: collapse; however removes the internal table elements, but it does not affect the layout of the table in any other way. The space normally occupied by the table elements will be filled by the subsequent siblings.

    Note: If the style rule visibility: collapse; is specified for other elements rather than the table elements, it causes the same behavior as hidden.


    CSS Visibility vs Display

    The display and visibility CSS properties appear to be the same thing, but they are in fact quite different and often confuse those new to web development.

    • visibility: hidden; hides the element, but it still takes up space in the layout. Child element of a hidden box will be visible if their visibility is set to visible.
    • display: none; turns off the display and removes the element completely from the document. It does not take up any space, even though the HTML for it is still in the source code. All child elements also have their display turned off, even if their display property is set to something other than none.

    Check out the following demo to find out how display and visibility affect the layouts.


    CSS Visibility vs Display DemoReset All

    Click on me i will removed
    Click on me i will hide
    Click on me i will removed
    Click on me i will hide
  • CSS Display

    CSS Display Property

    The CSS specification defines the default display value for all the elements, e.g. the <div> element is rendered as block, while the <span> element is displayed inline.

    Changing the Default Display Value

    Overriding the default display value of an element is an important implication of the display property. For example, changing an inline-level element to be displayed as block-level element or changing the block-level element to be displayed as an inline-level element.

    Note: The CSS display property is one of the most powerful and useful properties in all the CSS. It can be very useful for creating web pages that looks in a different way, but still follow the web standards.

    The following section describes you the most commonly used CSS display values.

    Display Block

    The block value of the display property forces an element to behave like block-level element, like a <div> or <p> element. The style rules in the following example displays the <span> and <a> elements as block-level elements:

    Example

    span {
    
    display: block;
    } a {
    display: block;
    }

    Note: Changing the display type of an element only changes the display behavior of an element, NOT the type of element it is. For example, an inline element set to display: block; is not allowed to have a block element nested inside of it.


    Display Inline

    The inline value of the display property causes an element to behave as though it were an inline-level element, like a <span> or an <a> element. The style rules in the following example displays the <p> and <li> elements as inline-level elements:

    Example

    p {
    
    display: inline;
    } ul li {
    display: inline;
    }

    Display Inline-Block

    The inline-block value of the display property causes an element to generate a block box that will be flowed with surrounding content i.e. in the same line as adjacent content. The following style rules displays the <div> and <span> elements as inline-block:

    Example

    div {
    
    display: inline-block;
    } span {
    display: inline-block;
    }

    Display None

    The value none simply causes an element to generate no boxes at all. Child elements do not generate any boxes either, even if their display property is set to something other than none. The document is rendered as though the element did not exist in the document tree.

    Example

    h1 {
    
    display: none;
    } p {
    display: none;
    }
  • CSS Visual Formatting

    CSS Visual Formatting Model

    The CSS visual formatting model is the algorithm that is used to process the documents for visual media. In the visual formatting model, each element in the document tree generates zero or more boxes according to the box model.

    The layout of these boxes is depends on the following factors:

    • box dimensions.
    • type of the element (block or inline).
    • positioning scheme (normal flow, float, and absolute positioning).
    • relationships between elements in the document tree.
    • external information e.g. viewport size, built-in dimensions of images, etc.

    Note: The document tree is the hierarchy of elements encoded in the source document. Every element in the document tree has exactly one parent, with the exception of the root element, which has none.


    Type of Boxes Generated in CSS

    Every element displayed on a web page generates a rectangular box. The following section describes the types of boxes that may be generated by an element.

    Block-level Elements and Block Boxes

    Block-level elements are those elements that are formatted visually as blocks (i.e. takes up the full width available), with a line break before and after the element. For example, paragraph element (<p>), headings (<h1> to <h6>), divisions (<div>) etc.

    Generally, block-level elements may contain inline elements and other block-level elements.


    Inline-level Elements and Inline Boxes

    Inline-level elements are those elements of the source document that do not form new blocks of content; the content is distributed in lines. For example, emphasized pieces of text within a paragraph (<em>), spans (<span>), strong element (<strong>) etc.

    Inline elements typically may only contain text and other inline elements.

    Note: Unlike block-level elements, an inline-level element only takes up as much width as necessary, and does not force line breaks.

    You can change how an element will be displayed on a web page using the CSS display property. You will learn about the display property in next chapter.

  • CSS Tables

    Styling Tables with CSS

    Tables are typically used to display tabular data, such as financial reports.

    But when you create an HTML table without any styles or attributes, browsers display them without any border. With CSS you can greatly improve the appearance your tables.

    CSS provides several properties that allow you to control the layout and presentation of the table elements. In the following section you will see how to use CSS to create elegant and consistent tables.

    Adding Borders to Tables

    The CSS border property is the best way to define the borders for the tables.

    The following example will set a black border for the <table><th>, and <td> elements.

    Example

    table, th, td {
    
    border: 1px solid black;
    }

    By default, browser draws a border around the table, as well as around all the cells, with some space in-between, which results in double border. To get rid of this double border problem you can simply collapse the adjoining table cell borders and create clean single line borders.

    Let’s take a look at the following illustration to understand how a border is applied to a table.

    Table Border Model Illustration

    Collapsing Table Borders

    There are two distinct models for setting borders on table cells in CSS: separate and collapse.

    In the separate border model, which is the default, each table cell has its own distinct borders, whereas in the collapsed border model, adjacent table cells share a common border. You can set the border model for an HTML table by using the CSS border-collapse property.

    The following style rules will collapse the table cell borders and apply one pixel black border.

    Example

    table {
    
    border-collapse: collapse;
    } th, td {
    border: 1px solid black;
    }

    Note: You can also remove the space between the table cell borders through setting the value of CSS border-spacing property to 0. However, it only removes the space but do not merge the borders like when you set the border-collapse to collapse.


    Adjusting Space inside Tables

    By default, the browser creates the table cells just large enough to contain the data in the cells.

    To add more space between the table cell contents and the cell borders, you can simply use the CSS padding property. Let’s try out the following example and see how it works:

    Example

    th, td {
    
    padding: 15px;
    }

    You can also adjust the spacing between the borders of the cells using the CSS border-spacing property, if the borders of your table are separated (which is default).

    The following style rules apply the spacing of 10 pixels between all borders within a table:

    Example

    table {
    
    border-spacing: 10px;
    }

    Setting Table Width and Height

    By default, a table will render just wide and tall enough to contain all of its contents.

    However, you can also set the width and height of the table as well as its cells explicitly using the width and height CSS property. The style rules in the following example will sets the width of the table to 100%, and the height of the table header cells to 40px.

    Example

    table {
    
    width: 100%;
    } th {
    height: 40px;
    }

    Controlling the Table Layout

    A table expands and contracts to accommodate the data contained inside it. This is the default behavior. As data fills inside the table, it continues to expand as long as there is space. Sometimes, however, it is necessary to set a fixed width for the table in order to manage the layout.

    You can do this with the help of CSS table-layout property. This property defines the algorithm to be used to layout the table cells, rows, and columns. This property takes one of two values:

    • auto — Uses an automatic table layout algorithm. With this algorithm, the widths of the table and its cells are adjusted to fit the content. This is the default value.
    • fixed — Uses the fixed table layout algorithm. With this algorithm, the horizontal layout of the table does not depend on the contents of the cells; it only depends on the table’s width, the width of the columns, and borders or cell spacing. It is normally faster than auto.

    The style rules in the following example specify that the HTML table is laid out using the fixed layout algorithm and has a fixed width of 300 pixels. Let’s try it out and see how it works:

    Example

    table {
    
    width: 300px;
    table-layout: fixed;
    }

    Tip: You can optimize the table rendering performance by specifying the value fixed for the table-layout property. Fixed value of this property causes the table to be rendered one row at a time, providing users with information at a faster pace.

    Note: Without fixed value of the table-layout property on large tables, users won’t see any part of the table until the browser has rendered the whole table.


    Aligning the Text Inside Table Cells

    You can align text content inside the table cells either horizontally or vertically.

    Horizontal Alignment of Cell Contents

    For horizontal alignment of text inside the table cells you can use the text-align property in the same way as you use with other elements. You align text to either left, right, center or justify.

    The following style rules will left-align the text inside the <th> elements.

    Example

    th {
    
    text-align: left;
    }

    Note: Text inside the <td> elements are left-aligned by default, whereas the text inside the <th> elements are center-aligned and rendered in bold font by default.

    Vertical Alignment of Cell Contents

    Similarly, you can vertically align the content inside the <th> and <td> elements to top, bottom, or middle using the CSS vertical-align property. The default vertical alignment is middle.

    The following style rules will vertically bottom-align the text inside the <th> elements.

    Example

    th {
    
    height: 40px;
    vertical-align: bottom;
    }

    Controlling the Position of Table Caption

    You can set the vertical position of a table caption using the CSS caption-side property.

    The caption can be placed either at the top or bottom of the table. The default position is top.

    Example

    caption {
    
    caption-side: bottom;
    }

    Tip: To change the horizontal alignment of the table’s caption text (e.g. left or right), you can simply use the CSS text-align property, like you do with normal text.


    Handling Empty Cells

    In tables that uses separate border model, which is default, you can also control the rendering of the cells that have no visible content using the empty-cells CSS property.

    This property accepts a value of either show or hide. The default value is show, which renders empty cells like normal cells, but if the value hide is specified no borders or backgrounds are drawn around the empty cells. Let’s try out an example to understand how it really works:

    Example

    table {
    
    border-collapse: separate;
    empty-cells: hide;
    }

    Note: Placing a non-breaking space (&nbsp;) inside a table cell make it non-empty. Therefore, even if that cell looks empty the hide value will not hide the borders and backgrounds.


    Creating Zebra-striped Tables

    Setting different background colors for alternate rows is a popular technique to improve the readability of tables that has large amount of data. This is commonly known as zebra-striping a table.

    You can simply achieve this effect by using the CSS :nth-child() pseudo-class selector.

    The following style rules will highlight every odd rows within the table body.

    Example

    tbody tr:nth-child(odd) {
    
    background-color: #f2f2f2;
    }

    A zebra-striped table typically looks something like the following picture.

    Zebra Striped Table

    Note: The :nth-child() pseudo-class select elements based on their position in a group of siblings. It can take a number, a keyword even or odd, or an expression of the form xn+y where x and y are integers (e.g. 1n, 2n, 2n+1, …) as an argument.


    Making a Table Responsive

    Tables are not responsive in nature. However, to support mobile devices you can add responsiveness to your tables by enabling horizontal scrolling on small screens. To do this simply wrap your table with a <div> element and apply the style overflow-x: auto; as shown below:

    Example

    <div style="overflow-x: auto;"> 
    
    &lt;table&gt;
        ... table content ...
    &lt;/table&gt;
    </div>
  • CSS Lists

    Types of HTML Lists

    There are three different types of list in HTML:

    • Unordered lists — A list of items, where every list items are marked with bullets.
    • Ordered lists — A list of items, where each list items are marked with numbers.
    • Definition list — A list of items, with a description of each item.

    See the tutorial on HTML lists to learn more about the lists and how to create them.

    Styling Lists with CSS

    CSS provides the several properties for styling and formatting the most commonly used unordered and ordered lists. These CSS list properties typically allow you to:

    • Control the shape or appearance of the marker.
    • Specify an image for the marker rather than a bullet point or number.
    • Set the distance between a marker and the text in the list.
    • Specify whether the marker would appear inside or outside of the box containing the list items.

    In the following section we will discuss the properties that can be used to style HTML lists.

    Changing the Marker Type of Lists

    By default, items in an ordered list are numbered with Arabic numerals (1, 2, 3, 5, and so on), whereas in an unordered list, items are marked with round bullets (•).

    But, you can change this default list marker type to any other type such as roman numerals, latin letters, circle, square, and so on using the list-style-type property.

    Let’s try out the following example to understand how this property actually works:

    Example

    ul {
    
    list-style-type: square;
    } ol {
    list-style-type: upper-roman;
    }

    Changing the Position of List Markers

    By default, markers of each list items are positioned outside of their display boxes.

    However, you can also position these markers or bullet points inside of the list item’s display boxes using the list-style-position property along with the value inside. In this case the lines will wrap under the marker instead of being indented. Here’s an example:

    Example

    ol.in li {
    
    list-style-position: inside;
    } ol.out li {
    list-style-position: outside;
    }

    Let’s take a look at the following illustration to understand how markers or bullets are positioned.

    List Style Position Illustration

    Using Images as List Markers

    You can also set an image as a list marker using the list-style-image property.

    The style rule in the following example assigns a transparent PNG image “arrow.png” as the list marker for all the items in the unordered list. Let’s try it out and see how it works:

    Example

    ul li {
    
    list-style-image: url("images/bullet.png");
    }

    Sometimes, the list-style-image property may not produce the expected output. Alternatively, you can use the following solution for better control over the positioning of image markers.

    As a work-around, you can also set image bullets via the background-image CSS property. First, set the list to have no bullets. Then, define a non-repeating background image for the list element.

    The following example displays the image markers equally in all browsers:

    Example

    ul {
    
    list-style-type: none;
    } ul li {
    background-image: url("images/bullet.png");
    background-position: 0px 5px;    /* X-pos Y-pos (from top-left) */
    background-repeat: no-repeat;
    padding-left: 20px;
    }

    Setting All List Properties At Once

    The list-style property is a shorthand property for defining all the three properties list-style-typelist-style-image, and list-style-position of a list in one place.

    The following style rule sets all the list properties in a single declaration.

    Example

    ul {
    
    list-style: square inside url("images/bullet.png");
    }

    Note: When using the list-style shorthand property, the orders of the values are: list-style-type | list-style-position | list-style-image. It does not matter if one of the values above is missing as long as the rest are in the specified order.


    Creating Navigation Menus Using Lists

    HTML lists are frequently used to create horizontal navigation bar or menu that typically appear at the top of a website. But since the list items are block elements, so to display them inline we need to use the CSS display property. Let’s try out an example to see how it really works:

    Example

    ul {
    
    padding: 0;
    list-style: none;
    background: #f2f2f2;
    } ul li {
    display: inline-block;
    } ul li a {
    display: block;
    padding: 10px 25px;
    color: #333;
    text-decoration: none;
    } ul li a:hover {
    color: #fff;
    background: #939393;
    }
  • CSS Links

    Styling Links with CSS

    Links or hyperlinks are an essential part of a website. It allows visitors to navigate through the site. Therefore styling the links properly is an important aspect of building a user-friendly website.

    See the tutorial on HTML links to learn more about links and how to create them.

    A link has four different states — linkvisitedactive and hover. These four states of a link can be styled differently through using the following anchor pseudo-class selectors.

    • a:link — define styles for normal or unvisited links.
    • a:visited — define styles for links that the user has already visited.
    • a:hover — define styles for a link when the user place the mouse pointer over it.
    • a:active — define styles for links when they are being clicked.

    You can specify any CSS property you’d like e.g. colorfontbackgroundborder, etc. to each of these selectors to customize the style of links, just like you do with the normal text.

    Example

    a:link {    /* unvisited link */
    
    color: #ff0000;
    text-decoration: none;
    border-bottom: 1px solid;
    } a:visited { /* visited link */
    color: #ff00ff;
    } a:hover { /* mouse over link */
    color: #00ff00;
    border-bottom: none;
    } a:active { /* active link */
    color: #00ffff;
    }

    The order in which you are setting the style for different states of links is important, because what defines last takes precedence over the style rules defined earlier.

    Note: In general, the order of the pseudo classes should be the following — :link:visited:hover:active:focus in order for these to work properly.


    Modifying Standard Link Styles

    In all major web browsers such as Chrome, Firefox, Safari, etc. links on the web pages have underlines and uses the browser’s default link colors, if you do not set the styles exclusively for them.

    By default, text links will appear as follow in most of the browsers:

    • An unvisited link as underlined blue text.
    • A visited link as underlined purple text.
    • An active link as underlined red text.

    However, there is no change in the appearance of link in case of the hover state. It remains blue, purple or red depending on which state (i.e. unvisited, visited or active) they are in.

    Now let’s see how to customize the links by overriding its default styling.

    Setting Custom Color of Links

    Simply use the CSS color property to define the color of your choice for different state of a link. But when choosing colors make sure that user can clearly differentiate between normal text and links.

    Let’s try out the following example to understand how it basically works:

    Example

    a:link {
    
    color: #1ebba3;
    } a:visited {
    color: #ff00f4;
    } a:hover {
    color: #a766ff;
    } a:active {
    color: #ff9800;
    }

    Removing the Default Underline from Links

    If you don’t like the default underline on links, you can simply use the CSS text-decoration property to get rid of it. Alternatively, you can apply other styling on links like background color, bottom border, bold font, etc. to make it stand out from the normal text a little better.

    The following example shows how to remove or set underline for different states of a link.

    Example

    a:link, a:visited {
    
    text-decoration: none; 
    } a:hover, a:active {
    text-decoration: underline;
    }

    Making Text Links Look Like Buttons

    You can also make your ordinary text links look like button using CSS. To do this we need to utilize few more CSS properties such as background-colorborderdisplaypadding, etc. You will learn about these properties in detail in upcoming chapters.

    Let’s check out the following example and see how it really works:

    Example

    a:link, a:visited {
    
    color: white;    
    background-color: #1ebba3;    
    display: inline-block;
    padding: 10px 20px;
    border: 2px solid #099983;
    text-decoration: none;
    text-align: center;
    font: 14px Arial, sans-serif;  
    } a:hover, a:active {
    background-color: #9c6ae1;
    border-color: #7443b6;
    }