- Sometimes it's desirable to change a style of the first and/or last elements in a container. You can do this by manually applying classes to your HTML elements: ("last-child" still not supported in IE8).
Change styles of First/Last Elements in CSS
1 <style type="text/css">
2 /* Source: http://www.apphp.com/index.php?snippet=css-change-styles-first-last-element */
3 p.first { margin-top: 0 !important; margin-left: 0 !important; }
4 p.last { margin-bottom: 0 !important; margin-right: 0 !important; }
5 /* or */
6 div#articles p:first-child { border:1px solid #c1c13a; }
7 div#articles p:last-child { border:1px solid #3ac13a; }
8 /* or */
9 div#articles > :first-child { text-align:left; }
10 div#articles > :last-child { text-align:right; }
11 </style>
12
13 <div id="articles">
14 <p class="first">1st article: lorem ipsum dolor sit amet...</p>
15 <p>2st article: lorem ipsum dolor sit amet...</p>
16 <p>3st article: lorem ipsum dolor sit amet...</p>
17 <p>4st article: lorem ipsum dolor sit amet...</p>
18 <p class="last">5st article: lorem ipsum dolor sit amet...</p>
19 </div>
Comments
-
Lamb 2012-08-01
does not work in IE 7,8,9
Sign in to leave a comment.

