Here is a snippet of my code:
<!DOCTYPE html>
<html>
<head>
<style>
code {
display: inline-block;
padding: 0;
color: red;
}
p > code:only-child {
display: block;
white-space: pre;
padding: 20px;
line-height: 125%;
background: #eee;
border: 1px solid #ccc;
margin: 1em 0;
}
</style>
</head>
<body>
<p>Assuming you have the IP <code>1.1.1.1</code>:</p>
<p><code>
echo "hello world";
echo "another command here";
echo "you are done";
</code></p>
</body></html>
However, this is causing an issue with how the page displays:
https://i.stack.imgur.com/LtM6O.png
The problem arises from the p > code:only-child
selector also affecting the <code>1.1.1.1</code>
. What CSS rule can I use to target a <code>
element only if it's the sole child and has no non-space character siblings?
Just to note: I am unable to modify the HTML directly as it was generated by inaccessible software. Additionally, JavaScript is off-limits for me. I am only permitted to add my own CSS rules.
I have numerous HTML documents like this one. Some contain segments like
<p>some text<code>/etc/path-to-conf</code>some text</p>
, others have <p>text<code>...</code>text<code>...</code>...</p>
, and some simply consist of <p><code>...</code></p>
. I wish to automate the process so that any <code>
element which stands alone without non-space character siblings appears as a block level element, while all other instances appear as inline-block elements.