When to use <p> vs. <br> [closed] - html

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
What's the verdict on when you should use another <p>...</p> instead of a <br /> tag? What's are the best practices for this kind of thing?
I looked around for this question, but most of the writing seems a bit old, so I'm not sure whether opinions about this topic have evolved since.
EDIT: to clarify the question, here is the situation that I am dealing with. Would you use <br>'s or <p>'s for the following content (imagine it's contained in a div on a homepage):
Welcome to the home page.
Check out our stuff.
You really should.
Now, these lines are not technically 'paragraphs'. So would you mark this up by surrounding the whole block in a <p> tag with <br>'s in between, or instead use separate <p>'s for each line?

They serve two different functions.
<p> (paragraph) is a block element which is used to hold text. <br /> is used to force a line break within the <p> element.
Example
<p>Suspendisse sodales odio in felis sagittis hendrerit. Donec tempus laoreet
est bibendum sollicitudin. Etiam tristique convallis<br />rutrum. Phasellus
id mi neque. Vivamus gravida aliquam condimentum.</p>
Result
Suspendisse sodales odio in felis sagittis hendrerit. Donec tempus laoreet
est bibendum sollicitudin. Etiam tristique convallisrutrum. Phasellus
id mi neque. Vivamus gravida aliquam condimentum.
Update
Based on the new requirements, I would personally use <p> to achieve the spacing as well as allow for styling. If in the future you wish to style one of these parts, it will be much easier to do so at the block level.
<p>Welcome to the home page.</p>
<p style="border: 1px solid #00ff00;">Check out our stuff.</p>
<p>You really should.</p>

You want to use the <p> tag when you need to break up two streams of information into separate thoughts.
<p> Now is the time for all good men to come to the aid of their country. </p>
<p>The quick brown fox jumped over the lazy sleeping dog.</p>
The <br/> tag is used as a forced line break within the text flow of the web page. Use it when you want the text to continue on the next line, such as with poetry.
<p> Now is the time for all good men to come to the aid of their country. <br/>
The quick brown fox jumped over the lazy sleeping dog. </p>
source

You should use <p> when you want to separate two paragraphs. From Wikipedia:
A paragraph (from the Greek paragraphos, "to write beside" or "written
beside") is a self-contained unit of a discourse in writing dealing
with a particular point or idea.
Use the <br> tag when you want to force a new line inside your paragraphs.

I may be breaking rules of etiquette when answering a question in this thread —as most of the answers thus far have been great —but here's how I approach this argument:
While I agree that a 'p' is element which is used to hold text and a 'br' tag is used to break text, in many occasions one may want to manipulate a 'block' of text that have actual line spaces between them;
Ex:
[my block of text]
Hello World!
--------this is a blank line------
You are so full of life!
[/my block of text]
Now many would argue that you can just place two 'p' elements in a 'div' and then manipulate that 'div', however I feel this is more time consuming (aka I'm lazy), and thus I stick with a double 'br' method —leaving me to manipulate just that one 'p.'
The issue with my unorthodox method is that if there is a specific styling applied to line-height, then there will definitely be a noticeable difference between the line-space created by a double 'br' vs. a 'p'.
I guess in when deciding which technique to follow, consider:
code readability
best practices
time
effects of shortcuts on the rest of your code

I wouldn't use <p> unless you intend to put content within it. Of course, this is merely an opinion (as I assume most answers will be).
However, it may be easier to style a <p> tag (there are more options at least) which may lend itself to be used more often. My only problem is that this is not very easy to read (when reading code) and I feel like those styles could be applied elsewhere (possibly to the tags that would surround the <p> tag.
Also, as other have stated, a <br /> tag should be used to separate content within a block element (such as the <p> tag). If you are using the following:
<p class="a">some content...</p>
<p class="break"></p>
<p class="b">some other content...</p>
Then I would argue that you should use styles on one or both of the tags surrounding the p.break tag to do any spacing that is required.

The p tag is a block level element, and is mainly used to add content, whereas the br tag is used to force a line-break within the element. That is, if you're going to follow semantics. A lot of people are doing that these days to add specific meaning to tags and whatnot. I hope that helps. Good luck, though. =)

Related

Should i use <header> or <main> for first page critical content? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm making a website using HTML5 and don't know which semantic element to use for the first page content the user sees first (full viewport height and width). I've read that the <header> element should be used for introductory stuff like website title and navigation. I don't even have that in my site, instead I have something like this, basically the first part of the website where you can see what it is about and links to some contact stuff.
It is a single page site, but if I had more pages, they would not have that content and I would probably make a navbar and put it in a <header>. With <main> I'd like to cover more stuff than only this.
Maybe an even different element? I don't know, it's like the combination of a <header>, a <section> and a <main>.
What do you think would be best?
Edit: This is not the entire page I have shown on the image. It is just the part that shows up first when you open the website, there is also content under it. I wanted to know what element to wrap over the thing in the image.
Here is the code (deleted classes and changed text to dummy text):
<header>
<address>
123 456 789
</address>
<div>
<h1>Heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nibh nibh, egestas eget justo maximus, consectetur ultricies ante. Quisque facilisis pellentesque sagittis. Donec iaculis tellus nisl, vitae sodales ipsum ornare id. Suspendisse potenti. Curabitur eu ornare quam. Fusce laoreet vitae quam at ornare. Etiam quis malesuada magna.</p>
Button
</div>
</header>
Is this OK?
(the phone icon on the bottom right has position: fixed and is outside the header)
This is honestly a matter of opinion.
Personally I wouldn't bother at all with sectional elements on such a small site, since it really just adds noise. It won't be helping, say, a blind person or a search engine crawler to understand better, because there's just not that much to understand.
However I admire your effort. I would put "heading" in header, the phone stuff in "footer" (even though it's at the top) and the rest in main.
Edit: Considering that this is just the top of your page, I believe considering it the header is reasonable. The code you posted is certainly valid html.

Curious about proper html structure for paragraphs and various text

So I've been working on my website for the past few weeks having started off with no understanding of HTML/CSS or JS. In two weeks i managed to learn enough that now I am actually custom coding websites for family and friends who's websites are horribly outdated and look like something from the dial up era.
With regards to my own website...
(which right now I am using a online site editor to speed up the process as I don't have much time to spare for now, I fully plan to rebuild it from scratch when I have the time but just needed something working for now.)
I have been doing a large amount of custom coding to make my site work and was curious about what the structure I should be using for my paragraph and text sections should be. Both for "in-editor" and for "from-scratch-development".
While I 'could' just use the editor's built in paragraph tool, I prefer to use the HTML element to get exactly the style I desire. I was wondering on what format I should use to create my paragraph layout as I am stuck on two possible formats to use. I would like to know which I should focus on to give better quality HTML format and to avoid bad habits.
I know that often the <h#> tags should be used for titles and important text and the <p> tags should be used for anything inside a large paragraph, but because of the default formatting of the editor & that of the browsers, I wanted to know if there is anything wrong with using the "< div >" tag option or if its improper somehow and I should use the "< p >" tag option.
< div class="some-header" >
<br>
Some Page Header
<br>
< /div >
<br>
<br>
< div class="some-paragraph" >
<br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Ut dignissim, tortor id fringilla tincidunt, mauris.
<br>
< /div >
_____________________________________________________
< h1 class="some-header" >
<br>
Some Page Header
<br>
< /h1 >
<br>
<br>
< p class="some-paragraph" >
<br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Ut dignissim, tortor id fringilla tincidunt, mauris.
<br>
< /p >
_________
So is there anything wrong with these options? Im really not sure if it even matters. If its just simply trivial semantics and doesn't really have any real impact. Also it should be noted that I tend to stuff my HTML and CSS full of little notes for myself (and anyone else that reads it) just so I don't get lost. Pretty much every single section of code is made all "pretty" with big block comments in fancy boxes and commented titles on EVERYTHING to help me remember what everything is. Any feedback or ideas on the matter are much appreciated.
There is nothing wrong with using div elements to encase text content and then using classes and ids to manipulate them. It is good practice however, to use appropriate elements provided in HTML for specific tasks.
For example, one can make a table out of div elements, but there is no reason to do so as there is already a structure available within HTML for that task. The Document Object Model, is what defines the logical structure of your page and it is good practice to keep the logical structure, logical.
I would suggest using the latter option with the p tags for your paragraph content.
I would also advice against using br tags in your code. You can separate your divs using margins instead. If you require one element to be spaced out a little more then all the other elements of the same class use an id.
Cheers!

HTML poetry tags [duplicate]

What to use for poem?
pre
blockquote
code
something else?
Don't use code (unless computer code is part of the poem). Don't use blockquote (unless you quote a poem).
white space / line breaks: pre or br
You may use the pre element. The spec gives an (informative) example:
The following shows a contemporary poem that uses the pre element to preserve its unusual formatting, which forms an intrinsic part of the poem itself.
<pre> maxling
it is with a heart
heavy
that i admit loss of a feline
so loved
a friend lost to the
unknown
(night)
~cdr 11dec07</pre>
However, I'd only use the pre element if the poem contains "more" than just meaningful line breaks (e.g. in this example the horizontal whitespace is meaningful).
If you have a simple poem, I'd go with the br element:
br elements must be used only for line breaks that are actually part of the content, as in poems or addresses.
container: p
For most poems, the p element is the right candidate (or several p elements, of course). The spec has an (informative) example:
<p>There was once an example from Femley,<br>
Whose markup was of dubious quality.<br>
The validator complained,<br>
So the author was pained,<br>
To move the error from the markup to the rhyming.</p>
Also:
For instance, an address is also a paragraph, as is a part of a form, a byline, or a stanza in a poem.
structure: (article, figure)
Depending on the context (content, page structure, …), a sectioning element might be appropriate (article in most cases).
Also depending on the context, the figure element might be appropriate:
Here, a part of a poem is marked up using figure.
<figure>
<p>'Twas brillig, and the slithy toves<br>
Did gyre and gimble in the wabe;<br>
All mimsy were the borogoves,<br>
And the mome raths outgrabe.</p>
<figcaption><cite>Jabberwocky</cite> (first verse). Lewis Carroll, 1832-98</figcaption>
</figure>
But don't use these in general for all poems, it really depends on the page if their use is correct.
misc. & trivia
someone proposed a poetry element (→ Rejected)
someone proposed a microformat for poems
discussion in the w3.org wiki: Explicit Markup to Semantically Express Poetic Forms (thanks for the link, steveax)
see also: on the mailing list
similar question on Webmasters SE: How to mark up a poem in HTML for SEO
I've looked for the same information and, similarly, haven't found any definitive "best practices" -- so I figured I'd just have to figure out my own method. The <p> tag does make some sense as a stanza marker, with lines divided by <br>s, per the spec -- BUT I've found that that markup style doesn't provide enough flexibility.
Specifically, I wanted control over indentation. For instance, if a line runs too wide for the width of the text column, it shouldn't just break: its continuation should be indented. This hanging indent can be achieved only (as far as I know) if each line is its own block element. So in my case I've made each poetry line a <p> with a class (say, "poetry-line"). Then I can use the following CSS:
.poetry-line {
text-indent: -2em;
margin-left: 2em;
}
In another example, the poem I was posting indented every other line, with some irregularities at the ends of stanzas. I couldn't achieve this effect with just <br>s between each line. I had to create a new class for the "indented-line" and apply it manually.
I'm just sharing my experience. I guess my suggestion is that you use a block-level element for each line, for formatting purposes. This could be a <p>, or I guess you could use CSS to set a <span>'s "display" to "block". In any case, the spec's example with <br>s between lines won't work for most poetry, I think: each line needs to be its own element.
I was also looking for a good option of marking up a poem, and finally I got to make one myself; therefore I share it here with you, and I hope it might be useful to somebody.
<article itemscope itemtype="http://schema.org/CreativeWork">
<link itemprop="additionalType" href="https://dbpedia.org/ontology/Poem" />
<h2 itemprop="name">Lorem Ipsum</h2>
<h4 itemprop="author">Jane Doe</h4>
<section itemprop="hasPart" itemscope itemtype="http://schema.org/CreativeWork">
<link itemprop="additionalType" href="https://dbpedia.org/ontology/Strophe" />
<ul itemprop="text">
<li>Lorem ipsum dolor sit amet,</li>
<li>consectetur adipiscing elit,</li>
<li>sed do eiusmod tempor incididunt</li>
<li>ut labore et dolore magna aliqua.</li>
</ul>
</section>
<section itemprop="hasPart" itemscope itemtype="http://schema.org/CreativeWork">
<link itemprop="additionalType" href="https://dbpedia.org/ontology/Strophe" />
<ul itemprop="text">
<li>Ut enim ad minim veniam, consectetur</li>
<li>adipiscing elit, squis nostrud</li>
<li> exercitation ullamco laboris nisi</li>
<li>ut aliquip ex ea commodo consequat</li>
</ul>
</section>
</article>
This is a minimal markup, and you can also add complementary markup, for each verse, or for the date, the genre, or the book it was published in, etc.

How can I markup in semantically correct way additional info in header? html5?

I'm struggling with semantics, while I enjoy thinking about meaning of the parts of the websites, it is becoming complicated...
Anyhow, if I've additional info box in the header (imagine tall header, with nav and h1 + additional text explaining what website is about and how it works). What will be logical/semantic element to contain it?
<figure id="info_how">
<h2>Share what you will do for money, at the marketplace for small services!</h2>
<p>bibendum auctor, nisi elit consequat ipsum. Duis sed amet <strong>nibh vulputate cursus</strong> sit.</p>
<a id="button_start" href="#">Start selling</a>
<a id="button_how" href="#">How does it work?</a>
</figure>
I'm thinking figure, but...
According to the specs:
The figure element represents some flow content, optionally with a
caption, that is self-contained and is typically referenced as a
single unit from the main flow of the document.
The element can be used to annotate illustrations, diagrams, photos,
code listings, etc. This includes, but is not restricted to, content
referred to from the main part of the document, but that could,
without affecting the flow of the document, be moved away from that
primary content, e.g. to the side of the page, to dedicated pages, or
to an appendix.
So figure sounds good to me!
Additionally, I'd place a <figcaption> element inside of the <h2> tags:
<h2><figcaption>Share what you will do for money, at the marketplace for small services!</figcaption></h2>
Because it does represents the "caption" of the figure.

How to semantically tag poem text?

What to use for poem?
pre
blockquote
code
something else?
Don't use code (unless computer code is part of the poem). Don't use blockquote (unless you quote a poem).
white space / line breaks: pre or br
You may use the pre element. The spec gives an (informative) example:
The following shows a contemporary poem that uses the pre element to preserve its unusual formatting, which forms an intrinsic part of the poem itself.
<pre> maxling
it is with a heart
heavy
that i admit loss of a feline
so loved
a friend lost to the
unknown
(night)
~cdr 11dec07</pre>
However, I'd only use the pre element if the poem contains "more" than just meaningful line breaks (e.g. in this example the horizontal whitespace is meaningful).
If you have a simple poem, I'd go with the br element:
br elements must be used only for line breaks that are actually part of the content, as in poems or addresses.
container: p
For most poems, the p element is the right candidate (or several p elements, of course). The spec has an (informative) example:
<p>There was once an example from Femley,<br>
Whose markup was of dubious quality.<br>
The validator complained,<br>
So the author was pained,<br>
To move the error from the markup to the rhyming.</p>
Also:
For instance, an address is also a paragraph, as is a part of a form, a byline, or a stanza in a poem.
structure: (article, figure)
Depending on the context (content, page structure, …), a sectioning element might be appropriate (article in most cases).
Also depending on the context, the figure element might be appropriate:
Here, a part of a poem is marked up using figure.
<figure>
<p>'Twas brillig, and the slithy toves<br>
Did gyre and gimble in the wabe;<br>
All mimsy were the borogoves,<br>
And the mome raths outgrabe.</p>
<figcaption><cite>Jabberwocky</cite> (first verse). Lewis Carroll, 1832-98</figcaption>
</figure>
But don't use these in general for all poems, it really depends on the page if their use is correct.
misc. & trivia
someone proposed a poetry element (→ Rejected)
someone proposed a microformat for poems
discussion in the w3.org wiki: Explicit Markup to Semantically Express Poetic Forms (thanks for the link, steveax)
see also: on the mailing list
similar question on Webmasters SE: How to mark up a poem in HTML for SEO
I've looked for the same information and, similarly, haven't found any definitive "best practices" -- so I figured I'd just have to figure out my own method. The <p> tag does make some sense as a stanza marker, with lines divided by <br>s, per the spec -- BUT I've found that that markup style doesn't provide enough flexibility.
Specifically, I wanted control over indentation. For instance, if a line runs too wide for the width of the text column, it shouldn't just break: its continuation should be indented. This hanging indent can be achieved only (as far as I know) if each line is its own block element. So in my case I've made each poetry line a <p> with a class (say, "poetry-line"). Then I can use the following CSS:
.poetry-line {
text-indent: -2em;
margin-left: 2em;
}
In another example, the poem I was posting indented every other line, with some irregularities at the ends of stanzas. I couldn't achieve this effect with just <br>s between each line. I had to create a new class for the "indented-line" and apply it manually.
I'm just sharing my experience. I guess my suggestion is that you use a block-level element for each line, for formatting purposes. This could be a <p>, or I guess you could use CSS to set a <span>'s "display" to "block". In any case, the spec's example with <br>s between lines won't work for most poetry, I think: each line needs to be its own element.
I was also looking for a good option of marking up a poem, and finally I got to make one myself; therefore I share it here with you, and I hope it might be useful to somebody.
<article itemscope itemtype="http://schema.org/CreativeWork">
<link itemprop="additionalType" href="https://dbpedia.org/ontology/Poem" />
<h2 itemprop="name">Lorem Ipsum</h2>
<h4 itemprop="author">Jane Doe</h4>
<section itemprop="hasPart" itemscope itemtype="http://schema.org/CreativeWork">
<link itemprop="additionalType" href="https://dbpedia.org/ontology/Strophe" />
<ul itemprop="text">
<li>Lorem ipsum dolor sit amet,</li>
<li>consectetur adipiscing elit,</li>
<li>sed do eiusmod tempor incididunt</li>
<li>ut labore et dolore magna aliqua.</li>
</ul>
</section>
<section itemprop="hasPart" itemscope itemtype="http://schema.org/CreativeWork">
<link itemprop="additionalType" href="https://dbpedia.org/ontology/Strophe" />
<ul itemprop="text">
<li>Ut enim ad minim veniam, consectetur</li>
<li>adipiscing elit, squis nostrud</li>
<li> exercitation ullamco laboris nisi</li>
<li>ut aliquip ex ea commodo consequat</li>
</ul>
</section>
</article>
This is a minimal markup, and you can also add complementary markup, for each verse, or for the date, the genre, or the book it was published in, etc.