Is it possible to cut a triangle from a <div> like in the picture below?
The background is actually not just colour, but in my case is a blurred picture, so I can’t simply cover the green <div> with a brown triangle image. Is there some other CSS way to achieve this effect? Thanks.
The illusion of it is possible: http://jsfiddle.net/2hCrw/4/
Tested with: IE 9, 10, Firefox, Chrome, Safari on PC and iPad.
::before and ::after pseudo elements are skewed to provide a side of the triangle each.
Wrapper used for clipping skewed pseudo elements. You may be able to avoid this by using your outer container as the wrapper.
Elements can still be styled with borders, shadows, etc.
Anything underneath will show through properly.
Demo with borders and drop shadow: http://jsfiddle.net/2hCrw/8/
This demo also adds a tweak for iPad with Retina to prevent a gap between the element and the pseudo elements (either caused by drop shadow bleed or sub-pixel rendering behavior).
<div id="wrapper">
<div id="test">test</div>
</div>
#wrapper {
overflow: hidden;
height: 116px;
}
#test {
height: 100px;
background-color: #ccc;
position: relative;
}
#test::before {
content:"";
position: absolute;
left: -8px;
width: 50%;
height: 16px;
top: 100px;
background-color: #ccc;
-webkit-transform: skew(-40deg);
-moz-transform: skew(-40deg);
-o-transform: skew(-40deg);
-ms-transform: skew(-40deg);
transform: skew(-40deg);
}
#test::after {
content:"";
position: absolute;
right: -8px;
width: 50%;
height: 16px;
top: 100px;
background-color: #ccc;
-webkit-transform: skew(40deg);
-moz-transform: skew(40deg);
-o-transform: skew(40deg);
-ms-transform: skew(40deg);
transform: skew(40deg);
}
As an alternative, you can use a transparent image and "extend" the element above it with pseudo elements. I have answered a similar question regarding a circle cut from an element and show support options down to IE7 (as well as future options for true clipping/masking in CSS).
You can do something like this with CSS masks (examples):
http://codepen.io/anon/pen/vgbEH (anti-triangle mask)
http://codepen.io/anon/pen/pEufn (triangle mask)
I used clip-path: polygon(…) property but only my Chrome seems to support it; you could instead create polygon images and reference them with mask-image for broader support.
It isn't possible to cut from divs in css, but it is possible to use an image overlaying the div to make it look like it has been cut.
.triangle{
background-image: url('cut.png');
width: 24px; height: 24px;
z-index: 1;
position: absolute; top: 32px; left: 15px;
}
It looks like there’s a bit of a drop shadow on your <div> as well, which I’m guessing the triangle should respect.
CSS doesn’t currently provide a way to achieve this directly. One approach would be to create an image of the green bottom area of the <div> with the triangle cut-out in it (using e.g. Photoshop), set it as the background of a <div> inside your original <div>, and position it outside of your original <div>.
Here’s a JS Fiddle example that hopefully explains the idea:
http://jsfiddle.net/7y6nz/
Related
I am fiddling around with making a parallax website, have been following the steps from Keith Clark to slowly get to know parallax. However, I stumble upon an issue that looks like a Firefox issue? On load, the first layer and the darkseagreen background layer are cut in half. If I change the translateZ property from -1px to 0, everything is loaded correctly but then the parallax effect isn't working anymore.
If I scroll down or adjust the size of my browser, the rest is visible, but I would like to have it visible on the initial load.
Link to codepen I'm using Firefox 80.0.
.parallax {
perspective: 1px;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
}
.parallax__layer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
padding: 100vh 0;
width: 100%;
}
.parallax__layer--base {
transform: translateZ(0);
}
.parallax__layer--back {
transform: translateZ(-1px);
background-color: darkseagreen;
width: 100%;
}
.title {
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
color: #fff;
text-align: center;
}
In my case it was an inherited 'overflow: hidden' that clipped the content in FireFox unnecessarily.
Reading through the original docs I found:
One important rule to keep in mind when grouping elements is, we
cannot clip the content of a group. Setting overflow: hidden on a
parallax__group will break the parallax effect. Unclipped content will
result in descendant elements overflowing, so we need to be creative
with the z-index values of the groups to ensure content is correctly
revealed/hidden as the visitor scrolls through the document.
With this you have to deal with the z-index cleverly, which is kind of an issue in my approach, but I can handle it.
In particular by using intersection observers. Once a certain intersection is reached the z-index is flipped. This makes the entire code more complex, which is what I'm afraid of, but it seems to work.
I have a set of three elements set up roughly like this:
HTML
<div class="background">
<div class="image">
<img src="img.png"/>
<div class="text">
<span>Text</span>
</div>
</div>
</div>
Assume that I have simple CSS to layer them with the background being third, the image being second, and the text being first in terms of distance from viewer, and that they're all centered with respect to the topmost container. The background has a background-image, and all images and div elements are transparent.
What I'm trying to do is to have them scroll at slightly different speeds relative to the viewport. I'm not particularly familiar with CSS transform and perspective properties, but I was wondering whether this is possible in just CSS. Any help?
Sure is! You're describing parallax, done with pure CSS. Here's an example that might help. CSS-tricks also describes this in depth, here
The key is setting transform to reflect the distance from the user.
.parallax {
perspective: 1px;
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
}
.parallax__layer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.parallax__layer--base {
transform: translateZ(0);
}
.parallax__layer--back {
transform: translateZ(-1px);
}
My code should do the following:
A background big semi-black bar that is rotated 5 degrees.
JSFiddle
<div class="con-fluid"><div class="_978y"></div></div>
.con-fluid{
width: 100%;
}
._978y{
background-color: #2d2d2d;
width: 200%;
height: 296px;
position: absolute;
-webkit-transform: rotate(5.13deg);
-moz-transform: rotate(5.13deg);
-ms-transform: rotate(5.13deg);
-o-transform: rotate(5.13deg);
transform: rotate(5.13deg);
}
/*as you can see, the rotation code is universal to make sure it works on almost all browsers/
The issue: it's not filling on both its ends. You can see it clearly in the code preview.
what I want is basically filling this empty area so it doesn't look off the chart. Any thoughts?
Thanks in advance.
I don't quite understand your query, however, I think you are asking about the missing "triangle" on the top right of the rectangle. It is there because you rotated the rectangle.
To hide it, put margin-top: -10px; within the css for ._978y, moving the rectangle up 10px.
I created a featured block with HTML5 and CSS3. This block includes a background-image and some text heading. You can see it live here: http://codepen.io/anon/pen/yNWxBb
As you can see I am now using margin-top to center the text in the vertical middle of the block. And make use of the pseudo-class ::after to add a transparant dark overlay above the background-image.
I know you can vertical align a div using table in combination with table-cell and vertical-align: middle, but than it messed my markup.
Does anyone know how to fix this? And is this the right markup to do this? Or should you recommend an other markup and manner to add the transparant background to the image?
Look out to you answer/advice.
Thank you in advance.
Two possible solutions to your problem:
http://www.smashingmagazine.com/2013/08/absolute-horizontal-vertical-centering-css/
http://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/
And yes, you might want to alter the markup in order to make this possible but both articles I'm pointing you to come with example code.
I do believe this is your solution. Just replace this class in your css and it will work fine I guess.
.features figcaption header {
position: absolute;
top: 50%;
-moz-transform: translateY(-50%);
-ms-transform: translateY(-50%);
-o-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
margin-left: auto;
margin-right: auto;
font-size: 24px;
line-height: 34px;
color: #FFF;
//position: absolute;
//top: 28%;
}
I am trying to set a background image on an img tag. Is this possible?
My HTML:
<div id="content">
<img src="my-image.jpg" alt="image" class="img-shadow">
</div>
My CSS:
#content img {
float:right;
margin:0 0 15px 15px;
border:4px solid #ffffff;
}
.img-shadow {
background-image:url('img-shadow.png');
background-repeat:no-repeat;
background-position:0 232px;
}
Using Chrome's "Inspect Element" I can see the path to the background is correct. It's just not showing up in the browser. Below is the desired effect I am going for. By the way. the foreground image dimensions are 258x258 (with border) and the background-image dimensions are 258x40.
An image with no transparency and no padding will cover up its own background image. Images having background images do work, provided there's some gap for the background image to show through.
Adding a padding around the image will suffice, if you just want the background image to show around the image. You can then set a negative margin of the same size, if you don't like the padding taking up space.
Setting the background position to something other than 0 0 will NOT suffice; no matter what the background position is set to, the background will never extend beyond the area taken up by the element (including padding, but excluding border and margin).
Here's a solution using a container element and CSS :after
Demo fiddle
HTML
<div id="content">
<div class="img-container">
<img src="http://placehold.it/258x258" alt="image" class="img-shadow" />
</div>
</div>
CSS
#content img {
border:4px solid #ffffff;
vertical-align: top;
}
.img-container{
position: relative;
display:inline-block;
}
.img-container:after {
content: url('http://placehold.it/258x40');
display: block;
position: absolute;
top: 100%;
right: 0;
}
UPDATE
And using CSS3 box-shadow
Demo fiddle
.img-container:after {
content: '';
display: block;
position: absolute;
top: 90%;
right: 0;
height: 20px;
width: 258px;
-webkit-transform: rotate(3deg);
-moz-transform: rotate(3deg);
-o-transform: rotate(3deg);
-ms-transform: rotate(3deg);
transform: rotate(3deg);
-moz-box-shadow: 0 10px 10px rgba(0,0,0,0.5);
-webkit-box-shadow: 0 10px 10px rgba(0,0,0,0.5);
box-shadow: 0 10px 10px rgba(0,0,0,0.5);
z-index: -1;
}
YES you can put a background image to an image.
.your-image{
padding-bottom:18px; /* <-- height of texture image */
background:transparent /* <-- to manage some-transparent.png don't set a bgColor */
url(txtr-bottom-shadow-divider.png) /* <-- Your bottom right texture */
no-repeat /* <-- */
100% /* <-- align right */
100% /* <-- align bottom */
scroll /* <-- avoid Yoda trolling for spam abuse. Joke */;
}
You noticed the padding? It is to display the background-texture, otherwise, the image will take 100% of available space (width and height) so you won't see anything.
yup. just make sure you set some padding so the background-image will peek through; demo: http://jsfiddle.net/jalbertbowdenii/p6fm4/1/
updated fiddle by removing all the padding. to get the desired "peek" effect set the two corners you are peeking out of with a little bit of padding and the others to 0. like so: http://jsfiddle.net/jalbertbowdenii/p6fm4/5/
Yes,
Here is the easiest way to do it:
In your CSS file
body
{
background-image:url('img-shadow.png');
background-repeat:repeat-x;
background-position:center;
background-size: 100% 100%;
}
It will set it for every page of your site
You do not need to use an IMG tag for background images. It is the best form to set your background image in the css, so that you can easily layer items on top of it. With a IMG tag you need to make sure that everything you place ontop of it is absolute positioning, so it doesn't move. This is a huge pain. Good Luck