how to handle 'double opacity' of two overlapping divs - css

I have two divs, both with 0.6 opacity. I need them to overlap but retain their opacity and not create a new combined opacity level. I can't use an image.
EDIT -- The little circle is supposed to have a canvas element in it. Not sure if pseudo-elements would be the best solution.
Is there anyway to do this with CSS, or should I just use canvas?
example -
http://dabblet.com/gist/1566209
HTML:
<div id="foo">
<div id="bar">
</div>
</div>
CSS:
/**
* Double Opacity
*/
body{background:green;}
#foo{
height:150px;
width:250px;
background:rgba(0, 0, 0, 0.6);
position:absolute;
left:40%;
top:20%;
}
#bar{
height:40px;
width:40px;
background:rgba(0, 0, 0, 0.6);
border-radius:40px;
position:absolute;
top:-15px;
left:-15px;
}

SUMMARY:
Depending on what is needed it can be tricky but the basic approach is pretty straight forward.
This approach is a little different from my first thought... but this has the same result.
I made a black/transparent pattern for the circle and set it to
:before.
The circle is then transformed rotate(180deg) and moved to fit on
the corner of the <div>.
Then I set the opacity of that circle to 0.6.
The <div> itself is not affected by the opacity.
Next I added the :after element and put an image as background
(you can control this via js if needed)
I added some effects to the image (border-radius, box-shadow,
border) to show how easily and independent this element can be
controlled.
I used a lighter background and set the opacity to 0.3 to show
the result
HERE'S THE FIDDLE: http://jsfiddle.net/pixelass/nPjQh/4/
Look at this version for some crazy results: http://jsfiddle.net/pixelass/nPjQh/5/
each of these examples only use a single div element
Basic rules. (these rules "could" be used to create a dynamic behavior with js)
position = absolute;
top = circleHeight / -2;
left = circleHeight / -2; //(left = top)
rotation = 180deg;
opacity = valueAofBackground;
bgColor = valueRGBofBackground;
#inner {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
z-index: -1;
background-color: rgba(0, 0, 0, 0.3);
padding:20px;
border-radius: 20px;
border-top-left-radius: 0;
}
#inner:before {
content: "";
background-image: -webkit-linear-gradient(transparent 50%, rgb(0, 0, 0) 50%, rgb(0, 0, 0)),
-webkit-linear-gradient(0deg, transparent 50%, rgb(0, 0, 0) 50%, rgb(0, 0, 0));
height: 40px;
width: 40px;
border-radius: 40px;
position: absolute;
top: -20px;
left: -20px;
-webkit-transform: rotateZ(180deg);
opacity:0.3;
}
#inner:after {
content: "";
background: url('http://lorempixel.com/10/10/sports/1/') no-repeat;
background-position:0;
height: 10px;
width: 10px;
border-radius: 10px;
position: absolute;
top: -6px;
left: -6px;
-webkit-box-shadow: 0 0 10px rgb(255,255,255);
border: 1px rgb(255,255,255) solid;
}
Better explanaition
Original commented version
http://jsfiddle.net/pixelass/nPjQh/10/
see the comments in the code below
#inner {
background: rgba(0,0,0,0.5) /*this is the full color-code of the div (with alpha)*/
}
#inner:before {
/*the solid color of the circle = rgbValue of the div*/
background-image: -webkit-linear-gradient(transparent 50%, rgb(0, 0, 0) 50%, rgb(0, 0, 0)),
-webkit-linear-gradient(0deg, transparent 50%, rgb(0, 0, 0) 50%, rgb(0, 0, 0));
/*opacity of the circle = alpha of the div*/
opacity: 0.5;
}
This example has a full transparent div ...the circle is a "pacman"- shape: http://jsfiddle.net/pixelass/nPjQh/14/
Managing the offset of the circle
Look at these examples that handle the offset of the circle (NOT USING PSEUDEO-ELEMENTS)
1:1 copy of the OP's code (15px offset): http://jsfiddle.net/pixelass/nPjQh/12/
With a lot smaller offset (5px): http://jsfiddle.net/pixelass/nPjQh/13/
(the content has the same opacity as the circle)
How does the offset work?
Control the background-size vs. the top and left
Rules:
top = left;
background-size = elementHeight * 2 + top * 2;
Look at the flower (it is also only one <div> with pseudo-elements)
the background-size is bigger than the circle. which creates the green leaves on the bottom
http://jsfiddle.net/pixelass/nPjQh/15/
CURRENT PROBLEM
See this fiddle: http://jsfiddle.net/pixelass/nPjQh/16/
If not using another layer as seen in the examples at the top of the post the content will be transparent. So if you only need an image inside the circle the above examples will work fine.
HOW TO SOLVE THIS ISSUE
If you need a canvas or another div inside the circle you would have to put the circle on the div and layer the needed div over the circle
See this fiddle: http://jsfiddle.net/pixelass/nPjQh/17/
change around a little and it will work fine. GET THE CODE FROM THE FIDDLE
Different shape /advanced Styling
If you use a different shape with flat sides, you could even put a border around the sum of the two divs.. or even add a box shadow
still using the simple markup of....
<div id="foo">
<div id="bar">
</div>
</div>
See the fiddle for the box-shadow: http://jsfiddle.net/pixelass/nPjQh/21/
Apply a border to the circle
Using -webkit-mask-image we could add a border to the circle.
http://jsfiddle.net/pixelass/nPjQh/24/
More examples:
Four circles around the div
http://jsfiddle.net/pixelass/nPjQh/25/
Markup:
<div id="foo">
<div id="bar1"></div>
<div id="bar2"></div>
<div id="bar3"></div>
<div id="bar4"></div>
</div>
Using this technique to make a tooltip
http://jsfiddle.net/pixelass/nPjQh/31/
Markup:
<div id="foo">
<div id="bar"></div>
I am a pure css tooltip with a semi-transparent background and a black border. <br/>
My width is static an my height is dynamic...
</div>

