Mask a div with css - html

Here is my Fiddle : http://jsfiddle.net/4wd6vmjL/
I want to mask a div to show my image skew . but i dont want image to skew.
now there is a gap in mask and image can't fill all mask .
.mask{
background-image: url('http://www.birds.com/wp-content/uploads/home/bird4.jpg');
height:200px;
-webkit-transform: skew(-16deg);
-moz-transform: skew(16deg);
-o-transform: skew(16deg);
transform: skew(16deg);
}
Any advice ? Thanks

You need to change your css of .mask class.
.wrapper{
display: block;
height:200px;
background: #f8f8f8 none repeat scroll 0 0;
text-align: left;
-webkit-transform: skew(-16deg);
-moz-transform: skew(-16deg);
-o-transform: skew(-16deg);
transform: skew(-16deg);
border-right:medium none;
margin-bottom: 26px;
margin-left: 44px;
overflow: hidden;
z-index: 100;
width:300px;
}
.mask {
background-image: url("http://www.birds.com/wp-content/uploads/home/bird4.jpg");
background-position: center top;
height: 480px;
transform: skew(16deg);
width: 430px;
padding-left: 70px;
}
<div class="wrapper">
<div class="mask">asdassadd</div>
</div>

Just add the image inside another div and counterskew it with the exact opposite. The hardest bit is positioning your div now, which would take some tweaking - depending on the angle your inside div needs to be bigger. I have also positioned it at the center of the wrapping div.
.mask {
position: relative;
left: 100px;
width: 200px;
height:200px;
overflow: hidden;
-webkit-transform: skew(-16deg);
-moz-transform: skew(-16deg);
-ms-transform: skew(-16deg);
transform: skew(-16deg);
}
.mask > * {
position: absolute;
left: 50%;
top: 50%;
width: 370px;
height:370px;
color: #fff;
background-image: url('http://www.birds.com/wp-content/uploads/home/bird4.jpg');
-webkit-transform: skew(16deg) translate(-50%,-50%);
-moz-transform: skew(16deg) translate(-50%,-50%);
-ms-transform: skew(16deg) translate(-50%,-50%);
transform: skew(16deg) translate(-50%,-50%);
}
<div class="mask"><div>This is just text to show your skew is now undone. This is just text to show your skew is now undone. This is just text to show your skew is now undone.This is just text to show your skew is now undone. This is just text to show your skew is now undone. This is just text to show your skew is now undone. This is just text to show your skew is now undone. This is just text to show your skew is now undone.This is just text to show your skew is now undone. This is just text to show your skew is now undone. This is just text to show your skew is now undone.</div></div>

Related

How to get text normally into a distorted shape(parallelogramm)?

I am a bit confused: I want to create some parallelograms and put text into it. But as i put text into it, the text aligns to the shape of the parallelogram. Here is what ive tried
.parallelogram {
width: 130px;
height: 75px;
background: blue;
-webkit-transform: skew(20deg);
-moz-transform: skew(20deg);
-o-transform: skew(20deg);
transform: skew(20deg);
}
<div class="parallelogram">
<div>
<p>Projects</p>
</div>
</div>
So as you can see, i created a Div within a class, that creates the parallelogram. Now if i try to put another p tag as child into the div tag that contains the shape, my text looks not normally. Is there any way to remove the inerhit of the child? So my text would look normaly or is there even a better way? Thanks
.parallelogram {
width: 130px;
height: 75px;
background: blue;
-webkit-transform: skew(20deg);
-moz-transform: skew(20deg);
-o-transform: skew(20deg);
transform: skew(20deg);
}
.parallelogram p{
transform: skew(-20deg);
}
<div class="parallelogram">
<div>
<p>Projects</p>
</div>
</div>
.parallelogram p{
transform: skew(-20deg);
}
I think you trying something like this :
.parallelogram {
width: 300px;
height: 100px;
background-color: green;
transform: skewX(30deg);
transform-origin: top;
margin: 10px;
display: flex;
justify-content: center;
align-items: center;
}
.parallelogram > span {
transform: skewX(-30deg);
}
<div class="parallelogram">
<span>Projects</span>
</div>

