{"id":3036,"date":"2017-02-07T17:26:15","date_gmt":"2017-02-07T23:26:15","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=2095"},"modified":"2017-02-07T17:26:15","modified_gmt":"2017-02-07T23:26:15","slug":"how-to-style-links-according-to-link-type","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/how-to-style-links-according-to-link-type\/","title":{"rendered":"How to Style Links According to Link Type"},"content":{"rendered":"<p>Using CSS, it&#8217;s pretty easy to style\u00a0a whole bunch of different elements of the same element type in different ways by assigning them different classes. However, if you want to get technical, this can also be achieved by using attribute selectors. Attribute selectors are used in CSS to select different HTML elements to style based on their inline HTML attributes. This tutorial is about using those attribute selectors to select and style anchor tags based on the type of file you&#8217;re linking to.<\/p>\n<p>Let&#8217;s say you have the following anchor tags:<\/p>\n<pre>&lt;a href=\"http:\/\/www.facebook.com\"&gt;Facebook&lt;\/a&gt;\n&lt;a href=\"mailto:email@gmail.com\"&gt;Email Me&lt;\/a&gt;<\/pre>\n<p>And you want to style each anchor tag differently &#8212; maybe you&#8217;d like the text of the Facebook link to be blue, and you&#8217;d like the &#8220;Email Me&#8221; text to be italic. Like we said earlier, you could style them differently by assigning different classes to each element, OR you could do it using attribute selectors, like this:<\/p>\n<pre>a[href^=\"http:\/\/\"]{\n\u00a0 \u00a0 color: blue;\n}\na[href^=\"mailto:\"]{\n\u00a0 \u00a0 font-style: italic;\n}<\/pre>\n<p>As you can probably gather, the syntax is relatively straightforward here. The elements you&#8217;re looking to select are followed by brackets that indicate their attribute values, written out like [href^=&#8221;value&#8221;]. Using these attribute selectors, you can quickly and easily style your anchor tags\u00a0according to the types of files that they link to.<\/p>\n<p>There is one exception to the syntax above. If you&#8217;re looking to single out an anchor tag that links to a pdf file, your CSS selector would need to look like the code snippet below. For this example, let&#8217;s say we wanted to make the font bigger:<\/p>\n<pre>a[href$=\".pdf\"]{\n\u00a0 \u00a0 \u00a0font-size: 25px;\n}<\/pre>\n<p>The syntax here changes only slightly &#8212; you replace the &#8220;^&#8221; symbol from the first examples with the dollar sign: &#8220;$&#8221;. Also note that you&#8217;re singling out the attribute by the .pdf extension, rather than what comes at the beginning of the link.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Using CSS, it&#8217;s pretty easy to style\u00a0a whole bunch of different elements of the same element type in different ways by assigning them different classes. However, if you want to get technical, this can also be achieved by using attribute [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/how-to-style-links-according-to-link-type\/\" title=\"Click to read 'How to Style Links According to Link Type'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[173,174],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3036"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=3036"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3036\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=3036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=3036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=3036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}