Clip content inside div with another div - html

I am working on some kind of drawing app.
There are elements inside a container div, and there is another div on top that should clip all the elements inside if it.
Here is the example: http://jsfiddle.net/n6m4n750/
The red rectangle "#clip must clip all the elements inside #container div, so any elements or part of an element that is outside of the #clip div, will be hidden.
How is it possible to do that?
Any help will be appreciated.

Add the following css to #clip:
box-shadow: 0px 0px 0px 25px white;
Here, the white shadow of the #clip overlaps the contents that need to be clipped which gives a clipping effect.
Here's a DEMO.

Not really posible to really clip it, as far as I know.
If your background is white, you can simulate the idea givind a huge white shadow around it
#clip {
position: absolute;
width: 300px;
height: 300px;
border: solid 2px red;
top: 50px;
left: 50px;
box-shadow: 0px 0px 0px 1000px white;
}
demo

Since you can't modify the html, i dont see a pure css solution. Maybe you could track the clipping with js.
const computeClip=function(target,mask){
let maskRect = mask.getBoundingClientRect();
let targetRect=target.getBoundingClientRect();;
let clip='rect('
+(maskRect.top-targetRect.top)+'px,'
+(maskRect.left+maskRect.width-targetRect.left)+'px,'
+(maskRect.top+maskRect.height-targetRect.top)+'px,'
+(maskRect.left-targetRect.left)+'px)';
target.style.clip=clip;
};
demo

Related

CSS - Darken image except for rectangle overlay

I have an <img> for which I want to highlight a certain area as shown below:
I'm trying to figure out a way to create the following effect using just CSS and no JS. I was originally thinking of using an inset border-box, but I need to be able to use percentages for both the location (e.g. top left of the highlighted area is 50% in from left and 80% down from right) and size of box and it appears that border-box can only take px values. I could use JS to keep resizing everything if the image size changes, but I don't want to do that.
Any ideas?
You can create one div element with img inside. And then use pseudo-element on div that will have large box-shadow, and you can position pseudo-element using position-absolute
div {
position: relative;
overflow: hidden;
display: inline-block;
}
div:after {
content: '';
position: absolute;
bottom: 5%;
left: 20%;
width: 40%;
height: 50%;
box-shadow: 0px 0px 0px 2000px rgba(0, 0, 0, 0.6);
}
<div><img src="https://s-media-cache-ak0.pinimg.com/736x/ff/00/5e/ff005e0fa600c51c5e36f6059bbe6053.jpg"></div>
Maybe try creating 4 boxes positioned all sides of the image overlapping as much as you need. Set the boxes color to black with a transparency, and adjust the sizes of them how you like. These boxes would sit ontop of the original image.

Bootstrap 3: Stacking Order in Navbar with Custom Image Styling

I have been trying to port the functionality of a simple rounded image class to work with Bootstrap v3.3.7. Essentially, I nest an img inside of a div, and apply an inset border with alpha-transparency. It works great, as seen in this simple jsFiddle:
Rounded Avatar jsFiddle
CSS:
.inset {
width: 48px;
height: 48px;
border-radius: 50%;
box-shadow:
inset 0 0 0 2px rgba(255,255,255,0.6),
0 1px 1px rgba(0,0,0,0.1);
}
.inset img {
border-radius: inherit;
width: inherit;
height: inherit;
display: block;
position: relative;
z-index: -1;
}
And the markup would look like:
<div class="inset">
<img src="http://rs775.pbsrc.com/albums/yy35/PhoenyxStar/link-1.jpg~c200">
</div>
However, when I attempt to use this inside of a navbar in Bootstrap v3.3.7, I am encountering what I presume to be a stacking issue that I just cannot resolve. As in the sample above, I had started out with the img having a z-index of -1, so that it would sit below the div. This puts it underneath the navbar. So, I had assumed that I could simply push up the z-index of the div and the img to a physical value, trying 998 for the img and 999 for the div.
When I do that, however, the div does not show. If I am to push the img below the div, however, I see the div and the border being correct - I just can't get it to display above the image. I've create a minimal example of this behavior as a Bootply.
Bootply with Avatar Rounding and Missing Inset Border
I am at a loss to explain this, (and my front-end skills leave a bit to be desired). Can only assume that there is something simple that I have to be missing. Any help would be greatly appreciated.
Thank you, in advance.
By giving it a z-index:-1, you're sending your image below the current stacking context. Because it doesn't have one, and because it doesn't have any parent with a background, it renders as you expect it to, but you shouldn't expect it to. And without giving it a z-index:-1, you can't make it render below its parent. But, again, it's not just below the parent, it's below the stacking context.
Here's what happens when I simply put your example inside: a div with background.
The problem is you want an effect applied to the parent to render above the child.
You could (and should) use either a sibling/child of the child, which could be either an unused pseudo-element of the parent or a pseudo-element of the child. But, since the child is an <img /> tag, it cannot have pseudo-elements, so we'll stick to the parent:
.inset {
width: 100px;
height: 100px;
position: relative;
border-radius: 50%;
}
.inset::after {
width: inherit;
height: inherit;
border-radius: inherit;
box-shadow:
inset 0 0 0 2px rgba(255,255,255,0.6),
0 1px 2px rgba(0,0,0,0.1);
position: absolute;
top: 0;
left: 0;
content: '';
}
.inset img {
border-radius: inherit;
width: inherit;
height: inherit;
display: block;
}
<div style="background-color: red;">
<div class="inset">
<img src="http://rs775.pbsrc.com/albums/yy35/PhoenyxStar/link-1.jpg~c200">
</div>
</div>
This will work anywhere you place it, regardless of current z-index or stacking context.
The other option, if you insist on Doing-it-wrong™ would be to wrap the current parent inside an element that would create a new stacking context
position:relative;
z-index: 0;
... but I find it harder to maintain, bloated and probably confusing for anyone not familiar with how stacking contexts work. This technique comes in handy when adding color overlays to images (instead of using an extra element, you just use the wrapper background). But, again, it confuses people so it shouldn't be used.

