Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display two divs on same line without blankspace in between, inside a div with contenteditable="true"

<div id="test" class="pad" contenteditable="true">
  <div id="a" class="fif">text1</div>
  <div id="b" class="fif">text2</div>
</div>  

As in above code I have a contenteditable div and many divs inside it (child divs). Number of child divs vary dynamically and also the content between div tags. I want text1 and text2 (i.e. content between div tags) to be displayed on same line, without any blank space in between. Also while typing in contenteditable div if I press ENTER key it should go to next line.

I tried float:left but it does not allow me to go to the next line when I press ENTER key while typing in contenteditable div. display:inline, span when used show blank space in between 2 div contents. I tried using flex from http://www.w3.org/TR/css3-flexbox/ but didn't get desired output.

like image 485
Vivek Dani Avatar asked Mar 01 '13 20:03

Vivek Dani


People also ask

How do I make two divs display on the same line?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How do I keep divs on the same line?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.

Can you get two divs at once?

With CSS properties, you can easily put two <div> next to each other in HTML. Use the CSS property float to achieve this.

Can two divs overlap?

In order that other div layers within the Outer Wrapper Div Layer can be floated over and overlapped with each other, the following setting is required: Set the position of these layers (e.g. layer1 and layer2) to Absolute position. Set the CSS z-index of these layers higher than the Outer Wrapper Div Layer.


2 Answers

Just make the <div>s have display: inline or display: inline-block and remove the white space between them in the HTML source.

Whether you use inline or inline-block depends on how you want content within the divs to be laid out. Here's an MDN article on the subject.

Demo: http://jsfiddle.net/timdown/GVZPX/

CSS:

.fif {
    display: inline; /* Or inline-block */
}

HTML:

<div id="test" class="pad" contenteditable="true">
  <div id="a" class="fif">text1</div><div id="b" class="fif">text2</div>
</div>
like image 199
Tim Down Avatar answered Sep 27 '22 22:09

Tim Down


<div>
  <div style="display: inline-block;">1</div>
  <div style="display: inline-block;">2</div>
</div>
like image 40
websky Avatar answered Sep 27 '22 21:09

websky