How can I center an absolutely positioned element in a div?
<body> <div style="position: absolute; left: 50%;"> <div style="position: relative; left: -50%; border: dotted red 1px;"> I am some centered shrink-to-fit content! <br /> tum te tum </div> </div></body>
Center absolute positioned div
If i understood your question then it will work as below.
You can do it in three ways.
No1 - Using position. Apply width 100% to button parent div and apply the button style as bellow.
.buy-btn{
display: inline-block; /* Display inline block */
text-align: center; /* For button text center */
position: absolute; /* Position absolute */
left: 50%; /* Move 50% from left */
bottom: 10px; /* Move 10px from bottom */
transform: translateX(-50%); /* Move button Center position */
}
No2 - Using parent div, apply width 100% to your parent div and remove the postion absolute from button.
.parentDiv {
width: 100%; /* for full width */
text-align: center; /* for child element center */
}
.buy-btn{
display: inline-block; /* Display inline block */
text-align: center; /* For button text center */
}
No3 - Using margin, apply width 100% to your parent div and remove the postion absolute from button.
.parentDiv {
width: 100%; /* for full width */
}
.buy-btn{
display: inline-block; /* Display inline block */
text-align: center; /* For button text center */
margin: 0 auto; /* For button center */
}
Centering position: absolute div in relative container in desktop and mobile
it is always safe to use transform to make the element center, see the following code
.child {
position: absolute;
width: 50px;
height: 50px;
background-color: red;
left: 50%;
top: 50%;
border-radius: 100%;
transform: translate(-50%,-50%);
}
when you use left and right positioning, it is always pushing div from the sides not from the center, so in order to make exactly at center transform is the easy fix.
Can not align absolute div in center
left: auto; right: auto
won't work like margin: 0 auto
for centering the div, but you have a fixed width and you can just set the left
property to half of what's left e.g. left: 5%
How to move div position absolute to center using css?
See comments in code. Every change has comment. Everything else is as you had it. (I just don't like inline styles, they make it so hard to read)
#main { position: relative; display: inline-block; text-align: center; border: 1px solid;}
#test { position: absolute; display: block; top: 50%; /* changed from 0 to 50% */ left: 50%; /* changed from 0 to 50% */ width: 70%; height: 100%; color: #FFF; background: rgba(0, 0, 0, .6);
transform: translate(-50%, -50%); /* added this line */}
#main img { width: 70%; height: auto; max-width: 100%; /* border: none; this is not needed */ vertical-align: middle; /* add this line */}
<div id="main"> <img src="https://i.ytimg.com/vi/SfLV8hD7zX4/maxresdefault.jpg"> <div id="test"></div></div>
Absolute Positioning to parent div
position absolute:
It will position itself according to the closest positioned ancestor.
In your case, you don't have any positioned ancestor, so your absolute elements will position to the viewport.
You have to give your ancestors a position different from static. I suggest using relative
in this case since it respects the flow of the page.
You have to add this in your CSS:
.div_1{
height:70vh;
border:1px solid blue;
margin:20px;
position: relative; // now this element is considered a positioned element.
}
.sub_div{
height:40vh;
border:1px solid green;
margin:20px;
position: relative;
}
You can read more about how position affects your elements at: https://cssreference.io/positioning/
Related Topics
How to Change Color Property of Mat-Slide-Toggle/ Overwrite CSS of Component
How to Avoid Extra Blank Page At End While Printing
Why Does Height 100% Work When Doctype Is Removed
How to Force Webkit to Redraw/Repaint to Propagate Style Changes
Css Force Image Resize and Keep Aspect Ratio
Font Awesome 5 Shows Empty Square When Using the Js+Svg Version
Column Order Manipulation Using Col-Lg-Push and Col-Lg-Pull in Twitter Bootstrap 3
Css 3 Slide-In from Left Transition
Is Embedding Background Image Data into CSS as Base64 Good or Bad Practice
How to Change the Color of an Svg Element
Is There a CSS Selector for Elements Containing Certain Text
Footer At Bottom of Page or Content, Whichever Is Lower
What Is the Default Padding And/Or Margin For a P Element (Reset Css)
Import Regular CSS File in Scss File
Difference Between Margin and Padding
How to Change the Cursor into a Hand When a User Hovers Over a List Item
Overflow-X:Hidden Doesn't Prevent Content from Overflowing in Mobile Browsers