Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML tag safe for enclosing elements that does nothing [closed]

Tags:

html

css

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?

like image 331
Netorica Avatar asked Feb 20 '14 03:02

Netorica


People also ask

Which tags in HTML don't need to be closed?

The void elements or singleton tags in HTML don't require a closing tag to be valid. These elements are usually ones that either stand alone on the page ​or where the end of their contents is obvious from the context of the page itself.

Is there an HTML tag that does nothing?

Show activity on this post. 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.

How do you enclose HTML tags?

Tags are always enclosed in angle brackets: < >. Tags are comprised of elements and attributes. An element is an object on a page (such as a heading, paragraph, or image), and attributes are qualities that describe that element (such as width and height). Tags usually travel in pairs.

What is a self closing tag HTML?

Self Closing HTML ElementsSome HTML tags (like img and br ) don't have their own content. These are known as self closing tags or empty tags. They look like this: A simple example: <br /> The br tag inserts a line break (not a paragraph break).


1 Answers

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()
like image 151
MBlanc Avatar answered Sep 22 '22 09:09

MBlanc