I only want to hide elements thats overflowing on the top. In my document blue is the background img and the child is skewed div.
fiddle
<div class="parent">
<div class=child>
</div>
</div>
.parent {
margin: 50px;
height: 200px;
width: 200px;
background: red;
}
.child {
height: 200px;
width: 200px;
background: blue;
transform: skewY(20deg);
-webkit-transform: skewY(20deg);
-moz-transform: skewY(20deg);
-o-transform: skewY(20deg);
}
Need to add another parent with overflow: hidden:
.ovh-parent {
overflow: hidden;
}
.parent {
margin: 50px;
height: 200px;
width: 200px;
background: red;
margin-top: 0;
}
.child {
height: 200px;
width: 200px;
background: blue;
transform: skewY(20deg);
-webkit-transform: skewY(20deg);
-moz-transform: skewY(20deg);
-o-transform: skewY(20deg);
}
<div class="ovh-parent">
<div class="parent">
<div class=child>
</div>
</div>
</div>
Does this work out for you? See the preview below...
Preview
You would need to add another div outside of the parent that would help with hiding the overflow.
.overflow{
margin: 20px;
overflow: hidden;
height: 300px;
width: 200px;
position: relative;
}
.parent {
height: 200px;
width: 200px;
background: red;
position: absolute;
top: 0;
}
.child {
height: 200px;
width: 200px;
background: blue;
transform: skewY(20deg);
-webkit-transform: skewY(20deg);
-moz-transform: skewY(20deg);
-o-transform: skewY(20deg);
}
<div class="overflow">
<div class="parent">
<div class=child>
</div>
</div>
</div>
You don't need to add another ancestor if you use this trick:
Add overflow: hidden to hide overflow
Increase the height to avoid clipping the overflow at the bottom
Add a negative margin to prevent the previous step from affecting following contents
Use a background image instead of a background color. For example, use a gradient.
Use background-size to set the size of that background image. Do not repeat the background.
.parent {
overflow: hidden;
margin: 50px;
height: 300px;
margin-bottom: -100px;
width: 200px;
background: linear-gradient(red, red) no-repeat;
background-size: auto 200px;
margin-top: 0;
}
.child {
height: 200px;
width: 200px;
background: blue;
transform: skewY(20deg);
-webkit-transform: skewY(20deg);
-moz-transform: skewY(20deg);
-o-transform: skewY(20deg);
}
<div class="parent">
<div class="child"></div>
</div>
Lorem ipsum
Related
I am making a design using CSS like this image.
Here is a parent element having 3 child elements. How can I make All child elements are overlapping one another without using: position: absolute; in 3 children. How can I make same pattern using position: static;.
.container {
width: 370px;
height: 370px;
margin: calc(50vh - 185px) auto;
background-image: radial-gradient(#fff, #fff, #fff, #eee, #ddd, #ccc);
}
.outer-flower {
width: 80px;
height: 350px;
border: 1px solid #f00;
position: absolute;
margin: 10px auto;
left: 155px;
}
.outer-flower:nth-child(1){
transform: rotate(0deg);
}
.outer-flower:nth-child(2){
transform: rotate(120deg);
}
.outer-flower:nth-child(3){
transform: rotate(240deg);
}
<div class="container">
<div class="outer-flower" data-rotate="0"></div>
<div class="outer-flower" data-rotate="120"></div>
<div class="outer-flower" data-rotate="240"></div>
</div>
You just have to center all three outer-flower in the container using
position: relative on .container and apply following css on outer-flower
position: absolute;
top: 50%;
left: 50%;
As they are in the center then you can rotate them accordingly:
No need to rotate the first element
Rotate the second element by 120deg
Rotate the third element by 240deg
body{
height: 500px;
}
.container {
position: relative;
width: 370px;
height: 370px;
margin: calc(50vh - 185px) auto;
background-image: radial-gradient(#fff, #fff, #fff, #eee, #ddd, #ccc);
}
.outer-flower {
width: 80px;
height: 350px;
border: 1px solid #f00;
position: absolute;
top: 50%;
left: 50%;
}
.outer-flower:nth-child(1) {
transform: translate(-50%, -50%);
}
.outer-flower:nth-child(2) {
transform: translate(-50%, -50%) rotate(120deg);
}
.outer-flower:nth-child(3) {
transform: translate(-50%, -50%) rotate(240deg);
}
<div class="container">
<div class="outer-flower" data-rotate="0"></div>
<div class="outer-flower" data-rotate="120"></div>
<div class="outer-flower" data-rotate="240"></div>
</div>
When you are not using absolute, every element will be either after one another or even in next lines based on the display property. If you have display:inline-block then you can have 3 divs in a single row. Then applying position:relative to the divs in order to move them to a single place and then rotating should be able to meet your requirement. The code is as follows:
P.S: I have changed the dimension values a little bit.
<style>
.container {
width: 250px;
height: 250px;
}
.outer-flower {
width: 78px;
border: 1px solid #f00;
height: 240px;
display:inline-block;
}
.outer-flower:nth-child(1){
transform: rotate(120deg);
position: relative;
left:80px;
}
.outer-flower:nth-child(2){
transform: rotate(0deg);
}
.outer-flower:nth-child(3){
transform: rotate(240deg);
position: relative;
right:80px;
}
</style>
Output:
To avoid using absolute but to maintain the divs as position static we cannot position them using left and top - static just ignores them.
Instead we can use margin positioning and no CSS position property need be set on any of the elements.
This snippet needs a bit of refinement to get the alignment exactly right (don't forget to allow for the border widths if needed, though box-sizing helps) but is given here as a pointer to a way forward.
body {
width: 100vw;
height: 100vh;
}
.container {
width: 370px;
height: 370px;
margin: calc(50vh - 185px) auto;
background-image: radial-gradient(#fff, #fff, #fff, #eee, #ddd, #ccc);
}
.outer-flower {
width: 80px;
height: 350px;
border: 1px solid #f00;
padding: 0;
margin: 10px auto;
box-sizing: border-box;
}
.outer-flower:nth-child(1){
transform: rotate(0deg);
}
.outer-flower:nth-child(2){
transform: rotate(120deg);
margin-top: -350px;
}
.outer-flower:nth-child(3){
transform: rotate(240deg);
margin-top: -350px;
}
<div class="container">
<div class="outer-flower" data-rotate="0"></div>
<div class="outer-flower" data-rotate="120"></div>
<div class="outer-flower" data-rotate="240"></div>
</div>
Use CSS grid to avoid position:absolute
.container {
width: 370px;
height: 370px;
margin: 20px auto;
display: grid;
background: radial-gradient(#fff, #fff, #fff, #eee, #ddd, #ccc);
}
.outer-flower {
grid-area: 1/1; /* make all of them on the same track so they will overlap */
width: 80px;
border: 1px solid #f00;
margin: 10px auto;
}
.outer-flower:nth-child(2) { transform: rotate(120deg) }
.outer-flower:nth-child(3) { transform: rotate(240deg) }
<div class="container">
<div class="outer-flower" data-rotate="0"></div>
<div class="outer-flower" data-rotate="120"></div>
<div class="outer-flower" data-rotate="240"></div>
</div>
I need to scale the middle div using transform scale(1.3). It works of course but the problem is that after scaling it overlaps neighbor divs. Is it possible to get rid of overlap using just CSS?
This is how it is now looks like:
But I want it this way
.main {
width: 100%;
height: 500px;
background-color: gray;
padding: 100px;
}
.box {
width: 100px;
height: 100px;
background-color: red;
margin: 2px;
display: inline-block;
border: 2px;
border-style: solid;
border-color: black;
}
.scaled-box {
width: 100px;
height: 100px;
-webkit-transform: scale(1.3);
-moz-transform: scale(1.3);
transform: scale(1.3);
display: inline-block;
background-color: green;
opacity: 0.7;
}
<div class="main">
<div class="box"></div>
<div class="scaled-box"></div>
<div class="box"></div>
</div>
.main {
width: 100%;
height: 500px;
background-color: gray;
padding: 100px;
--scale-rate:1.5; /* You can change scale from this line */
}
.box {
width: 100px;
height: 100px;
background-color: red;
display: inline-block;
border: 2px;
border-style: solid;
border-color: black;
}
.box:first-child{
margin-right: calc(((100px * var(--scale-rate)) - 100px) / 2); /* 100px is scaled-box width*/
}
.box:last-child{
margin-left: calc(((100px * var(--scale-rate)) - 100px) / 2); /* 100px is scaled-box width*/
}
.scaled-box {
width: 100px;
height: 100px;
-webkit-transform: scale(var(--scale-rate));
-moz-transform: scale(var(--scale-rate));
transform: scale(var(--scale-rate));
display: inline-block;
background-color: green;
opacity: 0.7;
}
<div class="main">
<div class="box"></div>
<div class="scaled-box"></div>
<div class="box"></div>
</div>
Edited:
margin-rate fixed
transform property does not reflects to the element width or height. Maybe you can use zoom instead. The downside is that Firefox does not support it: https://caniuse.com/?search=zoom
You can add a margin to the scaled box to prevent the overlap.
.scaled-box {
margin: 20px;
width: 100px;
height: 100px;
-webkit-transform: scale(1.3);
-moz-transform: scale(1.3);
transform: scale(1.3);
display: inline-block;
background-color: green;
opacity: 0.7;
}
In your preview, the divs are also aligned vertically which can't happen with a simple margin since they're inline. Use a flex in the main container to align them vertically.
.main {
display: flex;
align-items: center;
width: 100%;
height: 500px;
background-color: gray;
padding: 100px;
}
I need to create a box which has a rotated message on top with border-bottom, however I can't make it responsive. The bottom line should always go from border to border.
.holder {
width: 40%;
background-color: yellow;
height: 400px;
}
.rotated-text {
float: right;
text-align: center;
width: 25%;
border-bottom: 1px solid green;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
}
<div class="holder">
<div class="rotated-text">
<p>Hello! <br> This text is rotated</p>
</div>
</div>
I tried to use position: absolute or some clip-path but it didn't work out. Is should look something like this:
If it is not possible I guess I will have to use an image. Thanks!
I have a sample here. In this one the width and and height of rotated-text div is fixed. But it can stick to top right corner of its container always.
.holder {
width: 80%;
background-color: yellow;
height: 400px;
position:relative
}
.rotated-text {
position: absolute;
text-align: center;
width: 100px;
height: 65px;
border-bottom: 1px solid green;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
right:7px;
top: 26px;
background: red;
}
.rotated-text p{
margin:0;
}
.rotated-text:after{
content: "";
position: absolute;
left: -64px;
right:-63px;
bottom:0;
height: 1px;
background: green;
}
<div class="holder">
<div class="rotated-text">
<p>Hello! <br> This text is rotated</p>
</div>
</div>
Below is the snippet:
.out {
margin: 100px auto;
width: 50%;
height: 100px;
background-color: green;
}
.in {
width: 50%;
height: 50px;
background-color: red;
transform: translate3d(-20%, -40%, 0px) scale(0.7);
position: absolute;
}
<div class="out">
<div class="in">
</div>
</div>
As can be seen, the inner element (red square) goes out of the border/bound of the outer element (green square).
Does anyone have ideas about how to clip the part of inner element which goes out of the border of the outer element?
--
I find overflow: hidden doesn't work well because of the position: absolute property in the inner element..
Add overflow:hidden on the outer element:
.out {
overflow:hidden;
margin: 100px auto;
width: 50%;
height: 100px;
background-color: green;
}
.in {
width: 50%;
height: 50px;
background-color: red;
transform: translate3d(-20%, -40%, 0px) scale(0.7);
}
<div class="out">
<div class="in">
</div>
</div>
you can try this one:
.out {
margin: 100px auto;
width: 50%;
height: 100px;
background-color: green;
overflow:hidden;
}
.in {
width: 50%;
height: 50px;
background-color: red;
transform: translate3d(-20%, -40%, 0px) scale(0.7);
}
DEMO
I'm trying to skew a div, something similar to what https://digital.scotch.io/ has but actually it's not coming out right.
.skew1:before {
content:' ';
display: block;
height: 400px;
width: 100%;
background: black;
padding: 50px 0;
-webkit-transform: skewY(-2deg) translateZ(0);
-moz-transform: skewY(-2deg) translateZ(0);
-ms-transform: skewY(-2deg) translateZ(0);
-o-transform: skewY(-2deg) translateZ(0);
transform: skewY(-2deg) translateZ(0);
position: absolute;
left: 0;
bottom: -10px;
z-index: -1;
overflow: hidden;
}
<section>
<div class="container skew1">
<div class="row">
<div class="col-md-12">
<h2>This is my skew1!</h2>
</div>
</div>
</div>
</section>
Not really sure what I'm doing wrong. This is my JSfiddle. Any help please? Thanks.
what you need is not skewing. you need to set a 3d perspective to the div and rotate it on its Y axis:
.container {
margin-top: 20px;
width: 500px;
height: 150px;
position: relative;
perspective: 800px;
}
.container2 {
margin: 5px 82px;
width: 500px;
height: 150px;
position: relative;
perspective: 800px;
}
.container .inner {
width: 100%;
height: 100%;
display: block;
background: red;
transform: rotateY(-45deg);
}
.container2 .inner {
width: 100%;
height: 100%;
display: block;
background: blue;
transform: rotateY(45deg);
}
<section class="container">
<div class="inner"></div>
</section>
<section class="container2">
<div class="inner"></div>
</section>
The div wrapping the text needed to be position: relative;(check the original code) and also needed some height and padding to align the text vertically. This is the JSFiddle.