{"id":2515,"date":"2017-07-25T15:47:53","date_gmt":"2017-07-25T15:47:53","guid":{"rendered":"http:\/\/cssreset.com\/?p=2515"},"modified":"2017-08-02T20:25:32","modified_gmt":"2017-08-02T20:25:32","slug":"css-opacity-transparency-tips","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/css-opacity-transparency-tips\/","title":{"rendered":"CSS Opacity and Transparency Tips"},"content":{"rendered":"<h2>Opacity and Transparency<\/h2>\n<p>By definition, opacity and transparency in CSS define how visible an element is, whether in images, tables, or even RGBA (red green blue alpha) color values. Based on their root words, opacity is the measure of an element\u2019s opaqueness or solidity, while transparency is the measure of how easily you can see through it to what exists in the layer beneath. Regardless, they work in the same way \u2013 100% opaque means an element is fully visible while 100% transparent means it\u2019s completely invisible.<\/p>\n<p>Rather than using an expensive piece of software to create these effects, you can use CSS! With a few simple keystrokes, you can instantly change how an element in your page looks, or in some cases, even how it reacts when the mouse pointer hovers over it.<\/p>\n<h2>Opaque and Transparent Images<\/h2>\n<p>When modifying an image, the <strong>opacity<\/strong> property accepts values from 0.0 to 1.0, with the latter as the default. As such, the lower the value, the more transparent the image becomes.<\/p>\n<p>In the examples below, we used 0.2, 0.5, and 1.0 to give a side-by-side comparison:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" alt=\"straws-transparent\" src=\"http:\/\/www.cssnewbie.com\/wp-content\/uploads\/2017\/07\/straws-transparent.jpg\" width=\"533\" height=\"136\" \/><\/p>\n<pre>img {\r\n\r\nopacity: 0.5;\r\n\r\nfilter: alpha(opacity=50); \/* For IE8 and earlier *\/\r\n\r\n}<\/pre>\n<p>Note: We used the <strong>filter<\/strong> property because versions of Internet Explorer 8 and below don\u2019t yet recognize the <strong>opacity<\/strong> property.<\/p>\n<h1>Transparent Boxes and Tables<\/h1>\n<p>You can use the <strong>opacity<\/strong> property to add transparency to an element, including the background and all its child elements. For example, in the boxes below (using <strong>&lt;div&gt;<\/strong>, not <strong>&lt;table&gt;<\/strong> and its sub-elements), all the text becomes transparent as well.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" alt=\"box-opacity\" src=\"http:\/\/www.cssnewbie.com\/wp-content\/uploads\/2017\/07\/box-opacity.jpg\" width=\"635\" height=\"236\" \/><br \/>\nSee how the text\u2019s opacity also fades? If this is exactly what you want to happen, there\u2019s no need to modify anything else. You can use the code below to achieve this effect:<\/p>\n<pre>div {\r\n\r\nopacity: 0.3;\r\n\r\nfilter: alpha(opacity=30); \/* For IE8 and earlier *\/\r\n\r\n}<\/pre>\n<h1>Transparency Using RGBA<\/h1>\n<p>However, if you want only the background to change, while the text or other child elements would remain opaque, there is a way around it using RGBA. If you\u2019re used to hex codes, you can learn about the other ways to <a href=\"http:\/\/www.cssnewbie.com\/color-values-how-to-define-a-color-in-css\">define colors in CSS<\/a>, namely predefined, RGB\/RGBA, and HSL\/HSLA colors.<\/p>\n<p>RGBA stands for red green blue alpha, with the alpha parameter specifying the opacity for the RGB color. So, using RGBA color values, we can set the opacity for the background, while the text remains black:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" alt=\"box-rgba\" src=\"http:\/\/www.cssnewbie.com\/wp-content\/uploads\/2017\/07\/box-rgba.jpg\" width=\"635\" height=\"236\" \/><br \/>\nIn the above example, we used the RGB value <strong>171, 205, 239<\/strong>, and then the <strong>alpha<\/strong> parameter defines how opaque or transparent it is. For example:<\/p>\n<pre>div {\r\n\r\nbackground: rgba(171, 205, 239, 0.3) \/* Blue background with 30% opacity *\/\r\n\r\n}<\/pre>\n<h2>Opaque Text in a Transparent Box<\/h2>\n<p>Another really cool thing you can do with opacity and transparency is add text in a transparent box, probably to offset a really harsh or dark background image like in our example below.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" alt=\"box-transparent\" src=\"http:\/\/www.cssnewbie.com\/wp-content\/uploads\/2017\/07\/box-transparent.jpg\" width=\"665\" height=\"141\" \/><br \/>\nTo create this simple yet effective style, use the <strong>&lt;div&gt;<\/strong> element and name their classes as <strong>&#8220;background&#8221;<\/strong> and <strong>&#8220;transbox&#8221;<\/strong>. The first class is the background image and <a href=\"https:\/\/cssdeck.com\/blog\/\/css3s-border-width-property\/\">border<\/a>, while the second class is a separate background color and border. Lastly, use a paragraph to add text.<\/p>\n<p>First, we create a &lt;div&gt; element (class=&#8221;background&#8221;) with a background image, and a border. Then we create another &lt;div&gt; (class=&#8221;transbox&#8221;) inside the first &lt;div&gt;. The &lt;div&gt; element has a background color, and a border &#8211; the div is transparent. Inside the transparent &lt;div&gt;, we add some text inside a &lt;p&gt; element.<\/p>\n<p>Here is the code we used, which you can modify and experiment with using your own image and text:<\/p>\n<pre>&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;style&gt;\r\n\r\ndiv.background {\r\n\r\nbackground: url(green-tile.jpg) repeat;\r\n\r\nborder: 2px solid black;\r\n\r\n}\r\n\r\ndiv.transbox {\r\n\r\nmargin: 30px;\r\n\r\nbackground-color: #ffffff;\r\n\r\nborder: 1px solid black;\r\n\r\nopacity: 0.6;\r\n\r\nfilter: alpha(opacity=60); \/* For IE8 and earlier *\/\r\n\r\n}\r\n\r\ndiv.transbox p {\r\n\r\nmargin: 5%;\r\n\r\nfont-weight: bold;\r\n\r\nfont-family: Verdana;\r\n\r\nfont-size: 12px;\r\n\r\ncolor: #000000;\r\n\r\n}\r\n\r\n&lt;\/style&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;div&gt;\r\n\r\n&lt;div&gt;\r\n\r\n&lt;p&gt;This is sample text placed in a transparent box.&lt;\/p&gt;\r\n\r\n&lt;\/div&gt;\r\n\r\n&lt;\/div&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;<\/pre>\n<h2>Conclusion<\/h2>\n<p>There are many possible uses for opacity and transparency, whether it\u2019s purely for aesthetic or for emphasizing\/deemphasizing elements on your web page. It\u2019s certainly a cheaper alternative to relying on Photoshop or another photo editing software to do all of this for you!<\/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>Opacity and Transparency<\/p>\n<p>By definition, opacity and transparency in CSS define how visible an element is, whether in images, tables, or even RGBA (red green blue alpha) color values. Based on their root words, opacity is the measure of an element\u2019s [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/css-opacity-transparency-tips\/\" title=\"Click to read 'CSS Opacity and Transparency Tips'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":2518,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[41,6,7],"tags":[161,163,160,162,70,159],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2515"}],"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=2515"}],"version-history":[{"count":2,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2515\/revisions"}],"predecessor-version":[{"id":2519,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2515\/revisions\/2519"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=2515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=2515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=2515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}