Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select specific element before other element

Tags:

css

I have following HTML structure:

<div>   <h2>Candy jelly-o jelly beans gummies lollipop</h2>   <p>     Cupcake ipsum dolor sit amet. Sugar plum liquorice dragée oat cake cupcake.   </p>   <p>     Candy tiramisu bonbon toffee. Croissant pudding ice cream soufflé pastry toffee  chocolate bar. Tiramisu wypas tootsie roll icing fruitcake oat cake icing soufflé tiramisu.    </p>   <h2>Dessert pie cake</h2>   <ul>     <li>liquorice</li>     <li>powder</li>     <li>dessert</li>   </ul>   <h2>Chupa chups sweet dragée</h2>   <p>     Chocolate cake biscuit pie jelly-o chocolate bar. Marshmallow topping sugar plum apple pie brownie cotton candy dragée lemon drops. Soufflé cake toffee.   </p> </div> 

I would like to choose only that h2 which is directly before ul. How can I do this? In my content there are many more uls and many more h2s so the solution should be universal.

like image 941
user1292810 Avatar asked Apr 19 '12 09:04

user1292810


People also ask

How do you target a element before CSS?

The CSS ::before selector can be used to insert content before the content of the selected element or elements. It is used by attaching ::before to the element it is to be used on. In the example above we are prepending an asterisk and a space before every paragraph element on the page.

How do you style every element which has an adjacent item right before it?

You can easily style every element which has an adjacent item right before it using the Adjacent Sibling Selector (+). The adjacent sibling selector is used to select the element that is adjacent or the element that is next to the specified selector tag.

Is there a previous sibling selector?

No, there is no "previous sibling" selector. On a related note, ~ is for general successor sibling (meaning the element comes after this one, but not necessarily immediately after) and is a CSS3 selector. + is for next sibling and is CSS2. 1.

How do you select a specific element?

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.


1 Answers

As far as I know, CSS doesn't offer any selectors which will target before a selector. Could you select it as an h2 which is directly after a p (p + h2)?

h2 {     color: #1a1a1a; }  p + h2 {     color: #0cc; } 

Example on JSFiddle

As you can see, this is probably the best selector you can use if you're relying on CSS, although you could easily just add a class to each h2 that is before a ul. That would avoid the case where you have a paragraph section before another h2 and paragraph section.

You could do this using jQuery:

.highlight {     color: #0cc; }  $('ul').prev('h2').addClass('highlight') 

Example on JSFiddle

This selects every ul, then selects the h2 that is before it, before finally adding the .highlight class to it.

like image 107
djlumley Avatar answered Sep 22 '22 03:09

djlumley