CSS Advanced Selector - http://codepen.io/anon/pen/uhFtD


I am trying to target .section-box when it's appears for the first time and last time (in a row) within it's parent container. I am using the fluid grid bootstrap 2.3.2.

A requirement to target these without creation of additional classes or wrapping them in within another container whenever they appear in .row-fluid.

I am trying to add a margin-top:Xpx to the first row and a margin-bottom:Xpx to the last row on .section-box but not in between as it will use a pre-defined margin (in the example it has a 5px margin top and bottom). :first-child and :last-child will not cut it as they will not always be the first children or last when they appear as displayed in the example here:


Many Thanks...
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
Nop, this is not possible.

You'll need to use javascript for that.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexandre SimõesManager / Technology SpecialistCommented:
Here's an example using jQuery:
    <li style="display: none;">Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li style="display: none;">Item 5</li>

Open in new window

$('ul li:visible:first').css('color','red');
$('ul li:visible:last').css('color','blue');

Open in new window

In your case it should be something like:
$('.row-fluid .section-box:visible:first').css('margin-top','5px');
$('.row-fluid .section-box:visible:last').css('margin-bottom','5px');

Open in new window

Why are you using Bootstrap 2 - it's obsolete.

The CSS psuedo class is :first-child and :last-child

But I'm not sure what sections you are trying to apply it to - unless the markup is static where you can target based on nth-child you may not be able to get away without adding extra markup.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Alexandre SimõesManager / Technology SpecialistCommented:
As far as I understood, his problem is that sometimes the first and last items on the list aren't visible. :first-child and :last-child css selectors don't take that in consideration.

The only way I know to overcome this limitation is through Javascript.
Had a closer look at the markup and it's not going to be possible with a simple set of classes.
I'm not going to bother writing up css for something that is just an example, so here I've only done a sample on the second section


.container-fluid .row-fluid:nth-child(2) .row-fluid:first-child .section-box:first-child{
.container-fluid .row-fluid:nth-child(2) .row-fluid:last-child .section-box:last-child{

Open in new window

dvine_valentinoAuthor Commented:
Hi Guys,

Alexandre, firstly thank you so much for your reply and secondly I was extremely tired before I wrote this so it probably did not make a bunch of sense. I am not exactly sure if that is the solution I am looking for however.

The section-box will always appear (it does not have a visible property), it will never always be in the first row or last row of the parent container. So there will be rows before the section-box row appears. .section-box will always appear in an entire row of columns and not just individually as the example provided. I want to target the entire row of .section-box when it appears the first time with additional top padding and likewise with its last instance in an entire row.

If you refer to the codepen layout I originally provided, you will see that every time .section-box appears in a row for the first time and every time .section-box appears the last time in a row I want to target those elements with another selector. I am starting to think pure css will not cut it here and obviously js will likely be involved.

Gary, thank you for your response. I understand that it is an older framework, but hubspot being the management system, I have no control over, and so I am working with what has been provided as the host in this instance.

Again, I want to target section-box div in an entire row every time (columns amount will vary) it appears in a new parent container. I might have 4 rows of section-box each with 5 columns. So I would need to target the first 5 and the last 5 leaving the middle 10 inheriting the default style.

Please see the codepen update showing all the boxes marked green that need to be targeted. If it appears once I will need it to apply the style to top and bottom in that single row instance. (I’ve only created additional class of green for demonstration purposes to show which boxes I need to target – just to clarify it further.


Thanks :)
This is just for the last section, the others are easy following the same logic since you are highlighting all boxes


.container-fluid > .row-fluid:last-child .row-fluid:nth-child(3) .section-box {
background:blue !important

.container-fluid > .row-fluid:last-child .row-fluid:nth-child(6) .section-box {
background:blue !important

Open in new window

dvine_valentinoAuthor Commented:
Gary, again I only created an additional class and applied it because you were unable to understand my confusing gibberish. Again, the requirement was to not add additional classes to each section-box where they appear any time for the first or last time in a row.

I do not want to target the specific nth-child because the layouts will always vary and those set of rules are very specific. I need the flexibility of having a large array of layouts, but always specifically targeting the first row of section-box and last row of section-box on any layout. I do not want to have to write the logic for every instance where these boxes do occur, as it will become confusing and it will overwrite itself in every instance without the correct logic.

I think Pure CSS will not cut it here.
No, in that case CSS will not work and neither will Javascript
Well I suppose you could write some crazy dom traversal code in jQuery
dvine_valentinoAuthor Commented:
I've requested that this question be deleted for the following reason:

There is no solution to the question, due to the limitation of the current language standards.
Sometimes the answer is it cannot be done and that is a perfectly valid answer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.