Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1936 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     HTML/XHTML/DHTML/CSS :

Styling the last element

Thread title: Styling the last element
Reply  
Page 1 of 2 1 2 >
    Thread tools Search this thread Display Modes  
09-09-2010, 11:29 PM
#1
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old  Styling the last element

Is something like this possible?

Code:
<div>
<p>1</p>
<p>2</p>
<p>3</p>
</div>

div p:last {
 styling
}
So the only element to get styling is the last.

Reply With Quote
09-09-2010, 11:33 PM
#2
Gaz is offline Gaz
Gaz's Avatar
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
 
Posts: 2,097
iTrader: 26 / 100%
 

Gaz will become famous soon enough Gaz will become famous soon enough

Send a message via Skype™ to Gaz

  Old

Yep, you're looking for :last-child mate, cousin to :first-child.

Reply With Quote
09-09-2010, 11:40 PM
#3
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old

Whoop. Cheers.

Gawd. This has just made coding even easier. Love it. No more undeed markup just to curve the edges of a box.

Reply With Quote
09-09-2010, 11:43 PM
#4
Gaz is offline Gaz
Gaz's Avatar
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
 
Posts: 2,097
iTrader: 26 / 100%
 

Gaz will become famous soon enough Gaz will become famous soon enough

Send a message via Skype™ to Gaz

  Old

I agree. Be careful though, its not fully supported by all browsers (the only notable one to care about which doesn't support it is IE8). http://reference.sitepoint.com/css/p...lass-lastchild

Using jQuery is probably a better way to ensure complete support.

Reply With Quote
09-09-2010, 11:53 PM
#5
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old

bah humbug. guess i shouldn't use it.

Reply With Quote
09-30-2010, 05:24 AM
#6
mafiya is offline mafiya
mafiya's Avatar
Status: I'm new around here
Join date: Sep 2010
Location: USA
Expertise: xHTML,CSS,jQuery
Software: Coda
 
Posts: 4
iTrader: 0 / 0%
 

mafiya is on a distinguished road

  Old

Code:
<style type="text/css">
/* this will style the last element */
.class {
 background-color:#000;
}
</style>


<!-- the html code -->
<div id="thisdiv">
 <p>1</p>
 <p>2</p>
 <p class="last">3</p>
</div><!-- end thisdiv -->
This will fix it.

Reply With Quote
09-30-2010, 08:51 PM
#7
Gaz is offline Gaz
Gaz's Avatar
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
 
Posts: 2,097
iTrader: 26 / 100%
 

Gaz will become famous soon enough Gaz will become famous soon enough

Send a message via Skype™ to Gaz

  Old

Adding a class is easy. Using dynamic pseudo-selectors are the way forward though!

Reply With Quote
09-30-2010, 09:25 PM
#8
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old

@mafiya I'd go out of my way to make sure code is kept as short as possible. (: I really don't like practicing your method.

Thanks though.

Reply With Quote
10-01-2010, 10:58 AM
#9
Garyrae is offline Garyrae
Status: Request a custom title
Join date: Dec 2005
Location: Colchester, UK
Expertise:
Software:
 
Posts: 4,625
iTrader: 16 / 100%
 

Garyrae is on a distinguished road

Send a message via MSN to Garyrae Send a message via Skype™ to Garyrae

  Old

It gets a bit messy if you're using it lots, but you could use jQuery, like:

Code:
<style type="text/css">
/* this will style the last element */
.class {
 background-color:#000;
}
</style>


<!-- the html code -->
<div id="thisdiv">
 <p>1</p>
 <p>2</p>
 <p>3</p>
</div><!-- end thisdiv -->
Code:
$('#thisdiv p:last-child').addClass('last');

Reply With Quote
10-01-2010, 12:29 PM
#10
Gaz is offline Gaz
Gaz's Avatar
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
 
Posts: 2,097
iTrader: 26 / 100%
 

Gaz will become famous soon enough Gaz will become famous soon enough

Send a message via Skype™ to Gaz

  Old

Aye, you could do that. But requiring a whole JS library to just style a last child seems pretty cumbersome IMO. Of course, if you're already using jQuery it is probably the best/most compatible way.

The only problem that you'd have with THAT though, is what if people have JS off? My hunch is that the type of people who use browsers that don't support CSS' pseudo-selectors would also be the type of people who don't know whether they have JS on or off!

Reply With Quote
Reply  
Page 1 of 2 1 2 >


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed