在 css 中使用相邻选择器的边距

发布于 2024-09-25 21:13:24 字数 659 浏览 4 评论 0原文

我正在尝试在绝对定位的 div 中显示多个图像。 div 将文本对齐设置为居中,这很好地将图像置于中心。然后,我为图像的相邻同级选择器添加了左边距规则,为它们提供了一点空间,但又不会弄乱所有居中的优点。

默认情况下,图像是静态内联块元素。

.mydiv img + img
{
    margin-left:20px;
}

这让我的图像彼此之间有一点空间。但是,当图像溢出 div 时堆叠在另一个图像的顶部时,边距仍然适用,因此下面的图像不会垂直对齐。我想这是因为他们仍然是兄弟姐妹,他们只是在视觉上分开。

有没有其他方法可以使用 CSS 来实现此目的?我的 div 是液体,所以我真的不想一直使用固定边距。

这是一个示例图像: http://i54.tinypic.com/w8aogp.png

如您所见,第二行及下面的图像偏移了我想要的图像之间的边距。当然,我希望它们垂直对齐。如果我可以以某种方式使用选择器来表示“img 前面有隐式换行符”并删除边距?

不用介意“顶部”边距 - 无论图像是否直接与白色容器 div 相邻,它都将是一个固定数字。但是,每当 img 直接(视觉上)与容器 div 相邻时,我想将左边距归零。

I'm trying to display a number of images in an absolutely positioned div. The div has text-align set to center, which nicely puts the images in the center. I then added a margin-left rule with the adjacent sibling selector for the images, to give them a bit of space, but not messing up all the centering goodness.

Images are static inline-block elements, as per default.

.mydiv img + img
{
    margin-left:20px;
}

This give my images a bit of space between eachother. However, when the images gets stacked on top of another when they overflow the div, the margin still applies, so that the image below is not vertically aligned. I guess this is because they are still siblings, they are only visually separated.

Is there another way to achieve this using CSS? My div is liquid, so I don't really want to use fixed margins all the way..

Here is an example image: http://i54.tinypic.com/w8aogp.png

As you can see, the second row of images and below is offset by the margin I want between images. I would like them to align vertically, of course. If I could somehow use a selector for something like "img preceded by an implicit new-line" and remove the margin?

Never mind the "top" margin - it will be a fixed number regardless if the image is directly adjacent to the white container div or not. However, I want to zero the left-margin whenever an img is directly adjacent (visually) to the container div.

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

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

发布评论

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

评论(1

孤独难免 2024-10-02 21:13:24

如果您的父 div 具有可变宽度,并且您的图像保持浮动,我认为确保图像等距的唯一方法是声明 margin: 10px (在所有边上),或者,至少在左侧和右侧。这样,无论每行有多少图像,它们都将始终正确对齐。

另一方面,如果父 div 具有固定宽度,并且您确切知道每行有多少图像,则可以在每个 X 图像中插入一个空白 div将使您当前的选择器按需要工作,或者您可以创建一个新的分隔符类,例如 .sep ,您将为其声明 margin-left: 20px; 并将其分配给除连续第一张图像外的所有图像。

除此之外,我认为没有任何其他纯 CSS 解决方案。

If your parent div has a variable width, and your images are left floated, I think the only way to make sure your images are equally spaced is to declare margin: 10px (on all sides), or, at least on the left and right side. That way, regardless of how many images you have per row, they will always be properly aligned.

On the other hand, if the parent div has a fixed width and you know exactly how many images per row you have, you can either insert a blank div every X images which will make your current selector work as desired, or you can create a new separator class, say .sep for which you would declare margin-left: 20px; and assign it to all but the first image in a row.

Other than that, I don't think there are any other pure CSS solutions.

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