sab669
sab669

Reputation: 4104

Divs next to each other with nested divs on top of each other?

I'm trying to create the following layout:

enter image description here

(Note: This screenshot uses a table with 1 row and 2 data cells with nested divs -- I'm trying to remove the table, essentially)

My HTML is as follows:

<!-- outter most div containing everything -->
<div>

<!-- left div -->
<div style="max-width:50%; float:left;">
    <div class="bulletin margin-top">
        <div class="title  ">
            <span>BULLETIN BOARD</span>
        </div>
        <div class="content">
            @Html.Partial("~/Views/BulletinBoard.cshtml")
        </div>
    </div>
</div>

<!-- rightdiv -->
<div style="max-width:50%; float:left">

    <!-- top right -->
    <div class="inner announcements margin-top">
        <div class="title inner">
            <span>ANNOUNCEMENTS</span>
        </div>
        <div class="inner content fullwidth announcementcontent">
            @Html.Action("GetAnnouncement", "Announcement")
        </div>
    </div>

    <!-- bottom right -->
    <div class="inner facilitynews">
        <div class="title inner">
            OUR FACILITY NEWS
        </div>
        <div class="inner content">
            @Html.Action("GetFacilityNews", "FacilityMessage")
        </div>
    </div>
</div>
</div>

Here's a JS Fiddle which includes all my CSS:

https://jsfiddle.net/2caL1ost/

For the most part, none of the CSS does anything especially important; just sets padding, margins, colors etc.

When I run it, this is what I get:

enter image description here

The left half, the Bulletin Board, is displaying as desired. It's the right side that is not behaving as desired.

If I remove the inner class (postion: absolute;) then the white box "behind" the biege one on the right disappears, so I think they're simply overlapping each other?

How do you create nested divs next to each other and on top of each other in such a way that the content of one div won't shrink the other div?

Upvotes: 0

Views: 575

Answers (2)

user3589620
user3589620

Reputation:

Shortened solution with flexbox

Container: display: flex

HTML structure

<div class="container">
  <!-- Left side -->
  <div>
    <div class="box">
      <h4 class="title"></h4>
      <p>Text</p>
    </div>
  </div>
  <!-- Right side -->
  <div>
    <div class="box">
      <h4 class="title"></h4>
      <p>Text</p>
    </div>
    <div class="box">
      <h4 class="title"></h4>
      <p>Text</p>
    </div>
  </div>
</div>

In total

body {
  margin: 0;
}

.container {
  display: flex;
}

.container>div {
  flex: 0.5;
}

.box {
  padding: 10px;
}

.title {
  color: white;
  margin: 5px 0;
  padding: 5px;
}

.title.bulletin {
  background: lightgreen;
}

.title.news {
  background: red;
}

.title.announcements {
  background: lightblue;
}

@media screen and (max-width: 767px) {
  .container {
    flex-direction: column;
  }
}
<div class="container">
  <div>
    <div class="box">
      <h4 class="title bulletin">Title</h4>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure dolorem obcaecati impedit odio in velit adipisci molestias fugit, voluptatum consequatur exercitationem, veniam optio, quos dolor, corporis quo. Quidem, modi, eum?</p>
    </div>
  </div>
  <div>
    <div class="box">
      <h4 class="title news">Title</h4>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis dolorum quos facilis quisquam, at voluptatum provident, doloremque iure perspiciatis voluptates voluptas nihil reprehenderit minus tempore mollitia beatae odit! Soluta, numquam.</p>
    </div>
    <div class="box">
      <h4 class="title announcements">Title</h4>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem quo provident voluptatum, qui modi magnam, vero atque, laudantium, libero autem quae voluptate possimus inventore! Explicabo officia fuga, ex architecto et!</p>
    </div>
  </div>
</div>

Upvotes: 0

Temani Afif
Temani Afif

Reputation: 273640

I think you simply need to remove a lot of useless CSS (float, width, height, etc) from your code and you have your layout:

.bulletin {
  margin: 0px;
  margin-right: 1.2%;
}

.bulletin .title {
  color: #fff;
  font-size: 14px;
  font-weight: bold;
  text-transform: none;
  background-color: #89b907;
  font-weight: 200;
}

.bulletin .title span {
  margin-left: 10px
}

.bulletin .content {
  padding: 10px 0;
  display: inline-block;
  background: #fff;
  line-height: 28px;
  max-height: 250px;
  overflow-x: auto;
}

.bulletin .content p,
.bulletin .content>b {
  padding: 10px;
}

.bulletin hr {
  border-top: 1px dashed #8c8b8b;
}

.announcements .title {
  color: #fff;
  font-size: 12px;
  font-weight: bold;
  text-transform: none;
  margin-left: 0px;
  background-color: #099db4;
  padding-left: 0px;
  padding-right: 10px;
}

.announcements .title span {
  margin-left: 10px
}

.announcements .content {
  padding: 10px;
  background: #fff;
  line-height: 28px;
  padding-right: 0px;
  max-height: 250px;
  overflow-x: auto
}

.facilitynews {
  margin: 20px 0px
}

.facilitynews .title {
  color: #fff;
  font-size: 12px;
  font-weight: bold;
  text-transform: none;
  margin-left: 0;
  background-color: #e24242;
  padding: 8px;
}

.facilitynews .content {
  padding: 8px;
  background: #fff7da;
}
<!-- outter most div containing everything -->
<div>

  <!-- left div -->
  <div style="width:50%; float:left;">
    <div class="bulletin margin-top">
      <div class="title  ">
        <span>BULLETIN BOARD</span>
      </div>
      <div class="content">
        beep boop
      </div>
    </div>
  </div>

  <!-- rightdiv -->
  <div style="width:50%; float:left">

    <!-- top right -->
    <div class="inner announcements margin-top">
      <div class="title inner">
        <span>ANNOUNCEMENTS</span>
      </div>
      <div class="inner content fullwidth announcementcontent">
        announcement content
      </div>
    </div>

    <!-- bottom right -->
    <div class="inner facilitynews">
      <div class="title inner">
        OUR FACILITY NEWS
      </div>
      <div class="inner content">
        fac news content
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions