javascript - How to center the img element in a div, as shown in the example below
给我你的怀抱
给我你的怀抱 2017-05-16 13:21:39
0
13
1826

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

  • Question on April 20
  • 2 Comment
  • Invite to answer
  • edit

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

给我你的怀抱
给我你的怀抱

reply all(13)
左手右手慢动作

Give the parent element a display: table-cell and then set text-align and vertical-align which are more common

大家讲道理

css:

    .box{
        width: auto;
        height: 100px;
        position: absolute;
    }
    .box img{
        width: 300px;//随便改
        height: 100%;
        background: green;
    }
    .box .inner{
        width: 100px;
        height: 100px;
        background: red;
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        top: 0;
        margin: auto auto;
    }

html:

<p class="box">
    <img src="aaa.png" alt="">
    <p class="inner"></p>
</p>
洪涛

position + transform

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template