Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center image into figure

Tags:

html

css

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!

like image 336
eledgaar Avatar asked Aug 10 '14 21:08

eledgaar


People also ask

How do you center an image inside a figure?

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.

How do I center an image in a figure in HTML?

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.

How do I center an image in a figure in CSS?

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.

How do I horizontally center an image in a div?

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.


2 Answers

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.

like image 168
ralph.m Avatar answered Dec 03 '22 16:12

ralph.m


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;
}
like image 30
eledgaar Avatar answered Dec 03 '22 16:12

eledgaar