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>
Related
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.
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
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.
In our project I'd like to style our doxygen output differently.
Currently the generated html looks like the following:
<html>
<body>
<h1> Heading 1 </h1>
<h2> Heading 2.1 </h2>
<p> Paragraph 2.1.1 </p>
<p> Paragraph 2.1.2 </p>
<p> Paragraph 2.1.3 </p>
<h2> Heading 2.2 </h2>
<p> Paragraph 2.2.1 </p>
<p> Paragraph 2.2.2 </p>
<p> Paragraph 2.2.3 </p>
</body>
</html>
The <h2> is only styled with a font-size attribute and all <h2> and <p> tags are aligned on the left side of the document.
To let the content below any <h2> tag stand out visually I would like to indent the tags until the next <h2> tag.
What I tried so far is the following CSS rule:
h2 + * {
margin-left: 10px;
}
The * is used since there are also other tags present besides <p> tags.
However, this rule only indents the first paragraph following the <h2> tag and not all tags up to the next <h2> tag.
It should also be mentioned that the structure of the html can not be changed to wrap each section inside of a <div> for example.
It sounds like you want to indent all siblings after the first h2 except other h2s, in which case this should do the job:
h2 ~ *:not(h2) {
margin-left: 10px;
}
See the general sibling combinator and the negation pseudo-class as well as a live demo on jsbin.
There's a couple of options of varying complexity, the first is:
h2 ~ *:not(h2) {
margin-left: 1em;
}
JS Fiddle demo.
This approach selects all following-sibling elements of an h2 that is not itself an h2.
The second is slightly simpler:
body {
padding-left: 1em;
}
body h2 {
margin-left: -1em;
}
JS Fiddle demo.
This approach does, essentially, mean that all content except the h2 will be indented; so it's not quite perfect for your use-case, but may be adaptable to your specific requirements).
Hi have a Paragraph block with the following style applied:
form.filters p {
clear:both;
padding: 5px 0 5px 0;
}
INSIDE the Paragragn, I have a Div with the following style:
form.filters .inputHolder {
float:left;
}
When viewing the rendered page, the DIV seems to sit under the P, and when viewing the code in Chrome, the DIV acually is siting below the P..
Source Code:
<p>some stuff
<div>more stuff<div>
</p>
Viewed in Chrome:
<p>some stuff</p>
<div>more stuff<div>
Can anyone tell me what is happening and what I can do to resolve this?
Many Thanks
<div>s aren't allowed in <p>s. The HTML parser, when it sees the <div> tag, automatically closes the <p> element.
Use a <span> instead of a <div>.
According to the spec the <p> element cannot contain "block-level elements (including P itself)". My guess is Chrome is just trying to be helpful here.
Source: http://www.w3.org/TR/html401/struct/text.html#h-9.3.1