I think the only way would be to do the opacity separately,
e.g.
http://dabblet.com/gist/1566278

How about this: http://jsfiddle.net/rudiedirkx/TqRCw/
(Dabble's editor sucks!!)
It can't be done with only pseudo elements sadly =(
It can be done with only pseudo elements! See pixelass' answer. CSS3 is a requirement though.

Revised Answer
This fiddle is compatible with IE9 and resolves the duplication of background needed in my original answer. It does use pseudoelements to generate the circle. This solution spins off pixelass's "pacman" idea, only instead of using the newer background gradient css to generate, it uses the older (and little used or understood) clip property to make the circle in two parts. This solved the issue of your circle not being "centered" at the corner.
#foo {
height:150px;
width:250px;
background: rgba(0, 0, 0, 0.6);
position:absolute;
left:40%;
top:20%;
}
#bar {
height:40px;
width:40px;
position:absolute;
top:-15px;
left:-15px;
line-height: 40px;
}
#bar:before,
#bar:after {
content: '';
display: block;
background: rgba(0, 0, 0, 0.6);
border-radius: 40px;
width: 100%;
height: 100%;
position: absolute;
z-index: -1;
top: 0;
left: 0;
}
#bar:before {
clip: rect(0 40px 15px 0);
}
#bar:after {
clip: rect(15px 15px 40px 0);
}
Original Answer
You can do this (see fiddle). It pushes the circle below and "overlays" the portion that overlaps with a pseudoelement to reestablish the background color of the body:
body{background:green;}
#foo{
height:150px;
width:250px;
background:rgba(0, 0, 0, 0.6);
position:absolute;
left:40%;
top:20%;
}
#bar{
height:40px;
width:40px;
background:rgba(0, 0, 0, 0.6);
border-radius:40px;
position:absolute;
top:-15px;
left:-15px;
z-index: -1;
}
#bar:after {
content: '';
display: block;
background: green;
position: absolute;
right: 0;
bottom: 0;
width: 25px;
height: 25px;
}

I have created a Q/A to handle this scenario along with the 'hover' of such overlapped elements.
Overlapped elements with opacity and handling the 'hover' on those.
The solution is basically to set the opacity in the parent level instead directly on the children elements and to toggle those while hover, with JS.
HTML
<div class="wrapper">
<div class="first"></div>
<div class="second"></div>
</div>
JS
$(".first, .second").hover(function() {
$(".wrapper, .first, .second").not(this).toggleClass("add-opacity");
});
CODEPEN
Hope this helps.

Related

Add polygon as css background

