I have a two-column layout and want a fixed element to be centered horizontally in the right column.
How do I accomplish this as a fixed element isn't position relative to it's parent but rather to the document?
Note: the left column's width is known. The right column's width is "full width".
I'm using fixed (and not absolute) because the element has to follow scrolling.
.wrapper {
height: 200px;
display: flex;
background-color: green;}
.column {
width: 300px;
background-color: pink;
}
.content {
flex: 1;
background-color: green;
}
.fixed-element {
position: fixed;
left: 50%;
background-color: orange;
}
<div class='wrapper'>
<div class='fixed-element'>Please center me in green area.</div>
<div class='column'></div>
<div class='content'></div>
</div>
You can use calc to calculate center and transform for the unkown width of fixed element positioning.
Please check if the browser compability for the calc is ok for you, before using it in production.
And here is my solution
body {
margin: 0;
}
.wrapper {
height: 200px;
display: flex;
background-color: green;
}
.column {
width: 200px;
background-color: pink;
}
.content {
flex: 1;
background-color: maroon;
}
.fixed-element {
z-index: 111;
position: fixed;
right: calc(50% - 205px); //can't explain that +5px :)
-webkit-transform: translateX(-50%);
-moz-transform: translateX(-50%);
-ms-transform: translateX(-50%);
-o-transform: translateX(-50%);
transform: translateX(-50%);
background-color: orange;
}
<div class='wrapper'>
<div class='fixed-element'>Please center me</div>
<div class='column'></div>
<div class='content'></div>
</div>
Related
https://codepen.io/codepen19871/pen/KKQZgwQ
Is there a way to fix this behavior? I want to make sure that the div expands downward and never upward when adding text. Because of this, I need to used fixed width, but if I use fixed width, I can't make the div responsive to the size of the content. Is there a way to fix this?
.child {
background-color: rgba(255,255,255, 1);
min-width: 10%;
max-width: 20%;
top: 90px;
left: 50%;
position:absolute;
z-index: 2;
transform: translate(-50%, -50%);
overflow-wrap: break-word;
}
If I remove translate, it won't center. I also tried using other styling such as:
width: max-content;
But nothing works, there doesn't seem to be a solution that doesn't require javascript. If so, is there a way to make it work in React and make it behave like as though I used max-content?
I just need to center the div without the div expanding above, it's as simple as that.
Move child div under parent div and set Top 50%
.parent{
width: 100%;
height: 200px;
background-color: #8ae6a2;
position: relative;
}
.child {
background-color: rgba(255,255,255, 1);
min-width: 10%;
max-width: 20%;
top: 50%;
left: 50%;
position:absolute;
z-index: 2;
transform: translate(-50%, -50%);
overflow-wrap: break-word;
}
<div class="parent">
<div class="child">
234234234343434234324324343242342342343242343243423
</div>
</div>
You should use flexbox if you want to center your div, I'm not exactly sure what you're attempting to do, but here is my interpretation.
#parent {
width: 100%;
height: 200px;
background-color: green;
display: flex;
justify-content: center;
}
.child {
align-self: start;
max-width: 20%;
background-color: white;
overflow-wrap: break-word;
}
<div id="parent">
<div class="child">
100000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000
</div>
</div>
This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
How can I center an absolutely positioned element in a div?
(37 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed last year.
I would like to be able to position the gray container in the middle of the background, also vertically, but I can't understand how the percentages work, why the left one works and the top doesn't
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html {
font-size: 62.5%;
}
body {
background-image: url("wallpaper.png");
background-size: cover;
background-repeat: no-repeat;
position: relative;
top: 0px;
}
.container {
background-color: lightgray;
opacity: 50%;
width: 200px;
height: 200px;
left: calc(50% - 100px);
top: calc(50% - 100px);
position: relative;
}
<body>
<div class="container">
<div class="container-name"></div>
<div class="container-stats"></div>
</div>
</body>
Here is the preview
You need to give the body a height, or else it'll be the elements height, that's why you can't move top or bottom, since the parent (body) is only 200px, since that's all there is. Where the parent doesn't have a height property, it'll automatically adjust to it's content height, 200px in this case. Also, the top property from body can be removed.
You could add height: 100vh; to your body, that'll be the 100%/units height of the view port.
The calc() function you have is taking 50% of it's parent size, then substracting 100px, which in your case is 50% of your element. That'll center the element with percentages.
An alternative to your centering solution could be:
.container {
background-color: lightgray;
opacity: 50%;
width: 200px;
height: 200px;
left: 50%;
top: 50%;
position: relative;
transform: translate(-50%, -50%);
}
This will move your container 50% left of parents width, 50% from the top of parent and then, with transform: translate() move it -50% of it's own size X and Y
You need to give a height to body.
If parent div has a position relative then for setting div according to parent you should give a child div to position absolute.
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html {
font-size: 62.5%;
}
body {
background-image: url("wallpaper.png");
background-size: cover;
background-repeat: no-repeat;
position: relative;
top: 0px;
height: 100vh;
}
.container {
background-color: lightgray;
opacity: 50%;
width: 200px;
height: 200px;
left: 50%;
top: 50%;
position: absolute;
transform: translate(-50%, -50%);
}
<div class="container">
<div class="container-name"></div>
<div class="container-stats"></div>
</div>
HTML:
<div class="wrap-center">
<div class="container">
<div class="container-name"></div>
<div class="container-stats"></div>
</div>
</div>
CSS:
.container {
background-color: lightgray;
opacity: 50%;
width: 200px;
height: 200px;
}
.wrap-center {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
min-height: 500px;
}
I'm using some CSS I found in the wild to make flip cards. I've made a few adjustments in an attempt to make them fluidly responsive. My attempt is here:
https://jsfiddle.net/u18rhf6q/
css:
.flip-card-wrapper {
width: 50%;
height: auto;
}
.flip-card {
background-color: transparent;
width: 100%;
height: 100%;
perspective: 1000px;
}
.flip-card img {
width: 100%;
height: auto;
}
.flip-card-inner {
position: relative;
width: 100%;
height: 100%;
text-align: center;
transition: transform 0.8s;
transform-style: preserve-3d;
}
.flip-card-wrapper:hover .flip-card-inner {
transform: rotateY(180deg);
}
.flip-card-front, .flip-card-back {
position: absolute;
width: 100%;
height: 100%;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
}
.flip-card-front {
color: black;
}
.flip-card-back {
background-color: #c1272d;
color: white;
transform: rotateY(180deg);
}
html
<div class="flip-card-wrapper" >
<div class="flip-card" >
<div class="flip-card-inner">
<div class="flip-card-front">
<img src="https://dummyimage.com/600x600/000/fff" alt="Avatar" >
</div>
<div class="flip-card-back">
<h1>John Doe</h1>
<p>Architect & Engineer</p>
<p>We love that guy</p>
</div>
</div>
</div>
</div>
What I hope to happen is if I adjust the width of flip-card-wrapper, the contents would expand relative to that width and the flip would continue. However, it only works if I also supply a height to flip-card-wrapper. Since the front and back are both absolute, I can't a height to bubble up. Any ideas?
One possibility for keeping your card height responsive might be to set a padding-bottom or padding-top on your flip-card-inner class. Since those heights are based on the parent's width when you use a percentage, padding-bottom: 100%; should create a perfect square. If you want to play around with the sizing, there's a good overview of aspect ratio boxes at https://css-tricks.com/aspect-ratio-boxes/.
i'm trying to split the page in 2 parts, but with different sizes.
With the following code, i get 2 parts, but with same size.
http://jsfiddle.net/aL78z6kf/
The html part is:
<div class="split left">
<div class="centered">
<h2>Jane Flex</h2>
<p>Some text.</p>
</div>
</div>
<div class="split right">
<div class="centered">
<h2>John Doe</h2>
<p>Some text here too.</p>
</div>
</div>
And the Css is:
.split {
height: 100%;
width: 50%;
position: fixed;
z-index: 1;
top: 0;
overflow-x: hidden;
padding-top: 20px;
}
/* Control the left side */
.left {
left: 0;
background-color: orange;
}
/* Control the right side */
.right {
right: 0;
background-color: red;
}
/* If you want the content centered horizontally and vertically */
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
/* Style the image inside the centered container, if needed */
.centered img {
width: 150px;
border-radius: 50%;
}
Is it possible to do?
Thanks in advance
JsFiddle
Remove width:50% from .split class. And add width properties (whatever you want) to .left and .right class.
/* Control the left side */
.left {
left: 0;
background-color: orange;
width: 30%;
}
/* Control the right side */
.right {
right: 0;
background-color: red;
width: 70%;
}
I think this is a math question more than anything else, who knew I'd need to know geometry for CSS.
I have an upright background image inside a rotated container. The image has to cover the container perfectly, and the container itself needs be stuck to the wall so that the user doesn't see the rotation on the side. I want to have dynamic control over the sizing of this element, so I would like everything to be done with percentages.
The blue line is the "wall".
Since the image is upright it has to be quite a bit bigger than its container. I found the formula for this here. That's what it looks like:
So the dimensions are in percentages, what I haven't figured out how to calculate is the various positionings. Without the pixel values I added it looks like this:
So how do I calculate these distances?
Here is my CodePen, it's in SCSS, all the math is done in the code.
body {
padding: 2em 5em;
}
.wrapper {
border-left: 3px solid blue;
}
.wrapper .container {
opacity: 0.7;
width: 300px;
background-color: red;
border-radius: 0 40px 40px 0;
overflow: hidden;
transform: rotate(10deg);
margin-left: -42px;
}
.wrapper .container .sizing-wrapper {
width: 100%;
padding-top: 150%;
position: relative;
}
.wrapper .container .img {
position: absolute;
top: 0;
left: 0;
background: url("http://placekitten.com/300/450") no-repeat right top;
background-size: cover;
height: 110.0573842629%;
width: 124.5280657351%;
transform: rotate(-10deg) ranslateY(-31px) translateX(-32px);
}
<div class="wrapper">
<div class="container">
<div class="sizing-wrapper">
<div class="img"></div>
</div>
</div>
</div>
I shared my question on Facebook and Amit Sheen came up with a solution I initially dismissed of using transform-origin. Now that I've seen it work I'm not sure why I thought it wouldn't.
The fixed CodePen
To make sure the container is cut by the wall at the right place we need to rotate it from the top left corner by using transform-origin: 0 0.
And we need to center the image inside the container:
.img {
top: 50%;
left: 50%;
transform: rotate(-$deg) translateY(-50%) translateX(-50%);
transform-origin: 0 0;
}
I'm still curious what the mathematical solution to my question is, because I'm sure there is one, but for actual use this is probably better...
body {
padding: 2em 5em;
}
.wrapper {
border-left: 3px solid blue;
}
.wrapper .container {
opacity: 0.7;
width: 300px;
background-color: red;
border-radius: 0 40px 40px 0;
overflow: hidden;
transform: rotate(10deg);
transform-origin: 0 0;
}
.wrapper .sizing-wrapper {
width: 100%;
padding-top: 150%;
position: relative;
}
.wrapper .img {
position: absolute;
top: 50%;
left: 50%;
background: url("http://placekitten.com/300/450") no-repeat right top;
background-size: cover;
height: 110.0573842629%;
width: 124.5280657351%;
transform: rotate(-10deg) translateY(-50%) translateX(-50%);
transform-origin: 0 0;
}
<div class="wrapper">
<div class="container">
<div class="sizing-wrapper">
<div class="img"></div>
</div>
</div>
</div>