Do you know a HTML-Snippet Validator?

2019-03-17 03:33发布

问题:

I'm searching for a tool that would allow me to check whether a certain snippet of HTML would be valid in it's proper context.

I'd input something like

<dd>
    my definition 
    <div>
        div inside &lt;dd&gt; is allowed
    </div>
</dd>

instead of the whole document. A ordinary validator will complain about the missing dl-tag, but most of the times I just want to know whether a certain element is valid inside another one or not.


I'll try to explain it more detailed. Consider the following snippet:

<form>
    <label>Name: <input /></label>
</form>

Would be valid, but to check it I have two options:

  1. Validate the whole document: Most of the times this is good enough, but sometimes when I'm working on partial HTML snippets or embedded HTML it's quite some trouble. I'd have to copy the whole thing to a new HTML document and validate that.
  2. Just copy the snippet and validate it with the W3C validator and ignore some of the errors.

Basically I'd like to check, whether an element contains only elements it's allowed to contain.

回答1:

You can actually use the W3C validator to check a snippet.

Choose the 'Validate by Direct Input' tab and select More Options. In there there is a radio button to 'Validate HTML fragment'. http://validator.w3.org/#validate_by_input+with_options

It will wrap your page in valid html so the errors which you see are only due to your snippet.