Remove leading whitespace from whitespace: pre element - html

I want to use a custom style for code snippets in my blog. I defined the following style:
mystyle {
background: #C3FFA5;
border: solid 1px #19A347;
color: #191919;
display: block;
font-family: monospace;
font-size: 12px;
margin: 8px;
padding: 4px;
white-space: pre;
}
I use it as follows:
<mystyle>
int main() {
cout << "Hello World" << endl;
}
</mystyle>
This gives the following output. I have tried on Firefox and Google Chrome.
I want to remove the extra line at the start of the block. Obviously, I understand where the newline comes from, and that I can use <mystyle>int main() { instead. If I use <pre> instead of <mystyle>, there is no extra newline, so is it possible to do this with my custom style too?

Check out the answer to this very similar question:
.mystyle:first-line {
line-height: 0px;
}
Might require a modern-ish browser, though.

Adjust margin-top to whatever line-height you have set.
.text {
margin-top: -1em;
white-space: pre-line;
}
This works for FF too, which :first-line hack can't fix.

When we use white-space: pre-wrap, then when the page renders, it also takes space from your html file. So suppose we have:-
<div style="white-space:pre-wrap"> <!-- New Line -->
<!-- 2 space --> This is my text
</div>
The text will render in this way:-
<leading line>
<2 spaces >This is my text
Hence we should HTML like this:-
<div style="white-space:pre-wrap">This is my text</div>

Add the style to the <pre> tag, using a class. For example (trying to keep it simple here).
<pre class="console">
// Some code here to be styled.
</pre>
<pre class="some-other-style">
// Some code here to be styled.
</pre>
Then your CSS looks like this:
pre.console {
color: #fff;
background-color: #000;
}
pre.some-other-style {
color: #f00;
background-color: #fff;
}
If it doesn't do what you want then I'm confused by your question, just comment and I'll remove the answer.

This is for you who want to remove the trailing space only from the first line of the formatted output
instead of writing the code like this
<pre>
This is my code </pre>
write it like this
<pre>This is my code</pre>
or in case you are using a <div> tag as shown below
<div style="white-space:pre-wrap">
This is my code
</div>
write it like this
<div style="white-space:pre-wrap">This is my code</div>

Code formatting is at the essence here, make sure each line start at the first character of that line:
<pre class="code">
int main() {
cout << "Hello World" << endl;
}
</pre>
The following CSS will suffice:
pre.code
{
background: #C3FFA5;
border: solid 1px #19A347;
color: #191919;
display: block;
font-family: monospace;
font-size: 12px;
margin: 8px;
padding: 4px;
}
Read this article on whitespace, and the following on how to 'fight it'. Although the last article discusses whitespace between inline elements, the formatting solution relates to your issue.

How about
<mystyle>into main() {
// ...
}</mystyle>
No white space before or after...

Just fix your template, man. Thats easiest way:
<mystyle>int main() {
cout << "Hello World" << endl;
}</mystyle>

Not a pure CSS solution but works for me:
<script>
document.querySelectorAll('pre').forEach((e) => {
e.innerHTML = e.innerHTML.trim();
})
</script>

This solution works for me when dealing with white-space: pre-line.
Add span inside white-space element.
<p class="whitespace-pre-line">
<span>Your text goes here</span>
</p>

Related

Add quotes around some words, but only when copy pasting - opposite of the q tag

I'm trying to do somewhat of the opposite of the <q> tag. The q tag visually displays quotes in the web page, but if you copy paste the text, the quotes are not present in the pasted text. I want the opposite - no visual quotes in the web page, but quotes are present in the copy pasted text.
Why? Because I sometimes prefer to use other visual styles in my webpage instead of quotes. But when a user copies the text, since I cannot rely on them pasting the text into something capable of preserving the rich formatting/styles, I wish to revert to using quotes because they're simple ascii characters which will work in any plain text context.
I'll give a concrete example to help clarify.
Given:
.qq { background-color: yellow; }
I am <span class=qq>some quoted</span> text.
When the user views the page, they should see:
But if they were to copy the text and then paste it somewhere, the pasted text would be:
I am "some quoted" text.
Is it possible via just css? I'd prefer not to use js.
I don't really care what the html/css needs to be, so if quotes, or the q tag etc... need to be present in the html source instead of a span, that's totally ok.
The only way I can think of without JS is to put the quotes in the markup and hide them using CSS. The markup will get a bit messy though. Something like:
.lq,
.rq {
font-size: 0;
color: transparent;
}
I am <i class="lq">“</i>some quoted<i class="rq">”</i> text.
using JS you can do this way
window.addEventListener("copy", (e) => {
event.preventDefault();
const selection = document.getSelection();
let selectedText = selection.toString();
let selectedTextWithQuotes = `"${selectedText}"`;
// FOR SAVING IN CLIPBOARD
event.clipboardData.setData("text/plain", selectedTextWithQuotes);
});
Using only css you could use transparent double quotes:
.qq { background-color: yellow; }
quote {
display: inline-block;
width: 0px;
color: transparent;
}
I am <quote>"</quote><span class=qq>some quoted</span><quote>"</quote> text.
UPDATE
A not recommended alternative. It is compatible with 75% of browsers. (I'm referring to the # font-face: size-adjust property), but it's an alternative.
For the main font you create a character set that includes only the double quote, but with the size-adjust property at 0%. The remaining text will be rendered with the secondary font.
I am "some quoted" text.
#font-face {
font-family: 'myFont';
src: url(https://fonts.gstatic.com/s/crimsonpro/v23/q5uUsoa5M_tv7IihmnkabC5XiXCAlXGks1WZzm1MP5s-.woff2) format('woff2');
unicode-range: U+0022;
size-adjust: 0%;
}
.yourBody {
font-family: myFont, 'courier new';
}
.qq { background-color: yellow; }
<div class="yourBody">
I am "<span class=qq>some quoted</span>" text.
</div>
.quote-character-left {
margin-right: -0.5em;
opacity: 0;
}
.quote-character-right {
margin-left: -0.5em;
opacity: 0;
}
.quote-text {
display: inline;
background-color: yellow;
}
<p>I am <span class="quote-character-left">"</span><span class="quote-text">some quoted</span><span class="quote-character-right">"</span> text.</p>

HTML (With a little php) - Can't maintain indent of text on wrapping text

I've been on this problem for hours. I'm using PHP to display some HTML. It works, but I can't maintain the text indent on a long wrapping line of inserted text.
I've recreated the issue in HTML with the same issue for your convenience.
<style>
.indent {
padding-left: 1.5em;
text-indent:-1.5em;
}
</style>
<html>
<main>
<b class="do_something">X</b> <span class="indent"> Here are some words. When it wraps to the next line I really want them to stay in line with everything in the span, under the word HERE, rather than return under the BOLD "X" value. Cheers for the help. </span>
</main>
</html>
Now I've come close to fixing it using a display block, but alas the block creates a new line in the span and I need to stay on the same line as the X, which is important. I also tried flex but no joy.
Any ideas? Thanks.
Here's my php, which probably isn't relevant.
echo '<b class="do_something">X</b>', str_repeat(' ', 3);
echo '<span class = "indent">', nl2br($insert_words), '</span><br>';
echo '<hr>';
//And my other CSS:
.indent {
display:block;
margin-left:25px;
}
The problem is that you are using span which is an inline element.
You also do not need text-indent. It is giving you unexpected results because it only applies to one line of text, it has no effect on the lines that wrap.
You could achieve the desired result using flex like on the following example. I added some background color so you can see how the elements align themselves.
.indent {
padding-left: 1.5em;
text-indent: -1.5em;
background-color: #f8d7da;
}
.do_something {
background-color: #fff3cd;
}
#flex-container {
display: flex;
}
#flex-second {
padding-left: 1em;
background-color: #d4edda;
}
#x { background-color: #cce5ff; }
#fixed-width-x {
float: left;
}
<p>Example using flex</p>
<div id="flex-container">
<div id="x"><strong>X</strong></div>
<div id="flex-second"> Here are some words. When it wraps to the next line I really want them to stay in line with everything in the span, under the word HERE, rather than return under the BOLD "X" value. Cheers for the help. </div>
</div>
<p>Your example below</p>
<main>
<b class="do_something">X</b> <span class="indent"> Here are some words. When it wraps to the next line I really want them to stay in line with everything in the span, under the word HERE, rather than return under the BOLD "X" value. Cheers for the help. </span>
</main>

Show code in a P tag without loading it

I have a simple style like this,
.codecontain{
padding: 10px;
background-color: #34495e;
color: #ecf0f1;
max-width: 200px;
border-radius: 10px;
}
and I would like to display a code in it like this,
<div class="codecontain">
<p><hr></p>
</div>
but when I do it HTML displays the result but I want HTML to show text.
I know there are syntax highlighters but I don't want to use them.
Adding to André's answer, here's a codepen, his answer is correct.
<div class="codecontain">
<p><hr></p>
</div>
http://codepen.io/anon/pen/wGwKqe
Another approach would be using
<xmp></xmp>
as suggested here How to print code on HTML
Be careful as xmp is deprecated.
also, possible duplicate
You have to replace the < with < and the > with >.
If you do not want to do it manually you could use some javascript to convert it.
I wrapped the html in comments (and removed the comments in js) for two reasons. One is to avoid having the html rendered normally and then overwritten, and the seconds so that you can have invalid html as you did. (the hr element is not valid inside a p so the browser would correct it and the script would show the corrected html)
var code = document.querySelectorAll('.codecontain');
Array.prototype.forEach.call(code, function(node) {
node.textContent = node.innerHTML.slice(4,-3);
});
.codecontain {
padding: 10px;
background-color: #34495e;
color: #ecf0f1;
max-width: 200px;
border-radius: 10px;
white-space: pre;
}
<div class="codecontain"><!--
<p><hr></p>
--></div>

