19

I have elements (for example a div or a paragraph) which I don't want to inherit styles from their parent elements.

for example

   <style>
      div{
         padding:3px;
       }
       /* and so many other basic tags styled like p, table, td, span, etc*/
   </style>
    <nonsensetag id="textToGrab">
       <div>Hello World</div>
       <p>Hi world</p>
    </nonsensetag>

so basically I can't use div or p to wrap the HelloWorld and Hiworld because it will be formatted by css rules but the real reason why I want to just wrap them in a tag so I can grab them using javascript while not affecting its current style.

alert($('#textToGrab').text());

now what is the html tag that doesn't really do nothing? or do I need to wrap it with valid html tag and reset all the styles forcefully because css cascades? or its is safe to create my own html tag? if there's a possibility that I can add something in the standard html schema?

I read this question already but it doesn't quite fit on my situation

How to break CSS inheritance?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Netorica
  • 18,523
  • 17
  • 73
  • 108
  • `span` are styled throughout my pages I can't use it or else I need to reset its style just to use it for enclosing elements – Netorica Feb 20 '14 at 03:15
  • @BrandonBoone, that wouldn't be correct if the wrapped a block level element. – JakeParis Feb 20 '14 at 03:15
  • 1
    Question: Why are your css selectors so broad? Why can't you use `#textToGrab div { ... }` for instance? – Papa Feb 20 '14 at 03:22
  • The first sentence refers to preventing inheritance, but the rest refers to something completely different, namely elements matching some selectors in your CSS rules. Try to explain the real problem, with a real example. If you have a rule for `div` and you cannot change that, then you apparently cannot use `div` as a wrapper, and then you will need ugly tricks—but do you really need to have such “broad rules”? – Jukka K. Korpela Feb 20 '14 at 07:16
  • how this question can be unclear with all the specific questions to ask in bold – Netorica Feb 11 '15 at 01:08

3 Answers3

13

What is the html tag that doesn't really do nothing?

The <span> and <div> tags signify no specific meaning and are intended only for markup.

or do I need to wrap it with valid html tag and reset all the styles forcefully because css cascades?

Resetting all the styles forcefully takes a boatload of CSS. If you feel you need to do this, you probably need to rethink carefully about what you are doing. You should be working with the cascading styles, not against them.

or its is safe to create my own html tag?

Never, ever do this. While modern browsers won't break (they are quite permissive), HTML has been standardised to stop everyone from inventing their own tags.

if there's a possibility that I can add something in the standard html schema?

Well, technically, you can. But it's just not worth the hassle.

Also, citing the w3.org:

Don't do this! Documents need to have a meaning as well as correct syntax. SGML and XML only define syntax. HTML and XHTML define meaning. If you add elements that aren't defined by a standard, only you yourself know what they mean. And in 20 or 50 years, even you may not know it anymore…

How can this be done then?

IMO, the whole problem here is that your CSS styling is too broad. You should move the formatting to a .class when possible. This gives you more fine-grained control over where you apply each .class and allows for greater code reuse. You'll sidestep the whole cascading issue entirely and will be able to wrap the sections in <span> tags.


NOTE: The rest of the answer didn't really fulfil the requirements, but I'd rather keep instead of deleting it, in case somebody finds it useful

You can mark tags without using a .class by using a custom data-* attribute:

<div data-yank='field1'>Hello World!</div>
<p data-yank='field2'>Hi world</p>

The attributeEquals selector allows you to choose appropriate element:

$( "input[data-yank='field2']" ).text()
Community
  • 1
  • 1
MBlanc
  • 1,773
  • 15
  • 31
5

You can use <section>. It's a HTML5 tag but all modern browsers support.

Papa
  • 1,628
  • 1
  • 11
  • 16
2

You can use any element for this. Try wrapping the code you want to use in JS with <div class="jsOnly"> for example. Then in your stylesheet just do

.jsOnly {
    display: none;
}

Everything inside that element will be accessible in the DOM, but you won't see it on the page.

JakeParis
  • 11,056
  • 3
  • 42
  • 65
  • I styled the element with CSS so it can be viewed but must not be altered when I wrapped it... – Netorica Feb 20 '14 at 03:19
  • 2
    Then change your stylesheet so that you are not styling divs by themselves. You should really only style a
    with a class or id anyway.
    – JakeParis Feb 20 '14 at 03:20