4

Can anyone explain what <br /> does to the page? The result is unexpected. I tested the code on the latest version of Chrome and Firefox, the result is the same on both browsers. So there must be some explanation to this. I am not trying to do a clearfix. I am just curious how it happened.

<br/> used with position:absolute;:

.box{
  position: absolute;
  padding:1em;
  border:1px solid black;
}
<div class="box"></div>
<br/>
<div class="box"></div>

<br/> used with float:left;:

.box{
  float:left;
  padding:1em;
  border:1px solid black;
}
<div class="box"></div>
<br/>
<div class="box"></div>
J.Joe
  • 644
  • 2
  • 7
  • 20
  • 3
    [BR](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br) is used where you would need to break a line. Do not use it for styling. Even the `clear` it can be used for is obsolete - also read this: http://stackoverflow.com/questions/1726073/is-it-sometimes-bad-to-use-br – mplungjan May 14 '16 at 04:58
  • Addition to @Rubix_Revenge answer, `
    ` is a text level semantics tag. Although it represents a line break, it is not specifically designed for element styling. Try continue to use CSS for desired page design.
    – Litestone May 14 '16 at 05:05
  • For the clear fix, just use `
    ` it will work perfectly for the float case.... for the `absolute` case use `left` / `top` `position`.
    – Sufyan Jabr May 14 '16 at 05:18
  • 1
    Poor OP can't catch a break. "I am not trying to do a clearfix" - yet someone provides a solution for a clearfix anyway. – BoltClock May 14 '16 at 05:46

6 Answers6

4

As mentioned, the br element introduces a line break. How exactly this element is implemented in CSS varies greatly across browsers, but as you're dealing with two block-level boxes, I'm not surprised the behavior is consistent here.

The difference in behavior between floating and absolute positioning is that floats never intersect with each other normally (unless you force them to, using negative margins), whereas absolutely positioned elements can intersect because they're not aware of one another (and neither is the rest of the layout).

But note that position: absolute by itself does not change an element's static position (i.e. where it would otherwise be if it wasn't absposed). See the following questions:

This is why the br affects the layout of the second absolutely positioned element. If you hide the first element, it becomes much clearer that the br is just starting on the first line of the document, unaware of the first element, but the second element is aware of the br:

.box {
  position: absolute;
  border: 1px solid black;
  padding: 1em;
}

.box:first-child {
  visibility: hidden;
}
<div class="box"></div>
<br>
<div class="box"></div>

As for floats, the CSS2.1 spec says that floating elements cannot be higher than the line box containing the box of a preceding element. I'm guessing that the br generates an inline box that lives in a new line box, rather than at the end of the previous line (as you would expect of a carriage return), which is why the second floating element starts at the second line of the document rather than the first line.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
2

<br>(<br/> is in XHTML) is a line break. It is analogous to the ASCII character CR(carriage return). It is an inline element, which means it's designed to be used inside text in divs and as a part of spans etc.

In your code, as the br is analogous to adding a CR, the effect will be the same as adding a line of text above the second block in place of the br.

I've done so here:

.box{
  position: absolute;
  padding:1em;
  border:1px solid black;
}
<div class="box"></div>
abc
<div class="box"></div>

Above, the position is absolute, so the divs were superimposed on one another. Adding the br has caused the second one to move down one line.

Below, the text is added above the second block, but the blocks don't superimpose, and float to the left.

.box{
  float:left;
  padding:1em;
  border:1px solid black;
}
<div class="box"></div>
abc
<div class="box"></div>

Reference: Mozilla Developer Network: <br> Element

Reference 2: Learn CSS Positioning in Ten Steps

cst1992
  • 3,823
  • 1
  • 29
  • 40
  • But why does `position: absolute;` respect the space taken by `
    `? It is the smae as `position: fixed;` here.
    – J.Joe May 14 '16 at 05:27
  • You have not specified `top`, that's why. If you set `top: 0px;`, it'll look as you expect. – cst1992 May 14 '16 at 05:36
  • In your second example, the text `abc` is pushed to the very right. `but the blocks don't superimpose, and float to the left.`??? – J.Joe May 14 '16 at 12:35
  • Text does not float to the right, but the blocks float to the left. The function of float is exactly that: to make a 'wrap-around' of text around images similar to that in newspapers. See the 7th point in the second reference. – cst1992 May 14 '16 at 13:14
