Jump to content

[SOLVED] help on sibling selector


departedmind

Recommended Posts

Not a very good book then is it. If it was, your code would be working.

 

!important is sloppy coding as well. It means you haven't structured your CSS well. So if that was in the book, then its another indicator that it's not a great book.

 

So let us know what you are trying to do, and we will help you do it the right way.

Link to comment
Share on other sites

Actually Haku, you are incorrect. The plus sign is an adjacent sibiling selector.

 

Check out -> http://css.maxdesign.com.au/selectutorial/selectors_adjacent.htm

 

I never use it, as it is unnecessary.

 

div.contact_us h5 + p + p { padding-bottom:0px!important; }

 

This code means that the last p in this html code will have a bottom padding of 0.

 

<div class="contact">
<h5>Heading</h5>
<p>Paragraph 1</p>
<p>Paragraph 2 with 0 bottom padding.</p>
</div>

 

Departedmind, does your code look exactly the same as mine? Why are you using the !important css property? Important usually is required to rewrite a more specific element by a less specific css styling property - that creates a problem in IE!

 

Post your html code.  ;)

Link to comment
Share on other sites

div.contact_us h5 + p + p { padding-bottom:0px!important; }

 

This code means that the last p in this html code will have a bottom padding of 0.

 

<div class="contact">
<h5>Heading</h5>
<p>Paragraph 1</p>
<p>Paragraph 2 with 0 bottom padding.</p>
</div>

 

Departedmind, does your code look exactly the same as mine? Why are you using the !important css property? Important usually is required to rewrite a more specific element by a less specific css styling property - that creates a problem in IE!

 

Post your html code.  ;)

 

Yeah, same as yours , it works great in Firefox , It didn't work in IE so I added !important but it didn't work.

Not a very good book then is it. If it was, your code would be working.

 

!important is sloppy coding as well. It means you haven't structured your CSS well. So if that was in the book, then its another indicator that it's not a great book.

 

So let us know what you are trying to do, and we will help you do it the right way.

 

http://search.barnesandnoble.com/Beginning-CSS/Richard-York/e/9780470096970

 

So, this book is bad.....

 

 

Link to comment
Share on other sites

I agree.

 

I have no idea if that book is any good or bad. Never read it. But, a couple things make me suspect that it's not that great:

 

1) It's telling you to use !important. This is sloppy coding. If he told you *about* it, but said not to use it, then I don't see a problem. If he is telling you to use it, it's not good.

2) It says the book has updates for Firefox 2. That means it was originally written for firefox 1, which is a few years back now. That means there is a good chance the book is out of date.

Link to comment
Share on other sites

@haku

That book is good but a bit old, I learned lot of things from that book, it does not tell "me" to use !important, I used it cause IE was behaving strangely.

 

Why do you say !important is sloppy coding as well.

 

@TheFilmGod

I cannot add a class name, cause h5, p and p are dynamically created content in DNN module, so I thought sibling selector could work here.

 

Anyway thanks...

Link to comment
Share on other sites

Why do you say !important is sloppy coding as well.

 

CSS works on the principal of specificty. Basically it means that a selector that is more specific will take precedence over one with less specificity. For example:

 

HTML:

<a class="target" href="#">link</a>

 

If your CSS looks like this:

 

a {color: red;}

a.target {color: blue;}

 

The link above will be blue, because a.target is more specific than a. However, if you use !important, it takes it out of the flow of the specificity. This means that you could add a more specific tag, and it still won't take precedence, even though it should.

 

This makes code harder to adjust in the future when you come back to it.

 

This is why it's considered sloppy, it's somewhat of a hack (though not a real hack, they are different). Instead, you should write tags to be more specific if you want a certain tag to take precedence.

Link to comment
Share on other sites

Hey Departedmind, here's a neat (advanced) technique I use!  ;)

 

Do this:

 

<div class="contact_us">
<h5>Heading</h5>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</div>

 

 

You can use the firstchild selector and do some reverse coding.

 

First, set the bottom padding to 0 for all paragraphs in the div. Then use the firstchild selector to "undo" the padding for the first paragraph. This is the most browser compatible way to style elements through css.

 

Here's an example:

.contact_us p {
padding-bottom: 0; /* I recommend this: padding: top right 0 left; */
}

/* "Undo" the padding for the first pargraph */
.contact_us p:first-child {
padding-bottom: 5px; /* Set it to what it was originally. */
}
}

 

I hope that helps.

FilmGod out.  ;)

Link to comment
Share on other sites

div.contact_us h5 + p + p { padding-bottom:0px!important; }

 

does not works in IE any Help

 

That's because IE6 does not support the adjacent selector. IE7 does.

 

If you are just trying to control the space between paragraphs in a "contact" section you might be able to achieve what you are after by using margins instead. Set each <p> to have a top-margin (and no other vertical margins or padding). You will get space between the two paragraphs but without unwanted space below the final paragraph.

 

If you are not floating the paragraphs or heading then you will find that the margin between the <h5> and the first <p> will also collapse (i.e. the space between the elements will be equal to the whichever margin is largest, they will not combine).

 

You can use the firstchild selector and do some reverse coding.

 

That won't work because the first paragraph in .contact_us is not the first-child. The h5 is the first-child.

Link to comment
Share on other sites

I think the first-child method is a good idea here.

 

Could you possibly wrap the paragraphs in another div and then use the first-child.

 

<div "parwrap">

<p>Paragraph 1</p>

<p>Paragraph 2</p>

</div>

 

Then style both as you want the second one to be and add- to default:

 

.parwrap:first_child{}

 

Is there no way of assigning a class to the last one?

Link to comment
Share on other sites

I can see how the sibling selector could be of use in certain situations. Now that I know about it, I will probably use it at some time. The only problem is that if you add an extra element in between the reference and the selector, your CSS will fall apart, which isn't very good.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.