Show DevBest Simple CSS New Window Hack

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
Got a little bored and decided to write this up, it's simply a little CSS hack that targets 'a' elements with the target of either _blank or _new, which means links attached to the 'a' element will open in a new page, then it places a little background image of a new window icon that I grabbed from Google next to it.

Many sites use this for accessibility, but they decide to add a class of 'newwindow' (or something similar) to the 'a' element, whereas this removes the need for an additional class name so long as the target is either _blank or _new

Code is below:
HTML:
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>New Window</title>
        <style>
            body {
                font-family: "Helvetica", sans-serif;
                font-size: 1em;
                line-height: 1.2;
            }
           
            /*
               select elements whose target is '_blank' or '_new'
               meaning the attached link will open in a new page
            */
            a[target="_blank"],
            a[target="_new"] {
                background: url('newwindow.png') no-repeat center right;
                height: 12px;
                width: 14px;
                padding-right: 20px;
            }
        </style>
    </head>
   
    <body>
        <ul>
            <li><a href="http://google.co.uk" target="_blank">Google UK</a></li>
            <li><a href="index.html">Home page</a></li>
            <li><a href="http://eminem.com" target="_new">Eminem</a></li>
            <li><a href="http://facebook.com" target="_blank">Facebook</a></li>
            <li><a href="about.html">About this website</a></li>
        </ul>
    </body>
</html>

Here is the link to the icon:

Or you can view it on

Hope it helps,
Mark
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
Neat little trick man.. Usually I would have taken a slightly different route using :after in CSS and assigning it a specific class, but like you said that involves adding a new class.

Nice share! :)
 

GiiOkiiD

New Member
Jul 21, 2014
2
0
I can't put a link in here because of my new account. But W3schools says that it is not supported on: IE, Edge, Chrome, Firefox, safari, opera
 

Users who are viewing this thread

Top