So at first I set the blue container's height and width to 25px and to translate it to the top right corner of the green container I simply translate x to 100-25=75px and it works fine,
<div id="app" style="margin: 20px">
<div
style="
position: absolute;
height: 300px;
width: 300px;
background-color: red;
"
></div>
<div
style="
position: absolute;
height: 100px;
width: 100px;
background-color: green;
"
></div>
<div
style="
position: absolute;
height: 25px;
width: 25px;
background-color: blue;
transform-origin: center center;
transform: translateX(75px);
"
></div>
</div>
Now I want to achieve the same with scale. I set height and width of blue container to 40px and I scale it to 0.625 which basically sets the height and width of blue container to 25px like above. However now when I want to translate it I can't translate with the above value i.e. 75px. I tried putting the blue container back to top left and again tried translating to 75px but it still doesn't work,
<div id="app" style="margin: 20px">
<div
style="
position: absolute;
height: 300px;
width: 300px;
background-color: red;
"
></div>
<div
style="
position: absolute;
height: 100px;
width: 100px;
background-color: green;
"
></div>
<div
style="
position: absolute;
height: 40px;
width: 40px;
background-color: blue;
transform-origin: center center;
transform: scale(0.625) translateX(-15px) translateY(-15px)
translateX(75px);
"
></div>
</div>
Translate then scale and set the origin to be top right
<div id="app" style="margin: 20px">
<div
style="
position: absolute;
height: 300px;
width: 300px;
background-color: red;
"
></div>
<div
style="
position: absolute;
height: 100px;
width: 100px;
background-color: green;
"
></div>
<div
style="
position: absolute;
height: 40px;
width: 40px;
background-color: blue;
transform-origin: top right;
transform:translate(60px) scale(0.625);
"
></div>
</div>
Or like below (96 = 60/0.625)
<div id="app" style="margin: 20px">
<div
style="
position: absolute;
height: 300px;
width: 300px;
background-color: red;
"
></div>
<div
style="
position: absolute;
height: 100px;
width: 100px;
background-color: green;
"
></div>
<div
style="
position: absolute;
height: 40px;
width: 40px;
background-color: blue;
transform-origin: top right;
transform:scale(0.625) translate(96px);
"
></div>
</div>
UPDATE
without transform-origin (30% = ((1-0.625)/2)/0.625) * 100%)
<div id="app" style="margin: 20px">
<div
style="
position: absolute;
height: 300px;
width: 300px;
background-color: red;
"
></div>
<div
style="
position: absolute;
height: 100px;
width: 100px;
background-color: green;
"
></div>
<div
style="
position: absolute;
height: 40px;
width: 40px;
background-color: blue;
transform:translate(60px) scale(0.625) translate(30%,-30%);
"
></div>
</div>
Related
How can I center this image that I have in this div in a way that it won't move the 'line' div? I want the line to be touching the top of the square too.
.black {
background: black;
}
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>
Here is one way to prevent it from disrupting the flow layout of your container:
you can make the container a position of relative, and the image a position of absolute, positioned off the top and left by 50%, then transform it so that the center of the image is in the center position.
You could also just make the image a background-image of the div instead of using an image element, which may be easier to manipulate.
.black {
background: black;
}
.square {
position: relative;
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>
</div>
I'm not sure I understand your exact desired end goal. But, if I understand correctly, you could create a flex parent to justify the image, and then position the line absolutely within that. See -
.black {
background: black;
}
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.image {
height: 60px;
width: 60px;
}
.line {
width: 4px;
background-color: red;
position: absolute;
left: 0;
top: 0;
bottom: 0
}
<div class="square black">
<div class="line"></div>
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
</div>
You can just use these css for .square and .image
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
position: relative;
}
.image {
height: 60px;
width: 60px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
You can easily center a image by using CSS position absolute. By making the position of square black class "absolute" and apply to properties "top: 45%;" and "left: 47%" . By applying this your problem will be definitely solve.
.black {
background: black;
}
.square {
display: flex;
align-item: center;
justify-content: center;
width: 200px;
height: 500px;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
</div>
</div>
.black {
background: black;
}
.square {
position: absolute;
top: 45%;
left: 47%;
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
position: absolute;
top:50%;
left:50%;
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>
.black {
background: black;
}
.square {
width: 200px;
height: 500px;
margin: 37px auto;
border-radius: 2px;
}
.image {
height: 60px;
width: 60px;
}
.line {
width: 4px;
height: 500px;
background-color: red;
}
<div class="container">
<div class="square black">
<img class="image" src="https://c.neh.tw/thumb/f/720/5659673474629632.jpg">
<div class="wrapper">
<div class="line"></div>
<div class="rectangle"></div>
</div>
</div>
My code
<div style="background-color: grey; width: 900px; height: 900px;">
<div style="width: 200px; height: 200px; background-color: green; right: 50px;">
</div>
<div style="width: 300px; height: 200px; background-color: blue; right: 100px;">
</div>
<div style="width: 300px; height: 200px; background-color: yellow; right: 100px;">
</div>
</div>
I also read reference at here https://developer.mozilla.org/en-US/docs/Web/CSS/right . I want right: 50px; have affect. How to fix?
Your <div>'s doesn't got any position attribut.
Sample :
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div style="position: relative; background-color: grey; width: 900px; height: 900px;">
<div style="position: absolute; top: 0; width: 200px; height: 200px; background-color: green; right: 50px;">
</div>
<div style="position: absolute; top: 200px; width: 300px; height: 200px; background-color: blue; right: 100px;">
</div>
<div style="position: absolute; top: 400px; width: 300px; height: 200px; background-color: yellow; right: 100px;">
</div>
</div>
</body>
</html>
You need to set position for each div as follows.
<div style="background-color: grey; width: 900px; height: 900px; position: relative;">
<div style="width: 200px; height: 200px; background-color: green; right: 50px; position: absolute;">
</div>
<div style="width: 300px; height: 200px; background-color: blue; right: 100px; position: absolute; top: 200px;">
</div>
<div style="width: 300px; height: 200px; background-color: yellow; right: 100px; position: absolute; top: 400px;">
</div>
</div>
You have to give position attribute along with the right attribute
as right, left, top and bottom only works with position other than static and by default the position of all the HTML elements are static, that is why it is not working
So, You have add a position property to make this work
<div style="background-color: grey; width: 900px; height: 900px; position: relative">
<div style="width: 200px; height: 200px; background-color: green; right: 50px; position: absolute">
</div>
<div style="width: 300px; height: 200px; background-color: blue; right: 100px; position: absolute">
</div>
<div style="width: 300px; height: 200px; background-color: yellow; right: 100px; position: absolute">
</div>
</div>
<div style="overflow-y: scroll; overflow-x: scroll; height: 200px; width: 200px">
<div style="border-color: black; height: 100px; width: 30px; border: 2px; border-style: solid; position:absolute; left: 20px; top: 200px">
</div>
</div>
I would expect that the div inside the scroll-able div doesn't show up outside the scroll-able div.
How can I archive this ? The Rectangle should only be visible in the scroll-able div.
you need to reset position, either for the parent or the child : example below
/* CSS here for demo purpose to put both example side by side*/
body {
display:grid;
grid-template-columns:1fr 1fr;
grid-auto-flow:row dense;
}
body>*{grid-column:1;margin:auto;}
body >:nth-child(2) ~ * {grid-column:2;}
<p>position relative on parent,<br><b> so the parent becomes the reference for the absolute child</b></p>
<div style="overflow-y: scroll; overflow-x: scroll; height: 200px; width: 200px;position:relative;">
<div style="border-color: black; height: 100px; width: 30px; border: 2px; border-style: solid; position:absolute; left: 20px; top: 200px">
</div>
</div>
<p> static position on children<br><b> so it is part of the flow</b></p>
<div style="overflow-y: scroll; overflow-x: scroll; height: 200px; width: 200px">
<div style="border-color: black; height: 100px; width: 30px; border: 2px; border-style: solid; margin-left: 20px; margin-top: 200px">
</div>
</div>
Absolutely positioned elements Are removed from their parents, and displayed over the DOM. Remove position:absolute; and change your top and left to margin-top and margin-left
.one{
overflow-y: scroll;
overflow-x: scroll;
height: 200px;
width: 200px"
}
.two{
border-color: black;
height: 100px;
width: 30px;
border: 2px;
border-style: solid;
margin-left: 20px;
margin-top: 200px
}
<div class="one">
<div class="two">
</div>
</div>
Add position: relative to the top div
<div style="position: relative; overflow-y: scroll; overflow-x: scroll; height: 200px; width: 200px">
<div style="border-color: black; height: 100px; width: 30px; border: 2px; border-style: solid; position:absolute; left: 20px; top: 200px">
</div>
</div>
Snippet Demo:
.container {
position: relative;
overflow-y: scroll;
overflow-x: scroll;
height: 200px;
width: 200px;
}
.box {
height: 100px;
width: 30px;
position: absolute;
top: 200px;
left: 20px;
border: 2px solid black;
}
<div class="container">
<div class="box">
</div>
</div>
I have 4 div, which look something like this
Desired output:
Current code:
<div class="center aligned" style="width: 100%;height: 7em; padding: 2em; position:absolute;">
<div class="ui small grey label fluid progress_padding_top_bottom" style="z-index:1;height: 7em; border-radius: 0; padding-right: 0; padding-left: 0; background-color: #E8E8E8 !important;">
<div style="background-color: #21BA45; width: 5%; height: 7em; float:left;"></div>
<div style="background-color: #ffdd00; width: 20%; height: 7em; float:left;"></div>
<div style="width: 100%;" class="center aligned">ABC</div>
</div>
Update: This is a part of a progress bar, so both red and yellow width will change
Wrap the colored divs in their own container and position it absolutely unde rthe content.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.wrapper {
width: 50%;
margin: 1em auto;
background: lightgrey;
height: 7em;
display: flex;
justify-content: center;
align-items: center;
position: relative;
font-weight: bold;
}
.content {
position: relative;
z-index: 1;
}
.underlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.red {
height: 100%;
background: red;
width: 25%;
float: left;
}
.yellow {
height: 100%;
background: yellow;
float: left;
width: 25%;
}
<div class="wrapper">
<div class="content">ABC</div>
<div class="underlay">
<div class="red"></div>
<div class="yellow"></div>
</div>
</div>
This produces what you're trying to putput
https://jsfiddle.net/wcsfnbuL/
<div class="center aligned" style="width: 100%;height: 7em; padding: 2em; position:absolute;">
<div style="background-color: red; width: 15%; height: 7em; float:left; margin: auto;"></div>
<div style="background-color: #ffdd00; width: 15%; height: 7em; text-align: center; vertical-align: middle; line-height: 7em; float:left; margin: auto;">ABC</div>
<div class="ui small grey label fluid progress_padding_top_bottom" style="z-index:1;height: 7em; border-radius: 0; padding-right: 0; padding-left: 0; background-color: #E8E8E8; float: left; width: 15%"></div>
</div>
For some reason, attribute vertical-align: middle; is not applied to span tag. Here is the code:
<div style="position: relative; background-color: gray; padding: 1%; width: 100%; height: 60px;">
<div style="background-color: green; width: 50%; height: 100%">
<span style="position: absolute; display: inline-block; text-align: center; width: 100%; vertical-align: middle">
50%
</span>
</div>
</div>
Since you know the height of the outer div is 60px, you can set the line-height of the span to match it:
<div style="position: relative; background-color: gray; padding: 1%; width: 100%; height: 60px;">
<div style="background-color: green; width: 50%; height: 100%">
<span style="position: absolute; display: inline-block; text-align: center; width: 100%; vertical-align: middle;line-height: 60px;">
50%
</span>
</div>
</div>
You could use line-height to center your text vertically, in this case use line-height: 60px
<div style="position: relative; background-color: gray; padding: 1%; width: 100%; height: 60px;">
<div style="background-color: green; width: 50%; height: 100%">
<span style="position: absolute; display: inline-block; text-align: center; width: 100%; line-height: 60px;">
50%
</span>
</div>
</div>