Left align skewed text in container

I have to apply skew and rotate on an element. It works fine but the skewed text isn't left aligned in it's container (see the result image):
The text on the left is overflowing the container: the H (from "Hello") and the T (from "The") alignment is not right.
This is what I am trying to achieve:
.skew-parent-wrapper {
width: 300px;
margin-left: 100px;
margin-top: 50px;
border: 1px solid red;
padding-bottom: 30px;
}
.skew-text {
-moz-transform: rotate(-10deg) skew(-30deg, 0deg);
-webkit-transform: rotate(-10deg) skew(-30deg, 0deg);
-o-transform: rotate(-10deg) skew(-30deg, 0deg);
-ms-transform: rotate(-10deg) skew(-30deg, 0deg);
transform: rotate(-10deg) skew(-30deg, 0deg);
}
<div class="skew-parent-wrapper">
<h1 class="skew-text">Hello Welcome to the skew text</h1>
</div>
One way of aligning the skewed text on a vertical line is to manualy set a negative text-indent. This technique also requires to set a transform-origin on bottom left :
.skew-parent-wrapper {
width: 300px;
margin-left: 100px;
margin-top: 50px;
border: 1px solid red;
padding-top: 30px;
}
.skew-text {
transform: rotate(-10deg) skew(-30deg, 0deg);
transform-origin: 0 100%;
text-indent: -15px;
}
<div class="skew-parent-wrapper">
<h1 class="skew-text">Hello Welcome to the skew text</h1>
</div>
This technique works on text that wraps only on two lines. For text with more than 2 lines, you will need to wrap each line in a tag (like a <span>) :
.skew-parent-wrapper {
width: 300px;
margin-left: 100px;
margin-top: 50px;
border: 1px solid red;
padding-top: 30px;
}
.skew-text span{
display:block;
transform: rotate(-10deg) skew(-30deg, 0deg);
transform-origin: 0 100%;
}
<div class="skew-parent-wrapper">
<h1 class="skew-text">
<span>Hello Welcome to the</span>
<span>skewed text with</span>
<span>several lines many</span>
<span>many many lines</span>
</h1>
</div>
Note that you need to set the <span> to display:block because transforms don't apply on inline elements.
With rotation and skew you will get a behaviour like perspective so the oversizing is a result of that.
You need to manually scale down your skew-text and then disable the overflow of its container so you won't see any overlapping text.
That's the only idea for me to fix this.
It's hard to achieve the alignment if you use a rotation as transform.
Use a skew to get the inclined lines. You can (to some extent) get the angle on the letters using italics
Also, there is no need for all the vendor specific tranforms
.skew-parent-wrapper {
width: 300px;
margin-left: 100px;
margin-top: 50px;
border: 1px solid red;
padding-bottom: 30px;
}
.skew-text {
transform: skewY(-20deg);
font-style: italic;
background-color: goldenrod;
}
<div class="skew-parent-wrapper">
<h1 class="skew-text">Hello Welcome to the skewed text that can span several lines</h1>
</div>

Center div on screen that has been rotated and scaled with css3

