CSS: Div width as wide as it's content

CSS: Div width as wide as it's content

css width: auto fit
auto adjust div width based on content
css width fit-content
css width percentage of parent
css get width of element
css center div
css div not full width
how to set div width to fit the screen

I have a problem with the width of a div, I want to have the width of the div equaled to the width of it's content.

The problem I have is, if I resize the browser down to a width of 1024 the content doesn't float anymore, which is correct but I'd need the div around it to be as wide as it's content.

Here is the code im using to test it:

Thanks in advance!!!

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Content with Menu</title>
    <style type="text/css">
        .box {
            border: 1px solid #AAA;
            border-radius: 4px;
            padding: 10px;
            margin-bottom: 20px;
            display: -moz-inline-stack;
            display: inline-block;
            vertical-align: top;
            zoom: 1;
            *display: inline;
        }

        div.box ol li label {
            font-size: 12px;
            position: relative;        
            float: left;
            width: 11em;
            margin-right: 1em;
            padding-top: 0.5em;
        }

        input {
            border:1px solid #CCC;
            font: 12px Arial,Helvetica,sans-serif;  
            width: 160px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box">
            <ol>
                <li>
                    <label>
                    TEST 1.1
                    </label>
                    <input type="text" disabled="disabled" />
                    <input type="text"/>
                    <span></span>
                    <span></span>
                </li>
                <li>
                    <label>
                        TEST 1.2
                    </label>
                    <input/>
                </li>
                <li>
                    <label>
                        TEST 1.3
                    </label>
                    <input/>
                </li>
            </ol>
        </div>

        <div class="box">
            <ol>
                <li>
                    <label>
                    TEST 2.1
                    </label>
                    <input type="text" disabled="disabled" />
                    <input type="text"/>
                    <span></span>
                    <span></span>
                </li>
                <li>
                    <label>
                        TEST 2.2
                    </label>
                    <input/>
                </li>
                <li>
                    <label>
                        TEST 2.3
                    </label>
                    <input/>
                </li>
            </ol>
        </div>
    </div>
</body>


To prevent such things as misplacement of content when window is resized to a low size, use min-width. This way, you can expand your content, but you set a minimum width, so that your content doesn't get "squeezed"...

How to make a div fill a remaining horizontal space using CSS , How do I change the width of a div in CSS? Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.


agreeing with what @lvozor said,

your markup become's like this :

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Content with Menu</title>
    <style type="text/css">
        .box {
            border: 1px solid #AAA;
            border-radius: 4px;
            padding: 10px;
            margin-bottom: 20px;
            display: -moz-inline-stack;
            display: inline-block;
            vertical-align: top;
            zoom: 1;
            *display: inline;
        }

    div.box ol li label {
        font-size: 12px;
        position: relative;        
        float: left;
        width: 11em;
        margin-right: 1em;
        padding-top: 0.5em;
    }

    input {
        border:1px solid #CCC;
        font: 12px Arial,Helvetica,sans-serif;  
        width: 160px;
    }
</style>
</head>
<body>
<div class="box" style="min-width:1024px;">
    <div class="box">
        <ol>
            <li>
                <label>
                TEST 1.1
                </label>
                <input type="text" disabled="disabled" />
                <input type="text"/>
                <span></span>
                <span></span>
            </li>
            <li>
                <label>
                    TEST 1.2
                </label>
                <input/>
            </li>
            <li>
                <label>
                    TEST 1.3
                </label>
                <input/>
            </li>
        </ol>
    </div>

    <div class="box">
        <ol>
            <li>
                <label>
                TEST 2.1
                </label>
                <input type="text" disabled="disabled" />
                <input type="text"/>
                <span></span>
                <span></span>
            </li>
            <li>
                <label>
                    TEST 2.2
                </label>
                <input/>
            </li>
            <li>
                <label>
                    TEST 2.3
                </label>
                <input/>
            </li>
        </ol>
    </div>
</div>
</body>

How to Create a Fixed-Width Layout with CSS, How do I make a div fill the remaining width? I'm trying to learn CSS to get away from table tags and just getting started. I'm trying to follow along with a friends CSS file to hopefully get a better understanding. One of the first things I'm trying to do is make a box around some information but have that box only be as wide as the text.not the whole width of the browser.


With React and styled-components.

Render:

<StyledInput
      value={value}
      length={String(value).split('').filter(v => typeof +v === 'number').length}
    />

Styling (with rem):

const StyledInput = styled.input`
   width: ${p => `${p.length * 0.9}rem`}; // adjust the number accordingly
`; 

Styling (with px):

const StyledInput = styled.input`
   width: ${p => `${p.length * 16}px`}; // adjust the number accordingly
`; 

width, layout to be positioned in the center of the browser. The child div has a width of 400px but the parent has no width declared. Again, if you add a border, it can happen that the parent div is expanded to 100% while the child div is only 400px. You however may want it to be as wide as the child element – 400px (plus any paddings or margins declared).


What I get instead is that the paragraphs inherit the width of the div father node which is wider. #container { width: 30%;  Set the width of an <input type="text"> element to 100px. However, when it gets focus, make it 250px wide: input [type=text] { input [type=text]:focus { Try it Yourself » CSS tutorial: CSS Height and Width. CSS tutorial: CSS Box model. CSS reference: height property. HTML DOM reference: width property. Previous Complete CSS Reference Next


This <div> element has a max-width of 500px, and margin set to auto. Tip: Resize the browser window to less than 500px wide, to see the difference between the  Since animations (probably) aren’t involved here, it’s probably not super necessary, but you could pull the container back to the edge with transforms instead. @media (min-width: 40em) { .full-width { width: 100vw; transform: translateX(calc((40em - 100vw)/2)); } }


CSS width Property div.a { width: auto; border: 1px solid black; } div.b { width: 150px; border: 1px solid However, when it gets focus, make it 250px wide:. The width property in CSS specifies the width of the element’s content area. This “content” area is the portion inside the padding, border, and margin of an element (the box model)..wrap { width: 80%; } In the example above, elements that have a class name of.wrap will be 80% as wide as their parent element.