0

I think I know what's going on. Because both divs are of class box, both are float left, but because you are breaking the line between the first and second, the break is applied before the second box, hence it's pushed down one line.

<br />, is intended to be used to separate text. If you want to move position of divs, CSS styling is definitely the way to go.

0

The unexpected results is not caused by the <br /> it's caused by the position:absolute and the float:left.

position:absolute works like when you add an image to a word document, and you choose 'display in front of text', which is what caused the two divs in the first example to intersect into each other.

float:left: I can explain it as the gravity for these items now is pulling them to the left, hence the <br /> made the other div little bit lower but next to each other.

I am a little surprised by 1st result though as I expected the both divs will exactly match over each other with no affect from the <br />.

cst1992
  • 3,823
  • 1
  • 29
  • 40
Sufyan Jabr
  • 791
  • 4
  • 20
0

The <br> tag can be related to a carriage return. Another way to think of this is that it is a <div>, except that it doesn't have any content.

Css can be used to hold formatting of text as well. Use white-space:pre; to keep the content's formatting, like spaces and line-ends.

Links that can help you:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br
http://www.w3schools.com/tags/tag_br.asp

UPDATE:
I overlooked your other question. The unexpected results are more responsible to what you did with the css 'position:' value, and how they then affect the <br>. Remember: the <br> tag is not exactly a line-break, so it still is affected by other css attributes. It can even be possible to apply css to the <br>, although if you do this, I recommend to simply use the white-space:pre css code on the parent, or use a <span> to achieve this.

pepperjack
  • 673
  • 6
  • 20
  • the w3schools is not adding anything to the MDN explanation, it even steals the "use only for poems and addresses" and, no it is not like a div – mplungjan May 14 '16 at 05:10
  • Sorry about that, I was trying to find links that were trustworthy for the author of the question. – pepperjack May 14 '16 at 05:12
  • 1
    w3school is not trustworthy for much. – mplungjan May 14 '16 at 05:13
  • How? Many teachers recommend it, and I have never seen it be wrong. – pepperjack May 14 '16 at 05:17
  • Many teachers? That is worrisome. The problem with w3schools is it is very tempting since it looks complete, but since its inception it has been riddled with mistakes and poor practices - so much that a site was created to criticise it: http://www.w3fools.com/. I will click if I forgot some CSS attribute but never fully trust the result, relying on prior knowledge to ignore the mistakes. – mplungjan May 14 '16 at 05:20
  • I have yet to see something that is wrong, and if something was ever found that is wrong (which I have never seen, let me stress), it must have been fixed very quickly. Many of my teachers recommend this, so do my tutors. Please tell me something that they got wrong. If you are just trying to argue, please let me answer other people's questions. – pepperjack May 14 '16 at 05:21
  • I am not arguing. I have submitted dozens of corrections to their site which is now better but I still come across poor practices. MDN and http://www.webplatform.org/ are better resources – mplungjan May 14 '16 at 05:23
  • Put simply, w3Schools is a rookie site. Once you get into serious programming, you have to move past it to some more comprehensive sites such as MDN. – cst1992 May 14 '16 at 05:24
  • I fully agree with both of you, although this question would reside in the 'rookie' category, so usage of 'serious programming' documents aren't needed. – pepperjack May 14 '16 at 05:26
  • 1
    @cst1992 - it is used by rookies and that was very worrying since they then came here with poor practices that had to be unlearned. Anyway, Evan needs to get back to answering questions ;) – mplungjan May 14 '16 at 05:26
  • 1
    Wow, I feel like a douche bag after I re-read that. Sorry mplungjan. – pepperjack May 14 '16 at 05:48
0

The <br /> tag is equivalent to <br> in HTML5. Basically, it means that break at that point and goes to new line. For example:

<p> hello world<br><br> how are</p>

Output is :

hello world

how are

After hello world it jump to next line then again jump to another next due to another break statement.

In html some elements are block elements which means that they automatically break into new line after complete like div, h1 after complete they control jump to new line but some elements are inline elements that had not capability to jump into new line like p, span. So br is used to break the line in html.

cst1992
  • 3,823
  • 1
  • 29
  • 40
  • Use snippets if you want to show code output. You can also use external sites like Plunker or jsFiddle. – cst1992 May 14 '16 at 05:31