Link to home
Start Free TrialLog in
Avatar of dvine_valentino
dvine_valentino

asked on

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

Hi,

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:

http://codepen.io/anon/pen/uhFtD

Many Thanks...
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here's an example using jQuery:
http://jsfiddle.net/AlexCode/v7og2b6n/
<ul>
    <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>
</ul>

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.
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.
Hmmm,
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

http://codepen.io/anon/pen/hrvml

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

Open in new window

Avatar of dvine_valentino
dvine_valentino

ASKER

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.

http://codepen.io/anon/pen/tvFHw

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

http://codepen.io/anon/pen/Imkwp

.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

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
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.