I have the following jsfiddle:
https://jsfiddle.net/quacu0hv/
I cant figure out how to center this div. The fact that it is rotated makes it hard to actually center the object on screen. How exactly can this be achieved with pure css? I imagine its due to the point of origin that changed its position (upper left vertex of the div).
div {
transform: rotate(-45deg) scale(2) translate(-50%, -50%);
opacity: 1 !important;
top: 50%;
left: 50%;
width: 200px;
height: 200px;
background: black;
position: absolute;
}
Try rearranging the transform values and see what happens ;)
Turns out order does matter. If you think about it, it does makes sense:
Rotate > Scale > Translate
Once you've rotated it, the origin has been rotated too. That's why your square moves "left" and "up" from the origin.
Translate > Rotate > Scale
This is what you want to do. Position before you make any other adjustments that can affect the origin.
Use CSS transform-origin: 50% 50% or try 0 0. Remove position: absolute first.
This is at 0 0
This is at 50% 50%
This is at 45% -290% Centered?
Yeah, looks centered to me, see Full Page. Anyways, as you can see from the other answers transform-origin is the best solution. Scott suggested to remove the transform: -50% -50% which makes perfect sense if you wanted the div centered in the first place, but if you wanted that in there still and have it centered as a square in a rectangle (height is smaller than width), then 45% by -290%.
SNIPPET
.box {
position: relative;
}
.center {
transform: rotate(-45deg) scale(2) translate(-50%, -50%);
transform-origin: 45% -290%;
opacity: 1 !important;
width: 200px;
height: 200px;
background: black;
position: absolute;
}
<div class='box'>
<div class='center'></div>
</div>
You could just remove translate(50%, 50%);
div {
transform: rotate(-45deg) scale(2);
opacity: 1 !important;
top: 50%;
left: 50%;
width: 50px;
height: 50px;
background: black;
position: absolute;
}
<div></div>
Fiddle
or add transform-origin: 0 0; to start transformations in the upper left corner.
div {
transform: rotate(-45deg) scale(2) translate(-50%, -50%);
transform-origin: 0 0;
opacity: 1 !important;
top: 50%;
left: 50%;
width: 50px;
height: 50px;
background: black;
position: absolute;
}
<div></div>
Fiddle
Using transform-origin you can get the result, also scale and rotate goes before positioning.
div {
transform-origin: 0 0;
transform: rotate(-45deg) scale(2) translate(-50%, 50%);
opacity: 1 !important;
width: 100px;
height: 100px;
background: black;
}
<div></div>
jsfiddle: https://jsfiddle.net/quacu0hv/8/

Rotate element and place in corner of parent

