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
Related
I'm trying to make a triangular shaped background image overlay a full width image.
It is easy enough to make a triangular shaped background image using border-with, border-color and background-image, like so:
border-width: 350px 50vw 0px 0px;
border-color: white transparent transparent transparent;
background-image: url(/img/rainbow4.jpg);
But as there is white space you cannot overlay on top of another image, you get the following http://codepen.io/anon/pen/ENVWRz
If you set:
border-color: transparent transparent transparent transparent;
Then the image will appear square, so this doesn't work.
I've managed to make it work using clip-path, but this is very poorly supported across many browsers – so I am trying to avoid this approach.
you can also take a look at mix-blend-mode or background-blend-mode , but also tricky to use and not so much supported yet.
div.triangle_test {
width: 0;
height: 0;
border-style: solid;
border-width: 350px 50vw 0px 0px;
border-color: white transparent transparent transparent;
display: block;
position: absolute;
z-index: 0;
right: 0;
background-image: url('http://img06.deviantart.net/25de/i/2012/134/3/1/037_by_koko_stock-d4zq28i.jpg');
}
div.full_width {
mix-blend-mode: darken;
background-image: url('http://somebodymarketing.com/wp-content/uploads/2013/05/Stock-Dock-House.jpg');
width: 100%;
height: 350px
}
<div class="triangle_test"></div>
<div class="full_width"></div>
http://codepen.io/gc-nomade/pen/JRdEVO
I'm in somewhat of a situation as I need to make minor CSS adjustments but do not have permission to touch the graphics.
In this case, i have a background image (that has a width of 461px in Photoshop) and I need to extend it 4-5 pixels.
The current for CSS for this image is:
#screenvolumeslidermenu{
background:url(../html_cmi/lopa/volume_bg_overlay.png) no-repeat;
background-size:100%;
width:461px;
height:51px;
position:fixed;
z-index:15;
top:678px;
left:70px;
display:none;
}
It didn't initially have the no-repeat property, but if I take that out and just increase the current width, the image will start to repeat. If I leave it in as is and increase the width, no visible change is made but using inspect element the width is technically increasing. See here:
I want the right side of the black rectangle to extend a few more pixels on the right side.
I tried using background-size but couldn't get any decent results.
What is the best solution here to extend the image width without touching it in Photoshop?
In your situation. The ideal method would be to create the whole thing using CSS. Create the main rectangle and use :before and :after to create the arrow with border styles. Check this site out
.arrow_box {
position: relative;
background: #88b7d5;
border: 4px solid #c2e1f5;
}
.arrow_box:after, .arrow_box:before {
right: 100%;
top: 50%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
.arrow_box:after {
border-color: rgba(136, 183, 213, 0);
border-right-color: #88b7d5;
border-width: 30px;
margin-top: -30px;
}
.arrow_box:before {
border-color: rgba(194, 225, 245, 0);
border-right-color: #c2e1f5;
border-width: 36px;
margin-top: -36px;
}
1) background-size can accept two values, for example
background-size: 200% 100%;
should let you stretch only one dimension (can use pixels too).
2) if you don't want to touch proportions, consider creating another, narrower element that has the same background image, but aligned to the right edge, and place it over the first element, aligned to the right. Then by moving the right piece like a slide ruler, you'll be able to vary overall width without distorting your graphics.
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.
At first, when I say centered, I mean both, vertically and horizontally centered.
I have a table with several tiles (divs) like in Windows 8 with background.images.
Every tile has a centered label (also a div) with a description and a semi transparent background.
Now I'd like to add another div between the tile itself and the label. These div should have a semi-transparent background-color as an overlay of the underlying tile-image-div.
But when I add this overlay-div, my label is not centered horizontally anymore, it is placed at the top of the tile. How can I keep it centered?
This is my code on fiddle:
fiddle (please take a look)
(The problem in the fiddle code is this line:
<div class="SemiTransOverlay"> When I delete this div everything is centered correctly.
What do I have to change to keep everything centered and keep div?)
first of all why you are using tables for layout purposes? we are in 21st century, so start using div's and for accomplishing the semi transparent div to place behind the label and vertically aligned label you need to use position: absolute; and top: -50%;, I've also modified line-height for div.SemiTransLabelGross and also used position: relative and z-index properties
Demo
CSS
table.Kacheln
{
border-spacing: 5px;
border-collapse:separate;
border:0px;
}
.wrap {
position: relative;
height: 100%;
}
td.KachelFlavourGross01
{
text-align:center;
/*background:url(../img/Div/bg.jpg) no-repeat 0 0;*/
background-color:#FF0000;
width:404px;
height:200px;
}
div.SemiTransOverlay
{
height:100%;
width:100%;
background-color:rgba(255, 255, 255, 0.5);
position: absolute;
z-index: 1;
}
div.SemiTransLabelGross
{
font-size:2em;
font-family:Verdana;
font-style:italic;
line-height:60px;
background-color: rgba(255, 255, 255, 0.75);
width:404px;
height:60px;
z-index: 2;
position: absolute;
top: 50%;
margin-top: -30px; /* Half of height */
}
Aligning vertically, what I do is, set padding instead of height.
eg:
div.SemiTransLabelGross
{
font-size:2em;
font-family:Verdana;
font-style:italic;
line-height:120%;
background-color: rgba(255, 255, 255, 0.75);
width:404px;
padding: 20px 0; /* removed the height property */
}
Sorry for misunderstanding, from the above answer, I think you wanted that light pink div itself to be vertically centered. In that case, add padding to its parent.
div.SemiTransOverlay
{
width:100%;
background-color:rgba(255, 255, 255, 0.5);
padding: 20% 0;
}
OR, you can add margin to
div.SemiTransLabelGross {
margin-top: 20%;
}
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.