Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Div Question

Status
Not open for further replies.

booeyOH

Programmer
May 30, 2007
48
US
Hello,
When I put a <div> without a width property it defaults to 100%. Why is this, and is there a way to have it default to the width of the data in it?

Thanks,
Bryan
 
Hi

That is the default for block elements. If not want it like that maybe you should use other element.

Anyway, one solution so far :
Code:
div { display: inline; }
If not like it, tell us about your goal, not just two vague sentences.

Feherke.
 
Ok, here is an exmaple of what I am doing:

<div style="border: 1px solid #000000;background:#c9c9c9;">
<div style="float:left;margin-left:1px;margin-right:1px;cursor:pointer;border-right: 1px #000000 solid;border-left: 1px; #000000 solid;" onMouseover="this.style.background='#EFEFEF';"
onMouseout="this.style.background='#c9c9c9';" onClick="alert('click')">Q1</div>
<div style="float:left;margin-left:1px;margin-right:1px;cursor:pointer;border-right: 1px #000000 solid;border-left: 1px; #000000 solid;" onMouseover="this.style.background='#EFEFEF';"
onMouseout="this.style.background='#c9c9c9';" onClick="alert('click')">Q2</div>
</div>

But I want it to look like this (without using the tables)

<div style="border: 1px solid #000000;background:#c9c9c9;">
<table border=0><tr><td>
<div style="float:left;margin-left:1px;margin-right:1px;cursor:pointer;border-right: 1px #000000 solid;border-left: 1px; #000000 solid;" onMouseover="this.style.background='#EFEFEF';"
onMouseout="this.style.background='#c9c9c9';" onClick="alert('click')">Q1</div>
</td><td>
<div style="float:left;margin-left:1px;margin-right:1px;cursor:pointer;border-right: 1px #000000 solid;border-left: 1px; #000000 solid;" onMouseover="this.style.background='#EFEFEF';"
onMouseout="this.style.background='#c9c9c9';" onClick="alert('click')">Q2</div>
</td></tr></table>
</div>

Any ideas?
 
Hi

Use [tt]float[/tt].
CSS:
div.container {
  clear: left;
  background: silver;
  border: solid black 1px;
  padding: 2px;
}
div.element {
  float: left;
  margin: 0 3px;
  border-right: solid black 1px;
}
div.element:hover {
  background: whitesmoke;
}
HTML:
<div class="container">
<div class="element">Q1</div>
<div class="element">Q2</div>
&nbsp;
</div>

Feherke.
 
So I have to add the "&nbsp;" at the end? I put <div></div> in , so it wouldn't take up any horizontal space, is that ok?

Thanks,
Bryan
 
Hi

Bryan said:
So I have to add the "&nbsp;" at the end?
Or you can use the clearfix trick. But if you give up with the [tt]div[/tt]s, is more simple :
CSS:
div.container {
  background: silver;
  border: solid black 1px;
  padding: 2px;
}
span.element {
  margin: 0 2px;
  border-right: solid black 1px;
}
span.element:hover {
  background: whitesmoke;
}
HTML:
<div class="container">
<span class="element">Q1</span>
<span class="element">Q2</span>
</div>

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top