I'm trying to position an element in the bottom right corner of it's parent. At the moment, you can see it's in the center, but touching the bottom:
http://jsfiddle.net/03r7gabp/
Ideally, I'd like it to be touching the bottom, flushed all the way to the right (what right: 0; typically does).
This is the desired effect:
Is this possible? I'm already using:
transform-origin: bottom left;
There are couple of solutions to achieve that, however I found this one more flexible.
Due to the fact that you can combine multiple transform notations, you'll end up with:
.info {
position: absolute;
bottom: 0; right: 0;
transform: rotate(-90deg) translateX(100%);
transform-origin: 100% 100%;
}
The key point is that a percentage value on translate() notation is relative to the size of bounding box. I.e. the size of border-box of the absolutely positioned element in this case.
Here is a demo:
.container {
width: 300px;
height: 200px;
border: 1px solid red;
position: relative;
}
.info {
position: absolute;
bottom: 0; right: 0;
background-color: gold;
-webkit-transform: rotate(-90deg) translateX(100%);
-moz-transform: rotate(-90deg) translateX(100%);
-o-transform: rotate(-90deg) translateX(100%);
-ms-transform: rotate(-90deg) translateX(100%);
transform: rotate(-90deg) translateX(100%);
-webkit-transform-origin: 100% 100%;
-moz-transform-origin: 100% 100%;
-ms-transform-origin: 100% 100%;
-o-transform-origin: 100% 100%;
transform-origin: 100% 100%;
}
<div class="container">
<div class="info">
hello this is a sentence
</div>
</div>
Okay, so here's how I've done it!
http://jsfiddle.net/03r7gabp/2/
CSS
.info {
position: absolute;
bottom: 0;
left: 0;
background-color: lime;
transform: rotate(-90deg);
transform-origin: bottom left;
margin-left: 100%;
width: 200px; /* height of container */
}
The only bodge that I'm not happy with is the width having to be a fixed value (equal to the height of the container).
Breakdown
Step 1
Not much to see here, move along!
Step 2
Note that we've specified the origin of rotation to be the bottom left. Imagine you stick a pin in that corner of the <div> and you then spin it about this point.
Step 3
In the previous image our <div> was sat on the outside of the container. We can now adjust its margin to make it appear on the other side. Remember that the percentage specified is a measure of the width of the parent element
Step 4
Now we "bump" the item to the bottom of the container.
Step 5
Finally we have to give our div a width equal to the height of the parent container
An alternative solution would be to set the container to have overflow: hidden; and then put the width of the child element to arbitrarily large. Obviously if the length of sentence exceeded the height of the container, the words would be clipped (e.g. http://jsfiddle.net/03r7gabp/6/)
One solution is to change transform-origin: bottom left; to right and also change frombottom: 0 to top: 35px
.container {
width: 300px;
height: 200px;
border: 1px solid red;
position: relative;
}
.info {
position: absolute;
top: 35px;
right: 0;
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-o-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
transform: rotate(-90deg);
transform-origin: bottom right;
}
<div class="container">
<div class="info">
hello this is a sentence
</div>
</div>

Align rotated div with another div

I have a main div (the red div in the fiddle) that has a smaller vertical tab on the side (the blue div in the fiddle).
The RED div is standard BUT the Blue div is rotated through 90 degrees (as I need to have vertical text in it). This is where the problems starts.
The red div is vertically positioned at 50% so it is in the middle of the page and locked with scrolling etc.
I want to align the blue div so that the top edge of the blue div is at the same Y position as the top edge of the red div.
I would prefer NOT to use jQuery but can do if required.
Desired output :
Fiddle is here : http://jsfiddle.net/kBKf6/
Here is the code I am using :
<div id="main" style="position: fixed; top: 50%; margin-top: -250px; left:0; height: 500px; width: 450px; background-color:red;">
Main Content Div
</div>
<div id="vertical_div" style="overflow:hidden; position: fixed; left:350px; height:40px; width:200px; margin: auto; background-color:blue; text-align:center; color:white; -webkit-transform: rotate(90deg) translate(-50%, -50%); -moz-transform: rotate(90deg) translate(-50%, -50%); -ms-transform: rotate(90deg) translate(-50%, -50%); -o-transform: rotate(90deg) translate(-50%, -50%); transform: rotate(90deg) translate(-50%, -50%);">
Side Tab
</div>
You don't need JS to align the rotated div. You can define a transform origin in CSS then, it becomes easy to align.
Side note : You can remove the -moz- and -o- vendor prefixes see caniuse
DEMO
HTML :
<div id="main">Main Content Div
<div id="verticaldiv">Side Tab</div>
</div>
CSS :
#main {
position: fixed;
top: 50%;
margin-top: -250px;
left:0;
height: 500px;
width: 450px;
background-color:red;
}
#verticaldiv {
overflow:hidden;
position: absolute;
left:100%;
bottom:100%;
height:40px;
width:200px;
background-color:blue;
text-align:center;
color:white;
-webkit-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
-ms-transform-origin:0 100%;
-webkit-transform-origin: 0 100%;
transform-origin: 0 100%;
}
You can also do it without relying on hardcoded sizes that move your div into position, but you need a wrapper around your .verticaldiv
demo:
http://jsfiddle.net/MCr6f/
demo 2:
http://jsfiddle.net/9LtKw/ (to show that different sizes don't matter)
html:
<div class="one">
Hello
<div class="pivot">
<div class="two">
Pretty!
</div>
</div>
</div>
css:
.one {
background: red;
position: relative;
float: left;
/*strange and difficult sizes*/
font-size: 3.237827em;
padding: 10px;
}
.pivot {
position: absolute;
top: 0;
right: 0;
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
transform: rotate(90deg);
width: 0px;
height: 0px;
}
.two {
background: blue;
color: white;
position: absolute;
left: 0;
bottom: 0;
/*strange and difficult sizes*/
font-size: 12px;
padding: 0.3em;
}