Putting a block level element inside a

element

前端 未结 2 929
无人及你
无人及你 2021-01-14 08:00

I know that

is to be used specifically with inline elements. But what if you change an inline element like into a block-leve

相关标签:
2条回答
  • 2021-01-14 08:38

    It's HTML5 valid and it's not that bad in certain situations e.g.

    <p>
       This is some text <span class="highlight">I am a pizza</span> and this is some more text...
    </p>
    
    .highlight {
      background: yellow;
    }
    
    0 讨论(0)
  • 2021-01-14 09:01

    A span element is always a text/inline/phrase element in HTML, and the HTML syntax rules that restrict p element content to such elements relate to HTML only. So they are not affected by CSS settings that may make a span a block element in the CSS (rendering) sense.

    In CSS, you can assign any defined value to the display property, no matter what the element is like. CSS is ignorant of the meanings of elements as defined in HTML or other markup language specifications.

    Thus, there is no formal objection.

    Whether it is good style, or otherwise acceptable, is more complicated. There does not seem to be any statement on this in specifications, but it is reasonable to say that you should not change basic rendering features elements in vain. For example, in normal conditions, you should not use span and then say display: block in CSS, when there is the more logical approach of using div. One reason to this principle is that it keeps your document in a better shape in non-CSS rendering situations or when all or some of your style sheet is not applied.

    On the other hand, you would not change display in vain if you have a text paragraph and you wish to render part of its content as a block, e.g. as a centered or indented line, possibly with a background color that stretches through the available width. You cannot use div inside p, so the more natural markup is not available.

    Since the example is not a real one, it is impossible to say whether it is OK to deploy this approach in your case.

    0 讨论(0)
提交回复
热议问题