Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Min-width, max-width css use smallest width

Tags:

html

css

Alright, so what I'm hoping to do is to create a DIV that will autosize based on the content that's in it, but it should use the smallest width possible. I haven't a clue as to how to do this.

So, if I have a DIV tag that has 3 characters which is no doubt under 200px wide, what I want is for the div to be 200px then. If there's a lot more text I would like it to auto size up so that the text will fit.

**Is this possible with just ***CSS*?****

** UPDATE **

Let me see if I can explain this better. I have a DIV:

+++++++++++++++++++++++++++++++++
|-------------------------------|
|-----text this longer text-----|
|-------------------------------|
+++++++++++++++++++++++++++++++++

What I am hoping to have happen is if the text is changed it will still be at least 200px wide. Like this:

+++++++++++++++++++++++++++++++++
|-------------------------------|
|-------------text--------------|
|-------------------------------|
+++++++++++++++++++++++++++++++++

But if it was even longer text then the first example it would continue to expand outward past 200px wide:

++++++++++++++++++++++++++++++++++++++++++++++++++++++
|----------------------------------------------------|
|-----text this longer texttext this longer text-----|
|----------------------------------------------------|
++++++++++++++++++++++++++++++++++++++++++++++++++++++

However, I do have a limit (limited amount of space) that it can go so I need to put a maximum width to it. Does that make sense or do I need to clarify things more?

like image 202
user1470118 Avatar asked Aug 09 '12 19:08

user1470118


People also ask

Should I use min width or max width in CSS?

When to use which: min-width or max-width. If you are designing your website for smaller devices first then set your default CSS breakpoints with min-width and adjust for larger devices accordingly. Meanwhile, if you are designing for larger devices first then use max-width and then tune for smaller devices accordingly ...

How min width and max width works in CSS?

Max-width and min-width can be used together to target a specific range of screen sizes. @media only screen and (max-width: 600px) and (min-width: 400px) {...} The query above will trigger only for screens that are 600-400px wide. This can be used to target specific devices with known widths.

Does Min width override width?

The min-width property in CSS is used to set the minimum width of a specified element. The min-width property always overrides the width property whether followed before or after width in your declaration.

Does Min width override max width?

max-width overrides width , but min-width overrides max-width .


2 Answers

The issue is that since <div>s are block-level elements, they do not auto-resize to fit their content, although their width can be explicitly defined. Inline elements like <span> do auto-size to fit their content, but they don't accept defining their width through CSS. The workaround is to make your <div> behave somehow like a table cell. Here's an example:

CSS:

.widthlim {
    min-width: 200px;
    max-width: 1000px;
    background-color: red; /*This is just for us to be able to see the width*/
    display: table-cell;
    word-wrap: break-word;
    word-break: break-word;
}

HTML:

Example 1:<br/>
<div class = "widthlim">
Hello
</div>
Example 2:<br/>
<div class = "widthlim">
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
</div>
Example 3:<br/>
<div class = "widthlim">
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello 
</div>

(Note how we had to add <br/>s before the <div>s because they're no longer completely block-level).
Here's how that looks when rendered: The above example rendered This seems to work on Firefox, Chrome, and IE8 (for IE8 you need to specify a DOCTYPE). I hope that helped!

like image 145
Chris Avatar answered Sep 24 '22 03:09

Chris


You can "shrink-wrap" a div in a few different ways. The easiest is probably to display: inline; but a float will behave like you describe as well.

Check out http://haslayout.net/css-tuts/CSS-Shrink-Wrap for details and more methods.

If you want the minimum width to be 200px (contradictory to your first paragraph) you can use min-width: 200px; along with display: inline-block for instance.

like image 24
xec Avatar answered Sep 25 '22 03:09

xec