0

Is there a way in CSS to adjust the style of a parent element, if a nested element exists?

For example, let's say we have a button with padding on the right and left of 10px.

<button>Submit</button>

Now, let's say we add an icon to this:

<button><i class="icon"></i> Submit</button>

Due to the icon, the spacing on the left of the button is too wide. Is there a way without adding an additional class to the button, to say if it contains <i>, make the padding less on the left?

Thanks!

dzm
  • 22,844
  • 47
  • 146
  • 226

2 Answers2

0

Alas, You cannot do this at the moment. It is proposed for css4.

Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
0

Sounds to me like you should just use a different element to target. This is where elements which are void of semantic meaning like <span> come in handy:

<button><span class="text">Submit</span></button>
<button><i class="icon"></i> <span class="text">Submit</span></button>

If you use that HTML, you could just target .text with the margin/padding instead.

Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119