You want to put the margin-right
for the last element of the line, but in the structure of its HTML, there is no defined line. To do so, I recommend creating a container for every 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 -->
This way, you have more control over each 'line' of images, and now you can use the selector :last-of-type
to add the margin-right: 0
, which was your question. your CSS would then be:
.container-imagens > .linhasFotos:last-of-type{margin-right: 0}
Remember that this selector will only work if, in your HTML, the elements .linhaFotos
are the direct children of .container-imagens
. You can read more about the selectors here.
The advantage of working with the :last-of-type
is that if tomorrow, for whatever reason, you decide that your grid will contain three images - instead of two - on each line, you ensure that only the last element (i.e., the most right), will inherit the properties of the selector. Using the selectors as the :nth-child
you hold the 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 recommend 3 things:
- Avoid any inline style rule, such as
width
and height
as tag attributes <img>
. It is much easier (and prudent) to control such rules in a separate . css file.
- Although HTML 5 is a magical entity and now allows this kind of thing (i.e., it will work), put anything other than text inside a tag
<h2>
is not a good practice, since this tag is a text. In your case, I see no reason to use <h2>
, but each one is each one. =)
- CSS allows you to cascade style rules, so you can dry 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 than "The last H2, son of the . container-images, will not have margin".
I hope I’ve helped! =)
EDIT: The third proof-of-concept container shows the leanest HTML, as I suggested in item 3.
It worked. Thank you very much
– Joao Nivaldo