固定 DIV 问题中的另一个居中 DIV

发布于 2024-11-15 12:06:45 字数 1719 浏览 11 评论 0原文

我在这里尝试一个非常简单的过程,基本上是尝试将客户徽标集中在这个主要客户 div 中。我最近刚刚开始从事网页设计业务,虽然我可以阅读一些解决方案,但我在将它们应用到我的结构中时遇到了困难。

基本上我有几个客户端框,每个客户端框内都有一个 PNG 图像:

            <div id="clients">
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
            </div>

我希望能够将客户端框集中在具有固定权重的客户端 div 上。我尝试过使用 display: inline-block 但这似乎没有多大作用。我假设这是因为我已经强迫它们浮动:向左,但我不知道如何在不这样做的情况下保持它们在 div 中的位置。就像我说的,我对 CSS 是个新手,这就是我为所有 CSS 所做的事情。

这是我为客户端和客户端框 CSS 所做的:

           #clients {
           background-image: url("img/images/clients_bg.png");
           border-bottom: 1px solid #333333;
           border-top: 1px solid #666666;
           float: left;
           margin-top: 120px;
           padding: 10px;
           width: 778px;
           }
           .client-box {
           background: none repeat scroll 0 0 #bcb546;
           float: left;
           font-family: verdana;
           font-size: 11px;
           height: 60px;
           margin-right: 10px;
           opacity: 0.8;
           padding-top: 40px;
           text-align: center;
           width: 100px;
           }
           .client-box:hover {
           opacity: 1;
           }

根据我的理解,这应该不难实现,但到目前为止我还没有任何运气,可能是因为我的大脑专注于某种做事方式,而它只是不会让步。任何帮助将不胜感激。

您可以在此处查看实时网站。

谢谢你。

I am attempting a very simple procedure here, basically trying to center the client logos within this main clients div. I've just recently started with this web design business and while I can read some of the solutions out there, I'm having trouble applying them to my structure.

Basically I have a few client boxes, each is going to have a PNG image inside of them:

            <div id="clients">
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
            </div>

I'd like to be able to center the client-box's on the client's div that has a fixed weight. I've tried using display: inline-block but that didn't seem to do much. I'm assuming that's because I've already forced them to float: left but I don't know how I can maintain their position in the div without doing so. Like I said I'm quite a novice with CSS and this is what I've been doing for all my CSS.

Here's what I have for clients and client-box CSS:

           #clients {
           background-image: url("img/images/clients_bg.png");
           border-bottom: 1px solid #333333;
           border-top: 1px solid #666666;
           float: left;
           margin-top: 120px;
           padding: 10px;
           width: 778px;
           }
           .client-box {
           background: none repeat scroll 0 0 #bcb546;
           float: left;
           font-family: verdana;
           font-size: 11px;
           height: 60px;
           margin-right: 10px;
           opacity: 0.8;
           padding-top: 40px;
           text-align: center;
           width: 100px;
           }
           .client-box:hover {
           opacity: 1;
           }

From my understanding, this shouldn't be hard to achieve, but so far I have not had any luck probably because my brain is fixated on a certain way of doing things and it just won't budge. Any help would be greatly appreciated.

You can see the live site here.

Thank you SO.

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

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

发布评论

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

评论(3

烟─花易冷 2024-11-22 12:06:45

我尝试过使用 display: inline-block
但这似乎并没有多大作用。

float: left 强制 display: block,因此 display: inline-block 不会产生任何效果。

.client-box 上,您需要:

  • 删除 float: left
  • 添加 display: inline-block

最后,在父元素(#clients)上,您需要添加text-align: center

在此处输入图像描述

I've tried using display: inline-block
but that didn't seem to do much.

float: left forces display: block, so display: inline-block would have no effect.

On .client-box, you need to:

  • remove float: left
  • add display: inline-block.

Finally, on the parent element (#clients), you need to add text-align: center.

enter image description here

梦醒灬来后我 2024-11-22 12:06:45

如果外部 div 具有固定宽度,您可以设置内部 div 的边距以占据适当的空间。
例如:

<div class="outer">
    <div class="inner">
        stuff
    </div>
</div>

CSS

.outer { width: 600px; }
.inner { width: 400px; margin-left: 100px; margin-right: 100px; }

或者您可以使用 margin-left: auto;右边距:自动;然而,这(就像世界上其他一切一样)在 IE 中不起作用。

希望这有帮助!

If your outer div is of a fixed width you can set the margins for the inner div to take up the appropriate space.
eg:

<div class="outer">
    <div class="inner">
        stuff
    </div>
</div>

CSS

.outer { width: 600px; }
.inner { width: 400px; margin-left: 100px; margin-right: 100px; }

Alternatively you can use margin-left: auto; margin-right: auto; however that (like everything else in the world) doesn't work in IE.

Hope this helps!

无边思念无边月 2024-11-22 12:06:45

我将向客户端框添加一个包装器,其宽度等于客户端框的总宽度。

例如,在您上面发布的实时站点中,有 5 个客户端框,每个框的宽度为 100 像素,右边距为 10 像素。因此,添加一个宽度为 5 x (100 +10) = 550px 的 div 包装器,并将包装器以“margin-left:auot”和“margin-right:auto”居中。

<div style="width: 550px;margin-left: auto; margin-right: auto;">

                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
                <div class="client-box">CLIENT LOGO</div>
</div>

I will add a wrapper to the client-boxes, whose width is equal to the total width of client-boxes.

For example, in the live site you post above, there are 5 client boxes, and each of them are 100px width with 10px margin-right. So add a div wrapper with width 5 x (100 +10) = 550px, and center the wrapper with "margin-left:auot" and "margin-right:auto".

<div style="width: 550px;margin-left: auto; margin-right: auto;">

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