I want to have a div where the upper part has a black background and is a polygon(0 0, 100% 0, 100% 780px, 0 670px). The other part of the background of this div should be white. How can I do that?
When you say polygon, do you mean a clip-path polygon?
Maybe something like this? I made the DIV 1000x1000 arbitrarily.
.polygon-bg
{ width: 1000px;
height: 1000px;
position: relative;
}
.polygon-bg::before
{ background-color: Lime;
content: "";
clip-path: polygon(0 0, 100% 0, 100% 780px, 0 670px);
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
.polygon-bg .content
{ position: relative;
z-index: 2;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="polygon-bg">
<div class="content">
<h1>Some text...</h1>
</div>
</div>
</body>
</html>
For a simple shape like this you could use linear-gradients in an background-image CSS declaration as you can have as many ‘images’ as you like, separated by commas, and each with its own positioning.
Your shape is a rectangle with a triangle beneath. Instead of using different colors to merge into one another we can just use black for both the start and end colors.
So here is the snippet to create a class called polygon which will make the background color of the div white and overlay one black polygon.
.polygon {
width:400px;/* set these as required */
height:1024px;
background:
linear-gradient(black,black) top/100% 670px,
linear-gradient(to bottom left,black 50%,transparent 50%) 0px 670px /100% 110px;
background-color:white;
background-repeat:no-repeat;
}
<div class="polygon"></div>

Adding a CSS gradient to an image--NOT a background image

This is a page that uses a carousel (I believe flexslider). The images in this carrousel are NOT background images. I need to add a gradient to the image, going from the bottom up, and from dark to zero opacity, so that I can make the text more legible. Is this possible?
http://hungersolutionsny.magadev.net
Personally I am not a big fan of adding markup just for styling. I would go for a pseudo element :before or :after
The code would look something like this:
HTML
<div class='slideshow-wrapper'>
<img src='http://www.placekitten.com/800/300'/>
<h2 class='title'>Some title</h2>
</div>
CSS
.slideshow-wrapper {
position:relative;
float: left;
}
.title {
position: absolute;
left: 0;
right: 0;
text-align: center;
z-index: 2;
}
.slideshow-wrapper:before {
content: '';
position:absolute;
top:0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,1) 100%);
z-index: 1;
}
And an example: http://jsfiddle.net/VrGeM/
Overlay the image with an absolutely positioned <div> that's the same size as the slider. Give that <div> the gradient. Ensure that it's above the images but below the text on top of the images.
It's also pretty easy to create a transparent PNG to use rather than a CSS gradient, which will have the advantage of working in older versions of IE.
The way I ususally do this is via an absolutely-positioned DIV which sits on top of the images and contains the text. Then I give that an opacity like so:
background-color: rgba(0, 0, 0, 0.56);
If you want a gradient with opacity, this is a good tool which makes that easy: http://www.colorzilla.com/gradient-editor/
There are a number of ways to tackle this. Mainly targeting the background CSS property. If you're looking to target the text that is overlapping the image you could use something simple like this:
body.front #region-content #flexslider-1 ul.slides .views-field-field-banner-statement {
background-color:rgba(0, 0, 0, 0.5);
}
It doesn't apply a gradient but it does supply a black background with 50% opacity.
I usually don't use a gradient in this way... when faced with this problem in the past I have always used an inset box-shadow on a div wrapped around the image. Something like this...
<div class="img-wrap">
<img src="" />
</div>
And then in CSS apply the box-shadow to a pseudo selector...
.img-wrap {
display: block;
position: relative;
}
.img-wrap:before {
display: block;
content: '';
position: absolute;
width: 100%;
height: 100%;
-webkit-box-shadow: inset 0 -100px 80px 0px rgba(0,0,0,0.4);
}
img {
display: block;
width: 100%;
height: auto;
}
Check out this CodePen if you want to see it live... http://cdpn.io/qGwLe

CSS Only Folded Corner Div

