Jump to content

I can't control the width of an inline div.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
I am trying to make a horizontal bar graph with divs. It's pretty easy if I just have them on separate lines. I just write:


<div class="horizontal_bar" style="width:100px"></div>


.horizontal_bar{

	height: 13px;

	background-color: red;

}


If I do that, I get a red bar that's 100 pixels wide and 13 pixels high. But I want to add a label to the left of the div, so I've made it inline, like this:


display: inline;


For some reason, this overrides the 100 pixel width I specified and makes it so that the width of the div is equal to the width of the contained elements, not 100 pixels. That is to say, if I have a div that I've set to 100px and it has a single 5 pixel wide asterisk in it, the resulting width of the div is 5 pixels. If there's nothing in the div, it doesn't show up at all.

This is the reason I hate CSS. If I could just fix this problem, then all my frustrations would be gone and my relationship with this particular markup language would be much more positive. Can anyone help me on this?

EDIT: I'm aware that it's bad practice to use inline styles. That's not the issue, so please don't bring it up.
Programming is a journey, not a destination.

#2
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
I'm guessing that no one else knows the solution to this problem either. My conclusion is that CSS sucks.
Programming is a journey, not a destination.

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
CSS requires full knowledge of semantic markup which many do not have, that is why it is confusing.

Can you provide a full working example of your problem? I cannot replicate it from what you had written, Maybe try it with a <span>, two nested spans should fit nicely into eachother, divs are only suitable for containers.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
ethikz

ethikz

    Programmer

  • Members
  • PipPipPipPip
  • 112 posts
exactly that is why no one but nullw0rm can help lol

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
I didn't answer as i can't replicate your problem. In chrome okay i see no div( width 0 i assume) but it works just normal in IE. And works ok if i use "display:inline-block" in chrome.

It also works in both chrome and IE if I use the normal "display:inline" together with "position:absolute".

Inline should be the default tho, no?

#6
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
The default for divs is display:block.
Programming is a journey, not a destination.

#7
Joanzn

Joanzn

    Newbie

  • Members
  • Pip
  • 2 posts
Isn't a <div> tag used for block elements....I think u should try a <span> (inline) tag instead?

#8
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
Divs can be either inline or block, depending on how the attribute is set. Making them inline allows you to put multiple divs side by side rather than having an automatic newline between each of them.
Programming is a journey, not a destination.

#9
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Hi,

You can use "display: inline-block" and it will work in IE 9 and other major browsers. If you want it to work in IE 6/7/8 you have to change div to span.

You can either use "float: left" instead of "display: inline-block" to get desired result.

Test page: http://loutontheweb....line-block.html