You want to put margin-right
to the last element of the line, but in the structure of your HTML there is no defined line. To do this, I recommend creating a container for each two photos, as follows:
<div id="container">
<div class="container-imagens>"
<h2 class="linhasFotos">
<a title="nome da linha" href="/lancamentos">
<img src="../../public/images/foto-01.jpg" class="img-zoom" alt=""/>
</a>
</h2><!--Toda página deve conter pelo menos 1 (uma) tag h2-->
<h2 class="linhasFotos">
<a title="nome da linha" href="/lancamentos">
<img src="../../public/images/foto-01.jpg" class="img-zoom" alt=""/>
</a>
</h2>
</div> <!-- end of .container-imagens -->
<div class="container-imagens">
.
.
.
</div> <!-- end of .container-imagens -->
</div> <!-- end of #container -->
In this way, you have more control over each 'line' of images, and now you can use the :last-of-type
selector to add margin-right: 0
, which was your question. your CSS would then look like this:
.container-imagens > .linhasFotos:last-of-type{margin-right: 0}
Note that this selector will only work if, in your HTML, .linhaFotos
elements are direct children of .container-imagens
elements. You can read more about the selectors here.
The advantage of working with
:last-of-type
is that if for any reason you decide that your grid will contain three images - instead of two - in each line, you guarantee that only the last element (ie, the rightmost ), will inherit the properties of the selector. Using selectors like
:nth-child
will hold math in a way that can be difficult to escape later. I created a Pen as proof of concept, and you can see it
here .
I can recommend 3 things:
Avoid any inline style rules such as width
and height
as attributes of the <img>
tag. It is much easier (and prudent) to control such rules in a separate .css file.
Although HTML 5 is a magic entity and now allowing this kind of thing (ie, it will work), putting anything other than text inside a <h2>
tag is not a good practice, since this tag is a text tag. In your case, I see no reason to justify using <h2>
, but each is each. =)
CSS allows you to cascage style rules, so you can wipe your HTML to make it simpler. In your case, your selector would become
.container-imagens > h2:last-of-type{margin-right: 0}
, which is much easier to read and understand that "The last h2, son of .container-images, will not have margin".
I hope I have helped! =)
EDIT: The third concept proof container shows the leaner HTML, as I suggested in item 3.