Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line-breaks between divs render a space. How to eliminate it from HTML?

I have the following layout

<div style="width:100px">
    <div style="width:50%; display: inline-block;">
        div1
    </div>
    <div style="width:50%; display: inline-block;">
        div2
    </div>
</div>

because in html there's a change-line between closing and opening div (div1 and div2), browsers add a "space" character in place of the line-break which results in having the second div to display underneath the first div.

However if you remove the \n between the div1 and div2 then they display next to each other which is the expected behaviour.

<div style="width:100px">
    <div style="width:50%; display: inline-block;">
        div1
    </div><div style="width:50%; display: inline-block;">
        div2
    </div>
</div>

However this makes code looks ugly. I am currently using <DOCTYPE html>. I have also tried switching to XHTML but didn't work. I am pretty sure there's some way of eliminating this behaviour of line-breaks, any ideas?

FYI: I do not want to use float or parse my html output in php during rendering to remove the line-breaks.

like image 650
ptheofan Avatar asked Oct 25 '12 06:10

ptheofan


People also ask

Which HTML tag preserves spaces and line breaks of text?

The HTML <pre> which is used to put a preformatted text into an HTML document preserves spaces and line breaks of the text. Watch a video course CSS - The Complete Guide (incl. Flexbox, Grid & Sass) Example of preserving spaces and line breaks of a text with the HTML <pre> tag: ¶

How to prevent a Div from breaking to the next line?

An HTML <div> (division) is a block-level elementdesigned to not display any HTML elements next to it unless its default behavior is changed. Below are all the different methods of preventing a div from breaking to the next line. Tip Depending on why you want to break a div, also consider a <span> tag.

How do you preserve line breaks in HTML?

Preserve Newlines, Line Breaks, and Whitespace in HTML Sometimes you want to display the content in the same format as typed in a textarea. For example, a text from a textarea may contain line breaks or empty lines. The white-space CSS property is helpful displaying the text in the same way it’s provided by the user.

How do I Break a Div in HTML?

Depending on why you want to break a div, also consider a <span> tag. It is an inline-level element and does not break to the next line unless its default behavior is changed.


1 Answers

One more possible way is to set the font-size of the parent to zero and then set the font-size of child elements to whatever you need. Like:

#mybar { font-size: 0; }
#mybar span { font-size: 14px; }

However, you have to be aware that font-size = 0 is displayed different depending on the browser: http://webdesign.about.com/od/fonts/qt/tipfont0.htm

I tested in Firefox and works as expected. If I remember correctly, the minimum font size can be also set depending on the browsers, so, it may not be consistent.

like image 164
lepe Avatar answered Oct 19 '22 10:10

lepe