Add Opacity to Part of an Image (CSS) - css

How can one add opacitiy to just the left 100px of a 600px wide image in css? Is there a css property for that?
I have tried to add an overlapping div and add opacity to this div, but that is a pain in the back and does not look as a good solution.

Well i found that overlapping div with position:absolute is the only solution for this because their is no property in css to catch half image.
HTML
<div class="parent">
<div id="opacity_div"></div>
<img class="half_fade" src="http://i.stack.imgur.com/W7mPR.jpg?s=32&g=1">
</div>
CSS
.parent{
position:relative;
}
#opacity_div{
background:#fff;
height:20px;
width:20px;
position:absolute;
top:18px;
left:6px;
opacity:0.5 /* manipulate to desired opacity */
}
img.half_fade {
position:absolute;
top:0;left:0;
z-index:-1000;
}
Example : http://jsfiddle.net/JMBFS/81/
Checkout this question to understand better : https://drupal.stackexchange.com/questions/70025/how-to-apply-opacity-to-just-a-portion-of-the-image/70029

The solution is to use overlay the image element with another image element, using position absolute and clipping (http://codepen.io/anon/pen/jqpwgV).
HTML:
<img src="img.jpg" id="image-overlay" />
<img src="img.jpg" id="image" />
CSS:
#image-overlay{position:absolute;clip: rect(0px,498px,374px,100px);}
#image{opacity:0.5;}
If you want to be future ready. Use clip-path with graceful degradation in your CSS. See code below (or http://codepen.io/anon/pen/zqLdxW).
#image-overlay{position:absolute;
clip: rect(0px,498px,374px,100px);
-webkit-clip-path: inset(0px 0px 0px 100px);
clip-path: inset(0px 0px 0px 100px);
}
#image{opacity:0.5;}

Related

How to achieve curved drop shadow effect with CSS?

I'm trying to achieve the following drop shadow effect using CSS:
I have tried styling an <hr> element with a linear gradient as the background image as an alternative to using the box-shadow rule on an image however it does not produce the desired curved shadow effect.
Is what I am trying to do possible via CSS only ?
Here is my current code:
HTML
<section class="section-categories section-fruits">
<div class="row">
<figure class="categories">
<img src="res/img/category/fruits.png" alt="Offers" class="categories__fruits">
</figure>
<div class="categories__explore">
<p>Fruits & Vegetables</p>
<p>A variety of fresh fresh and vegetables</p>
<button>Explore fruit and veg</button>
</div>
</div>
</section>
CSS
/* Using Box Shadow, but didn't get the desired effect */
.section-categories{
height: 250px;
margin: 20px 0px;
box-shadow: 0px 1px 1px rgba(0,0,0,0.3);
}
One pure CSS approach might be to use the radial-gradient function on a pseudo element as follows:
/* The shadow CSS class element */
.shadow {
position:relative;
}
/* The CSS peseudo element that achieves the shadow effect */
.shadow:after {
content:'';
display:block;
position:absolute;
bottom:-1rem;
/* The main idea with this technique is to use a radial gradient to simulate the
desired effect */
background:radial-gradient(farthest-corner at 50% 0px, grey 0%, transparent 50%);
width:100%;
height:1rem;
}
/* The styling below is not part of the technique and is included to support the snippet */
div {
display:flex;
flex-direction:row;
justify-content:center;
align-items:center;
}
div button {
background:red;
}
<div class="shadow">
<img src="https://i.pinimg.com/originals/2b/1c/f5/2b1cf5525873467315eaa0c07394d302.jpg" height="100px" />
<button>Explore</button>
</div>
The idea here is to define a pseudo element :after the actual element casting the shadow (ie the <div> in the snippet above), which contains a radial-gradient. To simulate the desired effect, the radial-gradient is colored with a dark inner and transparent outer, with the center of that gradient being offset to the upper edge of the pseudo element via the farthest-corner at 50% 0px parameters.
Hope that helps

Proper use of opacity with nested DIVs? [duplicate]

This question already has answers here:
Specify parent divs opacity but make it not affect children HTML elements
(4 answers)
Closed 9 years ago.
So I'm trying to create a lightbox like feel. I created a #blackout div and an #enlargedBOX div.
The #blackout div has it's opacity set to 90%, because I want the background website to show through just a bit, however i do NOT want my #enlargedBOX div to use that same opacity. It seems that #blackout forces its own opacity onto anything within itself. How can i stop that?
<div id="blackout">
<div id="enlargedBOX">
<img src="" width="500" height="500" border="0" />
</div>
</div>
Here's a jsFiddle
You'll see that the RED background shows through on the white #enlargedBOX div.
Just use rgba() - DEMO
#blackout {
position:absolute;
top:0px;
left:0px;
height:100%;
width:100%;
overflow:auto;
z-index:100;
background: rgba(0, 0, 0, .9);
}
Take the enlargedBOX dialog div out from within the blackout background overlay div, and give it a higher z-index.
jsFiddle example
#enlargedBOX {
position:relative;
z-index:101;
margin:50px auto;
padding:0px;
width:500px;
height:500px;
background:#FFF;
opacity:1;
}
<div id="blackout"></div>
<div id="enlargedBOX">
<img src="" width="500" height="500" border="0" />
</div>

CSS: help positioning a clipped image in Chrome/Safari. IE and FireFox look good

