React removes spacing between elements - html

Here is a fiddle to demonstrate:
html
<div>
<button>One</button>
<button>Two</button>
<button>Three</button>
</div>
<div id="app"></div>
js
function Buttons() {
return (
<div>
<button>One</button>
<button>Two</button>
<button>Three</button>
</div>
);
}
ReactDOM.render(<Buttons />, document.querySelector("#app"))
Basically whether or not I have any stylesheets, putting elements next to each other causes them to have 9px of space between them (at least on Chrome) but doing the exact same thing inside a react component puts no space between them. I would like to know why they are not consistent and how to make them consistent if possible.

The reason for this difference is that React removes whitespace at the beginning and ending of a line.
The space you see between the buttons in your html example is due to the whitespace characters between the <button> tags in your html code.
If you change your React code to the following, you will get a result similar to your html example:
function Buttons() {
return (
<div>
<button>One</button> <button>Two</button> <button>Three</button>
</div>
);
}
ReactDOM.render(<Buttons />, document.querySelector("#app"))
My advice would be to let React remove the whitespace between the elements, and use css to properly control the spacing.

JSX removes whitespace at the beginning and ending of a line. It also removes blank lines. New lines adjacent to tags are removed; new lines that occur in the middle of string literals are condensed into a single space.
So the following declaration in html
<button>1</button>
<button>2</button>
Actually takes in consideration the line break.
The same declaration in jsx is the equivalent of writing like this in html
<button>1</button><button>2</button>

Related

Im trying to change the html code of a website

Im trying to change the html code of a website. As you can see here: https://share-your-photo.com/b12b204e8c
The Code starts with an h3 tag. i want to replace it with a p tag. at beginning with <p class=and at the end of the code with </p>. But the code turns red at the end. can you give me a solution how i can do?
So I see that you had the <h3></h3> wrapped around everything and that is not the proper syntax in html. Its called hierarchy. Hierarchy is
An element that is directly above another element in the hierarchy is
called the parent of the element below it. The element below the
parent is called the child. When two elements are equal in the
hierarchy, they are known as siblings.
--Thx Google... A <h3></h3> tag can't wrap all of those elements only tags like a <div> can or a <span>. A <div> tag is a block element. So if you have 2 <div> tags like this... <div>HI</div> and <div>Hello</div> "HI" will be on top and "Hello" will be on the bottom but if it were <span> It will make it horizontal <span>Hi</span> and <span>Hello</span> the output will be "Hi Hello". If you check out this link they briefly explain nesting. But there are many many places you can go to understand this [https://www.w3schools.com/html/html_elements.asp1 Hopefully that helps you! Keep on coding! 👍
You said: "_the code turns red at the end...". The end of what??? Are you trying to edit the code locally in an editor such as sublime, vscode?
Basically, think of an Html document as a Word document that contains titles, subtitles, etc..
In Html documents, we have the following hierarchy structure.
<h1>My Page Title</h1>
<h2>My Page Subtitle</h2>
<p>here we can have only text, <strong>bold text</strong>, <i>italic</i>, and other styled text with <span>spanned texts</span></p>
<div>
<h3>Layouts</h3>
<p>you can use divs to structure your layout, so, doesn't make sense to have divs inside paragraphs.</p>
<p>If you want, you can break lines with <br> tags</p>
</div>

Problem rendering code blocks with Jekyll in .github.io site

The problem
My post containing some code blocks renders them like "box in a box".
This is the post: https://telatin.github.io/N50-with-conda/ and here what I see from Firefox: http://prntscr.com/oujb6u
The code
This is the source code: https://raw.githubusercontent.com/telatin/telatin.github.io/master/_posts/2019-08-19-N50-with-conda.md
I'm new in using Jekyll, and I tried both triple backticks (as I usually do in GitHub, and the four-spaces at the begin of the line (current implementation). Both renders the same.
Question
I don't understand if the problem is in the code I write or in the template I'm using. What should I do to have a nicer output (single framed)?
If you inspect the generated HTML, you'll see that there are nested blocks with the same class .highlight:
<div class="highlighter-rouge">
<div class="highlight">
<pre class="highlight">
<code>CHECK=$(n50 contigs.fasta)</code>
</pre>
</div>
</div>
What you can do is alter your CSS to be more specific:
i.e. instead of .highlight, use div.highlight for base code-block styles and use
div.highlight > pre.highlight to style the pre block elements if required.

