I have a question that is more than spoken, but I can't find a solution.
I have this HTML:
<section class="section" id="section-2">
<div class="wrapper" id="wrapper">
<div class="slider-container" id="slider-container">
<figure class="panel-image">
<img src="imgs/projects/3d-particles-image.png">
</figure>
<figure class="panel-image">
<img src="imgs/projects/3d-particles-image.png">
</figure>
</div>
</div>
</section>
And this CSS:
.wrapper {
position: absolute;
width: 100%;
height: 100%;
overflow: hidden;
}
.slider-container {
position: absolute;
width: 100%;
height: 100%;
}
.panel-image {
position: absolute;
width: 100%;
height: 100%;
text-align: center;
line-height: 100%;
}
.panel-image > img { ????????? }
I need center the img into figure, but the position of "panel-image" need to be absolute. Any suggestions?
Thanks a lot!
An <img> element is an inline element (display value of inline-block ). It can be easily centered by adding the text-align: center; CSS property to the parent element that contains it.
Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.
To center an image with CSS Grid, wrap the image in a container div element and give it a display of grid . Then set the place-items property to center. P.S.: place-items with a value of center centers anything horizontally and vertically.
To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.
Something like this works in modern browsers:
.panel-image > img {
position: absolute;
left: 50%;
top: 50%;
-webkit-transform:translate(-50%, -50%);
-moz-transform:translate(-50%, -50%);
-ms-transform:translate(-50%, -50%);
-o-transform:translate(-50%, -50%);
transform:translate(-50%, -50%);
}
There are more elaborate tricks for older browsers.
Thanks @ralph.m! Works fine!!
I find another solution:
.panel-image {
position: absolute;
width: 100%;
height: 100%;
text-align: center;
display: flex;
}
.panel-image > img {
margin: auto;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With