【问题标题】:Align three images to spread across the page evenly对齐三个图像以均匀分布在页面上
【发布时间】:2011-10-28 14:28:26
【问题描述】:

我有一个围绕三个子 div 的“主”div。每个子 div 包含一个图像。我的目标是让三个图像在我的页面上均匀对齐。图像大小相同(200 像素 x 166 像素)。如果有某种方法可以在不使用十几个 div 的情况下做到这一点,那将是极好的。重申我的问题,我不希望所有三个图像都居中。我希望左边的 img 距页面左侧 30 像素;正确的 img 距离页面右侧 30 像素,我希望中心 img 完全位于两者之间。

HTML:

    <div id="aboutimages">
        <div id="aboutimgleft">
            <img src="Stylesheets/images/manpc.jpg" alt="" />
        </div>
        <div id="aboutimgcenter">
            <img src="Stylesheets/images/coffee.jpg" alt="" />
        </div>
        <div id="aboutimgright">
            <img src="Stylesheets/images/keyboard.jpg" alt="" />
        </div>
    </div>

CSS:

#aboutimages{
text-align: center;
    width: 100%;
    margin: 0 auto;
    float:left;
    }

#aboutimgleft img{
    float:left;
    padding-bottom:20px;
    padding-top:20px;
    padding-left:0px;
    width:250px;
} 

#aboutimgcenter {
    margin: 0 auto; 
    width: 70%;
}

#aboutimgcenter img{
    text-align:center;
    width:250px;
    height:166px;
    padding-bottom:20px;
    padding-top:20px;
}

#aboutimgright img{
    float:right;
    padding-bottom:20px;
    padding-top:20px;
    padding-right:0px;
    width:250px;
    height:166px;
} 

我迫切需要任何类型的帮助 - 这让我非常沮丧。

谢谢,

埃文

【问题讨论】:

    标签: css html image center


    【解决方案1】:

    有几种方法可以解决这个问题,这里有一种方法:

    #aboutimages {
        position:relative; /* constrain absolutely positioned child elements */
    }
    
    #aboutimgleft {
        float:left; /* Float the first element */
        margin-left:30px; /* Here's that 30px you wanted */
    } 
    
    #aboutimgcenter {
        margin: 0 auto; /* Center the second element */
    }
    #aboutimgright {
        /* easier than float:right, but that could work too with some tweaking */
        position:absolute;
        top:0;
        right:30px; /* Here's that other 30px you wanted */
    }
    

    只要图像在这些 div 中,它们就会对齐。

    演示:http://jsfiddle.net/KPJP4/

    【讨论】:

    • 由于某种原因,同时使用您的代码和我的代码,右侧的图像总是不正常。
    • 我不建议您将您的代码与我的代码混合,而是使用我提供的代码作为基础。这是另一个使用&lt;img&gt; 标签的版本:jsfiddle.net/KPJP4/1 如果您可以向我展示您在使用此代码时遇到的问题,我很乐意提供帮助。
    • SO上没有私信系统。当问题仍然存在时,您可以将您的 URL 添加到您的帖子中以供参考,但我看不出这个解决方案如何不完全符合您的要求。您可能需要确保没有其他样式干扰。
    • 好的,我看到两个问题:1) 你的#aboutimages div 不正常,尝试添加float:left;width:100%; 2) 3 个 div 需要具有相同的宽度您为图像指定的。这在 FF5 中为我解决了问题。
    【解决方案2】:

    应该这样做:

    <style type="text/css">
    #aboutimgleft, #aboutimgcenter, #aboutimgright {
        float: left; 
        width: 33.33%;
    }
    
    #aboutimgleft img {
        margin-left: 30px;
    }
    
    #aboutimgcenter {
        text-align: center;
    }
    
    #aboutimgright {
        text-align: right;
    }
    
    #aboutimgright img {
        margin-right: 30px;
    }
    </style>
    

    【讨论】:

    • 唯一的问题是 33.33% 不准确,在第 3 个 div 之后右侧可能会出现 1px 的间隙。
    • 确实如此。我希望它是一个不需要定位的快速解决方案(因为我不知道页面内还有什么)。定位可用于使其像素完美。
    • 有趣的是,他在 3 个包装 div、固定宽度的图像和固定宽度的布局上有单独的 id。这个特殊问题可以通过简单的浮动和边距来解决,事实上,对于一个简单的解决方案来说,包装 div 甚至不是必需的。
    【解决方案3】:

    实现此功能的一种方法是将外包装设置为特定宽度并将其设置为 text-align: center;,然后将内部的所有 div 设置为 display: inline-block; 并分别浮动。我在这里用你的代码设置了一个测试页面(稍微简化了一点):http://jsbin.com/uworex 所以你可以看到它现在是如何为我工作的。

    【讨论】:

    • 嗯,这很奇怪。你有什么浏览器和分辨率?这是一个稍微不同的版本,它使用 100% 的宽度而不是 1000px:http://jsbin.com/uworex/2/
    猜你喜欢
    • 2021-07-16
    • 2014-03-09
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 2015-03-28
    • 2013-04-03
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多