Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use CSS + sign [duplicate]

Tags:

html

css

Very easy question. I have been using CSS for a while but rarely use + sigh for my code. Could any one explain what the best time is to use it? I couldn't find anything from Google...Thanks a millions...

like image 351
FlyingCat Avatar asked Jul 26 '10 00:07

FlyingCat


People also ask

How do you duplicate elements in CSS?

Short answer no, in css you can't create content or display it multiple times. Usually this repeated content is taken care of via server side technologies (PHP, JSP, ASPX) etc. If your site is static content (no server side processing) then your best bet is just copy and past.

Can you have 2 ids in CSS?

You cannot have multiple IDs for a single div tag. There is never any need for multiple ids on the same tag since ids are unique either one would serve to uniquely identify that specific tag provided you first get rid of the other id which is stopping things working.

Can ID be used multiple times in CSS?

An id must be unique in a page. Use a class if you want to describe a group of elements. why we should not use id selector two times in a page while its working fine.


1 Answers

I've found that adjacent sibling selectors are useful when you want to apply special styling to the first element of a series of elements. For example, if you want to style the first paragraph of an article differently from the rest, you could use:

p {
    /* styles for the first paragraph  */
}

p + p {
    /* styles for all other paragraphs */
}

This can replace using class="first" in many situations.

EDIT: Using the first-child pseudo-class would work better for this specific case, since it's supported by a wider range of browsers. (Thanks alex)

You might also want to adjust how certain elements are positioned when next to each other. If you have an unordered list that looks fine on its own, but needs less padding at the top when it's next to a paragraph, you could use:

ul {
    padding-top: 1em;
    /* Default ul styling */
}

p + ul {
    padding-top: 0;
    /* special styling for lists that come after paragraphs */
}
like image 126
derekerdmann Avatar answered Oct 16 '22 16:10

derekerdmann