Z-index "stacking" in creating a shadow

I have the following HTML structure that I'd like to keep nested:
<div class="parent">
<div class="shadow">Shadow here!</div>
</div>
CSS:
.parent {
background: blue;
z-index: 2;
height: 200px;
}
.shadow {
background: lightgrey;
z-index: 0;
opacity: 0.4;
position: relative;
top: 194px;
}
Essentially, I want the div .shadow to be underneath the div .parent. If you look at the rendering at the below link, you can see that part of the parent's blue background goes through the shadow; instead, I'd like the parent element to cover that overlapping part (stacked on top of, I guess you can say):
https://jsfiddle.net/9ya7kb67/
How could I do this? I'm fiddling around with z-index, but that isn't helping.
This is simple... give your shadow z-index property negative value like this. You can also manage z-index by giving higher value. Or you can use box-shadow property to make shadow.
.parent {
background: blue;
z-index: 2;
height: 200px;
}
.shadow {
background: lightgrey;
z-index: -1;
opacity: 0.4;
position: relative;
top: 194px;
}
You can use box shadow with CSS3 https://css-tricks.com/snippets/css/css-box-shadow/
CSS box-shadow works by creating a shadow behind an element. Thus, the element is already on top of the shadow. Here, z-index is not required.
the code:
.parent {
background: blue;
height: 200px;
box-shadow: 0 8px 0 4px light grey;
}
is doing the same thing regardless if you include z-index or not.
I'm not sure if it is the answer you are looking for, but you can always use the CSS property box-shadow on your parent to get a 'shadowy' effect.
See this fiddle here
You can find box shadow generators online to make your life easier, such as this one
.shadow requires a negative z-index value as z-index is inherited from it's parent and is comparatively displayed. z-index:0; gives it the same overall z-index the parent has, and as the child was declared after the parent, it is therefore on top. This means that by setting it to z-index:-1 you are placing the shadow behind the parent.
However, if you simply want a box-shadow I would recommend actually using CSS3 Box Shadows instead of creating additional elements.
Because the shadow is INSIDE of the parent, there is no way to make the parent appear on top of whatever is inside. This is because in CSS a child element inherits the z-index of its parent as a BASE z-index. It can have its own z-index but it can never be less than its parent.
EDIT: Box shadow solution -
https://jsfiddle.net/9ya7kb67/3/
.parent {
background: blue;
z-index: 2;
height: 200px;
box-shadow: 0 8px 0px 4px lightgrey;
}
EDIT2: I stand corrected about it being impossible to put a child below a parent. You CAN actually do this, but that is assuming that the parent does not already have a z-index set. If the parent has a z-index, THEN it is impossible to place it on top of a child.

Shadow not showing up

I am trying to place shadows in one of the div and it's not showing up. Here is one div where I am trying to implement the shadow:
#intro {
padding: 0px;
margin: 0px auto;
width: 100%;
float:inherit;
overflow: hidden;
height: 800px;
position:inherit;
background-color: #00b3e1;;
box-shadow: 0 0 50px rgba(0,0,0,0.8);
}
Here is the URL: http://rachelchaikof.com/awareness/
The reason you can't see the shadow is because the next div (#one) is directly below it, and the shadow is rendering beneath #one. Remove the background image from #one and the shadow becomes visible.
Add this to #intro's CSS to make the shadow visible:
position: relative;
z-index: 10;
If you want shadows visible on the other text areas, you'll need to adjust their z-index values as well, with the top element (#intro) having the highest value.
Another scenario which I had today. Box-shadow was not showing up in spite of setting position relative to the div. Apparently there was no content next to this div which had box shadow.
Once the content was added, box-shadow showed up.

How to make an element with variable width always stick out a specific width?

I have a div that has a variable width, depending on its content. I want to use it for a menu bar that slides in from the side of the page when the user clicks it, so it has to stick out. I want it to stick out exactly 16px (because the arrow image has that size), no matter how wide it actually is.
How can I realize that without using JavaScript?
EDIT:
Thanks for your answers! But it came to my mind that I could do it just like I did with the navbar on that site – modify the width instead of sliding it in.
See here: http://dev.mezgrman.de/tagwall/
The easiest way to do that is to add another class to your menu item when it is collapsed and set another width there and a text indent like so (instead of write again all your css in a new class)
.collapsed {
width: 16px;
text-indent: -9999px;
background: url("/images/arrow_left.png") no-repeat scroll right center rgba(0, 0, 0, 0.85);
}
Now the only thing you have to do in javascript is to add and remove that class depending on the user's click. (You won't get rid of javascript. because css doesn't know when you click an element)
http://jsfiddle.net/LruWn/
No matter how long the .box is, it will always overlap the .container only by exactly 16px:
html:
<div class="container"><div class="box">text</div></div>​
css:
.container {
position: relative;
outline: 1px solid red;
width: 100px;
height: 100px;
}
.box {
width: 70px;
position: absolute;
left: 100%;
margin-left: -16px;
outline: 1px solid black;
}​
Add overflow: hidden; to .container to see how it might look like in action.
I solved my problem by modifying the width of my element now. Silly me.