Is it possible to absolute position skewed element so its left-bottom corner stay close (0px) to container's border?
#one{
position:absolute;
background-color:darkkhaki;
width:800px;
height:200px;
left:50%;
transform: translateX(-50%)
}
#rect{
position:absolute;
background-color: salmon;
width:400px;
height:200px;
left:50%;
transform: translateX(-50%) skew(-25deg);
}
#marker{
position:absolute;
background-color:red;
width:5px;
height:200px;
left: 200px;
}
<div id="one">
<div id="rect"></div>
<div id="marker"></div>
<div>
I've marked with red line position of the rectangle side before skewing it. I'm looking for a way to position the rectangle, so its left-bottom corner touches the red line and no JS allowed.
I cannot simply use 'left: Ypx', because the whole thing is going to be keyframes-animated (changing skew, rotateX + constant perspective on outer element).
Otherwise, maybe you can suggest another way for making animation: the pictures that are slowly 'getting up' from laying-on-the-table position?
edit:
CODEPEN
You can use transform-origin Property.
For yours case i put <div id="marker"> inside <div id="rect"> and change transform-origin to 0% 100%.
Look at my example:
#one{
position:absolute;
background-color:darkkhaki;
width:800px;
height:200px;
left:50%;
transform: translateX(-50%)
}
#rect{
position:absolute;
background-color: salmon;
width:400px;
height:200px;
left:50%;
transform: translateX(-50%) skew(-25deg);
}
#marker{
position:absolute;
background-color:red;
width:5px;
height:200px;
top: 0px;
left: 0px;
transform: skew(25deg);
-webkit-transform-origin: 0% 100%;
transform-origin: 0% 100%;
}
<div id="one">
<div id="rect">
<div id="marker"></div>
</div>
<div>
And this Working Fiddle
Related
My end goal is a draggable, resizable, Scalable, and rotatable element, just like the example on: https://daybrush.com/moveable/ only by using css width,height, and transform: rotate, translate.
Say I have a div with following css:
.rect {
background-color: red;
width: 200px;
height: 100px;
top:100px;
left:100px;
position: absolute;
transform: rotate(0deg);
}
<div class="rect"></div>
If I want to resize the div horizontally to the left, I just change the width by x pixels. If I want to change it to the right I just change the width by x pixels, and translate(-xpx, 0).
But what if I change the angle? From trying a lot of stuff, I found some of the x and y values for translate to the respective angle, however I feel like there is a more straight forward way than just guessing. E.g: For 90deg, if I want to resize to the left by x px I do translate(-x0.5px, x0.5px).
More: what if I want to change both the width & height at the same time?
P.S.: I would rather avoid using libraries, transform: scale or svg
P.P.S:Example to further demonstrate the problem, just changing the width:
.rect {
background-color: red;
width: 200px;
height: 100px;
top:100px;
left:100px;
position: absolute;
transform: rotate(45deg);
animation: expand 5s infinite
}
#keyframes expand {
from {width: 200px;}
to {width: 2000px;}
}
<div class="rect"></div>
Fixed, stretching the left side of the original rectagle (now up since rotated 90deg):
.rect {
background-color: red;
width: 200px;
height: 100px;
top:100px;
left:100px;
position: absolute;
transform: rotate(90deg);
animation: expand 3s infinite
}
#keyframes expand {
from {
width: 200px;
}
to {
width: 800px;
transform: rotate(90deg) translate(-300px, 300px);
}
}
<div class="rect"></div>
You can apply several transformations to the same object and they will be composed in the order that you specify. Move then rotate, is different than rotate then move.
.rect {
background-color: red;
width: 200px;
height: 100px;
top:100px;
left:100px;
position: absolute;
}
.t1 {
background-color: #40d04080;
/* green shaded rectangle: rotate after translation */
transform: translate(2cm, 0) rotate(30deg);
}
.t2 {
background-color: #f0404080;
transform: rotate(30deg);
}
.t3 {
background-color: #4040f080;
/* blue shaded rectangle: translate after rotation */
transform: rotate(30deg) translate(2cm, 0) ;
}
<div class="rect t1"></div>
<div class="rect t2"></div>
<div class="rect t3"></div>
The inner div has a variable height depends on the length of the text inside, which is always shorter than the outer div. I'd like the inner overlay div to slide up from top:100% to bottom:0 when the outer div is hovered. My CSS code below does not produce the slide up effect I want. It simply pops up the inner div at the bottom of the outer div.
<div class="outer">
<div class="inner">This is a content block.</div>
</div>
.outer {
position:relative;
background-color:#eee;
width:150px; height:150px;
overflow:hidden;
}
.inner {
position:absolute; z-index:10;
box-sizing:border-box; width:100%; padding:10px;
background-color:rgba(0,0,0,0.5); color:#fff;
left:0; right:0; top:100%; bottom:auto;
transition:top 300ms, bottom 300ms;
}
.outer:hover .inner {
top:auto; bottom:0;
}
your code cannot work because transition doesn't work from/to auto.
you could set bottom:0 by default and play with transfom, e.g.
.outer {
position:relative;
background-color:#eee;
width:150px; height:150px;
overflow:hidden;
}
.inner {
position:absolute; z-index:10;
box-sizing:border-box; width:100%; padding:10px;
background-color:rgba(0,0,0,0.5); color:#fff;
left:0; right:0; bottom:0;
transform: translateY(100%);
transition: transform 300ms;
}
.outer:hover .inner {
transform: translateY(0);
}
How can I implement the following shape using CSS?
The right side should be slanted and the top corners should be rounded :
You can use pseudo-elements, border-radius and transform rotate to create the rounded edges and the oblique right part :
output : FIDDLE
div{
display:inline-block;
padding:1em 5em 1em;
position:relative;
overflow:hidden;
border-top-left-radius: 10px;
}
div:after{
content:"";
position:absolute;
top:0; left:0;
width:100%; height:100%;
background-color:#E70101;
z-index:-1;
border-top-right-radius: 15px;
-ms-transform: skewX(10deg);
-webkit-transform: skewX(10deg);
transform: skewX(10deg);
-ms-transform-origin:100% 100%;
-webkit-transform-origin:100% 100%;
transform-origin:100% 100%;
}
<div>Some text</div>
Another option is to use 3d perspective transform: http://lea.verou.me/2013/10/slanted-tabs-with-css-3d-transforms/
I have a div with an inner span with text. This inside span should be centered vertically and horizontally all the time:
http://jsfiddle.net/QW4Wk/
<div>
<span>Text aligned center</span>
</div>
The div has a transition when the mouse is over, which changes its width and height.
div{
width:200px;
height:200px;
background:black;
position:relative;
-webkit-transition:width 10s,height 10s;
}
span {
position:absolute;
color:white;
bottom:0;
right:0;
}
div:hover{
width:250px;
height:250px;
}
However in Chrome (at least) the text looks choppy while the transition is running. I guess this is because the transition goes 1 by 1px and therefor the "center style" has to go back and forward 1px.
Is there someway to fix this to look smoother, something like subpixel?
Thanks.
try this for absolute centering the text..
span {
margin: auto;
color:white;
text-align:center;
height:10px;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
and of course lessen your transition speed.
Apply a different change to the span. a transform can be subpixel
div{
width:200px;
height:200px;
background:black;
position:relative;
-webkit-transition:width 10s,height 10s;
}
span {
position:absolute;
color:white;
top:75px;
left:50px;
width: 100px;
transition: -webkit-transform 10s;
-webkit-transform: translate(0px, 0px);
}
div:hover{
width:250px;
height:250px;
}
div:hover span {
-webkit-transform: perspective(999px) translate(25px, 25px);
}
fiddle
Why when I use position absolute and percentage width I have this glitch when I hover on div above?
There is example. I have this glitch on little more complicated site.
<div class="box"> text </div>
<div class="container">
<div>
.box {
width: 50%;
height: 50%;
background: red;
}
.box:hover {
transition: 0.5s;
-webkit-transform: translate(0, 6px);
}
.container {
position:absolute;
top:40px;
width:40%;
height:50px;
float:left;
background: blue;
color:white;
}
http://jsfiddle.net/TsUEH/
When you hover on red text then width of blue div are shaking.
How can i avoid this without removing percentage and position absolute?
It works fine for me, but if you find an element "shaking" (esp in Chrome), it's likely because of the translate function not working with the z-index correctly
If you need to fix it, you can use this code (lifesaver):
-webkit-backface-visibility: hidden;
-webkit-transform: translateZ(0) scale(1.0, 1.0);