Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent linebreak after </div>

Tags:

html

css

Is there a way to prevent a line break after a div with css?

For example I have

<div class="label">My Label:</div> <div class="text">My text</div> 

and want it to display like:

My Label: My text

like image 566
Fabiano Avatar asked Aug 26 '10 15:08

Fabiano


People also ask

How do you not break a line after div?

display:inline; will turn the div into the equivalent of a span . It will be unaffected by margin-top , margin-bottom , padding-top , padding-bottom , height , etc.

Does div cause a line break?

It is an inline-level element and does not break to the next line unless its default behavior is changed. To make these examples easier to use and understand for all types of computer users, we're using the style attribute in the div.

How do you stop a line break?

Use white-space: nowrap; or give that link more space by setting li 's width to greater values. I prevented line break in li items using display: inline; . Maybe this will also help others with similar problems. Its important to be careful with display: inline as it can have side effects.

How do I stop a line breaking in HTML?

The <nobr> HTML element prevents the text it contains from automatically wrapping across multiple lines, potentially resulting in the user having to scroll horizontally to see the entire width of the text.


1 Answers

display:inline;

OR

float:left;

OR

display:inline-block; -- Might not work on all browsers.

What is the purpose of using a div here? I'd suggest a span, as it is an inline-level element, whereas a div is a block-level element.


Do note that each option above will work differently.

display:inline; will turn the div into the equivalent of a span. It will be unaffected by margin-top, margin-bottom, padding-top, padding-bottom, height, etc.

float:left; keeps the div as a block-level element. It will still take up space as if it were a block, however the width will be fitted to the content (assuming width:auto;). It can require a clear:left; for certain effects.

display:inline-block; is the "best of both worlds" option. The div is treated as a block element. It responds to all of the margin, padding, and height rules as expected for a block element. However, it is treated as an inline element for the purpose of placement within other elements.

Read this for more information.

like image 137
Jeff Rupert Avatar answered Sep 19 '22 08:09

Jeff Rupert