Is it OK to use <div> inside <code>? - html

Is it OK to use "div" tag inside "code" tag? I am using NetBeans 8.0.1 for writing some html pages. And at some point I had to use "div" inside "code". You can see a part of my code in http://jsfiddle.net/125ypcum/
NetBeans gives me the following error
Element "div" not allowed as child of element "code" in this context.
The result is what I want however the error troubles me. Is it a problem in anyway? Is there a way to get the same result without an error?
The code from jsfiddle:
<div class="codeDiv">
<pre>
<code class="black">stat<sub>0</sub>;
<div class="back-red codeBoxMargin">if (expr<sub>1</sub>)
<div class="back-green codeBoxMargin"> if (expr<sub>2</sub>)
{
stat<sub>1</sub>;
stat<sub>2</sub>;
}
else
<div class="back-blue codeBoxMargin"> if (expr<sub>3</sub>)
{
stat<sub>4</sub>;
stat<sub>5</sub>;
}
else
stat<sub>6</sub>;
</div></div>else
stat<sub>7</sub>;
</div>stat<sub>8</sub>;</code></pre>
</div>

code is an inline element, while div is a block element. Block elements must not appear inside inline elements.
If you want to have a div inside a preformatted container, use pre. Note, however, that pre by default shows line-breaks as-is. Or use a span element inside code (as proposed by Lowe Bäckström in a comment).

If you want to mark up your code with the code element, but you want a particular presentation style, use elements like div to set the arrangement of the elements, and then <code> to mark up the code itself. For your html, this would be:
<div class="codeDiv black">
<code>stat<sub>0</sub>;
</code>
<div class="back-red codeBoxMargin">
<code>if (expr<sub>1</sub>)
</code>
<div class="back-green codeBoxMargin">
<code> if (expr<sub>2</sub>)
{
stat<sub>1</sub>;
stat<sub>2</sub>;
}
else
</code>
<div class="back-blue codeBoxMargin">
<code> if (expr<sub>3</sub>)
{
stat<sub>4</sub>;
stat<sub>5</sub>;
}
else
stat<sub>6</sub>;
</code></div></div><code>else
stat<sub>7</sub>;
</code>
</div><code>stat<sub>8</sub>;</code>
</div>
NB: I have removed the <pre> tags!
You can then set the whitespace to be preserved using the css declaration code { white-space: pre; } and set your font to an appropriate monospace font.
code {
white-space: pre;
font: 1em/1.5 Menlo, Consolas, 'DejaVu Sans Mono', Monaco, monospace; /* or whatever */
}
Here's the JSFiddle.
<code> is an inline element meant for marking up spans of text within block-level elements like <div>, <p>, and so on. While most browsers will happily render your original html, it would be syntactically incorrect.

Related

How to select all pre elements inside of class, recursively?

I have a very small project (html + css), where all of the "pre" tags had a CSS style. It was looking like this:
pre { color: blue; }
But now, I want to add this into a very large project, and I don't want to ruin everything. I want to modify only those "pre" tags, which are related to this specific small thing. So my idea is, that I will put everything - which is related to this - in the inside of a wrapper "div" element, and only those "pre" tags should be affected with the CSS which are in the inside of this wrapper, no matter how deep.
An example:
<pre> not this </pre>
<div> not this </div>
<pre> also not this one </pre>
<div class="wrapper">
<pre> this should be affected </pre>
<pre> this should be affected </pre>
<pre> this should be affected </pre>
<div>
<h1>
<span>
<pre> This should be also affected, but it is sadly not working. </pre>
</span>
</h1>
</div>
</div>
This is my CSS, but this is only working for the top-level "pre" tags:
.wrapper pre {
color: blue;
}
If I modify the CSS to this:
pre {
color: blue;
}
That will work for all of the "pre" elements, but the problem is, I want it only for those "pre" elements which are in the inside of the wrapper element.
How can I solve this? I was reading stackoverflow and other sites about CSS, and I am trying to solve this but I cannot figure this out. Thank you.
The code you provided already works. Do you have more CSS rules in your other project that are maybe preventing your inner pres to not become blue?
.wrapper pre {
color: blue;
}
<pre> not this </pre>
<div> not this </div>
<pre> also not this one </pre>
<div class="wrapper">
<pre> this should be affected </pre>
<pre> this should be affected </pre>
<pre> this should be affected </pre>
<div>
<h1>
<span>
<pre>This should be also affected, but it is sadly not working.</pre>
</span>
</h1>
</div>
</div>

Selecting strangely nested elements

I can't seem to find a situation similar enough that I can figure out a solution. Without changing the html and adding classes and ids how can I select the a tag and the pre tag from the following code? I've included my attempts.
div.info p.c6 span a {
background-color: red;
}
p.c6 span pre {
background-color: blue;
}
<div class="info">
<h1 class="c4">
<a name="h.6q469n2havqi"></a><span>Title</span>
</h1>
<p class="c6">
<span>
<pre>
words
</pre>
<br>
Top of Page
<br>
</span>
</p>
</div>
Because the p element can only contain phrasing content, and in this case it contains flow content, the browser is closing the element and invalidating your selectors.
In short, the pre element is flow content and cannot be contained inside a p element. Therefore, the browser is overriding your HTML structure to maintain valid mark-up. Here's what it looks like:
The browser has essentially converted your p descendants into p siblings.
You need to restructure your HTML for your selectors to work.
If you can't change the HTML, a sibling selector will work.

