将浮动左侧 div 在主体中居中

发布于 2024-08-12 06:30:48 字数 426 浏览 2 评论 0原文

我有这段代码

<body>

    <? while.. (blah blah blah){ ?>

        <div class="product">
             something
        </div>

        <? } ?>
</body>

,css文件的

.product{ width:350px; float:left; }

问题是主体没有宽度,嗯......实际上它有90%,但我的问题是在不同的分辨率上,它对我来说非常适合(1680px宽度),但是当我切换到 1024 时,产品似乎已关闭(向左对齐)

是否有可能将这些 div 在其父容器上使用非静态居中?

i have this piece of code

<body>

    <? while.. (blah blah blah){ ?>

        <div class="product">
             something
        </div>

        <? } ?>
</body>

and the css file is

.product{ width:350px; float:left; }

the problem is that body doesn´t have a width, well... actually it has, 90%, but my problem is on the different resolutions, it works perfect for me (1680px width) but when i switch to 1024 as example, the products seems to be off (aligned to the left)

is there any posibility to center those divs with a non static with on their parent container?

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

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

发布评论

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

评论(3

阳光的暖冬 2024-08-19 06:30:48

如果您想要多个 div,全部作为一个组居中,则必须将它们包装在父 div 中,然后使用边距将其居中。

您的另一个选择是使它们 display: inline; 并在正文上使用 text-align: center 将它们居中。 (或者考虑使它们成为跨度,默认为内联)

If you want multiple divs, all centered as a group, you'll have to wrap them in a parent div, then center that using margins.

Your other option would be to make them display: inline; and use text-align: center on the body to center them. (Or consider making them spans, which default to inline)

尹雨沫 2024-08-19 06:30:48

如果你关闭浮动,你可以使用

margin-right: auto;
margin-left: auto;

if you turn floating off, you can use

margin-right: auto;
margin-left: auto;
奢望 2024-08-19 06:30:48

如果您为 div 声明一个百分比宽度,然后执行 margin-left:auto;margin-right:auto;,这样可以吗?

您可以给图像一些左右填充,这应该使您的图像居中。

If you declare a width in percentage for the div, and then do margin-left:auto;, margin-right:auto;, would that be OK?

You could give the images some left and right padding and that should make your images centered.

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