tags:

views:

635

answers:

4

Hello,

I have some questions about basic CSS that I was unable to understand or find an answer for.

First, I tried placing 3 div tags within another div tag. The first main div tag containing the 3 other tags had nothing set for it except a size, which was 400px by 400px. Of the other 3 divs inside, all were 20px by 20px, and 1 was assigned float:left, and the other two were assigned a style that was float right. All attributes were defined within a style, and the two divs that were float:right were assigned the same style. My problem is that out of the 2 divs, the one that came last in the code, would appear first in a browser, and I did not understand the reasoning for this.

Here is the code:

<html>
<head>
<style>
#main{ 
    border: red 4px dashed;
    width: 25%
    height: 25%,
    }
#left{ 
    float: left; 
    width: 20px;
    height: 20px,
    }
#right{ 
    float: right; 
    width: 20px;
    height: 20px,
    }
</style>
</head>
<body>
<div id="main">
<div id="left">1</div>
<div id="right">2</div>
<div id="right">3</div>
</div>
</body>
</head>
</html>
+5  A: 

My problem is that out of the 2 divs, the one that came last in the code, would appear first in a browser, and I did not understand the reasoning for this.

I think that You misunderstood a "appear first". You set Your divs to be floating right. So a "2" div, which is FIRST in Your code, is FIRST to be floated right, so it goes first to the right side. A "3" div is then floated, so i goes to the left side of the "2" div - because "2" was first, it took first place at the right side of the browser window, and div "3" took second place at the right side of the window.

In this case "second place at the right side of the window" means "first, looking from the left" ;-)

Falco Foxburr
+1 this is correct - the other answers are pointing out some other issues with the code sample, but they don't address the problem. this answer explains what's going on.
nickf
+1  A: 

I don't know about the layering problem, but you can't have two elements with the same ID. You probably want:

...
<div class="right">2</div>
<div class="right">3</div>
...

and change #right to .right in the CSS.

Graeme Perrow
that won't change his situation though. though it's not valid HTML, the browser still will render the CSS "correctly"
nickf
Perhaps, but I assumed that if you're doing something "illegal", the browser is free to interpret it whatever way it wants, which may not give you what you expect depending on the browser. I figure the best way to *start* is to have correct HTML.
Graeme Perrow
you're correct in general, but in this case, it doesn't change anything.
nickf
+1  A: 

I think your problem is that you are using id instead of class. An ID is supposed to be unique, so the second div with id="right" is the only one with that id.

You could change your code such that you have:

< div class="right" >2< /div >

< div class="right" >3< /div >

(instead of id="right")

And in the css you would have:

.right {

float: right;       

width: 20px;

height: 20px,

}

(instead of #right)

Winston Smith
+1  A: 

At first, I would use class and not id for the divs. But there are also some typo's in the css:

#main{ 
    border: red 4px dashed;
    width: 25%;  /* <-- Missing ; */
    height: 25%; /* <-- change , to ; */
}
#left{ 
    float: left;        
    width: 20px;
    height: 20px; /* <-- change , to ; */
}
#right{ 
    float: right;       
    width: 20px;
    height: 20px; /* <-- change , to ; */
}
Gamecat