简体   繁体   中英

Image Centering Issue Inside Parent Div Element

I used JavaScript to create a slideshow on the homepage of my site. The problem I'm having is that the image will not center to the page, despite my best efforts. I can get it centered if I set the image child element like so:

#slider img {
    left: 218px;}

but that doesn't center for different window sizes. I'm using fairly large images, but they can always be resized later. I used placeholders for the fiddle. How can you fix this?

Fiddle

U can easily center image by set up:

#slider img {
  display: block;
  margin: 0 auto;
  width: 100%; // or your own width
}

Your images inside #slider have a position: absolute . This negates the scope of the container and the image is no longer relatively positioned inside the container div#slider .

Use margin: auto and display: block without absolute positioning.

Example: https://jsfiddle.net/sukritchhabra/smndp65m/

尝试

<div align='center'>Hello world</div>

您也可以执行margin:auto

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM