简体   繁体   English

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

[英]Margin using adjacent selector in css

I'm trying to display a number of images in an absolutely positioned div. 我正在尝试在绝对定位的div中显示许多图像。 The div has text-align set to center, which nicely puts the images in the center. div的文字对齐设置为居中,可以很好地将图像居中。 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. 但是,当图像在div溢出时堆叠在另一个图像之上时,仍然会留有边距,因此下面的图像不会垂直对齐。 I guess this is because they are still siblings, they are only visually separated. 我猜这是因为他们仍然是兄弟姐妹,只是在视觉上分开。

Is there another way to achieve this using CSS? 还有使用CSS实现此目的的另一种方法吗? My div is liquid, so I don't really want to use fixed margins all the way.. 我的div是流动的,所以我真的不想一直使用固定边距。

Here is an example image: http://i54.tinypic.com/w8aogp.png 这是一个示例图片: 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? 如果我能以某种方式将选择器用于“ img之后是隐式换行符”之类的内容,并删除边距?

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. 不用管“顶部”边距-不管图像是否直接与白色容器div相邻,它都将是一个固定数字。 However, I want to zero the left-margin whenever an img is directly adjacent (visually) to the container div. 但是,每当img直接(在视觉上)与容器div相邻时,我都希望将左边距清零。

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. 如果您的父div的宽度可变,并且图像浮动,我认为确保图像等距的唯一方法是声明margin: 10px (在所有面上),或者至少在左右两侧侧。 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; 另一方面,如果父div的宽度固定,并且您确切知道每行有多少个图像,则可以每X幅图像插入一个空白div ,这将使您当前的选择器正常工作,或者您可以创建一个新的分隔符类,例如.sep ,您将.sep声明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. 除此之外,我认为没有其他纯CSS解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM