mabuzer
mabuzer

Reputation: 6717

force div to fill specified area?

what's the best way to force DIV to fill specified width? I have the following code that generates and assign width to DIV:

function createLabel(str) {
        var textDiv = document.createElement('div');
        textDiv.style.width = '200px';
        textDiv.style.display = 'inline';
        textDiv.appendChild(document.createTextNode(str));
        return textDiv;
}

I want created DIV's to have the same width, whatever text length is.

alt text http://j.imagehost.org/0613/show-case.png

Upvotes: 0

Views: 10136

Answers (4)

Graham Clark
Graham Clark

Reputation: 12966

You might run into trouble specifying both a width and display = 'inline'. The width won't have any effect. You'll need to keep the default display mode for a div element, i.e. block. If you need other elements at the same horizontal position, you'll need to use float as well.

Upvotes: 1

Pekka
Pekka

Reputation: 449603

Giving it a width attribute?

e.g.

<div style="width: 300px"> 

or do you mean something else? In that case, please elaborate on your question.

Upvotes: 0

instanceof me
instanceof me

Reputation: 39178

I'd go with the CSS width property, paying attention to the box model problem. Unless there is some specific requirements.

Upvotes: 0

PHP
PHP

Reputation: 1709

<div style="width : x%">

x= numeric value

Upvotes: 0

Related Questions