Calculate number of letters and apply CSS

Guys this question is related to this one > Apply CSS to the words in a paragraph written in brackets (parenthesis)
As the databse is not in my control, i'm trying to find some alternatives. Is there a way in CSS to count the number of characters in a sentence and then apply CSS to the rest of the characters?
1ch = width of a "0" (zero)
1ex = height of an "x" (lower case)
ex seems more accurate. Like #BoltClock♦ said, it's not counting, but it's a way to limit the number of characters. I'd try to "do some CSS with the rest" but OP was not specific, and frankly, I have no idea.
Update
The best I can come up with is putting the remaining text in a :after and then style the content.
p.fifteen { max-width: 15ex; outline: 1px solid red; }
p.seventeen { max-width: 15ch; outline: 1px solid red; }
p.fifteen:after { content: 'fghijklmnop'; font-size: 36px; color: red; }
p.seventeen:after { content: 'hijklmnop'; font-variant: small-caps; font-weight: 900; }
<p class="fifteen">123456789abcde</p>
<p class="seventeen">123456789abcdefg</p>
You'll need JavaScript to do that just use somestring.length like so:
var someString= 'abc';
var str = someString.length;
console.log(string);
Result: 3
Check this out for more info http://www.quirksmode.org/js/strings.html
or jQuery method is:
Html
<div id="selector">Some Text</div>
jQuery
$('#selector').text().length;

How to Wrap Text Within an Image-Link? HTML CSS

I have some code which displays images from imgur.
When hovering over an image, I want the title attribute to appear at the top of the image.
I have achieved all of this, but when the text becomes visible, it only appears on one line and writes over itself when it runs out of space, as in the image.
I would like the text to start on a new line once it reaches the end of it's container.
I have tried adding word-wrap: break-word; to each of the CSS selector below, as well as to a P selector (as the link is wrapped in a p-tag).
Any advice on how to resolve this is much appreciated!
I have the following html:
<section id='photos'>
<p>
<a class='hovertext' href='http://i.imgur.com/gallery/eWlGSdR.jpg' title='Opened my window shade while flying over Japan, noticed a volcano was erupting. (OC) [2448x2448]'>
<img src='http://i.imgur.com/eWlGSdR.jpg' alt=''>
</a>
</p>
And the following CSS:
a.hovertext {
position: relative;
text-decoration: none !important;
text-align: left;
}
a.hovertext:before {
content: attr(title);
position: absolute;
top: -1;
padding: 0.5em 20px;
width: 90%;
text-decoration: none !important;
color: red;
opacity: 0.0;
}
a.hovertext:hover:before, a.hovertext:focus:before {
opacity: 1.0;
}
As Dinesh said in the comments, this was caused by poor code awareness, as elsewhere in the code, I was calling 'line-height:0;' on the #photos element.
Removing this solved the problem.
i think you coul use some java script on this, if you only want to make it add a extra line to it, correct me if im wrong.
Here's an example what i think you mean:
First add this text next your class="hovertext" :
id="HoverText"
Add this part after your body or paste the code between script into a .js file and call it with
<script src="filename.js"></script>
<script>
HoverText=document.getElementById("HoverText");
HoverText.onclick=function(){ClickToShowText()};
function ClickToShowText(){
HoverText.innerHTML+="<br>New line with text";
}
</script>
just use the break tag at the end of the text that's supposed to be on the first line.
</br>
easy