div has as extra unwanted space on top inside pre

I can't understand why there is an empty space on the top. I haven't applied any styles to it
<pre>
<div class="codeBlock">
<ol>
<li>(function() {</li>
<li>function $codeBlock() {</li>
<li>return {};</li>
</li>}</li>
</ol>
</div>
</pre>
pre has white-space: pre default style. From W3Schrools about white-space: pre:
Whitespace is preserved by the browser. Text will only wrap on line breaks. Acts like the <pre> tag in HTML
Of course you can move pre's content to one line but not sure this will flexible and maintainable.
So you can change white-space CSS property to normal or nowrap (depending on need of wrapping):
pre {
white-space: nowrap;
}
.codeBlock ol, li {
margin: 0;
}
<pre>
<div class="codeBlock">
<ol>
<li>(function() {</li>
<li>function $codeBlock() {</li>
<li>return {};</li>
</li>}</li>
</ol>
</div>
</pre>
If you want to return white-space behaviour for ol you can also set
.codeBlock ol { white-space: pre }
Ok so heres the thing. pre takes every character and interpret it in it's display, that mean it will litterally "show" a carriage return. Try to remove the spaces. Lets say you you twig, apply {% spaceless %}. If you use something else, use the proper function, but if you do it manually, do like i just did in the example below :) Cheers
<pre><div class="codeBlock"><ol><li>(function() {</li><li>function $codeBlock() {</li><li>return {};</li><li>}</li></ol></div></pre>

CSS properties don’t work for paragraphs with pre elements inside

I have a paragraph that contains a <pre> element and some text, like the following:
<p class="par1">
<pre>
this is second paragraph
ok
ok
</pre>
These text are inside the paragraph must be RED
</p>
And I have used the following code to change the background color of the paragraph, but it doesn’t affect the paragraph and I don’t know why.
<style>
.par1{
background-color:red;
color:green;
}
</style>
Here’s the whole code:
<!doctype html>
<html>
<head>
<title>Test id and class attribute</title>
<style>
.par1 {
background-color: red;
color: green;
}
</style>
</head>
<body>
<div class="div1">
Some text
<h1>An important heading</h1>
<p class="par1">
<pre>
this is second paragraph
ok
ok
</pre>
This text is inside the paragraph and it must be red.
</p>
</div>
</body>
</html>
I know that if I use the class of the div .div1, it works fine, but I want to know why the first one doesn’t work.
.div1{
background-color:red;
color:green;
}
As per W3c specs say, you can't have a pre inside a p
4.4.1 The p element
Content model:
Phrasing content.
Where Phrasing Content is:
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
you can use instead a span and setting it as display:block which will make it a block level element
.par1 {
background-color: red;
color: green;
display: block
}
<div class="div1">
Some text
<h1>An important heading</h1>
<span class="par1">
<pre>
this is second paragraph
ok
ok
</pre>
These text are inside the paragraph must be RED
</span>
</div>
as #dippas said, it's about the <pre>-tag inside a <p>-tag
<p>-tags can not contain block-level elements. as <pre> is a block-level element, browsers seem to close the <p>-tag, before the <pre>-tag opens (see your browser inspector). thus the styles on <p> could not be inherited by the <pre>-tag
for a good discussion with helpful hints, see:
<pre> tag making browsers close paragraphs
EDIT:
In the W3C specs, it is said that "A paragraph is typically a run of phrasing content (...)".
https://www.w3.org/TR/html5/dom.html#paragraphs

CSS: The font is bold, but it shouldn't be

HTML:
<div id="e_ticket_info" class="e_ticket_font" runat="server" Visible="False">
<p>
Some text.
</p>
</div>
CSS:
.e_ticket_font {
font-weight: normal;
}
The HTML code is on content page, which is inside master page.
The issue is - the text is bolded out, but it shouldn't be.
How can I can get rid of it?
Try
.e_ticket_font p {
font-weight: normal;
}
because you are not targetting p tag.
Dipesh's answer is correct. I'll just add a bit explanation. CSS is cascading style sheet, means the style for any element/class/id can be mentioned at multiple places and applied in the order in which they are included. In your case, some other style seems to override your style to make it bold since your snippet will not make it bold.
Considering this, as a general best practice, always target the specific elements if you are not sure if it's class will be styled somewhere else or not.
Thus, .e_ticket_font p {... is prferable than .e_ticket_font {.... If there are multiple paragraphs and you want only some of them to be different, then again use classes/ids, like
.e_ticket_font p#heading {...
.e_ticket_font p#content {...
.e_ticket_font p.specialpara {
and so on.
Another way to make it sure is to apply css inline for that element, but this should not be used generously for many elements as it affects the "structure should be separate than presentation" principle
<div runat="server" Visible="False">
<p class="e_ticket_font">
Some text.
</p>
</div>
CSS:
.e_ticket_font {
font-weight: normal !important;
}
try inline css because if you don't know if there are other css classes are specified in masterpage for <P>
something like:
<div runat="server" Visible="False">
<p style="font-weight: normal;" >
Some text.
</p>
</div>
it will work for sure, then you can check for other css references for <P>
or (for each) element below .e_ticket_font:
.e_ticket_font * {
font-weight: normal;
}
i advice a rare use of !important in case of runaway bubbling your DOM
but mind of the selector detail.. if there is any css selector which describes the object on a directer way like...
.e_ticket_info#e_ticket_info {
font-weight: bold;
}
...css will pick that one with privileg!