3

Hi I have the below HTML, Inside the Container I have Header, section and div. With my current CSS below the div with class rightSideDiv does not show to right to the section element.

.container {
  height: 500px;
  widht: 500px;
  background-color: red;
}

.headerTitle {
  display: inline-block;
  height: 24px;
  margin: 24px 24px 0;
  padding: 0;
  line-height: 24px;
}

.sectionClass {
  width:249px;
  height:200px;
    background-color: yellow;

}

.rightSideDiv {
    width:249px;
  height:200px;
  border: 4px solid green;

}
<aside>
  <div class="container"> 
    <header class="headerTitle"> Header Title  </header> 
    <section class="sectionClass"> .  </section>
    <div class="rightSideDiv"> </div>
  </div>
  </aside>

The section and div should be shown side by side. I dont want to modify the current HTML structure. I have tried specifying float:left or right but both doesn't seem to work.

user804401
  • 1,990
  • 9
  • 38
  • 71

4 Answers4

2

Apply float: left; to both containers, use width: 50%; instead of px and display: block; header

.container {
  height: 500px;
  width: 500px;
  background-color: red;
}

.headerTitle {
  display: block;
  height: 24px;
  margin: 24px 24px 0;
  padding: 0;
  line-height: 24px;
}

.sectionClass {
  width:50%;
  height:200px;
  background-color: yellow;
  float: left;

}

.rightSideDiv {
  width:50%;
  height:200px;
  background-color: pink;
  float: left;

}
<aside>
  <div class="container"> 
    <header class="headerTitle"> Header Title  </header> 
    <section class="sectionClass"> .  </section>
    <div class="rightSideDiv"> </div>
  </div>
  </aside>
Dean Coakley
  • 1,675
  • 2
  • 11
  • 25
0

Try using flexbox and display:flex instead. With very few changes to css you can get something like this: https://jsfiddle.net/vnuz47va/2/

.container {
  height: 500px;
  width: 520px;
  background-color: red;
  display:flex;
  flex-wrap:wrap;
  justify-content:space-between;
}

.headerTitle {
  display: inline-block;
  height: 24px;
  margin: 24px 24px 0;
  padding: 0;
  line-height: 24px;
  width:100%;
}

.sectionClass {
  width:249px;
  height:200px;
  background-color: yellow;

}

.rightSideDiv {
  width:249px;
  height:200px;
  border: 4px solid green;
}
<aside>
  <div class="container"> 
    <header class="headerTitle"> Header Title  </header> 
    <section class="sectionClass"> .  </section>
    <div class="rightSideDiv"> </div>
  </div>
</aside>
McVenco
  • 1,011
  • 1
  • 17
  • 30
aMJay
  • 2,215
  • 6
  • 22
  • 34
0

change your css with this :

.container {
    height: 500px;
    width: 500px;
    background-color: red;
}

.headerTitle {
    height: 24px;
    margin: 24px 24px 0;
    padding: 0;
    line-height: 24px;
}

.sectionClass {
    float : left;
    width: 50%;
    height:200px;
    background-color: yellow;

}

.rightSideDiv {
    float : right;
    width:50%;
    height:200px;
    border: 4px solid green;

}

you can use float right and left to align your div, however your container has a width to 400 and your 2 div are 249+249 = 498 so there is a problem here..

Dean Coakley
  • 1,675
  • 2
  • 11
  • 25
Léo R.
  • 2,620
  • 1
  • 10
  • 22
0

Change the H2 to display: block;, and then add float:left; to both boxes.

When you want divs side-by-side through floating, float them the same direction.

rightSideDiv is 8 pixels taller than the other. That is because the 4px border is added on top of the height. Consider using box-sizing: border-box;, which makes the border get absorbed into the set height, instead of being added on top of it.

.container {
  height: 500px;
  width: 600px;
  background-color: red;
}

.headerTitle {
  display: block;
  height: 24px;
  margin: 24px 24px 0;
  padding: 0;
  line-height: 24px;
}

.sectionClass {
  width:249px;
  height:200px;
  background-color: yellow;
  display: inline-block;
  float: left;

}

.rightSideDiv {
    width:249px;
  height:200px;
  border: 4px solid green;
  display: inline-block;
  float: left;
}
<aside>
  <div class="container"> 
    <header class="headerTitle"> Header Title  </header> 
    <section class="sectionClass"> .  </section>
    <div class="rightSideDiv"> </div>
  </div>
  </aside>
Lee A.
  • 33
  • 6