wrong margin size after transforms? - css

I am confused by the size of the margins in this example on chrome (v 56.0.2924.87 as of posting)
<div class='a'><div class='b'></div></div>
Here is the simple css:
.a {
position: absolute;
width: 100px;
height: 400px;
background-color: red;
overflow-y: scroll;
}
.b {
position: absolute;
top: 0;
left: 0;
height: 4000px;
width: 100%;
background-color: pink;
transform-origin: 50% 0%;
transform: scale( 0.25 );
margin-top: 20px;
margin-bottom: 20px;
}
http://codepen.io/jedierikb/pen/EZRZbx?editors=1100
Some questions:
Why is the top margin different from the bottom margin? The top margin takes up 20px while the bottom just a few pixels.
Why isn't the top margin scaled? I expected the margin value of 20px to be scaled just like the content.
What value would I used for the bottom margin to make it the same as the top margin?

Related

How to dynamically fit a rotated element to the parent element with CSS?

I have 2 divs: the .container and its child .element.
The container is centered on the page, both elements have position: absolute and have vw and vh for width and height.
The difference between them is that the parent element has 25vh for height and 25vw for width, however the child element has the opposite: 25vh for the width and 25vw for height. This means that the width of one is equivalent to the height of the other.
Then, I used transform: rotate(90deg) to rotate the child div. Now they look the same.
I want to move only the child element so that it dynamically fits into the parent element. But I'm not getting it done.
I've tried to use positioning properties, transform-origin, translate and so on with percentage or vw and vh units, but nothing is working.
How to fit this div into the parent div while remaining that way on any screen size?
body {
background-color: #333;
}
.container {
background-color: #000;
position: absolute;
height: 25vh;
width: 25vw;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.element {
position: absolute;
background-color: #abc;
transform: rotate(90deg);
height: 25vw;
width: 25vh;
}
<div class="container">
<div class="element"></div>
</div>
You need to consider transform-origin and some translation too
body {
background-color: #333;
}
.container {
background-color: #000;
position: absolute;
height: 25vh;
width: 25vw;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.element {
position: absolute;
background-color: #abc;
height: 25vw;
width: 25vh;
transform: translateY(-100%) rotate(90deg);
transform-origin: bottom left;
}
<div class="container">
<div class="element"></div>
</div>

Why is margin stretching the body's width?

I'm trying to use margin on an element, but it ends up stretching the body's width. Can find the reason why.
.navbar {
width: 100%;
position: absolute;
top: 0;
left: 0;
background: $c-white;
margin: 0.5rem;
}

CSS Bottom Not Working When Combined with Top?

I am attempting to create a top and bottom margin for a div and when I specify only the bottom margin it works correctly:
#RightBar{
display: block;
position: fixed;
height: 100%;
width: 25px;
bottom: 150px;
right: 0px;
background: Aqua;
}
<div id="RightBar"></div>
However when I add the top margin, the bottom margin all of a sudden doesn't work at all - it's almost as if it no longer reads it:
#RightBar{
display: block;
position: fixed;
height: 100%;
width: 25px;
bottom: 150px;
top: 25px;
right: 0px;
background: Aqua;
}
<div id="RightBar"></div>
Any ideas on what may be causing this?
It's because you also have a height declaration of 100%, which can't fit between your 25px top and your 150px bottom. A conflict results in one of the rules being ignored, usually the oldest one.
Remove the height to fix this. You don't need it now that the height can automatically be calculated from the top and bottom:
#RightBar{
display: block;
position: fixed;
width: 25px;
top: 25px;
bottom: 150px;
right: 0px;
background: Aqua;
}
<div id="RightBar"></div>

Rounded skewed top shape in css

I need to make a div that looks like this:
With text in the middle in css. I tried looking at transforms and other 3d stuff, but I couldn't figure it out, especially without ruining the text.
You can use a skewed Y pseudo element as a background of the element. This won't affect the content :
div {
position: relative;
display: inline-block;
padding: 20px 50px;
overflow: hidden;
}
div:before {
content: '';
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
background: #FFD505;
-webkit-transform-origin: 0 0;
-ms-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: skewY(5deg);
-ms-transform: skewY(5deg);
transform: skewY(5deg);
z-index: -1;
border-radius: 10px 10px 0 0;
}
<div>Some text</div>
The div has overflow:hidden so that the overflowing parts of the pseudo-element are hidden.
The pseudo element has a negative z-index so it stacks behind the content of the div
The transform-origin is set to 0 0 so that top left of the skewed pseudo element doesn't move.
you could use a skew'ed pseudo element for this, which ensures the text won't be skewd as well:
.wrap {
height: 100px;
width: 400px;
position: relative;
overflow: hidden;
padding-top: 30%;
}
.wrap:before {
content: "";
position: absolute;
border-radius: 5px;
height: 100%;
width: 100%;
left: 0;
top: 20%;
background: tomato;
transform: skewY(5deg);
z-index: -2;
}
<div class="wrap">hello
</div>

Parent div is not expanding as child div grows

I have an inner div in an outer div.
When I am setting top: SomeValue for innerdiv, it overlaps outer div.
#div1{
position: relative;
top: 10px;
left: 20px;
width: 50%;
background: green;
}
#div2{
position: relative;
top:5px;
left: 20px;
width: 80%;
background: red;
}
Here is my jsfiddle
How to overcome it?
You cannot use Top attribute for this. As it will ignore it's parent's boundaries.
Use padding-top instead
{ position: relative; padding-top:5px; left: 20px; width: 80%; background: red; }
Relative Positioning:
A relative positioned element is positioned relative to its normal
position.
http://www.w3schools.com/css/css_positioning.asp
You're using relative positioning on div2, so it is ignoring the fact that it's a nested div and moving it away from it's normal position.
I've updated the fiddle with a workaround:
http://jsfiddle.net/P6dbe/2/
The fiddle removes the relative position of div2 and adds padding to div1, with the below css:
#div1
{
position: relative;
padding-top: 5px;
padding-bottom: 5px;
padding-left: 20px;
top: 10px;
left: 20px;
width: 50% ;
background: green;
}
#div2
{
width: 80%;
background: red;
}
You are positioning div2 partially outside of div1. If you want to constrain div2 within div1 you need to set overflow:hidden on div1.
#div1
{ position: relative; top: 10px; left: 20px;
width: 50% ; background: green; overflow:hidden;}

Resources