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
More
report
closure
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List . At the same time, the users you @ will also receive notifications
Default sort
Time sorting
13 answers
3
There is a way, but instead of using the img tag, add background-image to p.
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List. At the same time, the users you @ will also receive notifications
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
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List . At the same time, the users you @ will also receive notifications
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
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List . At the same time, the users you @ will also receive notifications
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
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List. At the same time, the users you @ will also receive notifications
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)
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
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List. At the same time, the users you @ will also receive notifications
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
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List . At the same time, the users you @ will also receive notifications
0
First give p a position:relative; Then give img a {
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List . At the same time, the users you @ will also receive notifications
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List . At the same time, the users you @ will also receive notifications
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
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List. At the same time, the users you @ will also receive notifications
0
If you don’t consider compatibility issues, you can take a look object-fit
Answered on May 3
Comment
edit
CRIMX865 Reputation
Comments support some Markdown syntax: **bold**_italic_[link](http://example.com)> Quote`code`-
List . At the same time, the users you @ will also receive notifications