I am working on a website on which we have use parallax effect. In that there are some images which are triangle shaped, like this
& the image is transparent because it overlaps the above DIV. I am trying so many things with css. but didn't get the desired result. I achieve that desired result with fixed width. Check this http://jsfiddle.net/eJ7Sf/2/ but does not work with fluid width. Check what i still try but didn't work
http://jsfiddle.net/ceGGN/3/
http://jsfiddle.net/eJ7Sf/1/
NOTE: i know about css3 MASK property but it's not work in firefox previous browsers. I want the capability till firefox 3.6.13
Updated Answer (Pure CSS3)
Extreme requirements sometimes need extreme solutions. I've built upon my original answer (below) to make a pure css solution that works (and can be made to work better, if you want to invest the time in it). The current example is a bit "choppy" in rendering, which may be okay for you, but if not, you will need to extend the already obscene number of #media queries that are driving it (it would likely be much easier to implement using LESS or SASS; I put together a formula driven excel spreadsheet to help rapidly generate the numbers). It uses this code:
HTML
<div class="box">
<img src="yourImage.jpg" />
</div>
CSS
.box{
height:300px;
min-width: 100px; /*could be different, but you ought to have some min*/
overflow:hidden;
}
.box img {
height: 100%;
width: 100%;
-ms-transform-origin: 100% 100%; /* IE 9 */
-webkit-transform-origin: 100% 100%; /* Safari and Chrome */
-moz-transform-origin: 100% 100%; /* Firefox */
-o-transform-origin: 100% 100%; /* Opera */
transform-origin: 100% 100%;
}
/*Sample... you need alot of these--my fiddle example has 51*/
#media screen and (min-width: 100px) {
.box {
-ms-transform:skewY(45deg);
-moz-transform:skewY(45deg);
-webkit-transform:skewY(45deg);
-o-transform:skewY(45deg);
transform:skewY(45deg);
}
.box img {
-ms-transform:skewY(-90deg);
-moz-transform:skewY(-90deg);
-webkit-transform:skewY(-90deg);
-o-transform:skewY(-90deg);
transform:skewY(-90deg);
}
}
Here's how to calculate the degrees
Assuming height: 300px with the narrow side approximately 100px tall and equal angles on the trapezoid. This means the upper and lower offset is (300px - 100px) / 2 = 100px. Then the .box angles are set off the #media query min-width amounts according to this formula:
Angle = arctan(100/min-width) /*100 is the upper/lower offset as above*/
For the .box img angle take the Angle and multiply by -2. So that will yield your .box and .box img media queries and transforms as this pseudocode:
#media screen and (min-width: [your target min-width]) {
.box {transform: skewY(Angle)}
.box img {transform: skewY(-2*Angle)}
}
How smooth it functions depends completely upon how micro scale you make your changes to min-width to get a new angle setting. As I stated in my comment in the CSS code above, my example uses 51 media query calls and still has some choppiness to it.
Would it be better to use some javascript solution instead... probably, but that is totally up to the designer, so I offer this here as purely a proof of concept that it can be made to work with pure css.
Original Answer
This seems to be achieving a fluid width. I don't know how much control you want of either how much or what part of the image is being shown, so it may not entirely fit your needs, but it does make a flexible width image using transforms to give it a fake perspective look.
With SVG clip-path
You can achieve this shape with an inline SVG and the clipPath element :
<svg viewbox="0 0 10 6.7">
<defs>
<clipPath id="clip">
<polygon points="10 0, 10 6.7, 0 4.7, 0 2" />
</clipPath>
</defs>
<image xlink:href="http://i.imgur.com/RECDV24.jpg" x="0" y="0" height="6.7" width="10" clip-path="url(#clip)"/>
</svg>
With CSS clip-path
You can also achieve this shape with the CSS clip-path property. Browser support is pretty low (see canIuse) but it is an easy approach.
Here is an example :
img{
-webkit-clip-path:polygon(0% 20%, 100% 0%,100% 100%,0% 80%);
clip-path:polygon(0% 20%, 100% 0%,100% 100%,0% 80%);
}
<img src="http://i.imgur.com/5NK0H1e.jpg" alt=""/>
If you play a bit with pseudoelements and 2DTransforms (supported since Firefox 3.5) you can achieve this effect : http://jsfiddle.net/fcalderan/T5KPA/1/
I tried on Chrome and Firefox. For Opera and IE9 you need to add proprietary prefixes.
Markup is really essential:
<figure class="triangle">
<img src="http://cssglobe.com/lab/angled/img.jpg">
</figure>
and the css to obtain this effect:
img { display: block; }
.triangle {
position : relative;
overflow : hidden;
padding : 0; margin: 0;
display : inline-block;
}
.triangle:after,
.triangle:before {
content : "";
position : absolute;
z-index : 2;
left : -50%;
width : 200%;
height : 40%;
display : block;
background : #fff;
}
.triangle:before {
top: -24%;
-webkit-transform: rotate(-10deg);
-moz-transform: rotate(-10deg);
transform: rotate(-10deg);
}
.triangle:after {
bottom: -24%;
-webkit-transform: rotate(10deg);
-moz-transform: rotate(10deg);
transform: rotate(10deg);
}
I coloured the rotated pseudoelements using white but, of course, you can change the color so it fits the real background color
Related
I cant get the css perspective property to behave the same across different screen sizes, as I cant understand to what to base the value given to it?
For example if I have some flipping effect on mobile and I give perspective:1000px and its looking good
but when i give the same value on desktop than the effect look different a bit and I just "guessing" the right value depending on screen size.
here is simple jsfiddle example
<div class=container>
<div class='e'>
</div>
</div>
css:
.container{
perspective:1000px;
width:100vw;
height:100vh;
}
.e {
height: 80%;
width: 80%;
background-color: red;
transform-style: preserve-3d;
transition-duration: 1s;
transform-origin: 100%;
}
.e:hover {
transform: rotateY(180deg) translateZ(0);
}
is there any formula to get the right perspective value to look the same in all screen sizes?(it does not accept percentage only pixels)
Using calc() is also an option.
In a project, I wanted to tweak the perspective. After identifying what looked good on the smallest mobile resolution and what looked good on the largest mobile resolution, I used calc to create a simple scale that grows with the viewport:
perspective: calc( 720px + ( 100vw - 320px ) * 7 );
.container{
perspective:100vw;
width:100vw;
height:100vh;
}
set perspective to 100vw to always have perspective = to your screens width.
Credit to #vals in the comments above
I'm trying to create a responsive triangle div which will sit at the top of the page as a header.
I was able to achieve that with the following code:
div{
width: 0;
height: 0;
border-style: solid;
border-width: 200px 400px 0 0;
border-color: #007bff transparent transparent transparent;
}
<div></div>
The problem is that I want this triangle to be responsive to the width of the page and change proportionally in height as well.
I tried setting width and height to percent based, however that produced a really small triangle which you can see here:
http://jsfiddle.net/Ltbzkq0e/1/
How to make the borders work with percent without having to use webkits? Is that possible, if not how do I achieve this effect with webkits?
EDIT:
I would also like to fit content in this div. At the moment the only way I can think of is to use absolute positioning and set height to -20px, etc... Is there a better way of accomplishing this?
You can use transform-rotate and a pseudo element to create a responsive triangle. This technique is detailed here : CSS triangles with transform rotate.
For your specific case it could look like this :
DEMO
.tr{
padding-bottom:30%;
position:relative;
overflow:hidden;
}
.tr:before{
content:'';
position:absolute;
top:0; left:0;
width:120%; height:100%;
background-color : #0079C6;
-webkit-transform-origin:0 100%;
-ms-transform-origin:0 100%;
transform-origin:0 100%;
-webkit-transform: rotate(-17deg);
-ms-transform: rotate(-17deg);
transform: rotate(-17deg);
}
.content{
position:absolute;
}
<div class="tr">
<div class="content"> ... CONTENT HERE ...</div>
</div>
If you need IE8 support, you will need to use a JS fallback. This answer describes a way to achieve it.
if you don't care about IE8 and recent Android support — and since you need to have border-width proportional to the page size — you can use viewport-based (vw and vh) units
e.g.
border-width: 100vw 100vh 0 0;
example fiddle: http://jsfiddle.net/swbfqemr/
I have jquery adding a class to scale a div in certain circumstances:
$("#wrapper").addClass("doubleDiv");
The css:
.doubleDiv{ transform: scale(2); }
#wrapper { height:100%; position:relative; z-index:2; background-color:#111; max-width:320px; min-height:480px; margin:0px auto; }
It doubles the size fine, but the div's content gets shifted up past the top of the browser, so that roughly the top third is hidden, and you cannot scroll further up.
I removed each of the styles in #wrapper one by one, until none were left. Apart from screwing up the layout, the same thing happens when the scale is carried out.
To eliminate the chance of any of my jquery causing it, I coded
alert('stop1');
$("#wrapper").addClass("doubleDiv");
alert('stop2');
The display problem occurs before 'stop2' is displayed.
What else could cause this?
Cheers
Demo
you need to add transform-origin
css
.doubleDiv {
transform: scale(2);
transform-origin:50% 0%;
-ms-transform-origin:50% 0%;/* IE 9 */
-webkit-transform-origin:50% 0%; /* Chrome, Safari, Opera */
}
Transform scale scales the object in place. Use transform-origin to define from where you want to scale. Because scaling does not affect positioning at all and the default scaling point is center centre, it moves off screen. Check the follow code to see how it solves this problem:
<html>
<head>
<style type="text/css">
#div {
-webkit-transform: scale(2,2);
-webkit-transform-origin: top left;
background: red;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div id="div"></div>
</body>
</html>
I am trying to create a box with a jagged edge, that can actually be used as a HTML element should be, and can resize etc.
Finally got my head around border-image, got it looking nice, and then when I rotate it, it gets a gap between the border-image and the main fill:
I googled it, and found an answer on SO telling someone to set
-webkit-backface-visibility: hidden;
This cleared it up, but obviously only in webkit browsers.
I tried using -moz-backface-visibility as well, but it didn't clear the issue up in Firefox.
Any suggestions?
jsFiddle
e: I actually thought I may be able to fix it by setting a background color, and then setting the background-clip to padding-box, but honestly it just left me in the same position.
One trick that fixes the problem both in Webkit and FF is setting perspective (instead of backface visibility)
.box.one {
-webkit-transform: perspective(999px) rotate(1deg);
-moz-transform: rotate(1deg);
-ms-transform: rotate(1deg);
-o-transform: rotate(1deg);
transform: perspective(999px) rotate(1deg);
}
fiddle
Adding an after pseudo class with negative margin seems to fix the Firefox issue.
.rough:after {
content: "";
display: block;
margin: -1px;
height: 302px;
background: black;
}
Fiddle demo: http://jsfiddle.net/Wkk7W/3/
Note that the display:block seems to be an essential part of my hack/fix.
Update: Depending on your plans for content inside the div, that exact example might not suit. However, I think the concept could be tweaked depending on your requirements - e.g. using a 3px wide black border instead of a background fill, and using position:absolute to allow other text to be layered on top of the box.
Gonna answer myself, because this solution actually covers my needs of it being "as a html element should be, and can resize etc", even though I developed this solution from Grants answer.
http://jsfiddle.net/Wkk7W/6/
Set the element to position:absolute, then give it a pseudo element with:
content: "";
display: block;
position: absolute;
top: 0;
width: 102%;
margin: -1px 0 0 -1%;
height: 102%;
background: black;
z-index: -1;
This way it keeps the elements width and height, z-index: -1 to put it behind the text. It might not require the display:block, i didn't check.
There are still a few tiny gaps but they are basically impossible to cover and I am happy with it the way it is.
Suppose I've the background like this:
And the normal repeated background look like this:
Now I want the inversely repeated of background-image should look like this:
And even if possible again repeat inversely like this:
Anyway the third option is unnecessary coz we can make like that by taking repeated image
Try this type of image so u get cross related look like you mentioned on last picture
I don't think there is a proper way with css.
The easiest might be for you to edit your picture like this:
and repeat it then.
It is possible to add multiple background to one div. However there is no background transform property, thus not support the multiple backgrounds.
I'm not sure if this would work for you, but you can use :after psuedo-class for this:
div
{
width: 400px;
height: 200px;
background: url('http://placehold.it/400x200');
background-repeat: no-repeat;
position: relative;
-webkit-transform: scaleY(-1);
transform: scaleY(-1);
}
div:after
{
content: '';
display: block;
position: absolute;
width: 400px;
height: 200px;
background: url('http://placehold.it/400x200');
background-repeat: no-repeat;
-webkit-transform: scaleY(-1);
transform: scaleY(-1);
top: -200px;
}
Where the second background is re-inverted and the first background inverted. Of course you can edit this to your wishes.
jsFiddle 2 backgrounds
Where you can even do this with 3 backgrounds
jsFiddle 3 backgrounds
I hope you can work with this!
it is posible to flip the image once with CSS, However css3 does not support(afaik) doing alternating repeated background image.
You will have to use the second image that you posted as the background. This conatins the whole pattern that you want repeated.
For Firefox only you could use -moz-element
<div class="repeated-background"></div>
<div id="mybackground">
<img src="myimage.png" />
<img src="myimage.png" class="vflip" />
</div>
css:
.repeated-background {
background: -moz-element(#mybackground) repeat;
}
.vflip {
-moz-transform: scaleY(-1);
-o-transform: scaleY(-1);
-webkit-transform: scaleY(-1);
transform: scaleY(-1);
}
But that's not really a sane way to approach things :)