Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline Blocks and Text Wrapping with CSS

I want to display a checkbox, followed by some text that wraps around below itself. The HTML without any CSS looks as follows:

<input type="checkbox" checked="checked" />
<div>Long text description here</div>

I want it to display similar to:

X   Long Text
    Description
    Here

It currently wraps around like this

X   Long Text
Description Here

This is easy to do with tables, but I need it to be in CSS for other reasons. I thought a combination of display: inline-block / float: right / clear / spans instead of DIVs would work, but I've had no luck so far.

like image 367
geographika Avatar asked Feb 11 '10 09:02

geographika


People also ask

Can inline-block wrap?

While using the property display: inline-block will wrap the element to prevent the text inside from extending beyond its parent. Lastly, using the property display: block will put the element on its own line and fill its parent.

What is inline and block in CSS?

The display: inline-block Value Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.

How do you prevent inline-block elements from wrapping?

As a result, the elements can sit next to each other. The major difference between display: inline; and display: inline-block; is that, display: inline-block; also allows us to set the width and height of the element. We can prevent inline-block divs from wrapping by adding suitable Bootstrap classes.

How do I wrap text in next line in CSS?

break-word: This is the actual CSS syntax that tells the browser to wrap a long text over to a new line. normal: It breaks each word at the normal points of separation within a DOM.


2 Answers

Wrap the checkbox and label in a container div (or li - i do forms with lists often) and apply

<div class="checkbox">
  <input type="checkbox" id="agree" />
  <label for="agree">I agree with checkbox</label>
</div>




.checkbox input {
      float:left; 
      display:block;
      margin:3px 3px 0 0;
      padding:0;
      width:13px;
      height:13px;
     }

.checkbox label {
      float:left;
      display:block;
      width:auto;
     }
like image 55
easwee Avatar answered Oct 21 '22 19:10

easwee


Try this:

input { float: left; }
div { margin-left: 40px; }

Tune the margin-left to how much space you want. The float: left on the checkbox basically takes it out of the block layout so it doesn't push down the text.

like image 32
cletus Avatar answered Oct 21 '22 19:10

cletus