I'm using CSS to style some existing documents, and using a lot of adjacent sibling selectors (e.g. item1 + item2) to apply spacing, etc. between elements. However, many of the documents also include <style> tags at the top of the page, or scattered throughout where custom styling has been applied. The custom styling itself is fine, but unfortunately the <style> tags themselves are interfering with my CSS selectors. For example, if I want to apply a margin-top to every element except the first, I would usually use * + * (aka, the "lobotomized owl" approach), and this works great UNTIL somebody puts a style tag at the top. Now the style tag is being read as the first element, and so EVERY element on the page is being selected. I do not know the element types in advance; they can be any combination of valid HTML code (div, span, p, table, etc...) and need to account for nested elements t as well. The key selector I am trying to fix is the adjacent sibling selector starting with a wildcard ( * + item).
Using something like :not(style) + item was my first thought, but then if there are any <style> tags in the middle of the page somewhere, any element after one of those will be styled incorrectly as well.
Is there a surefire way of doing this purely with CSS? I am not able to edit the HTML files themselves, but might be able to pre-process them with Javascript before they are rendered if I have to.
Edit: Bonus question, how do I select the first element in the page that is not a <style> tag? I.e, how do I now do body > *:first-child without selecting a <style> tag?
For example, my CSS file and then a target HTML:
div {
background-color: yellow;
}
*+div {
background-color: lime;
margin-top: 1em;
}
<style>
div {
font-weight: bold;
}
</style>
<div>Yellow</div>
<div>Green</div>
<div>Green</div>
<div>Green</div>
<div>Green</div>