How to create space before or at the end of the text contained between html tags?

Asked

Viewed 29,816 times

14

I need to put a space before a text that is found between tags html, what is the right way to do ? No css...

div{float:left;}
<div>Texto 1</div><div>       texto que quero um espaço antes</div>

6 answers

14


The problem of &nbsp; is that it adjusts the spacing by font size (font-size), so to adjust the layout is very difficult, what you can do is to adjust margin or padding:

.foo {
    float:left;
}
.bar {
    padding-left: 100px;
}
<div class="foo">Texto 1</div>
<div class="foo bar">texto que quero um espaço antes</div>

what is the right way to do it? No css...

It would use CSS simply to adjust, there is no correct way, after all it can achieve the effect in several ways and if dispensing CSS in HTML is almost the same as wanting to ride with a car using only the wheels and chassis, but all discovered.

In the basic HTML &nbsp; and &emsp; there is a tag that can help achieve the effect, would be the <pre>, for example:

<pre>
foo bar                       foobar baz
</pre>

Note: Every browser engine already injects a CSS technically into HTML, which gives the default format to things, sometimes called user-agent-stylesheet


When to use entities?

There are entities for various things, for example:

  • Accentuation
  • Spacing
  • Hard to write characters via keyboard

Understand that entities came before CSS was so functional, before the most we had was font-size and color in CSS, so we depended on things like:

  • Many spacings as &nbsp;
  • Many line breaks with <br>
  • Tables to make menus

The result of this were usually somewhat confusing Htmls.

Many of these entities can still be used, but today they are perfectly replaced by more practical things within the CSS itself, in cases of accents or iso-8859-1 or UTF-8 well configured will have no problems.

Some difficult characters are replaced by font icons (Woff, ttf, etc), or if you use UTF8 on your page you have many emojis that solve, of course many things can still be done with entities which is much simpler.

For example, a short space with 3 &nbsp; already resolve, if you have more maybe it is better to have an html element, so you can control by width:

.space {
    display: inline-block;
    width: 100px;
}

.signature {
    display: inline-block;
    width: 100px;
    border-bottom: 1px #000 dotted;
}
<p>
Bla bla bla <span class="space"></span> Bla bla bla
Bla bla bla <span class="space"></span> Bla bla bla
Bla bla bla <span class="space"></span> Bla bla bla
</p>

<p>
Bla bla bla <span class="signature"></span> Bla bla bla
Bla bla bla <span class="signature"></span> Bla bla bla
Bla bla bla <span class="signature"></span> Bla bla bla
</p>

If it’s the tag <title> and need spaces can simply use the &nbsp;:

<title>Carro&nbsp;&nbsp;&nbsp;Barco</title>

Of course you have to be aware that for SEO making descriptive and reduced titles is critical, so it will hardly use entities in titles, unless it’s something specific.

  • right, but in which cases the use of entities for html space, there is this exception ?

  • @Magichat entities see before CSS is so functional, it comes from an era where what we had best was the <br> and the <p> and even tables to make the columns of the layout, today it is dispensable, except when will write a text, although an element with display: inline-block; width: <medida da separação>; can solve much better, and without getting stuck the measure of the source.

  • I understand, but I still wonder if there’s an exception...

  • @Magichat ready, edited again. It was with some errors that it was impossible to understand the last code.

6

I don’t know if there’s anything right about this case, but you can use it &nbsp;

Rendering a blank space.

div{float:left;}
<div>Texto 1</div><div>&nbsp;texto que quero um espaço antes</div>

Or else &emsp; that renders a tab.

div{float:left;}
<div>Texto 1</div><div>&emsp;texto que quero um espaço antes</div>

6

Use white-space: pre:

div {
  float:left;
  white-space: pre;
}
<div>Texto 1</div><div>       texto que quero um espaço antes</div>

According to the W3school:

pre: Whitespace is preserved by the browser. Text will only wrap on line breaks. Acts like the <pre> tag in HTML

That is to say:

pre: Blank spaces are preserved by the browser. The text will only be broken in line breaks. It works equal to the tag <pre>

  • 1

    Well remembered the white-space, +1.

  • It has the other attributes, but I don’t think you should quote them: pre-wrap, pre-line and the like...

  • 1

    Undoubtedly the white-space is the ideal solution to the question, saving a lot with visual code.

5

You can use the text-indent.

.indent {
  text-indent: 50px;
  display: inline-block;
}
<div>Texto 1 <span class="indent">texto que quero um espaço antes</span></div>

5

Óras, use margin for that reason:

div { float: left }

div:nth-child(1) {
  margin-right: 6%
}
<div>Texto</div><div>texto que quero um espaço antes</div>

3

And &emsp; to insert a tab space.

div{float:left;}
<div>Texto 1</div><div>&emsp;texto que quero um espaço antes</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.