Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize the div when the span inside div is wrapped?

Tags:

html

css

Currently I have a div which has a max-width:200px, inside the div there are some spans with a property white-space:nowrap and display: inline-block.

.a {
  max-width:200px;
}

.b {
 display: inline-block; 
 white-space:nowrap;
}

<div class="a">
 <span class="b">A</span> <span class="b">B</span>
 <span class="b">C</span>
</div>

The current behavior is that when the length of span A and span B is over the max-width of div, span B is wrapped into next line.

<div class="a">
 <span class="b">A</span> Empty space here
 <span class="b">B</span> Empty space here
 <span class="b">C</span> Empty space here
</div>

However, the width of div is not resized corresponding to the length of the span. It always has a width of 200px which causes the problem that there are empty space after span.

What I want is to make sure the div resize by the length of the span, without any empty space.

Anyone can help me with this? I'm not asking for an answer directly. Some explanation would be better.

like image 629
Francis Zhu Avatar asked Sep 15 '16 00:09

Francis Zhu


People also ask

Can I use span inside div?

Here, divs separate out larger chunks of content, and spans wrap segments within these divs. We can use the div element to separate contents of a page into chunks. Each of these orange blocks is a div element. We can use a span tag to change the color of text.

How do you auto adjust the div width according to content in it?

One way you can achieve this is setting display: inline-block; on the div . It is by default a block element, which will always fill the width it can fill (unless specifying width of course).

How do I keep my div from wrapping to the next line?

If you want to prevent them from wrapping, either remove the white space you have used for formatting, or add white-space:nowrap; to the rule for .

How do I keep my div from moving when I resize windows?

first add a parent div/wrap and put those two divs of yours into it. Overall, whatever size you add to the min-width itll scale the parent div down to the size specified. once the window is sized down past your specified size, itll behave like any other window. this is my way of doing it.


1 Answers

Using max-width will not allow the div to be wider than given value (200px in this case) using min-width instead of max-width might help you...

like image 51
Ankur Pathak Avatar answered Sep 28 '22 01:09

Ankur Pathak