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>
Centering an image vertically in a div with a dynamic height
You can give the parents divs relative positioning and the images absolute positioning:
img { width:25px; position:absolute; margin:auto; top:0; bottom:0;}.container-fluid > div { text-align:center; height: calc(100vh/5); position:relative;}.container-fluid > div:nth-child(odd) { background:yellow;}
<div class="container-fluid"> <div> <img src="http://lorempixel.com/output/food-h-g-164-166-5.jpg"> </div> <div> <img src="http://lorempixel.com/output/food-h-g-164-166-5.jpg"> </div> <div> <img src="http://lorempixel.com/output/food-h-g-164-166-5.jpg"> </div> <div> <img src="http://lorempixel.com/output/food-h-g-164-166-5.jpg"> </div> <div> <img src="http://lorempixel.com/output/food-h-g-164-166-5.jpg"> </div></div>
How do I horizontally center an absolute positioned element inside a 100% width div?
If you want to align center on left attribute.
The same thing is for top alignment, you could use margin-top: (width/2 of your div), the concept is the same of left attribute.
It's important to set header element to position:relative.
try this:
#logo {
background:red;
height:50px;
position:absolute;
width:50px;
left:50%;
margin-left:-25px;
}
DEMO
If you would like to not use calculations you can do this:
#logo {
background:red;
width:50px;
height:50px;
position:absolute;
left: 0;
right: 0;
margin: 0 auto;
}
DEMO2
Align in middle dynamic image inside slider with absolute position
To vertical align to middle, I like to use:
.elem {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
Of course, you need all the vendor prefix (-webkit-transform, -moz-transform).
This is compatible with all majors browsers (IE9 and up)
How do I centre absolutely positioned content of unknown width?
Either use JavaScript to calculate the width and move it,
use a CSS hack to move the element right and left by 50%,
or don't absolutely position it.
This answer is incredibly short, but it is to the point. If you require something to be centralised (meaning you would like the browser to determine where the centre point is, and position it there), then you can't use absolute positioning - because that takes away control from the browser.
To fade the paintings in over each other I need to position them
'absolute' to stop them from stacking.
This is where your problem lies. You have assumed that you need absolute positioning for the wrong reason.
If you are experiencing problems placing elements on top of each other, wrap the images in an absolutely positioned container which is 100% width and has text-align: center
If you do feel that absolute positioning is necessary, the following hack can be used to achieve your desired results:
div {
position: absolute;
/* move the element half way across the screen */
left: 50%;
/* allow the width to be calculated dynamically */
width: auto;
/* then move the element back again using a transform */
transform: translateX(-50%);
}
Obviously the above hack has a terrible code smell, but it works on some browsers. Be aware: this hack is not necessarily obvious to other developers, or other browsers (especially IE or mobile).
How do you horizontally center an element with position: absolute?
Generally, when an element is position: absolute
the parent is position: relative
.
Then, the absolute element is left: 50%; transform: translateX(-50%);
for horizontal centering. For vertical centering top: 50%; transform: translateY(-50%);
.parent { position: relative; height: 200px; // needs height/width cause 'absolute' child takes no space width: 200px; border: 1px solid red;}
.child-1 { position: absolute; height: 50px; width: 50px; border: 1px solid blue; left: 50%; transform: translateX(-50%);}
.child-2 { position: absolute; height: 50px; width: 50px; border: 1px solid orange; top: 50%; transform: translateY(-50%);}
.child-3 { position: absolute; height: 50px; width: 50px; border: 1px solid green; top: 50%; left: 50%; transform: translate(-50%, -50%);}
<div class='parent'> <div class='child-1'>horizontal</div> <div class='child-2'>vertical</div> <div class='child-3'>both</div><div>
Related Topics
How to Specify the Maximum Number of Columns Repeat() Will Create Using Auto-Fit/Fill
Incremental Slides Do Not Work with a Two-Column Layout
What's the Difference Between CSS Inherit and Initial
Css: What's a Good Way to Create a Raised Box Effect
Ie11 Flexbox Preventing Text Wrapping
Do I Need to Generate a CSS File from Pygments for My Jekyll Blog, to Enable Colorful Code Snippet
(Css) Eliminating Browser's 'Selected' Lines Around a Hyperlinked Image
Node + React - Hyphenated CSS Class Names
How to Change the Play Icon of Embedded Youtube Videos
How Bad Is It in Practice to Over-Nest Selectors in SASS/Scss
Css: How to Set Container Size Equal to Background Image Size
Css: Auto Resize Div to Fit Container Width
Bootstrap 3 Responsive Desktop and Mobile Layout
Is There Any Guide on "When to Use Display:Block When :Inline and When :Inline-Block" and Why
How to Make Double Lines Border in CSS, Each Line in Different Color, Without Using Background Image