Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

CSS 3.... Have you even Started CSS 2?

Featured Replies

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 :aggressive: doesn't support this but hey no surprise there...

 

Anyway that's it, I hope some of you will learn something here!

 

Laters

 

Oli 8)

Wow that's brilliant - that will save me a lot of time and coding! Thanks :yahoo:

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

  • 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 :)

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.

  • 3 weeks later...

nifty, id never heard of that. but yeas, too many people with ie6 to warrant its use as of yet in my opinion.

  • 3 weeks later...

thats a real helpful tip olivier. Thanks.

I think i'll start using it right away instead wait another 6-8 months.

  • 4 weeks later...

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.

  • 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

I know, just stating. But i know what you mean, i only found out from a professional coder.

  • Author
I know, just stating. But i know what you mean, i only found out from a professional coder.

:unknw: don't know what you mean...

But it's all good to me :good:

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

  • 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

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

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.

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

Of course, depends how quickly you learn, and how easily you understand new techniques. Also how much time you have =D Half term rules.

  • 2 months later...

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.

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 ;)

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 ;)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.