Child and adjacent selectors

Are they used much?

There seems to much better ways by adding things in already written css rules rather than writing a new one.

e.g adjacent rule for zero-ing indent of paragraph. Why not just style whole paragraph with and id or class and add something in one of those…? Why write an adjacent.

Or are they just ammo rules in case they are needed.

P.S I’m very new to css

You can be sure things like this were added for a reason.

Say you want margins on all paragraphs except the first on following a heading. Yes, you could put a class or id on the first paragraph. BUT… in today’s world, a lot of content is added by clients, so you don’t know which is going to be the first paragraph. But with the adjacent rule, it doesn’t matter, as you can just declare that any paragraph following a heading have no margin.

That’s just one example of its usefulness.

They can be very helpful. One of the examples I’m using them for is on sites where the content is Dynamic. So you basically create just one div, since the rest is generated in your output (repeat region), Let say that you want a top and bottom margin of 20px for all records except the first one. In that case those adjacent selectors become very handy since now you can indeed target the first record.

mmm ok kinda confused but thats just because Im new lol

I didnt really understand the one div e.g. Simply because the repeat region part (output) bit lost me.

I really am a novice but am loving it here

I will try to simplify it a bit. On a dynamic some content comes from the database. Lets say you would like to output a list with friends (first name & surname) On a static site the html would be something like this:


<div>
first name1 surename1
</div>

<div>
first name2 surename2
</div>

<div>
first name3 surename3
</div>

etc etc..

When this content on the other hand would come from the database, the structure would be completely diffent. First of all, do you receive the data using a query (MS SQL, mySql, Access etc.) in combination with a server side scripting language (PHP, Coldfusion…). When the data is received you output it using the server side scripting laguage. For example in Coldfusion (the language I’m using) The output for the above static example would look something like:


<cfoutput query="myFriends">
<div>
#first_name# #surename#<br />
</div>
</cfoutput>

Depending on the query used this would output the list of your friends. As you can see is there only one physical div involved, which will be repeated with the output query.

ahhh ok starting to get ya. Im just gonna learn as much css as I can then call on it when I need to.

Cheers guys

Good for you! Show us some work when the time is there :slight_smile:

lol Ill try but I think my whole site might just be able to change. Please refer to my newest thread