2

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>
3
  • you probably shouldn't have two DIVS with an id of "right" -- ids are supposed to be unique. Commented Nov 17, 2008 at 14:02
  • agreed - change the 'id="left/right"' to 'class="left/right"' (arguments about semantics aside), and change your css to ".left" and ".right" Commented Nov 17, 2008 at 14:07
  • Can I suggest separating out your questions? It'll make it easier for people to give specific answers :) Commented Nov 17, 2008 at 14:11

8 Answers 8

6

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" ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

+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.
1

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 ; */
}

Comments

0

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.

3 Comments

that won't change his situation though. though it's not valid HTML, the browser still will render the CSS "correctly"
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.
you're correct in general, but in this case, it doesn't change anything.
0

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)

Comments

0

you can use this code

<div id="main">
        <div id="left">1</div>
        <div id="right">3</div>
        <div id="right">2</div>
    </div>

Comments

0

You cannot use the same id for more than one time.

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

Better way you can do it with assigning class.

Comments

0

Try this:

   <!DOCTYPE html>
<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 class="left">1</div>
    <div class="right">3</div>
    <div class="right">2</div>
</div>
</body>
</html>

Changes made:

Replaced commas with semicolons in the CSS properties. Changed duplicate IDs to classes. Changed the order of the last two divs within the main div to control their appearance order.

Comments

-1
<html>
<head>
<style>
#main {
    border: 4px dashed red;
    display: block;
    overflow: hidden;
}
#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">3</div>
  <div id="right">2</div>
</div>
</body>
</head>
</html>

1 Comment

The answer, while valid code, does not explain WHY you changed what you changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.