In my project we need to display code snippet, so I am using pre and code(both) tag.
PROBLEM
While displaying the code snippet it is displaying white spaces before and after the actual content. How to remove these white spaces before and after the snippet.
Remove the whitespace inside your pre tag.
Example:
<pre>
This is a test.
We want a new line here.
</pre>
should be
<pre>This is a test.
We want a new line here.</pre>
With HTML 5 pre tag doesnt supported anymore if you want to use its features.
Make your own css class like this...
.pre
{
border: 1px solid #999;
page-break-inside: avoid;
display: block;
padding: 3px 3px 2px;
margin: 0 0 10px;
font-size: 13px;
line-height: 20px;
word-break: break-all;
word-wrap: break-word;
/*white-space: pre;
white-space: pre-wrap;*/
background-color: #f5f5f5;
border: 1px solid #ccc;
border: 1px solid rgba(0, 0, 0, 0.15);
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
font-family: Monaco, Menlo, Consolas, "Courier New", monospace;
font-size: 12px;
color: #333333;
}
.pre code
{
padding: 0;
color: inherit;
white-space: pre;
white-space: pre-wrap;
background-color: transparent;
border: 0;
}
Cancelling /*white-space: pre;
white-space: pre-wrap;*/ will make your white spaces works like div.
use it as a class to give the same look to your elements.
The <pre> tag is for pre-formatted text. That means you need to do the formatting yourself - all of it, including making sure the whitespace is exactly what you want to display. Don't output excess whitespace between your <pre> tags and the content inside of them.
You need to make sure the code is formatted correctly, the pre tag tells the browser to show the text inside the pre "as is".
A little thing that I my self has found useful is to use this php to import the file so I don't have to cut and paste all the time.
<?php
$file = "code/hello_world.c";
print "<p>Filename: ".$file."</p>\n";
print "<div class=\"codebox\"><pre>\n\n";
$lines = file($file); foreach ($lines as $line_num => $line)
{
echo htmlspecialchars($line) . "";
}
print "\n</pre></div>\n";
?>
And then to make it look like code I add this in my css
PRE {
font-family: 'Monotype.com', Courier New, monospace;
font-size: 0.7em;
}
.codebox {
width: 90%;
background-color: #000000;
color: #FFFFFF;
}
.codebox pre {
margin: 0 0 0 20px;
}
Maybe you find it helpful.
Related
I'm trying to change the appearance of code sections on my Joomla 4 website using the Cassiopeia template. A sample HTML source looks like this:
<p>Trying to format a code section below.</p>
<pre><code>This is code line 1, starting at position 1
This is code line 2, starting at position 1 as well
This is code line 3, starting at position 1 as well
</code></pre>
<p>This text follows the code section.</p>
Without adding user CSS, this is displayed as follows:
All lines are left-aligned, as expected.
I'm adding the following CSS to the user.css file:
pre {
display: block;
width: auto;
max-height: 600px;
overflow: auto;
background-color: #eee;
border-radius: 10px;
border: 1px solid;
border-color: var(--cassiopeia-color-primary, #111 );
scrollbar-color: #ccc transparent;
margin: 20px 40px;
padding: 30px;
word-wrap: normal;
}
pre > code {
font-size: 1.0rem;
text-indent: 0;
color: #111;
white-space: inherit;
margin: 20px 20px;
}
The CSS (mostly) works as desired, i.e display the code in a bordered box with grey background. However, the first code line is indented by 2 characters. See here:
I tried to find the cause using Firefox Web Inspecting Tools (shift-crtl-i), but can't seem to find out. What is causing that 2 character indent?
Please update your code a bit, remove margins from styles for the code element and make it a block element:
pre {
display: block;
width: auto;
max-height: 600px;
overflow: auto;
background-color: #eee;
border-radius: 10px;
border: 1px solid;
border-color: var(--cassiopeia-color-primary, #111 );
scrollbar-color: #ccc transparent;
margin: 20px 40px;
padding: 30px;
word-wrap: normal;
}
pre > code {
display: block;
font-size: 1.0rem;
text-indent: 0;
color: #111;
white-space: inherit;
}
The <code> element is inline by default.
does anybody know how I can adjust the width of the text within a (code) tag so that it will always fit within the background? I am trying to let the text within the (pre) resize with the pre as the background resizes with the browser.
Pictures for reference:
Before minimize
After minimize
This is the current code I have.
HTML
<pre>
<code>
if (condition) { statement_1_runs_if_condition_is_true;
statement_2_runs_if_condition_is_true; } else {
statement_3_runs_if_condition_is_false;
statement_4_runs_if_condition_is_false; }
</code>
</pre>
CSS
pre {
background-color: #CCC5B9;
border: 1px solid #999;
border-radius: 3px;
display: block;
width: 60%;
margin: 0 0 0 30px;}
code{
color: #252422;
font-size: 14px;
font-family: Consolas;}
Thank you.
The pre tag preserves all text formatting such as white space and line breaks.
You can turn the word wrap back on in the pre tag css.
pre {
background-color: #CCC5B9;
border: 1px solid #999;
border-radius: 3px;
display: block;
width: 60%;
margin: 0 0 0 30px;
white-space: pre-wrap;
}
code{
color: #252422;
font-size: 14px;
font-family: Consolas;
}
This web page explains how to wrap text in pre tag. In short, you should add something like this in yuor css:
pre
{
overflow-x: auto;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -pre-wrap;
white-space: -o-pre-wrap;
word-wrap: break-word;
}
As it is code you should not allow extra word wrapping as that could affect what the code actually does, at least in some languages, and would not be correct if the user did some copy and pasting elsewhere.
A quick and simple way of making sure it always fits and maintains the correct line breaks is to set the font-size in terms of the viewport width.
body {
width: 100vw;
}
code {
font-size: 2vw;
font-family: monospace;
}
<pre>
<code>
if (condition) { statement_1_runs_if_condition_is_true;
statement_2_runs_if_condition_is_true; } else {
statement_3_runs_if_condition_is_false;
statement_4_runs_if_condition_is_false; }
</code>
</pre>
Of course, for this to work you need to know that your code will fit into the width with the given font size. This can be tested in advance and a suitable font-size chosen.
However, if you have a very dynamic situation where you are adding code say through JS during run time you will need the JS (can't be done in CSS unfortunately) to calculate the maximum line length (in characters) and adjust the font-size accordingly.
I am create code css below.
I want to horizontal scroll but when sentence is too long it's not create scrolling but sentence break and show in next line.
In image i want aaaa.. should be printed in only one line and bbb.. in second line.but aaa... sentence come on second line without tag.
html code & CSS code
this div tag container width is 80% of the screen.
code {
margin: 10px 0;
padding: 10px;
text-align: left;
display: inline-block;
overflow: auto;
font: 500 1em/1.5em 'Lucida Console', 'courier new', monospace;
background: #FAFAFA;
border: 1px solid #f2f2f2;
border-left: 4px solid #6d7fcc;
color:#000;
}
<code> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</br>
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</code>
Try using word break
word-break: normal;
After many try i get right answer..
white-space:nowrap;
This property remove white-space to add sentence to the new line.
I can't figure out what is causing the uneven spacing that you see in the image http://i.imgur.com/AZoXzYf.png (can't embed images yet ... sorry)
which comes from http://playclassicsnake.com/Scores. My relevant CSS is
.page-btn { background: #19FF19; color: #FFF; border: 0; border: 3px solid transparent; }
.page-btn.cur-page { border-color: #FFF; cursor: pointer; }
.page-btn + .page-btn { margin-left: 5px; }
and I've inspected the elements to make sure there's nothing fishy. What's the deal?
You have a new line character in your HTML just after your first button:
<button class="page-btn cur-page">1</button>
<button class="page-btn">2</button><button class="page-btn">3</button>
Make it all in 1 line and it will start to work without any extra spaces:
<button class="page-btn cur-page">1</button><button class="page-btn">2</button><button class="page-btn">3</button>
Your CSS is perfectly fine and doesn't need to be altered as mentioned by others..
Hi now try to this css
#page-btns-holder {
width: 80%;
margin-top: 12px;
font-size: 0;
}
div#page-btns-holder * {
font-size: 14px;
}
.page-btn {
background: #19FF19;
color: #FFF;
border: 0;
border: 3px solid transparent;
display: inline-block;
vertical-align: top;
font-size: 14px;
}
Define your btn display inline-block and remove space to inline-block element define your patent font-size:0; and child define font-size:14px; as like this i give you example
Remove Whitespace Between Inline-Block Elements
Try to make the font-size of the parent content 0, also try setting letter-spacing to 0.
I have made my own custom little blog and well, I realized it was ignoring whitespace within code tags. Well, the generated code is like
<div class="codebody">
Mycode<br/>
other indented code<br/>
othercode<br/>
</div>
my codebody class looks like
.codebody {
background-color: #FFFFFF;
font-family: Courier new, courier, mono;
font-size: 12px;
color: #006600;
border: 1px solid #BFBFBF;
}
Well, how can I make it so that indentation will show up in code tags, but it won't add double-line breaks because of the <br/>\n?
Well just figured out something.. I'm not sure that it works in all browsers, as it is a pretty nasty hack, but this is what I did
.codebody {
white-space: pre;
background-color: #FFFFFF;
font-family: Courier new, courier, mono;
font-size: 12px;
color: #006600;
border: 1px solid #BFBFBF;
}
.codebody br{
display: none;
}