Keep line breaks in HTML string

Using Angular 5 and Firebase, I am storing and retrieving movie review information. When creating and editing the review, line breaks are kept (I assume using an ngModel has something to do with this). However, when retrieving the review from a reader's perspective, the ReviewComponent, the line breaks are not kept. Logging the body still shows the line breaks and using Angular's json pipe shows as text\n\ntext showing where the line breaks should be.
Here is my HTML:
<main role="main" class="container">
<p style="margin-bottom: 2rem;">{{review.body}}</p>
</main>
I have also tried span and body.
How can I keep these line breaks in the review for the reader?
HTML, in general, uses br tags to denote a new line. A plain textarea tag does not use this, it uses whatever the user's system uses to denote a new line. This can vary by operating system.
Your simplest solution is to use CSS
<main role="main" class="container">
<p style="margin-bottom: 2rem;white-space:pre-wrap;">{{review.body}}</p>
</main>
This will maintain any "white space" formatting, including additional spaces.
If you want to actually replace the newline characters with br tags you can use the following regex
<main role="main" class="container">
<p style="margin-bottom: 2rem;" [innerHTML]="review.body.replace(/(?:\r\n|\r|\n)/g, '<br>')"></p>
</main>
Edit Thanks to ConnorsFan for the heads up on replace not working with interpolation.
by using the [innerText] directive the white spaces seem to be maintained, as well as the \n new lines
e.g. <small style="display:block" [innerText]="review.body"></small>
I had a similar situation where I had HTML code like this:
<div>{{msgTitle}}</div>
And I tried putting both '\r\n' and <br> in the string msgTitle, but neither of them worked to put a newline in the text displayed on the page. This link gave me the solution:
https://github.com/angular/angular/issues/7781
Use [innerHTML]="msgTitle" instead of {{msgTitle}}.
try span but set it like that:
span
{
display : table;
}
it should helps.

Replace text in a span that also contains other DOM elements

I have a code like this and I need to remove the ​ characters that are inserted automatically by the code that I have no control over (generated by SharePoint). They ruin the layout by inserting extra empty lines:
<div id="ctl00_PlaceHolderMain_ctl01__ControlWrapper_RichHtmlField" class="ms-rtestate-field" style="display:inline" aria-labelledby="ctl00_PlaceHolderMain_ctl01_label">
​​​
<span> ​
<div class="cg-division-intro-outer">
<div class="cg-division-intro-inner">
<div class="cg-division-intro-header">
<h1>Division Intro</h1>
</div>
... etc
Notice that the ​ entities are inserted as bare text, not wrapped into any element, so I cannot target them directly.
Here is what I tried:
Using visibility: hidden on the element containing the garbage and visibility: visible on my code. Has no effect.
Reducing font-size on the parent element containing the garbage to 0px and restoring the font-size on other elements. Has no effect.
Obtaining the innerHTML of the parent element, doing the .replace() and reinserting HTML back into the page - but then all the nodes will be lost/recreated, which means any attached listeners may be lost.
Tried using :not but didn't come up with a solution that works.
Here is the white bar created by those ​s:
DEMO
JS
use childNodes and change its value by nodeValue
var d = document.getElementById('div1').childNodes[0];
d.nodeValue = "new text"; // change value
// if you want to remove the element
d.parentElement.removeChild(d)
HTML
<div id="div1">
some texts
<div id="div2">
other elements
<div>hkeqvdkqbdklq</div>
</div>
</div>

Highlighting sections of HTML/code that is wrapped in <pre><code> tags

I am creating an online tutorial and I'm displaying the html code by wrapping it in the <pre><code> tags. I would like to be able to highlight certain sections of the code. Is there a way to do that when the html is wrapped in the the <pre><code> tags?
<div data-role="page">
<h1>Hello</h1>
</div>
I would like to be able to highlight the "page" value of the data-role attribute. I tried to surround the "page" code with a span tag and style it, but the span tag showed up in the code. I also tried to use < and $gt; thinking maybe that would escape the < > around the span tags. It did, but it showed up in the code.
Again, I'm trying to display the code (without screenshots) with certain sections of the code highlighted with yellow.
You have to escape everything but the tag. I used <mark> since it seems more semantically correct:
<pre><code><div <mark>data-role="page"</mark>>
<h1>Hello</h1>
</div></code></pre>
Example http://jsfiddle.net/MFzsS/1/