March 23, 200719 yr Hello! Some people discussed CSS3 here recently and I was wondering if any of you actually use some of the (cool) CSS 2 features? As a little intro, I thought I'd explain the adjacent sibling selector. As complex as the name sounds, the technique is actually pretty simple, the idea is to be able to target an element based on the element that is before it. Still doesn't make sense? Alright, here's an example. We have a site at work full of articles. Each article has the following structure: Title (<h1>) Lead(<p>) Body(<p>) The lead for each article has a class attribute of lead just so that we can style it and make it bold and a different color. Now the code is something like that <!-- In the xhtml page --> <h1>Title of the Article</h1> <p class="lead">This is a little intro to the following article</p> <p>This is the body of our article</p> /* In the Stylesheet */ .lead { font-weight: bold; color: #333; } Nothing wrong with that but semanticly speaking this isn't the best. So in CSS 2, they have introduce the adjacent sibling selector. Using the above, here's what the code looks like: <!-- In the xhtml page --> <h1>Title of the Article</h1> <p>This is a little intro to the following article</p> <p>This is the body of our article</p> /* In the Stylesheet */ h1 + p { font-weight: bold; color: #333; } Cool isn't it!!! Basically , the + sign target the <p> element positioned directly after the <h1> and ONLY THIS ONE. The body of our article would not inherit the style. Obviously..... IE6 and below doesn't support this but hey no surprise there... Anyway that's it, I hope some of you will learn something here! Laters Oli
March 24, 200719 yr Yup thats what we have to take care of its cool but what if it doesn't work with IE6 many people still use IE6 so this css2 would have to wait lol
March 24, 200719 yr Author This is a choice to make i suppose... I'm happy coding site that looks their best in modern browsers and simply degrades in "older" less compliant browsers. IE7 beeing a critical update from microsoft now , the majority of people still using IE6 (in my mind) are people with: 1 - a NON genuine version of windows 2 - an old version of windows I have IE6 and IE7 for testing purposes and I always make sure layouts don't break in both but for little features like the one discussed here, if it doesn't work in IE6, tough. I really don't want to under use technology for the sake of only a minority.. This technique only affects the aesthetic (visual) aspect of a site and does not affect the functionality of it so I'm happy using it
March 25, 200719 yr I totally agree with you there Olivier - I'm an advocate for using the latest browsers and always recomend people upgrade. I always state that I work and test on the latest browsers and just make sure that my designs degrade sympathetically in older browsers - but I don't go as far back as old versions of Netscape or IE. IE6 still needs to be supported, but in my opinion not for much longer. As you say the technique does not impair the functionality of the site and so I don't see what's wrong in using it.
April 10, 200719 yr nifty, id never heard of that. but yeas, too many people with ie6 to warrant its use as of yet in my opinion.
April 27, 200719 yr thats a real helpful tip olivier. Thanks. I think i'll start using it right away instead wait another 6-8 months.
May 22, 200719 yr You dont have to use +, eg : p#navigation img{ float: left; margin: 0px; padding: 0px; border: 0px; } I have been told that placing things like p for paragraph, of div for divs helps to jump quickly between css and html when using large css files. You know which is relative to which.
May 23, 200719 yr Author You dont have to use +, eg : p#navigation img{ float: left; margin: 0px; padding: 0px; border: 0px; } I have been told that placing things like p for paragraph, of div for divs helps to jump quickly between css and html when using large css files. You know which is relative to which. I think you misunderstood the point of the technique Oli
May 23, 200719 yr I know, just stating. But i know what you mean, i only found out from a professional coder.
May 23, 200719 yr Author I know, just stating. But i know what you mean, i only found out from a professional coder. don't know what you mean... But it's all good to me
May 23, 200719 yr I don't understand how this can be more semantic efficient, your saying that in css <tag> + <tag2> { the style } is more efficient that just doing <tag2> { the style }? and doing <tag class="the style"> </tag ? Wildo
May 23, 200719 yr Author Guys, I'm not sure you're getting it... You use this if you want to style the first element in a serie of similar elements. So if you want to do something like this ++++++++++++++++++++ This is the title of the article ( this is in a <h1>) This is the intro to your article (this is in a <p>) This is the full body of the article.... (this is a <p> too and so is the rest of the body) ++++++++++++++++++++ In your css, you style your <h1> then if you want to style the "intro" bit of your post but want to leave the other <p>'s unstyled, you can access this "intro" bit by using h1 + p. h1 + p will target the first <p> after the <h1> and the first one only!!! I don't understand how this can be more semantic efficient I give you a clue then... To style that "intro" you don't need to add any class or id to it. So instead of : <h1>Title</h1> <p class="intro">Intro text here</p> <--- watch the extra class here <p>1st paragraph of the rest of the post</p> <p>2nd paragraph of the rest of the post</p> <p>3rdparagraph of the rest of the post</p> You do this: <h1>Title</h1> <p>Intro text here</p> <--- watch... no class needed! <p>1st paragraph of the rest of the post</p> <p>2nd paragraph of the rest of the post</p> <p>3rdparagraph of the rest of the post</p> Makes sense? "Semantic efficiency" as you call it relates to the xhtml, not the css. The all point of coding a page in a semantic way is to reduce the use of extra (unessesary) mark-up. Hope this explains things for you Oli
May 23, 200719 yr ah ok that made more sense, thank you, it's nice, though I don't think I'll use it since < IE6 doesn't support it and still a frequent amount of people still use that browser. Wildo
May 25, 200719 yr Best to just stick with what you know if you're unsure anyway. Trying techniques can cause hassle. But this one in particular is pretty handy.
May 26, 200719 yr Best to just stick with what you know if you're unsure anyway. Trying techniques can cause hassle. But this one in particular is pretty handy. it needs a little practise, like everything in this world. Wildo
May 26, 200719 yr Of course, depends how quickly you learn, and how easily you understand new techniques. Also how much time you have =D Half term rules.
August 15, 200719 yr There's also a few CSS3 Selectors that are widely supported in modern browsers [att^=val] Represents an element with the att attribute whose value begins with the prefix "val". [att$=val] Represents an element with the att attribute whose value ends with the suffix "val". [att*=val] Represents an element with the att attribute whose value contains at least one instance of the substring "val". So for example, p[class^="hea"] {color: #F00;} Would apply the colour red to all paragraphs that have a class starting with "hea" p[class$="der"] {color: #F00;} Would apply the colour red to all paragraphs that have a class ending with "der" p[class*="ead"] {color: #F00;} Would apply the colour red to all paragraphs that contain a class with the string "ead" Taking the examples above, all would be applied given the code <p class="header">Here's some text</p> This method is supported by Gecko (Firefox, Mozilla Suite, Seamonkey, Camino, Flock), IE7, Opera 9, Safari 1.3 and 2.0, Omniweb and Konqueror. However, as with Olivier's example, there's no support for this in IE6 so you'll need to ensure that it degrades gracefully if you choose to use these selectors.
August 15, 200719 yr This article is semantically incorrect. A lead is like a subtitle, and so a <h3> or similiar should be used. <p> is used for the remaining body text. But yes, education about the adjacent selector is good, its just a shame we can't use it
August 15, 200719 yr This article is semantically incorrect. A lead is like a subtitle, and so a <h3> or similiar should be used. <p> is used for the remaining body text. I was referring to the header of a page, say for example where the logo, strapline etc would belong and not to headings. I should have probably used a different example though to avoid the confusion (a div may have been more appropriate) as I'd always advocate the correct semantic meaning for tags
August 16, 200719 yr This website very nicely breaks down the various selectors and illustrates with DOM tree illustration how they get applied: http://css.maxdesign.com.au/selectutorial/index.htm
Create an account or sign in to comment