Asked
Modified 7 months ago
Viewed 42k times
I have a HTML document with some distinct rows of text, is there some decided correct way to display them?
Example:
Here are some lines of text
Should I use the <p>
tag for each row, or is there some other/better way to do it?
Examples:
<p>Here are</p> <p>some lines</p> <p>of text</p>
or
<p> Here are <br> some lines <br> of text <br> </p>
Or something completely different?
The CSS & other things isn't really relevant at the moment, I'm just wondering which is the "most correct" way to use.
2
if you have a string with new lines that you want to display for example in a div, you can use white-space: pre-wrap
css style:
. multiline { white-space: pre-wrap; } <div> A multiline text for demo purpose </div>
1
Or you can try this without tag wrapping each line:
<div> Here are some lines of text </div>
1
The correct way to do things is using things made for the things you need. If you want a line break (enter), use <br>
; If you want to define a paragraph, use <p>
.
According to this, the <br>
element is used to insert a line break without starting a new paragraph. Hence you should prefer the second solution over the first.
w3schools comes with a marvelous article about style guides and coding conventions.
0
The spec makes it very clear that <br>
should never be used unless the line breaks are actually part of the content forming a single unit of text.
As these are distinct rows of text, not a single unit that happens to contain line breaks, they need to be split into separate <p>
elements.
There is no such thing in most correct way, at least according to the current specification of your needs. Yes, you can put them all in separate paragraphs, using the <p></p>
tag or you can separate them via a <br>
tag at every line. You can also use span
s combined with the white-space CSS attribute, so you have a lot of options. To choose the best option for you, you will need to try them out and see what fits your requirements the best.
If you want to create a multiline paragraph that maintains the line seperation in your code without throwing
s everywhere. Simply use the html tag.
1
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Required, but never shown
Required, but never shown
❮ Previous Next ❯
Reserved characters in HTML must be replaced with character entities.
Some characters are reserved in HTML.
If you use the less than (<) or greater than (>) signs in your text, the browser might mix them with tags.
Character entities are used to display reserved characters in HTML.
A character entity looks like this:
&entity_name;
OR
&#entity_number;
To display a less than sign (<) we must write: < or <
Advantage of using an entity name: An entity name is easy to remember.
Disadvantage of using an entity name: Browsers may not support all entity names, but the support for entity numbers is good.
A commonly used entity in HTML is the non-breaking space:
A non-breaking space is a space that will not break into a new line.
Two words separated by a non-breaking space will stick together (not break into a new line). This is handy when breaking the words might be disruptive.
Examples:
Another common use of the non-breaking space is to prevent browsers from truncating spaces in HTML pages.
If you write 10 spaces in your text, the browser will remove 9 of them. To add real spaces to your text, you can use the character entity.
Tip: The non-breaking hyphen (‑) is used to define a hyphen character (‑) that does not break into a new line.
Result | Description | Entity Name | Entity Number | Try it |
---|---|---|---|---|
non-breaking space | |   | Try it » | |
< | less than | < | < | Try it » |
> | greater than | > | > | Try it » |
& | ampersand | & | & | Try it » |
" | double quotation mark | " | " | Try it » |
' | single quotation mark (apostrophe) | ' | ' | Try it » |
¢ | cent | ¢ | ¢ | Try it » |
£ | pound | £ | £ | Try it » |
¥ | yen | ¥ | ¥ | Try it » |
€ | euro | € | € | Try it » |
© | copyright | © | © | Try it » |
® | registered trademark | ® | ® | Try it » |
Note: Entity names are case sensitive.
A diacritical mark is a "glyph" added to a letter.
Some diacritical marks, like grave ( ̀) and acute ( ́) are called accents.
Diacritical marks can appear both above and below a letter, inside a letter, and between two letters.
Diacritical marks can be used in combination with alphanumeric characters to produce a character that is not present in the character set (encoding) used in the page.
Here are some examples:
Mark | Character | Construct | Result | Try it |
---|---|---|---|---|
̀ | a | à | à | Try it » |
́ | a | á | á | Try it » |
̂ | a | â | â | Try it » |
̃ | a | ã | ã | Try it » |
̀ | O | Ò | Ò | Try it » |
́ | O | Ó | Ó | Try it » |
̂ | O | Ô | Ô | Try it » |
̃ | O | Õ | Õ | Try it » |
You will see more HTML symbols in the next chapter of this tutorial.
❮ Previous Next ❯
NEW
We just launched
W3Schools videos
Explore now
Play Game
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 1999-2022 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.
Tags: Web Page Front End Foundation html css
Use
tag, .
Note: All "<", ">" in the code is replaced by yet and && gt, if not replaced, does not recognize the HTML language, resulting in executing this code instead of displaying the code. If there are other special characters in the code, there is also a replacement. If you don't pre-add, all code completions will be considered a space that will wrap according to the size of the browser, and will not go from the side of the wrap. Here is the code
Results as below:
Results as below:
Note: I've seen the XMP tag online, but HTML5 doesn't support this tag in the HTML Manual.
Copy the code directly.
Here is the code
The default pointer under an arc is a strong reference: __ SICK & __ weak and __ unsafe comparison reference: __strong & __weak & __ Unsafe_unreted...
Using multiple GPUs in Pytorch requires the declared model to be initialized after the Declarative Model, such as: Then, after running the Python file The Model Training, all GPUs. ..
In the CEC Prisoners Group "The German Continental Group demonstrates its latest logistics robots" - the Anymal package delivery dog. For this, the result of the Circle of Media and Technology...
Association analysis is the task of finding interesting relationships in large-scale datasets. These relationships can take two forms: Frequent Item Sets: A set of items that appear frequently...
Spring_day04 (integration of three main frameworks) 1. Three frameworks (integration principle) The backend of a web project is divided into three layers. 2. Guide package (42) hibernate: hibernate/lib/required hiberna...
The unittest framework can be used not only for unit testing, but also for developing and executing web-based automated test cases. A test framework can ...
// Main interface layout // Nested layout // Main function MainActivity // Create a new bean class // Create an adapter // Tools...
Preparing the domain name and Alibaba Cloud server domain name: You can purchase the required domain names from major cloud service providers. I purchased an Alibaba Cloud domain name. To the cloud...
Figure configuration method, please refer to Article 1, experimental environment: Physics host host two virtual machines. Host Physical IP: 192.168.9.6/24 GW: 192.168.9.254 DNS: 8.8.8.8 Virtual...
Apollo Config Center client call introduction Config Center Create project Publish namespace Create local cache configuration file springboot project code Previous post: . ..
In this article, we'll cover the HTML basics you need to get started. Let's define "elements", "attributes", "tags" and other important concepts that you may have heard about, as well as their role in the language. We'll also show how HTML elements work, a typical HTML page, and explain other important aspects of the language. Along the way, so you don't get bored, we'll play around with a real HTML page!
Required knowledge: | Ability to work with a computer, availability of the necessary software, basic knowledge of working with files. |
---|---|
Purpose: | Get to know the HTML language and learn how to describe some of its elements. |
HTML (HyperText Markup Language) is not a programming language; it is a markup language used to define the structure of web pages visited by users. They can have a complex or simple structure, it all depends on the intention and desire of the web developer. HTML is made up of a number of elements that you use to span, wrap, or mark up different pieces of content so that it looks a certain way or works in a certain way. Inline tags can turn a piece of content into a hyperlink that can take you to another web page, italicize words, and so on. For example, consider the following string:
My cat is very angry
If we want the string to appear like this, we can define it as a "paragraph" by enclosing it in the tags of the "paragraph" element (
), for example:
My cat is very angry
Note : HTML tags are case insensitive, meaning they can be written in upper or lower case. For example, the tag
could be written as
,
,
,
, etc., and it will work just fine. The best practice, however, is to write all tags in lowercase for consistency, readability, and other reasons.
Let's look at the "paragraph" element in a little more detail:
The main parts of the element are:
Edit the line of text below in the Input field, wrapping it with the
and
tags (insert
before the line to make specify the beginning of element , and
after it to indicate the end of the element ) - these actions should italicize the line of text! You can see real time changes in field Output .
If you make a mistake, you can always start again using the button Reset . If the exercise is difficult for you, then click the button Show solution to see the correct answer.
You can also nest elements inside other elements - this is called nesting . If we want to emphasize that our cat is very angry, we can enclose the word "very" in a
element, which means that this word is extremely important in this context:
My cat is very angry.
You have to make sure the elements are nested properly: in the following example we open element p
first, then element strong
, then we close element strong
first, then p
. The following is incorrect:
My cat is very angry.
Elements must open and close correctly so that they are clearly inside or outside of each other. If they overlap as in the example above, then your browser will try to "think" for you what you meant, and you will get unpredictable results. So don't do it!
There are two important categories of elements in HTML that you should be aware of - block-level elements and inline elements.
(link) element, or accent elements like
or
. Look at the following example:
FirstSecondThirdfourth
fifth
sixth
is an inline element, so as you can see here, the first three elements are on the same line as each other with no spaces between them. On the other hand,
is a block-level element, so each element is on a new line, with space above and below each (this spacing is determined by the default CSS styling that browsers apply to paragraphs).
Note : HTML5 redefined element categories in HTML: see Element Content Type Categories. Although these definitions are more precise and unambiguous than those that came before, they are much more difficult to understand than "block" and "inline", so we will stick to them in this section.
Note : Do not confuse the terms "block" and "inline" used in this section with CSS display types of the same name. Although they are correlated by default, changing the display type in CSS does not change the element's category, nor does it affect what it can be nested in and what can be nested in it. This rather common confusion is one of the reasons why HTML5 has abandoned these terms.
Note : You may find references that include lists of block and inline elements to be useful - see Block-Level Elements and Inline Elements.
Not all elements match the above pattern: start tag, content, end tag. Some elements consist of a single tag and are usually used to insert something in the document where they are placed. For example, the element
inserts an image on the page in the exact place where it is located:
This will output the following to your page:
Note : Empty elements are sometimes called void elements .
Elements can also have attributes that look like this:
Attributes contain additional information about the element that you don't want to appear in the element's content. In this case, the class
attribute allows you to give the element an identifying name that can later be used to refer to the element with style information and other things.
The attribute must have:
Take element
as an example — stands for anchor and makes the text inside it a hyperlink. Can have multiple attributes, here are a few:
href
: The value of this attribute is set to the web address you want the link to point to when you click on it. For example, href="https://www.mozilla.org/"
. title
: The title
attribute describes additional information about the link, such as which page it leads to. For example, title="The Mozilla homepage"
. It will appear as a tooltip when you hover over the link. target
: The target
attribute specifies the browsing context that will be used to display the link. For example, target="_blank"
will display the link in a new tab. If you want to display the link in the current tab, simply omit this attribute. Change the line of text below in field Enter to point to your favorite website. First add the element
then the attribute href
and attribute title
. Finally, set the target attribute to
to open the link in a new tab. You can see the changes made in real time in field Output . You should see a hyperlink that, when hovered over, displays the contents of the title
attribute, and when clicked, navigates to the address in the href
attribute. Remember that there must be a space between the element name and each of the attributes.
If you make a mistake, you can always start again using button Reset . If the exercise is difficult for you, then click the button Show solution to see the correct answer.
Sometimes you will see attributes written without a value - this is perfectly acceptable. Such attributes are called booleans, and they can only have one value, which is basically the same as its name. As an example, let's take the attribute disabled
, which can be assigned to render input elements if you want them to be disabled (inactive) so that the user cannot enter any data into them.
For the sake of brevity, it's perfectly acceptable to write them like this (we've also placed an undisabled input element for reference to give you a better idea of what's going on):
Both outputs will look like this:
As you browse the web, you will encounter various unfamiliar ways of writing markup, including writing attribute values without quotes. This is valid under certain conditions, but will break your markup under others. For example, going back to our hyperlink exercise, we could write the main variant with only the attribute href
like this:
mozilla.org/>favorite website
However, as soon as we add the title attribute
in the same style, we will do wrong :
favorite website
At this point, the browser will misinterpret your markup, thinking that the title attribute
is on actually three different attributes - a title attribute with a value of "The" and two boolean attributes: Mozilla
and homepage
. This is obviously not what was meant and will result in an error or unexpected code behavior, as shown in the live example below. Try hovering over the link to see what the title text looks like!
Our advice: always use quotes in attributes - this will avoid such problems and, therefore, the code will be more readable.
In this article, you will notice that all attributes are enclosed in double quotes. However, you may see single quotes in other people's HTML documents. This is purely a matter of taste and you are free to choose which ones you prefer. Both of the following lines are equivalent:
Link to my example. Link to my example.
However, you need to make sure you don't mix them together. The following will be wrong!
A link to my example.
If you want to insert quotes of the same type, then you must use HTML entities. For example, this won't work:
A link to my example.
So you need do this:
A link to my example.
Below is an example of wrapping the main , standalone HTML elements that aren't very useful on their own. Let's see how the individual elements are combined to form the entire HTML page:0003
Test page This is my page
This is what we have:
: Document type declaration. A long time ago, back when HTML was young (1991/2), document types were used as references to a set of rules that an HTML page had to follow in order to be considered good, which could mean automatic error checking and other useful things. The document type declaration looked something like this: However, no one thinks much about them these days, and document types have become a historical artifact that must be included everywhere for everything to work properly.
is the shortest kind of document type that is considered valid. In fact, that's all you need to know about document types.
: The element
contains all the content on the entire page, and is sometimes referred to as the "root element".
: Element
. This element acts as a container for all the content that you want to include in your HTML document but don't want to be shown to visitors to your page. It includes things like the keywords and page description you'd like to show up in search queries, CSS to style your content, declaration of the supported character set, and more. You will learn more about this in the next article of this guide.
: This element sets the character encoding for your document to utf-8 , which includes most characters from all languages known to mankind. Essentially, the page will now be able to display any textual content that you can attach to it. There is no reason not to set this encoding, it will also avoid some problems later.
: Item
. This element sets the title of your page that appears in the browser tab that loads this page, and this title is also used to describe the page when you bookmark or favorite it.
: Element
. It contains all the content that you want to show to your page visitors - text, images, videos, games, audio tracks to play, or whatever. If you want to experiment with writing HTML on your computer, you can:
index.html
. Note : You can also find this basic HTML example on the MDN Learning Area Github repo.
Now you can open the browser and see what the code was rendered into, and then change it, refresh the page and see what happened. The page first looks like this:
For this exercise, you can edit the code locally on your computer as suggested above, or you can work in the editor below. The editor only shows the content of the
element. Try this:
. The text must be between the opening tag
and the closing tag
.
and an closing tag
If you get confused, you can always run the example from the beginning with the button Reset . Give up - see the answer by clicking on Show solution .
You may have noticed that the code examples in this article have a lot of spaces. This is not necessary at all - the following two examples are equivalent:
Dogs are stupid.
Dogs stupid.
No matter how much white space you use in your markup (which can include spaces and line breaks): the browser will parse all white space to a single space. Why use lots of spaces? The answer is: it's easy to understand - it's much easier to figure out what's going on in your code if it's conveniently formatted, and not just put together in one big mess. In our code, each nested element is indented by two spaces relative to the element in which it resides. You can use any formatting (in particular, the number of spaces to indent), but it's best to stick to one style.
In HTML, the characters <
, >
, "
, '
and &
are special characters. They are part of the HTML syntax itself. So how do you include one of these special characters in your text? For example, if you want to use an ampersand or less-than sign and not interpret it as a code
We must use mnemonic links - special codes that display special characters and can be used in the required positions.Each mnemonic link starts with an ampersand (&) and ends with a semicolon (;).0003
Letter character | Character equivalent |
---|---|
< | < |
> | > |
" | " |
' | ' |
& | & |
In the following example, you see two paragraphs that talk about web technologies:
In HTML, you define a paragraph with a
element.
In HTML, you define a paragraph with a <p">> element.
In the live output below, you may notice that the first paragraph is rendered incorrectly because the browser thinks the second element is
Note : A table of all available HTML entity references - Wikipedia: List of XML and HTML character entity references.
In HTML, as in most programming languages, it is possible to write comments in the code. Comments are ignored by the browser and not visible to the user, they are added to explain how the written code works, what certain parts of it do, etc. This practice is useful if you return to code that you have not seen for a long time or when you want to transfer it to someone else.
To turn part of the content of an HTML file into a comment, you need to put it in special markers
eg:
I'm not in the comments(
As you will see below, the first paragraph will be displayed on the screen, but the second one will not.