Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don’t wrap span elements

Tags:

I’ve got a list of <span> elements that can be moved left and right inside a <div> element, and if some spans go outside the div they should be hidden. This works fine using overflow: hidden.  However, if there are more spans than fit in the div, the spans wrap, which is undesired behaviour for my use case. How do I make the spans not wrap?

I’ve made a jsFiddle to show what I mean. When you click inside the .board you’ll add another .card. By the fourth card you’ll see the wrapping.

Note: The fact that spans are used is not really important, so if it can be made to work with e.g. list items, that would probably be okay. The important thing is that the elements can contain an image and some text underneath.

Here’s the code from the jsFiddle:

<div class="board">    <div class="cards"></div> </div> 
$('.board').mousemove(function(e) {     $('.cards').css({left: e.pageX}); });  $('.board').click(function(e) {    $('.cards').append("<span class='card'></span>")  }); 
.card {     border: 1px solid black;     width: 100px;     height: 100px;     float: left;     margin-left: 4px;     margin-right: 4px;    }    .cards {     position: relative;     top: 10px;  }  .board {     width: 400px;     height: 120px;     border: 1px solid red;     position: relative;     overflow: hidden; } 
like image 393
Jonas Avatar asked Jul 04 '13 05:07

Jonas


People also ask

How do you stop a span from wrapping?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).

How do you make P tags not wrap?

Give this style to the <p> tag. This will break the individual words on their characters. You might want to use word-break: normal; and give the parent a width .

Can a span wrap a div?

The <span> element shows the inline portion of a document. The <div> elements show a block-level portion of a document. A div is a block-level element and a span is an inline element. The div should be used to wrap sections of a document, while use spans to wrap small portions of text, images, etc.

What elements can go inside a span?

The span element is an inline element, which should contain only other inline elements and no block elements. From the spec: Generally, block-level elements may contain inline elements and other block-level elements.


2 Answers

You can use inline-block on .card in stead of float, and then disable wrapping with nowrap:

For .card:

display:inline-block; 

For .cards:

white-space:nowrap; 

http://jsfiddle.net/33kj4/1/

like image 173
MaX Avatar answered Sep 20 '22 12:09

MaX


Try adding this to your CSS:

.cards {     white-space: nowrap;     float: left; } 
like image 29
omma2289 Avatar answered Sep 19 '22 12:09

omma2289