html - 在div中居中图像

标签 html css

我正在尝试让这个图库中的图像自动居中,但无论我做什么,它们都拒绝居中。是的,我知道不应该再使用了,但它仍然可以工作。请帮忙 HTML

<div id='gallery_gallery'>
            <center>
                    <div id='imageList'>
                    <center>
                    <?php
                        $directory = 'images/gallery';
                        $files = scandir($directory);
                        $file_length = sizeof($files);
                        $to = 16;
                        $start = 2;
                        if($to > $file_length) $to = $file_length;
                        for($i=$start; $i <= $start-1+$to; $i++)
                        {
                            echo "<div class='galleryImage'><img src='images/gallery/".$files[$i]."' width='100%' onclick='showImage(\"".$files[$i]."\")'></div>";
                        }
                    ?>
                    </center>
                    </div>
                </center>
            </div>

CSS

#gallery_gallery
{
position: absolute;
top: 75px;
left: 80px;
width: 825px;
height: 960px;
}
#imageList
{
background-color: yellow;
width: 825px;
text-align: center;
}
.galleryImage
{
width: 180px;
height: 180px;
float: left;
overflow: hidden;
}
.galleryImage img
{
min-width: 100%;
min-height: 100%;
padding: 10px;
}
.galleryImage img:hover
{
cursor: pointer;
}

结果 http://sonny.hostingsiteforfree.com/gallery.html

最佳答案

我看到 CSS 不符合标准。

ImageWrapper galleryImageoverflow:hidden 图像 galleryImage imgpadding:10px 这会导致一切在这里变得不稳定。

我创建了一个 fiddle在这里您可以检查更改...

CSS:

#gallery_gallery {
    position: absolute;
    top: 75px;
    left: 80px;
    width: 825px;
    height: 960px;
    text-align:center;
    background:#777;
}
#imageList {
    background-color: yellow;
    width: 825px;
    text-align: center;
    margin-left:10px;
}
.galleryImage {
    width: 180px;
    height: 180px;
    float: left;
    overflow: hidden;
    margin:10px 10px;
}

.galleryImage img {
    min-width: 100%;
    min-height: 100%;

}
.galleryImage img:hover {
    cursor: pointer;
}

另外,我们需要关闭PHP代码中的img标签。使用标签 />

的自闭合

这会导致您的图像样式无法正常工作。

 echo "<div class='galleryImage'><img src='images/gallery/".$files[$i]."' width='100%' onclick='showImage(\"".$files[$i]."\")' /></div>";

检查 fiddle 是否正常工作.. :)

关于html - 在div中居中图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24901679/

相关文章:

javascript - Paper js 'dot' 路径在 Firefox 中显示,但在 IE 或 Chrome 中不显示

javascript - 调用replaceWith会出现 "not a function"错误(用文本和html的混合替换文本节点)

html - 导航栏不会在 Bootstrap 3 中居中

jquery - 鼠标移动时重新定位工具提示

javascript - 谷歌加一个按钮

javascript - 当文本溢出 div 时将其换行

html、css水平和垂直分割器

javascript - 如何优雅地从 DOM 中添加和删除元素

css - 相对定位的 <li> 中的绝对定位的 <img> 适用于 chrome 和 IE11,而不是 firefox

javascript - 如何删除具有相同 url 的 div 并仅保留值?