0

I have some html:

<p>
        <button type="submit" name="btn" value="Save">Save</button>
        <button type="submit" name="btn" value="Cancel">Cancel</button>
</p>

I would like to create a CSS selector that matches all "p" tags that contain the above button(s) so that I can add a :

padding-top:20px;

to it.

It would also be interesting to see if one can add this style to another parent like "div".

I guess one would just do something like:

p button, div button {}

The above applies the style to the button tag whereas I wish to add it to the "div" or "p" tag.

Thoughts??

Many thanks in advance.

4
  • 2
    Put a class on the <p>, select by class, and move on Commented Dec 3, 2013 at 0:24
  • 1
    You cannot select in css based on the child elements, you will need to use JavaScript/jQuery. Commented Dec 3, 2013 at 0:24
  • 1
    If you wanted to do this with jQuery.. $('p:has(button[value="Save"])').css("background","red"); jsFiddle example Commented Dec 3, 2013 at 0:30
  • Thank you for all of your comments. Very helpful. Did not know it could not be done as standard, but there is a workaround in JQuery. Commented Dec 3, 2013 at 0:31

1 Answer 1

2

jQuery answer:

$('p:has(button[value="Save"])').addClass('containsButtons');
Sign up to request clarification or add additional context in comments.

1 Comment

I actually went with the Class suggestion in the end as I wanted to keep it in CSS, but thank you anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.