Should ol/ul be inside p or outside?
The short answer is that ol
elements are not legally allowed inside p
elements.
To see why, let's go to the spec! If you can get comfortable with the HTML spec, it will answer many of your questions and curiosities. You want to know if an ol
can live inside a p
. So…
4.5.1 The
p
element:Categories: Flow content, Palpable content.
Content model: Phrasing content.
4.5.5 The
ol
element:Categories: Flow content.
Content model: Zero or more li and script-supporting elements.
The first part says that p
elements can only contain phrasing content (which are “inline” elements like span
and strong
).
The second part says ol
s are flow content (“block” elements like p
and div
). So they can't be used inside a p
.
ol
s and other flow content
can be used in in some other elements like div
:
4.5.13 The
div
element:Categories: Flow content, Palpable content.
Content model: Flow content.
Can we have ul inside a paragraph
No. See the spec.
A paragraph can contain "Phrasing content." which does not include list elements.
Phrasing content is the text of the document, as well as elements that mark up that text at the intra-paragraph level. Runs of phrasing content form paragraphs.
a abbr area (if it is a descendant of a map element) audio b bdi bdo br button canvas cite code data datalist del dfn em embed i iframe img input ins kbd keygen label map mark math meter noscript object output progress q ruby s samp script select small span strong sub sup svg template textarea time u var video wbr text
An 'ul' element can never be a child of a 'p' element
Please check the HTML specification, which clearly states that putting lists in a paragraph element is forbidden, and also give some examples on what could be done:
List elements (in particular, ol and ul elements) cannot be children of p elements. When a sentence contains a bulleted list, therefore, one might wonder how it should be marked up.
For instance, this fantastic sentence has bullets relating to
- wizards,
- faster-than-light travel, and
- telepathy,
and is further discussed below.
The solution is to realise that a paragraph, in HTML terms, is not a
logical concept, but a structural one. In the fantastic example above,
there are actually five paragraphs as defined by this speciication:
one before the list, one for each bullet, and one after the list.The markup for the above example could therefore be:
<p>For instance, this fantastic sentence has bullets relating to</p>
<ul>
<li>wizards,
<li>faster-than-light travel, and
<li>telepathy,
</ul>
<p>and is further discussed below.</p>
Authors wishing to
conveniently style such "logical" paragraphs consisting of multiple
"structural" paragraphs can use the div element instead of the p
element.Thus for instance the above example could become the following:
<div>For instance, this fantastic sentence has bullets relating to
<ul>
<li>wizards,
<li>faster-than-light travel, and
<li>telepathy,
</ul>
and is further discussed below.</div>
This example still has
five structural paragraphs, but now the author can style just the div
instead of having to consider each part of the example separately.
p tag not containing ul tag
Please see the answer from a similar question: List of HTML5 elements that can be nested inside P element?
Only phrasing content can be in between the <p>
tag and is described as follows:
Phrasing content is the text of the document, as well as elements
that mark up that text at the intra-paragraph level. Runs of phrasing
content form paragraphs.
Can we use any other TAG inside ul along with li?
For your code to be valid you can't put any tag inside a <ul>
other than an <li>
.
You can however, put any block level element inside the <li>
, like so:
<ul>
<li>
<h2>...</h2>
<p>...</p>
<p>...</p>
</li>
</ul>
Why won't this CSS selector work for li when inside p tag?
It's because ol/ul items aren't allowed inside of p elements.
Should ol/ul be inside <p> or outside?
Seems like a trick question for your exam then. Most likely you will want to brush up on the HTML spec since the question isn't truly based on CSS knowledge alone.
You are getting the described behavior because <p>
cannot contain block-level elements such as <ul>
or <ol>
. The browser (e.g., Chrome) knows this is not possible and thus try to handle your illegal structure by placing your block element in between two paragraphs:
Because of this, the rule you have declared for that li
does not apply.
p ul li { color: blue;}
<p> Hello <ul> <li>I'm never blue.</li> </ul> Goodbye</p>
Nesting a ul/ul within a p/p creates two paragraphs. Why?
Paragraphs may not contain block-level elements. Some browsers apparently assume you've made a mistake and attempt to correct it.
Read more
unordered list in a paragraph element
According to the HTML 5 specification, a paragraph may contain phrasing content, which still does not include other grouping elements:
http://www.w3.org/TR/html5/grouping-content.html#the-p-element
According to the HTML 4.01 specification, a paragraph may only contain inline elements:
http://www.w3.org/TR/html401/struct/text.html#h-9.3.1
9.3.1 Paragraphs: the P element
<!ELEMENT P - O (%inline;)* -- paragraph -->
<!ATTLIST P
%attrs; -- %coreattrs, %i18n, %events --
>
The correct markup in this case is to close your paragraph before starting the list.
Alternatively, you can use another tag other than paragraph (like <div>
) which is not processed in this way.
<div>
<ul> ... </ul>
</div>
Should I have aside element ouside or inside of main element?
In HTML5 it’s only defined that aside
is "related to the content around the aside
element".
In HTML 5.1 (CR) the definition became more specific, as it now says that aside
is "related to the content of the parenting sectioning content".
Following the newer definition, the aside
element should be inside of the section element to which it is related. The main
element is not a sectioning element (elements like article
, section
, body
, figure
etc. are). You can of course still place aside
in main
, but it will be related to the nearest sectioning element parent of main
.
That means there is no semantic difference (for aside
) in these two examples:
<body>
<main></main>
<aside><!-- related to the body --></aside>
</body>
<body>
<main><aside><!-- related to the body --></aside></main>
</body>
Example that shows a few different cases:
<body>
<main>
<article>
<aside><!-- related to the article --></aside>
<section>
<aside><!-- related to the section --></aside>
<blockquote>
<aside><!-- related to the blockquote (not to the section!) --></aside>
</blockquote>
<div>
<aside><!-- related to the section (not to the div!) --></aside>
</div>
</section>
</article>
<aside><!-- related to the body (not to the main!) --></aside>
</main>
<aside>
<!-- related to the body -->
<aside><!-- related to the (parent) aside --></aside>
</aside>
<nav>
<aside><!-- related to the nav --></aside>
</nav>
<footer>
<aside><!-- related to the body (not to the footer!) --></aside>
</footer>
</body>
Related Topics
Why Do Browsers Insert Tbody Element into Table Elements
Nesting Block Level Elements Inside the ≪P≫ Tag... Right or Wrong
What's the Proper Value For a Checked Attribute of an HTML Checkbox
Are Class Names in CSS Selectors Case Sensitive
Using HTML and Local Images Within Uiwebview
Percent Pie Chart With CSS Only
Html5 - Canvas Element - Multiple Layers
What's the Difference Between Disabled="Disabled" and Readonly="Readonly" for HTML Form Input Fields
Should I Put Input Elements Inside a Label Element
How to Use the "Required" Attribute With a "Radio" Input Field
Why Are Bootstrap Tabs Displaying Tab-Pane Divs With Incorrect Widths When Using Highcharts
How to Get HTML 5 Input Type="Date" Working in Firefox And/Or Ie 10