So I have been slamming my head against the wall trying several different methods online and can't get anything to work.
I have a div that needs to be fluid width, and its height needs to be variable as well.
The div sits on top of a tile-able background image. It has a 1px border around it.
I need the bottom right of the div to fold up, like a piece of paper.
I tried using an image, in a div anchored to the bottom. But that requires a fixed width or height as far as I can tell.
I tried this method but it requires a solid background color. I have a repeating image.
I tried this method, which uses gradients to control the opacity at the corner, this almost works, but my div requires a border. Applying the border ruins the effect.
background:
linear-gradient(45deg, rgba(255,0,0,0.4), rgba(255,0,0,0.4)),
linear-gradient(135deg, rgba(255,0,0,0.4), rgba(255,0,0,0.4)),
linear-gradient(225deg, transparent 10px, rgba(255,0,0,0.4)
background-size: 14px 14px, 50% 100%, 50% 50%, 50% 50%;
background-position: 100% 0, 0 0, 100% 100%, 100% 0;
background-repeat: no-repeat;
//then an :after pseudo class to create the corner fold
Any help would be greatly appreciated. Thanks.
This question got me bussy for some time, this seems to be a really hard thing to do with CSS only. I managed to achieve the effect(the paper flip with a border around the element) you wanted, but it requires alot of CSS and I don't know how far you want to go. I applied border-radius to the top right corner and used a triangle to overlap the border-radius. This did not cover the entire border radius, so I used a span to form 2 shapes to overlay the remaining gap.
Look at this fiddle for the result, any improvements are welcome
http://jsfiddle.net/EnVnW/
CODE:
body {
background: #444 url('http://leaverou.me/ft2010/img/darker_wood.jpg') bottom;
}
.arrow_box {
color:red;
position: relative;
background: #88b7d5;
border: 4px solid #c2e1f5;
height:400px;
border-radius:0 300px 0 0; /* here we give the box a round corner on the top right side */
}
.arrow_box:after, .arrow_box:before { /* we create 2 triangles in the top right corner, one for the border and one for the filling */
-ms-transform:rotate(-135deg); /* rotate to make the triangle have the right angle */
-webkit-transform:rotate(-135deg);
transform:rotate(-135deg);
bottom: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
top:42px;
right:-20px;
}
/* here we position the triangles in the top right corner */
.arrow_box:after {
border-color: rgba(200, 265, 0, 0);
border-bottom-color: #00b7d5;
border-width: 100px;
left: 100%;
margin-left: -240px;
}
.arrow_box:before {
border-color: rgba(194, 225, 245, 0);
border-bottom-color: #c2e1f5;
border-width: 105px;
left: 100%;
top:39px;
margin-left: -245px;
}
/* we still have a massive gap next to the triangle, so we fill it up with 2 rectangles. One on the left side of the triangle and one on the bottom side of the triangle, we also will give them a border to finish the line around the element */
.arrow_box span {
border-top: 4px solid #c2e1f5;
position:absolute;
width:150px;
height:140px;
background-color:black;
right:140px;
top:-4px;
background: #88b7d5;
}
.arrow_box span:after {
border-right: 4px solid #c2e1f5;
content: "";
position:absolute;
width:150px;
height:150px;
background: #88b7d5;
left:140px;
top:140px;
}
With CSS4 this will be alot easier to do, here you can read about it;
http://dev.w3.org/csswg/css-backgrounds-4/#border-corner-shape

Is it possible to make a "double arrow" with css3 content technique?

Im looking for a way to recreate this button with CSS only.
I know about the triangle technique and I also know how to add a border to it, but unfortunately I don't know any way to recreate this button (without adding additional wrappers or using images).
The buttons I need this style on are <input["submit"]> and ordinary <a>'s.
With one element, you could do it using gradients and skewed pseudo-elements for a link:
demo
(you could actually do it using just gradients, but then a hover action won't be triggered on hover on the arrow shape itself, but on hover on the rectangular element containing it)
HTML:
<a class='boo' href='#'>click me</a>
Relevant CSS:
.boo {
display: inline-block;
position: relative;
padding: .5em 2em;
background:
linear-gradient(60deg, dodgerblue 50%, transparent 50%) 100% 0,
linear-gradient(-60deg, transparent 50%, dodgerblue 50%) 100% 100%,
linear-gradient(-90deg, transparent 1em, dodgerblue 1em);
background-repeat: no-repeat;
background-size: 1em 50%, 1em 50%, 100% 100%;
}
.boo:before, .boo:after {
position: absolute;
right: -.2em;
width: .5em; height: 50%;
background: dodgerblue;
content: '';
}
.boo:before {
top: 0;
transform: skewX(30deg);
}
.boo:after {
bottom: 0;
transform: skewX(-30deg);
}
EDIT:
If your background is a solid color, not an image or a gradient, you could do it in a much simpler way, without using gradients (which means that this second method also has the advantage of working in IE9).
demo #2
.boo {
display: inline-block;
position: relative;
padding: .5em 2em;
background: lightblue;
}
.boo:before, .boo:after {
position: absolute;
right: -.3em;
width: .5em; height: 50%;
box-shadow: -.2em 0 0 white;
background: inherit;
content: '';
}
.boo:before {
top: 0;
transform: skewX(30deg);
}
.boo:after {
bottom: 0;
transform: skewX(-30deg);
}
You should use a background image. Create a transparent png containing the arrow.
You would need two elements, the outer would contain the background image, the inner would contain the text, and a background color which is the same as the one on the arrow. Alternatively, you could use a second background image instead of a background color, for example if your button is not just a flat color.
The trick is to align the box containing the text with the background image.
If your arrow is 20px tall, your inner box could be e.g. 16px plus 2px padding on each side (search for box model if you would like to understand this better).
The outer element can have a right-margin set to the approximate width of the arrow image.
I hope this makes sense. The general technique is called sliding doors. I suggest reading the entire article if you have the time.

How can I tint a background image with CSS?

I have a background image set up through CSS.
html {
background-image: url('../img/cello.jpg');
background-attachment: fixed;
background-size: 100%;
}
I plan on having a different background image for different pages of the website: so it's important that text is legible over it. Right now I've got a translucent black background to my #main content box in the middle like this in order to ensure legibility:
#main {
background: rgba(0, 0, 0, 0.5);
}
What I really want to do, though, is to have that kind of translucent background over the entire background image, because the black box looks a bit clunky. I've tried making a <div id=#tint> which includes the whole HTML document and giving rgba(0, 0, 0, 0.5) to #tint, but that doesn't work at all--I can either get nothing to change or I can get the entire background to become a simple grey with no background image visible at all. Is this simply not possible?
Use background-blend-mode for a simple tint
You can use the background-blend-mode css property:
.box {
width: 300px;
height: 300px;
background-size: cover;
background-image: url('https://placehold.co/300');
}
.background-tint {
background-color: rgba(200,100,0,.5);
background-blend-mode: multiply;
}
<div class="box background-tint"></div>
Place it on any element with a background image and you're good to go.
The property is well supported in modern browsers NOT including IE 11. For non supporting browsers you can use a polyfill.
Working demo
Other Options
Use filter for a complex tint
You can use the filter css property:
.box {
width: 300px; height: 300px;
background-size: cover;
background-image: url('https://placehold.co/300');
}
.background-tint {
filter: sepia(100%) saturate(200%) brightness(70%) hue-rotate(330deg);
}
<div class="box background-tint"></div>
Place it on any element with a background image and you're good to go.
In order to change the color change the hue-rotate value.
The property is well supported in modern browsers NOT including IE 11.
Working demo
Use a flat linear-gradient and a multiple background overlay
.background-tint {
background-image:
linear-gradient( rgba(0,0,0,.5), rgba(0,0,0,.5) ),
url('http://placehold.it/420')
}
I think this is the most widely used technique but it has the downside of being hardcoded i.e. you can't just take a class, stick it on an element and make a tint.
You could make this into a less or sass mixin, something like:
less
.background-tint(#tint-color, #image-url) {
background-image:
linear-gradient( #tint-color, #tint-color ),
url( #image-url )
}
sass
#mixin background-tint($tint_color, $image_url) {
background-image:
linear-gradient( $tint_color, $tint_color ),
url( $image_url )
}
Working demo
Use a transparent background
This method has the advantage of working on most browsers and is just a nice class you add to any element.
The downside is that if you have anything else inside of that element you will have to wrap it in a div with some kind of positioning position: relative would work best.
Example:
.box {
width: 300px; height: 300px;
background-size: cover;
background-image: url('http://placehold.it/300');
color: #facebc;
}
.background-tint { position: relative; }
.background-tint::after {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,.5);
margin: auto;
}
.u-relative { position: relative; z-index: 1; }
<div class="box background-tint">
<div class="u-relative">300 x 300</div>
</div>
Working Demo
I think you need to create an overlay element (potentially div) which has the sought translucent background. Something like:
.overlay {
z-index: 1;
height: 100%;
width: 100%;
position: fixed;
overflow: auto;
top: 0px;
left: 0px;
background: rgba(0, 0, 0, 0.7); /*can be anything, of course*/
}
And of course, a little demo: little link.
This worked great for me:
https://css-tricks.com/tinted-images-multiple-backgrounds/
.tinted-image {
background:
/* top, transparent red, faked with gradient */
linear-gradient(
rgba(255, 0, 0, 0.45),
rgba(255, 0, 0, 0.45)
),
/* bottom, image */
url(image.jpg);
}
And building on another answer, you can do this with existing colors in less like:
linear-gradient(
fade(#brand-primary, 50%),
fade(#brand-primary, 50%)
),
It would be the overlay property
https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blendingoverlay
But it's a draft. Don't rely on it
Try opacity:
opacity:0.4;
filter:alpha(opacity=40); /* For IE8 and earlier */
This is the simplest solution to the problem in my opinion.
.parent-div{
background-color : desired-color
}
#image-id{
opacity: dersired_value%
}
To increase readibity background-color: black and opacity percentages of range 50 to 60% seem to work nicely.

Resources