{"id":2626,"date":"2020-10-26T04:05:23","date_gmt":"2020-10-26T09:05:23","guid":{"rendered":"https:\/\/cssnewbie.com\/?p=2626"},"modified":"2020-10-26T04:05:23","modified_gmt":"2020-10-26T09:05:23","slug":"how-to-add-opacity-in-css","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/how-to-add-opacity-in-css\/","title":{"rendered":"How To Add Opacity In CSS"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-2627 size-large\" src=\"https:\/\/cssnewbie.com\/wp-content\/uploads\/2020\/10\/20590-1024x546.jpg\" alt=\"How To Add Opacity In CSS?\" width=\"1024\" height=\"546\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>CSS is used to design various styles for your web pages. The opacity property in CSS sets the opacity of an element. It describes the transparency level of an element. The level of opacity varies from 0.0-1.0. Here value 0.0 means completely transparent, 0.5 is 50% transparent and 1.0 means it is fully opaque. The default value for an image opacity is 1.<\/p>\n<p>It is very easy to add opacity to images using the opacity property. Also, note that this property sets the child element transparent as well. Thus making it hard to read the text inside a fully transparent element. The opacity of an element is set using the opacity property in your CSS file.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre>.class-name {\nopacity: 0.0; \/\/Opacity level\n}<\/pre>\n<p>You can also set the opacity of an element using <a href=\"https:\/\/css-tricks.com\/the-power-of-rgba\/\">RGBA<\/a> (here a stands for alpha value).<\/p>\n<p><strong>Let us see an example:<\/strong><\/p>\n<pre>&lt;html&gt;\n  &lt;title&gt;\n \u00a0\u00a0 How To Add Opacity In CSS\n \u00a0 &lt;\/title&gt;\n\n&lt;head&gt;\n&lt;link rel=\"stylesheet\" href=\"css.css\"&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&lt;img id=\"pic1\" src=\"https:\/\/cdn.pixabay.com\/photo\/2017\/09\/16\/16\/09\/sea-2755908__340.jpg\" width=\"300\" height=\"200\"&gt;\n&lt;img id=\"pic2\" src=\"https:\/\/cdn.pixabay.com\/photo\/2016\/05\/05\/02\/37\/sunset-1373171_960_720.jpg\" width=\"300\" height=\"200\"&gt;\n&lt;img id=\"pic3\" src=\"https:\/\/cdn.pixabay.com\/photo\/2018\/01\/14\/23\/12\/nature-3082832__340.jpg\" width=\"300\" height=\"200\"&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>CSS File:<\/strong><\/p>\n<pre>#pic1 {\nopacity: 1.0;\n}\n\n#pic2 {\nopacity: 0.7;\n}\n\n#pic3 {\nopacity: 0.3;\n}<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-2628 size-full\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/10\/Opacity1.png\" alt=\"How To Add Opacity In CSS?\" width=\"740\" height=\"233\" \/><\/p>\n<h3><strong>Opacity in Hover Effect<\/strong><\/h3>\n<p>In order to change the opacity of an image on mouse-over, the :hover selector is used along with the opacity property in CSS. This allows you to change the opacity of an image when you hover on an image. How does this hover effect work? When you hover over an image either you can make it transparent or opaque by setting image opacity and hover opacity property.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre>.image {\nopacity: 0.5; \/\/Image is 50% transparent\n}\n\n.image:hover {\nopacity: 1.0;\n\/\/Fully opaque on mouse-over\n}<\/pre>\n<p><strong>Let us see an example:<\/strong><\/p>\n<pre>&lt;html&gt;\n&lt;title&gt;\nHow To Add Opacity In CSS\n&lt;\/title&gt;\n\n&lt;head&gt;\n&lt;link rel=\"stylesheet\" href=\"opacity.css\"&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&lt;h2&gt;How To Add Opacity In CSS&lt;\/h2&gt;\n&lt;p&gt;We will add effects that will change with hover:&lt;\/p&gt;\n&lt;h3&gt; On Hover &lt;\/h3&gt;\n&lt;img id=\"i1\" src=\"https:\/\/cdn.pixabay.com\/photo\/2016\/05\/05\/02\/37\/sunset-1373171__340.jpg\" width=\"170\" height=\"100\"&gt;\n&lt;img id=\"i1\" src=\"https:\/\/cdn.pixabay.com\/photo\/2016\/08\/11\/23\/48\/italy-1587287__340.jpg\" width=\"170\" height=\"100\"&gt;&lt;br&gt;&lt;br&gt;\n&lt;h3&gt; Without Hover &lt;\/h3&gt;\n&lt;img id=\"i2\" src=\"https:\/\/cdn.pixabay.com\/photo\/2016\/05\/05\/02\/37\/sunset-1373171__340.jpg\" width=\"170\" height=\"100\"&gt;\n&lt;img id=\"i2\" src=\"https:\/\/cdn.pixabay.com\/photo\/2018\/01\/14\/23\/12\/nature-3082832__340.jpg\" width=\"170\" height=\"100\"&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/pre>\n<p><strong>CSS File:<\/strong><\/p>\n<pre>#i1 {\nopacity: 0.5;\n}\n\n#i1:hover {\nopacity: 1.0;\n}\n\n#i2 {\nopacity: 1.0;\n}\n\n#i2:hover {\nopacity: 0.5;}<\/pre>\n<h3><strong><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-2629 size-full\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/10\/Opacity2.png\" alt=\"How To Add Opacity In CSS?\" width=\"913\" height=\"380\" \/> <\/strong><\/h3>\n<h3><strong>Text In A Transparent Box<\/strong><\/h3>\n<p><strong>\u00a0<\/strong>When you add opacity property to a background it also sets the same property for its child. That means when you set opacity property to an image or <a href=\"https:\/\/www.bitdegree.org\/learn\/css-background\">background<\/a> it will also set the same property to the text inside it. In order to make it visible we use the transparent box property. The \u201ctransbox\u201d property sets the opacity only to the background and not to the text inside it.<\/p>\n<p><strong>Steps to add text in the transparent box:<\/strong><\/p>\n<p><strong>\u00a0<\/strong>You have to create a &lt;div&gt; element with a background image.<\/p>\n<ul>\n<li>Create another &lt;div&gt; inside the first &lt;div&gt; with class=\u201ctransbox\u201d which has background color and is transparent.<\/li>\n<li>Inside the second &lt;div&gt;, you can add text inside a &lt;p&gt; element.<\/li>\n<\/ul>\n<p><strong>Let us see an example:<\/strong><\/p>\n<pre>&lt;html&gt;\n&lt;title&gt;\nHow To Add Opacity In CSS\n&lt;\/title&gt;\n\n&lt;head&gt;\n&lt;link rel=\"stylesheet\" href=\"opacity.css\"&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&lt;div class=\"background\"&gt;\n&lt;div class=\"transbox\"&gt;\n&lt;p&gt;Let us add some content to this page.&lt;\/p&gt;\n&lt;\/div&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/pre>\n<p><strong>CSS File:<\/strong><\/p>\n<pre><strong>\u00a0<\/strong>div.background {\nbackground: url(https:\/\/cdn.pixabay.com\/photo\/2018\/09\/19\/23\/03\/sunset-3689760__340.jpg) repeat;\n}\n\ndiv.transbox {\nmargin: 30px;\nbackground-color: white;\nborder: 1px solid black ;\nopacity: 0.2;\n}\n\ndiv.transbox p {\nmargin: 10%;\nfont-weight: bold;\ncolor: #000000;\n}<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-2630 size-large\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/10\/Opacity3-1024x259.png\" alt=\"How To Add Opacity In CSS?\" width=\"1024\" height=\"259\" \/><\/p>\n<h3><strong>Conclusion<\/strong><\/h3>\n<p><strong>\u00a0<\/strong>In this tutorial, we learned how to add opacity in CSS. We also learned how to set opacity property on an image, in hover effect, and text in a transparent box. In the above examples, we used opacity, hover effect, and transparent properties. You can do this in another way by adding RGBA property in your CSS file.<\/p>\n<p>To learn more such CSS codes, subscribe to our <a href=\"http:\/\/www.cssnewbie.com\">blog<\/a>!<\/p>\n<p>To see how to add links in CSS, visit our previous article!<\/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>\n<p>&nbsp;<\/p>\n<p>CSS is used to design various styles for your web pages. The opacity property in CSS sets the opacity of an element. It describes the transparency level of an element. The level of opacity varies from 0.0-1.0. Here value 0.0 [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/how-to-add-opacity-in-css\/\" title=\"Click to read 'How To Add Opacity In CSS'\">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":[168],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2626"}],"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=2626"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2626\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=2626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=2626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=2626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}