barteloma
barteloma

Reputation: 6845

CSS div position in another div

I am new at CSS positioning but could not understand of positioning the boxes.

<div class="box">
    <div class="first"></div>
    <div class="second"></div>
    <div class="third"></div>
</div>

.box {
    width: 260px;
    overflow: hidden;
    background: #e2e2e2;
    padding: 10px;
    position:relative;
}

.first {
    width: 50px;
    height: 50px;
    background: #BDBDBD;
    float: left;
    margin: 10px;
}

.second {
    width: 60px;
    height: 60px;
    background: #889B7F;
    float: left;
    margin: 10px;
}

.third {
    width: 70px;
    height: 70px;
    background: #B98F91;
    float: left;
    margin: 10px;
    position:absolute;
    top:70px;
    left:30px;
}

Demo

  1. If I do not set the .box position, third box is appearing up front.
  2. If I set the .box position as relative, third box is appearing under box
  3. If I set set third box position as relative, it goes right.

What is the inner div position rule?

Upvotes: 0

Views: 51

Answers (2)

Rahul
Rahul

Reputation: 518

Remove position:absolute; from .third and it will look like This

Snippet:

.box {
    width: 260px;
    overflow: hidden;
    background: #e2e2e2;
    padding: 10px;
    position:relative;
}

.first {
    width: 50px;
    height: 50px;
    background: #BDBDBD;
    float: left;
    margin: 10px;
}

.second {
    width: 60px;
    height: 60px;
    background: #889B7F;
    float: left;
    margin: 10px;
}

.third {
    width: 70px;
    height: 70px;
    background: #B98F91;
    float: left;
    margin: 10px;
    top:70px;
    left:30px;
}
<div class="box">
    <div class="first"></div>
    <div class="second"></div>
    <div class="third"></div>
</div>

Upvotes: 1

user6864832
user6864832

Reputation:

hi just remove the third box position absolute and check it and it will be look like this then

your box will be like this

Upvotes: 0

Related Questions