This question already has answers here:
Center div horizontally and vertically inside another div
(2 answers)
How can I horizontally center an element?
(133 answers)
Closed 2 years ago.
I am trying to make a div sit in the direct center of another div. I tried to use margin: auto; to accomplish this, but the div only aligned in the center horizontally. How can I apply this for a center vertical align?
.topbar {
overflow: hidden;
width: 100%;
height: 50px;
background-color: #131218;
border-top-left-radius: 20px;
border-top-right-radius: 20px;
display: flex;
align-items: center;
justify-content: space-between;
}
.body {
display: block;
width: 100%;
height: 330px;
background-color: blue;
border-bottom-left-radius: 20px;
border-bottom-right-radius: 20px;
}
img {
height: 50px;
padding: 5px;
border-radius: 200px;
float: left;
will-change: transform;
animation: logofloat 1s ease-in-out infinite alternate;
}
#keyframes logofloat {
from {
transform: translateY(5px);
}
to {
transform: translateY(15px);
}
}
.authdiv {
background-color: red;
height: 200px;
width: 200px;
margin: auto;
}
<div class="topbar">
<img class="logo" src="https://dummyimage.com/50x50/fff/000.png&text=Logo">
</div>
<div class="body">
<div class="authdiv"></div>
</div>
You could use flexbox on your containing element:
.topbar {
overflow: hidden;
width: 100%;
height: 50px;
background-color: #131218;
border-top-left-radius: 20px;
border-top-right-radius: 20px;
display: flex;
align-items: center;
justify-content: space-between;
}
.body {
display: flex;
flex-flow: column nowrap;
width: 100%;
height: 330px;
background-color: blue;
border-bottom-left-radius: 20px;
border-bottom-right-radius: 20px;
}
img {
height: 50px;
padding: 5px;
border-radius: 200px;
float: left;
will-change: transform;
animation: logofloat 1s ease-in-out infinite alternate;
}
#keyframes logofloat {
from {
transform: translateY(5px);
}
to {
transform: translateY(15px);
}
}
.authdiv {
background-color: red;
height: 200px;
width: 200px;
margin: auto;
}
<div class="topbar">
<img class="logo" src="https://dummyimage.com/50x50/fff/000.png&text=Logo">
</div>
<div class="body">
<div class="authdiv"></div>
</div>
I usually put the element i want to center at 50% from top and 50% from left. Then use translate to pull the element back half it's size. So just add the 4 lines below.
.authdiv {
background-color: red;
height: 200px;
width: 200px;
margin: auto;
position:absolute;
left:50%;
top:50%;
transform:translate(-50% , -50%);
}
Related
I have tried achieving this effect using border-radius (code below) but I want the circle to be smaller, as shown in this example https://imgur.com/a/gKHtVXr and I don't think I can acheive this with border-radius.
<img class = "zyra-thumb" src = "thumbnail champion/thumb2.png" alt="zyra">
CSS:
.zyra-thumb{
width: 200px;
height: 200px;
object-fit: cover;
border-radius: 150px;
transition: border-radius 0.3s;
}
.zyra-thumb:hover{
border-radius: 10px;
}
you can use clip-path to display only center of the thumb image and
transition: clip-path 1s;to give needed animation on hover
.thumb {
background: purple;
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 10px;
}
.zyra-thumb {
width: 100%;
object-fit: cover;
clip-path: circle(25% at 50% 50%);
transition: clip-path 1s;
border-radius: 10px;
}
.zyra-thumb:hover {
clip-path: circle(100% at 50% 50%);;
}
<div class="thumb">
<img class="zyra-thumb" src="https://www.gravatar.com/avatar/0cf65651ae9a3ba2858ef0d0a7dbf900?s=256&d=identicon&r=PG&f=1" alt="zyra">
</div>
Based on your attached image I assume this is the effect you are trying to achieve.
.zyra-thumb {
background: url(https://i.stack.imgur.com/OR7ho.jpg) no-repeat center;
background-size: cover;
position: relative;
width: 400px;
height: 400px;
overflow: hidden;
display: inline-block;
border-radius: 20px;
}
.zyra-thumb::before {
content: "";
display: block;
width: 50%;
padding-bottom: 50%;
position: absolute;
top: 50%;
left: 50%;
z-index: 2;
transform: translate(-50%, -50%);
border: solid 400px #41373f;
border-radius: 50%;
transition: all ease-in-out 0.3s;
}
.zyra-thumb:hover::before {
width: 150%;
padding-bottom: 150%;
}
<div class="zyra-thumb">
</div>
Simply use border-radius:0px; in hover.
I have three elements and once I clicked one of it I want to move it to the center but from its position with a transition of, for example, 1 second. Also, I want to increase fade the text and show another text during the time when the box increases from 150px to 200px but for now I just don't know how to center it on a click. I did it but it goes from bottom right to the center but I want it to go from its position.
Also, I tried using $(this) to get certain div but it just didn't work
$(document).ready(function(e) {
$('.second').click(e => {
$('.second').addClass('red')
})
});
.main {
display: flex;
gap: 15px;
height: 90vh;
justify-content: center;
align-items: center;
position: relative;
}
.block {
width: 150px;
height: 100px;
box-shadow: 0px 0px 3px #000;
}
.red {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 150px;
height: 150px;
transition: 1s ease;
color: red;
#keyframes rise;
#keyframes rise {
0% { width: 200px; height: 200px }
100% { width: 150px; height: 150px }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='main'>
<div class="first block">Hello</div>
<div class="second block">Hi</div>
<div class="third block">hello</div>
</div>
Just change offset values of that element on click.
This is happening because everything is transitioning,
the sequence of centering the div in your code is like this:
First, its position becomes absolute
then it goes to the bottom right direction because you have top 50%; left 50%;
then it comes back to the center because of transform: translate( -50%, -50%)
You can fix it to ways:
Change the centering method to margins
$(document).ready(function(e) {
$('.second').click(e => {
$('.second').addClass('red')
})
});
.main {
display: flex;
gap: 15px;
height: 90vh;
justify-content: center;
align-items: center;
position: relative;
}
.block {
width: 150px;
height: 100px;
box-shadow: 0px 0px 3px #000;
}
.red {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 150px;
height: 150px;
transition: 1s ease;
color: red;
#keyframes rise;
#keyframes rise {
0% { width: 200px; height: 200px }
100% { width: 150px; height: 150px }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='main'>
<div class="first block">Hello</div>
<div class="second block">Hi</div>
<div class="third block">hello</div>
</div>
In this way, the div goes nowhere, just to the center.
Second way is to not transition position and transform
$(document).ready(function(e) {
$('.second').click(e => {
$('.second').addClass('red')
})
});
.main {
display: flex;
gap: 15px;
height: 90vh;
justify-content: center;
align-items: center;
position: relative;
}
.block {
width: 150px;
height: 100px;
box-shadow: 0px 0px 3px #000;
}
.red {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 150px;
height: 150px;
transition: 1s ease;
transition-property: width, height, color;
color: red;
#keyframes rise;
#keyframes rise {
0% { width: 200px; height: 200px }
100% { width: 150px; height: 150px }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='main'>
<div class="first block">Hello</div>
<div class="second block">Hi</div>
<div class="third block">hello</div>
</div>
The transition-property Property defines which properties to transition so, I put: width, height and color. You can change what you wanna transition
This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I have two div that each of them has an image and a caption.
I want image center vertically or fill div element without stretching or Any unusual resizing.
I searched and use any suggestion Like How TO - Center Elements Vertically- CSS Layout - Horizontal & Vertical Align and answers of some questions in stackoverflow, but none worked.
I want use and display this elements responsive
I use this pictures for test:
.border_slide {
overflow: hidden;
}
.other_titles
{
margin-top: 5px;
height: 120px;
}
.other_titles div
{
width:49.5%;
margin-bottom: 5px;
}
.other_titles_cap
{
position: absolute;
z-index: 100;
width:100%;
background-color: rgba(69,69,69,0.4);
height: 70px;
margin-top: 80px;
transition: all 0.3s ease 0s;
}
/*.other_titles_cap:hover
{
margin-top: 0;
height: 150px;
}*/
.title2
{
height: 150px;
background-color: #9bfff0;
/*margin-left: 15px;*/
float: right;
}
.title2 img
{
margin-top: 0;
transition: all 0.4s ease 0s;
}
.title2:hover .other_titles_cap
{
margin-top: 0;
height: 150px;
}
.title2:hover img
{
width: 115% !important;
opacity: 0.8;
transform:translate(7%,-10%);
-ms-transform:translate(7%,-10%);
}
.title3
{
height: 150px;
background-color: #ffd5c4;
float: left;
}
.title3 img
{
/* transform: translate(7%,-10%); */
-ms-transform:translate(7%,-10%);
margin-top: 0;
transition: all 0.4s ease 0s;
}
.title3:hover .other_titles_cap
{
margin-top: 0;
height: 150px;
}
.title3:hover img
{
width: 115% !important;
opacity: 0.8;
transform:translate(7%,-10%);
-ms-transform:translate(7%,-10%);
}
<div class="other_titles">
<div class="border_slide title2">
<div class="other_titles_cap">title of Post2</div>
<img class="" src="https://i.stack.imgur.com/k6HuQ.jpg" style="width:100%">
</div>
<div class="border_slide title3">
<div class="other_titles_cap">title of Post3</div>
<img class="" src="https://i.stack.imgur.com/90ten.jpg" style="width:100%">
</div>
</div>
I Mostly need this for responsive mode in width size below 520px.
You can test this here:
jsfiddle
Something like below snippet.
*, *:before, *:after{box-sizing: border-box;}
.other_titles{
display: flex;
justify-content: space-between;
flex-flow: wrap;
}
.border_slide {
width: calc(50% - 10px);
margin-bottom: 5px;
overflow: hidden;
position: relative;
height: 150px;
}
.border_slide img{
transition: all 0.4s ease 0s;
display: block;
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
position: absolute;
}
.other_titles_cap{
position: absolute;
z-index: 100;
width: 100%;
background-color: rgba(69,69,69,0.5);
height: 70px;
bottom: 0;
left: 0;
transition: all 0.3s ease 0s;
padding: 10px;
font-size: 16px;
color: #fff;
}
.border_slide:hover .other_titles_cap{
margin-top: 0;
height: 150px;
background-color: rgba(0,0,0,0.75);
}
.border_slide:hover img{
width: 115% !important;
opacity: 0.8;
transform: scale(1.2);
}
<div class="other_titles">
<div class="border_slide">
<img src="https://i.stack.imgur.com/k6HuQ.jpg">
<div class="other_titles_cap">Title of Post2</div>
</div>
<div class="border_slide">
<img src="https://i.stack.imgur.com/90ten.jpg">
<div class="other_titles_cap">Title of Post3</div>
</div>
</div>
Here you go with a solution
.border_slide {
overflow: hidden;
}
.other_titles
{
display: flex;
flex-direction: row;
align-items: center;
height: 100vh;
justify-content: space-around;
}
.other_titles div
{
width:49.5%;
margin-bottom: 5px;
}
.other_titles_cap
{
position: absolute;
z-index: 100;
width:100%;
background-color: rgba(69,69,69,0.4);
height: 70px;
margin-top: 80px;
transition: all 0.3s ease 0s;
}
/*.other_titles_cap:hover
{
margin-top: 0;
height: 150px;
}*/
.title2
{
height: 150px;
background-color: #9bfff0;
/*margin-left: 15px;*/
float: right;
}
.title2 img
{
margin-top: 0;
transition: all 0.4s ease 0s;
}
.title2:hover .other_titles_cap
{
margin-top: 0;
height: 150px;
}
.title2:hover img
{
width: 115% !important;
opacity: 0.8;
transform:translate(7%,-10%);
-ms-transform:translate(7%,-10%);
}
.title3
{
height: 150px;
background-color: #ffd5c4;
float: left;
}
.title3 img
{
/* transform: translate(7%,-10%); */
-ms-transform:translate(7%,-10%);
margin-top: 0;
transition: all 0.4s ease 0s;
}
.title3:hover .other_titles_cap
{
margin-top: 0;
height: 150px;
}
.title3:hover img
{
width: 115% !important;
opacity: 0.8;
transform:translate(7%,-10%);
-ms-transform:translate(7%,-10%);
}
<div class="other_titles">
<div class="border_slide title2">
<div class="other_titles_cap">title of Post2</div>
<img class="" src="https://i.stack.imgur.com/k6HuQ.jpg" style="width:100%">
</div>
<div class="border_slide title3">
<div class="other_titles_cap">title of Post3</div>
<img class="" src="https://i.stack.imgur.com/90ten.jpg" style="width:100%">
</div>
</div>
jsfiddle
Solution is using flex css.
Check the class other_titles
.other_titles {
display: flex;
flex-direction: row;
align-items: center;
height: 100vh;
justify-content: space-around;
}
I'm having some CSS issues I hope someone here can help with. I basically am trying to set a group of inline divs that slide out to the right, expanding to the width of the parent div containing them. As you can see from the jsfiddle I have (https://jsfiddle.net/0o9bw101/), the divs expand to the width of the parent div, instead of expanding only to the rightmost border of the parent div. If anyone can help I'd be quite thankful. Thank you in advance!
Here is the CSS I'm using in case you want to see it here:
.container {
width: 70%;
height: 100px;
background-color: black;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
vertical-align: top;
color: black;
}
.greyDiv:hover {
transition: 2s;
width: 70%;
position: absolute
}
Try this
.container {
width: 70%;
height: 100px;
background-color: black;
position:relative;
overflow:hidden;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
vertical-align: top;
}
.greyDiv:hover {
transition: 2s;
width: 100%;
position: absolute;
}
<div class='container'>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
</div>
EDIT:
The trick is to add another box inside main container.
DEMO: https://jsfiddle.net/0o9bw101/3/
<div class='container'>
<div class='invisible_container'>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
</div>
</div>
previous answer:
It's hard to do when you mix parent's with in % with children margins in px.
Also having parent's position set to something other than default helps a bit.
Here is working example for you:
.container {
width: 70%;
height: 100px;
background-color: black;
position: absolute;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
margin-left: 2%;
vertical-align: top;
}
.greyDiv:hover {
transition: 2s;
width: 96%;
position: absolute
}
DEMO: https://jsfiddle.net/0o9bw101/2/
This might not be quite what you were going for, but here elements will grow to the full width of the container (left to right) regardless of it's starting position using 2 extra elements per object.
The .inner is used to grow the background to the full width
The .placeholder is used to keep the other elements from collapsing left
#keyframes grow {
from {width: 0%;}
to {width: 92%;}
}
.container {
width: 70%;
height: 100px;
position: relative;
background-color: black;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: black;
display: inline-block;
margin: 20px 4%;
vertical-align: top;
position: relative;
z-index: 2;
}
.inner {
width: 0%;
height: 100%;
display: none;
background-color: transparent;
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
.placeholder {
display: none;
background-color: transparent;
height: 60px;
width: 60px;
margin: 20px 4%;
}
.greyDiv:hover {
width: 100%;
position: absolute;
left: 0;
margin: 20px 0;
background-color: transparent;
z-index: 4;
}
.greyDiv:hover + .placeholder {
display: inline-block;
}
.greyDiv:hover .inner {
display: inline-block;
left: 4%;
width: 100%;
background-color: white;
animation-name: grow;
animation-duration: 2s;
animation-fill-mode: forwards;
z-index: 5;
}
<div class='container'>
<div class='greyDiv'>1a<div class="inner">1x</div></div><div class="placeholder"></div>
<div class='greyDiv'>2a<div class="inner">2x</div></div><div class="placeholder"></div>
<div class='greyDiv'>3a<div class="inner">3x</div></div><div class="placeholder"></div>
<div class='greyDiv'>4a<div class="inner">4x</div></div><div class="placeholder"></div>
</div>
I just made a text window which gets bigger if you hover over it (with a transition that adds more height). It basically "drops" down to the bottom and pushes away the other text. Is there any way I can make it "drop" to the bottom by 50% and "climb" up by 50%?
#tcontent {
float: left;
background-color: yellow;
height: 150px;
width: 100%;
margin-top: 100px;
}
#mcontent {
float: left;
background-color: blue;
height: 30px;
width: 100%;
height: 150px;
transition: height 0.5s ease;
}
#bcontent {
float: left;
background-color: green;
height: 30px;
width: 100%;
height: 150px;
}
#mcontent:hover {
float: left;
background-color: blue;
height: 30px;
width: 100%;
height: 250px;
}
<div id="main">
<div id="tcontent">
</div>
<div id="mcontent">
</div>
<div id="bcontent">
</div>
</div>
This is awkward through conventional means as content flows from left to right, top to bottom and #mcontent needs space to move into above. However, this can be achieved by using flexbox.
Add #main with display: flex; to get its children to use the flexbox model
Add flex-direction: column; to #main to make the children order from top to bottom
Add height: 550px; to #main to make it as high as the three children will be when #mcontent is expanded
Add justify-content: center; to #main to center the children in the middle
The principle behind it is that the elements are set to always be in the middle of #main. When #mcontent grows, it pushes #tcontent up and #bcontent because they have space to move into. As they are set to be centered #mcontent will stay in the middle.
#main {
display: flex;
flex-direction: column;
height: 550px;
justify-content: center;
}
#tcontent {
background-color: yellow;
height: 150px;
width: 100%;
}
#mcontent {
background-color: blue;
height: 150px;
transition: height 0.5s ease;
width: 100%;
}
#mcontent:hover {
height: 250px;
}
#bcontent {
background-color: green;
height: 150px;
width: 100%;
}
<div id="main">
<div id="tcontent"></div>
<div id="mcontent"></div>
<div id="bcontent"></div>
</div>
The reason the div pushes and moves down is the default behavior of CSS height is to increase the height to the bottom. You can use a new CSS3 feature: Transformation . Precisely, the scale() function.
CSS:
Add these lines to your style
#mcontent {
transition: all 0.5s ease;
}
#mcontent:hover {
-ms-transform: scale(1, 1.2);
-webkit-transform: scale(1, 1.2);
transform: scale(1, 1.2);
height: 185px;
}
Also, in your stylesheet you're using multiple height properties together, in a single block. That's not a good practice, try cleaning those up.
#mcontent {
height: 30px; /* this line is not necessary */
height: 150px; /* as this line overrides the first one */
}
Please see my fiddle here.
I used margin-top: -50px; and animated the margin as well. Used margin for the top expansion and let the bottom expansion still make the div below slide.
EDITED CSS
#mcontent {
float: left;
background-color: blue;
height: 30px;
width: 100%;
height: 150px;
transition: height 0.5s ease, margin-top 0.5s ease;
}
#mcontent:hover {
height: 250px;
margin-top: -50px;
}
Use transform: scale(1,2); as shown in the snippet.
In your css you set height multiple times, try to avoid this. Also in the :hover declaration you only need to specify the parameters who change. So in this case only transform.
#tcontent {
float: left;
background-color: yellow;
height: 150px;
width: 100%;
margin-top: 100px;
}
#mcontent {
float: left;
background-color: blue;
width: 100%;
height: 150px;
transition: all 0.5s ease;
}
#bcontent {
float: left;
background-color: green;
width: 100%;
height: 150px;
}
#mcontent:hover {
transform: scale(1,2)
}
<div id="main">
<div id="tcontent">
</div>
<div id="mcontent">
</div>
<div id="bcontent">
</div>
</div>
If you can allow the effect to occur when hovering over #main and not just #mcontent, this works perfectly:
#main > div {
transition: all 0.5s ease;
}
#tcontent {
float: left;
background-color: yellow;
height: 150px;
width: 100%;
margin-top: 100px;
}
#mcontent {
float: left;
background-color: blue;
height: 30px;
width: 100%;
height: 150px;
/* new */
position: relative;
}
#bcontent {
float: left;
background-color: green;
height: 30px;
width: 100%;
height: 150px;
transition: all 0.5s ease;
}
#main:hover #mcontent {
height: 250px;
margin-top: -50px;
}
#main:hover #tcontent {
transform: translateY(-50px);
}
Fiddle demo here.