CSS 挫折:如何将不同宽度的浮动图像置于 LI 标签内?

发布于 2024-09-14 19:43:37 字数 504 浏览 2 评论 0原文

我正在开发一个具有 javascript 图像旋转器的网页,但它需要能够处理不同尺寸的图像。

我用作基础的代码在包含图像的浮动 LI 外部有一个 DIV,但无论我尝试什么,我似乎都无法使图像全部居中。唯一居中的是宽度为 800 像素的那一个。

我有一种感觉,这与浮动、位置或显示有关,但我已经搞砸了几个小时了,但没有运气。

这是我的网站:

这是我借用图像旋转器代码的地方: http://www.serie3.info/s3slider/ (代码)http://www.serie3.info/s3slider/demonstration.html(演示)

感谢您提供的任何帮助提供!

I'm working on a webpage that has a javascript image rotator, but it needs to be able to handle images of different sizes.

The code that I'm using as a base has a DIV outside floating LIs which contain the images, but no matter what I try I can't seem to get the images to all be centered. The only one that's centered is the one that's the full 800px wide.

I have a feeling it's something to do with float, position, or display, but I've been messing around for hours with no luck.

Here's my site:

Here's where I borrowed the image rotator code from:
http://www.serie3.info/s3slider/ (code) http://www.serie3.info/s3slider/demonstration.html (demo)

Thanks for any help you can provide!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁轨上的流浪者 2024-09-21 19:43:37

这也有效(在 Firefox 和 Safari 中,还没有尝试过 IE)。另存为 .html 作为示例。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="description" content="A picture and caption that you can change for everyone in the world to see.">
    <meta name="keywords" content="pic and words, pic words, caption">
    <title>Pic and Words</title>
    <link href="http://picandwords.be-better.net/styles.css" rel="stylesheet" type="text/css">

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://picandwords.be-better.net/s3Slider.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#slider1').s3Slider({
                timeOut: 4000 
            });
        });
    </script>
    <style>
        body, html
        {
            margin:0px;
            width:100%;
            height:100%;
            padding:0px;
        }
        #slider1
        {
            text-align:center;
            vertical-align:middle;
            margin:0px;
            padding:0px;
            width:auto;
            height:auto;
        }
        #slider1Content
        {
            text-align:center;
            display:table; /* this is KEY to centering the UL */
            position:static;
            margin:0px auto 0px auto;
            padding:0px;
            width:auto;
            top:auto;
        }
        #pageWrapper
        {
            width:810px;
            height:700px;
            margin:0px auto 0px auto;
            text-align:center;
        }
    </style>
</head>
<body>
    <div id="pageWrapper">
        <h1>Pic and Words</h1>
        <!--<a href="new_form.php">Upload</a> -->
        <br /><br />
        <div id="slider1">
            <ul id="slider1Content">
                <li class="slider1Image">
                    <div>
                        <img src="http://picandwords.be-better.net/uploaded_pics/1282179782-2zdr66e.jpg" 
                        alt="Somewhere down Baja on the way to Cabo." 
                        title="Somewhere down Baja on the way to Cabo." align="center" />
                        <span class="bottom">"Somewhere down Baja on the way to Cabo."</span>
                    </div>
                </li>
                <li class="slider1Image">
                    <div>
                        <img src="http://picandwords.be-better.net/uploaded_pics/1282180309-bicycle.jpg" 
                        alt="Drunk dude on a bike" 
                        title="Drunk dude on a bike" align="center" />
                        <span class="bottom">"Drunk dude on a bike"</span>
                    </div>
                </li>
                <li class="slider1Image">
                    <div>
                        <img src="http://picandwords.be-better.net/uploaded_pics/1282180338-captions03211.jpg" 
                        alt="Do not want!" 
                        title="Do not want!" align="center" />
                        <span class="bottom">"Do not want!"</span>
                    </div>
                </li>           
                <div class="clear slider1Image"></div>
            </ul>
        </div>
    </div>
</body>
</html>

This works as well (In Firefox and Safari, haven't tried IE). Save as a .html for an example.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="description" content="A picture and caption that you can change for everyone in the world to see.">
    <meta name="keywords" content="pic and words, pic words, caption">
    <title>Pic and Words</title>
    <link href="http://picandwords.be-better.net/styles.css" rel="stylesheet" type="text/css">

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://picandwords.be-better.net/s3Slider.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#slider1').s3Slider({
                timeOut: 4000 
            });
        });
    </script>
    <style>
        body, html
        {
            margin:0px;
            width:100%;
            height:100%;
            padding:0px;
        }
        #slider1
        {
            text-align:center;
            vertical-align:middle;
            margin:0px;
            padding:0px;
            width:auto;
            height:auto;
        }
        #slider1Content
        {
            text-align:center;
            display:table; /* this is KEY to centering the UL */
            position:static;
            margin:0px auto 0px auto;
            padding:0px;
            width:auto;
            top:auto;
        }
        #pageWrapper
        {
            width:810px;
            height:700px;
            margin:0px auto 0px auto;
            text-align:center;
        }
    </style>
</head>
<body>
    <div id="pageWrapper">
        <h1>Pic and Words</h1>
        <!--<a href="new_form.php">Upload</a> -->
        <br /><br />
        <div id="slider1">
            <ul id="slider1Content">
                <li class="slider1Image">
                    <div>
                        <img src="http://picandwords.be-better.net/uploaded_pics/1282179782-2zdr66e.jpg" 
                        alt="Somewhere down Baja on the way to Cabo." 
                        title="Somewhere down Baja on the way to Cabo." align="center" />
                        <span class="bottom">"Somewhere down Baja on the way to Cabo."</span>
                    </div>
                </li>
                <li class="slider1Image">
                    <div>
                        <img src="http://picandwords.be-better.net/uploaded_pics/1282180309-bicycle.jpg" 
                        alt="Drunk dude on a bike" 
                        title="Drunk dude on a bike" align="center" />
                        <span class="bottom">"Drunk dude on a bike"</span>
                    </div>
                </li>
                <li class="slider1Image">
                    <div>
                        <img src="http://picandwords.be-better.net/uploaded_pics/1282180338-captions03211.jpg" 
                        alt="Do not want!" 
                        title="Do not want!" align="center" />
                        <span class="bottom">"Do not want!"</span>
                    </div>
                </li>           
                <div class="clear slider1Image"></div>
            </ul>
        </div>
    </div>
</body>
</html>
小…楫夜泊 2024-09-21 19:43:37

添加:

left: 0;
padding: 0;

#slider1Content 样式。

然后从 .slider1Image 样式中删除 float: left;

请注意,源 CSS 包含此警告:在多个维度上“重要的是与图像宽度相同”。由于此 CSS 并未针对每个图像调整大小,因此每张图片周围都会有白色边距。

如果您希望动态重置这些关键维度(我认为没有这个网站看起来还不错),那么这是一个单独的问题,之前已经在这里回答过。

Add:

left: 0;
padding: 0;

to the #slider1Content style.

Then delete the float: left; from the .slider1Image style.

Note that the source CSS contains this warning: "important to be same as image width" on several dimensions. Since this CSS isn't sized for each image, you'll have that white margin around each picture.

If you desire to reset those key dimensions dynamically (I think the site looks OK without this), then that is a separate question that has been answered here on SO before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文