Clipping an image with CSS and everything looks great with FireFox and IE7+, but Chrome and Safari are not accepting the position:absolutevery nicely. Instead of staying "relative" to the parent containers, it's in fact using the absolute positioning coordinates.
I've tried putting containers and positioning those in different ways, but I'm not having any luck.
Unfortunately, I can not provide any example directly.
Here's the HTML:
<div class="grayBkgd marginTop10Px grid_3 grayVideoContainer alpha">
<a href="?ytID" class="noUnderlineHover curser-pointer">
<div class="clip">
<img src="an image" alt="" />
</div><br />
<div class="videoTextInfo">
<p class="videoTitle">Optical Windows Overview
</p>
</div>
<p class="grayText" style="float:right">0:37 mins</p>
</a>
</div>
The CSS:
<style type="text/css">
.grayVideoContainer{
text-align:center;
padding:3px 0 5px 0
}
.clip img {
position:absolute;
margin-left:-58px;
clip:rect(12px 120px 79px 0)
}
.videoTextInfo {
min-height:60px;
padding:70px 5px 0 5px;
color:#000
}
.grayText {
float:right;
margin-top:-10px;
padding:0 5px 0 0
}
</style>
Any ideas?
I tried looking for JQuery plugins to clip the image and see if I'd have an easier time positioning the elements, but I didn't have very much luck finding any promising plugins.
Are the parent containers positioned at all? If not, try to add position: relative; to the parent of the child that's position: absolute;. The relative positioning won't change the parent's position in the flow of your site but will allow you to have absolutely positioned children.
An absolutely positioned element's position should be specified with top, left, right, and/or bottom.
See: http://www.w3schools.com/css/pr_class_position.asp
The margin-left on .clip img I might expect not to work.

Transparent box (div) with images(as links) -- proportionally resizable?

This is actually a two part question. so I have this transparent div element floating over a background image, what I want is to have images(as links) inside the box. But not transparent. I have the transparent box but I can't seem to figure out how to make the contents not transparent, because I would also like those images(as links) within the box to scale proportionally to the web browser. my css so far is this:
#menu
{
position:absolute;
top:13%;
left:3%;
width:25%;
height:20%;
background-color:#ffffff;
filter:alpha(opacity=60);
opacity:0.6;
-moz-opacity:0.6;
-khtml-opacity: 0.6;
}
#work img
{
position:absolute;
margin: 2% 29%;
height:33%;
}
#infocontact img
{
position:absolute;
margin: 33% 29%;
height:33%;
}
#store img
{
position:absolute;
margin: 66% 29%;
height:33%;
}
and my html is this
<div id="menu">
<div id="work">
<img src="work.gif" /> </div>
<div id="infocontact">
<img src="info.gif" />
</div>
<div id="store">
<img src="store.gif" /></div>
</div>
so what I have now is those gif images scaling with the height (and subsequent width) of the browser. and what im trying to do is have those images scale with the scale of the box as well. so if for some reason you make the browser pretty small, the images dont stick out past the smaller transparent box.
i know its gotta be possible, i just cant figure out the right combination of css/html to make it work.
The CSS:
.pic1
{
position:absolute;
width:10%;
thisistheexactwidthofitscontaineralwaysinpercentmax-width:110px;
}
img.scaled,.scaled
{
width:100%;
}
The HTML:
<div class="pic1"><img class="scaled" src="images/yourpic.png" alt="" title="">
</div>
Give the image a higher zindex than its container and apply the link to the image, not the container.
Also, for scalable images, use the width attribute. Assign the width of the container in respect to its position in the flow of your page, and the img and .scale width to 100%.
You may have to play with the size of the container to get it proportional, but it's a query free solution and works well except for scaling down png images with transparent backgrounds.

I want to apply a overlay image on hover

But I am struggling.
Code I have for css is:
#gallery img {
width:700px;
height:213px;
margin:0;
padding:0;
}
So I thought ...
#gallery img:hover {
width:700px;
height:213px;
position: relative;
z-index:10000;
background: transparent url(../images/imgOverlay-Zoom.png) no-repeat center center;
}
Would work, but it doesnt.
The image I am transparently overlaying on hover is:
What am I doing wrong.
I think I may have a corrupt css tag somewhere.
Any help appreciated.
Make the #gallery have a background image rather than having an image tag inside it... otherwise it'll be on top of the background. Then have another div inside it which has the :hover pseudo-class. If it still doesn't work, take out the word transparent.
Or you could not overlay the image and just swap the original image for the combined image?
Hello there
I think you misunderstood the mechanics of CSS:
The image itself is an object and the background specified goes behind it.
So you have to make the non transparent image the background and specify the transparent one in the src. However this won't suit your needs.
A workaround would with CSS would be troublesome, so i would suggest to swap the whole image with a css hover or javascript onMouseover or jQuery - get familliar with those since it's the proper way.
Fixed.
css:
#container { position:relative; width:700px; height:213px;}
.image { position:absolute; width:700px; height:213px; z-index:0;}
.overlay { background-image:none); position:absolute; width:700px; height:213px; z-index:1;}
.overlay:hover { background: transparent url(images/imgOverlay-Zoom.png)no-repeat center center;}
html:
<div class="overlay" ></div>
<div class="image" ><img src="images/listing-page-with-gradient.png" /></div>

Resources