search

Home  >  Q&A  >  body text

javascript - How to center the img element in a div, as shown in the example below

Html code

<style>
    p {
        overflow: hidden ;
    }
    img {
        height: 100% ;
        width: auto ;
    }
</style>

<p>
    <img src='//foo.com/foo.jpg' />
</p>

Example picture of desired effect

The red area is p, the green area is img

In other words, img is wider

The width of img is not fixed, so you cannot use fixed margin-left to solve it

Default sort Time sorting

13 answers

3

There is a way, but instead of using the img tag, add background-image to p.

p {
    background-image: url(image-url);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;
}

  • Answered on April 20
  • Comment
  • edit

Mr. Xiang227 Reputation

1

Positioning picture marginleft:-(width/2) As for the width of img is not fixed, it can be obtained by js and then dynamically set marginleft

  • Answered on April 20
  • Comment
  • edit

Look at the world with a cold eye177 Reputation

1

Wrap another layer.

<p id="wrapper">
    <p id="wrapper2">
        <img src="demo_img3.jpg" alt="" />
    </p>
</p>
#wrapper{
    position: relative;
    width: 200px;
    height: 200px;
}
#wrapper2{
    position: absolute;
    left: 50%;
}
img{
    margin-left: -50%;
}
The width of

wrapper2 is the img width. The margin-left of img is equal to half of its own width, which is equivalent to left:-50%*width

  • Answered on April 20 · Updated on April 20
  • 2 Comment
  • edit

toBeTheLight4.9k Reputation

1

img is an inline-block element and can be directly text-aligned at the parent level: center;

  • Answered on May 3
  • Comment
  • edit

hugangqiang117 Reputation

1

In fact, we want to achieve the effect of horizontal centering. Here are four methods to achieve horizontal centering (Note: In each example below, the alignment operation of the child element is implemented, and the parent container of the child element is the parent element)

Use inline-block and text-align to achieve

.parent{
    text-align: center;
}
.child{
    display: inline-block;
}

Advantages: good compatibility;

Disadvantages: need to set child elements and parent elements at the same time

Use margin:0 auto to implement

.child{
    width:200px;
    margin:0 auto;
}

Advantages: good compatibility

Disadvantages: need to specify the width

Use table implementation

.child{
    display:table;
    margin:0 auto;
}

Advantages: You only need to set yourself

Disadvantages: IE6 and 7 need to adjust the structure

Use absolute positioning to implement

.parent{
    position:relative;
}
.child{
    position:absolute;
    left:50%;
    transform:translate(-50%);
}

Shortcomings: poor compatibility, available for IE9 and above

Practical flex layout implementation

the first method

.parent{
    display:flex;    
    justify-content:center;
}

The second method

.parent{
    display:flex;
}
.child{
    margin:0 auto;
}

Disadvantages: Poor compatibility, if a large area is laid out, efficiency may be affected

  • Answered on May 5
  • Comment
  • edit

yogi27 Reputation

0

P is wrapped outside. When img.onload, js calculates the width and assigns it to the outer layer. Center the outer content

  • Answered on April 21
  • Comment
  • edit

Jessica_loli16 Reputation

0

First give p a position:relative;
Then give img a {

position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);

}
If it is not compatible with c3, you can give img{

position: absolute;
left: 0;
top: 0;
bottom:0;
right:0;
margin:auto;

}

  • Answered on April 21
  • Comment
  • edit

clownzoo11 Reputation

0

1

img{
    display: block;
    margin: 0 auto;
}

2

p{
    text-align: center;
    
    img{
         display: inline-block;
    }
}

3

p{
    position: relative;
    
    img{
        position: absolute;
        left: 50%;
        top: 50%;
        margin: -(图片高度/2) 0 0 -(图片宽度/2)
    }
}

4 Use flex

  • Answered on April 21 · Updated on April 21
  • Comment
  • edit

Lone Wolf in the Wind227 Reputation

0

There is a layer of p on the outside and a layer of p on the inside to limit the size of the picture

  • Answered on May 3
  • Comment
  • edit

Fujinishi22 Reputation

0

If you don’t consider compatibility issues, you can take a look object-fit

  • Answered on May 3
  • Comment
  • edit

CRIMX865 Reputation

0

给我你的怀抱给我你的怀抱2773 days ago1817

reply all(13)I'll reply

  • 黄舟

    黄舟2017-05-16 13:23:39

    There is a way, but instead of using img tag, add background-image to p.

    p {
        background-image: url(image-url);
        background-repeat: no-repeat;
        background-size: cover;
        background-position: center;
    }

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 13:23:39

    Positioning picture marginleft:-(width/2) As for the width of img is not fixed, it can be obtained by js and then dynamically set marginleft

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:23:39

    Wrap another layer.

    <p id="wrapper">
        <p id="wrapper2">
            <img src="demo_img3.jpg" alt="" />
        </p>
    </p>
    #wrapper{
        position: relative;
        width: 200px;
        height: 200px;
    }
    #wrapper2{
        position: absolute;
        left: 50%;
    }
    img{
        margin-left: -50%;
    }

    wrapper2的宽度是img宽度。imgmargin-left就等于自身宽的一半,相当于left:-50%*width

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:23:39

    img is an inline-block element and can be directly text-aligned at the parent level: center;

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:23:39

    In fact, we want to achieve the effect of horizontal centering. Here are four methods to achieve horizontal centering (Note: In each example below, the alignment operation of the child element is implemented, and the parent container of the child element is the parent element)

    Use inline-block and text-align to implement

    .parent{
        text-align: center;
    }
    .child{
        display: inline-block;
    }

    Advantages: good compatibility;

    Disadvantages: need to set child elements and parent elements at the same time

    Use margin:0 auto to achieve

    .child{
        width:200px;
        margin:0 auto;
    }

    Advantages: Good compatibility

    Disadvantages: Need to specify the width

    Use table to implement

    .child{
        display:table;
        margin:0 auto;
    }

    Advantages: You only need to set up yourself

    Disadvantages: IE6 and 7 need to adjust the structure

    Using absolute positioning

    .parent{
        position:relative;
    }
    .child{
        position:absolute;
        left:50%;
        transform:translate(-50%);
    }

    Disadvantages: poor compatibility, available for IE9 and above

    Practical flex layout implementation

    The first method

    .parent{
        display:flex;    
        justify-content:center;
    }

    The second method

    .parent{
        display:flex;
    }
    .child{
        margin:0 auto;
    }

    Disadvantages: poor compatibility, if large-area layout is carried out, efficiency may be affected

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:23:39

    Wrap p outside. When img.onload, js calculates the width and assigns it to the outer layer. Center the outer content

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:23:39

    First give p a position:relative;
    and then give img a {

    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    }
    If it is not compatible with c3, you can give img{

    position: absolute;
    left: 0;
    top: 0;
    bottom:0;
    right:0;
    margin:auto;

    }

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 13:23:39

    1

    img{
        display: block;
        margin: 0 auto;
    }

    2

    p{
        text-align: center;
        
        img{
             display: inline-block;
        }
    }
    

    3

    p{
        position: relative;
        
        img{
            position: absolute;
            left: 50%;
            top: 50%;
            margin: -(图片高度/2) 0 0 -(图片宽度/2)
        }
    }
    

    4 用flex

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:23:39

    One layer of p on the outside, and one layer of p on the inside to limit the size of the picture

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 13:23:39

    You can take a look if you don’t consider compatibility issues object-fit

    reply
    0
  • Cancelreply