Dan Rais
Dan Rais

Reputation: 319

How can i make position divs like this in css?

I hope you're well! I am a bit new in writing css and I would like to achieve this result, but how? enter image description here

Here is my try and I will be grateful for any advice and explanations.

<style>
#wrapper {
   width: auto;
    height: auto;
    box-sizing: border-box;
    display: grid;
    grid-auto-flow: row;
    grid-template-columns: repeat(6, 1fr);
}

#left{
    text-align: left;
    grid-column: 1/4;
    margin-top: 2px;
    background-color: red;
    height: 200px;
}

#right {
    text-align: right;
    height: 100px;
    padding-top: 10px;
    margin-top: 2px;
    grid-column: 4/6;
    background-color: blue;
    margin-left: 10px;
}
</style>
</head>
<body>

<div id="wrapper">
<div id="left" > div1 </div>
<div id="right" style="height:50px;"> div2 </div>
<div id="right" > div3 </div>
<div id="right" style="height:50px;"> div4 </div>
</div>

</body>

Upvotes: 0

Views: 42

Answers (1)

ray
ray

Reputation: 27275

I think it's generally useful to think of these kinds of things as big blocks that contain smaller blocks. From that perspective you first have a layout with two columns, which you can achieve with a couple of simple grid or flex rules. There are many ways to do this, but here it is using grid:

.main-layout {
  display: grid;
  grid-template-columns: .35fr 1fr;
  gap: 1rem;
  min-height: 70vh;
}

.sidebar {
  padding: 1rem;
  background: skyblue;
}

.content {
  padding: 1rem;
  background: aliceblue;
}
<div class="main-layout">
  <div class="sidebar">sidebar</div>
  <div class="content">content</div>
</div>

Once you have that, you can fill in the "content" block without thinking much about the outer layout. This makes it pretty straightforward:

section {
  padding: 0.5em;
  margin-bottom: 1rem;
  background: lightblue;
}

h1 {
  font-size: 1.2rem;
  margin: 0;
  border-bottom: 1px solid white;
}

/* outer layout -- same as before */
.main-layout {
  display: grid;
  grid-template-columns: .35fr 1fr;
  gap: 1rem;
  min-height: 70vh;
}

.sidebar {
  padding: 1rem;
  background: skyblue;
}

.content {
  padding: 1rem;
  background: aliceblue;
}
<div class="main-layout">
  <div class="sidebar">sidebar</div>
  <div class="content">
    <section>
      <h1>This is a heading</h1>
      <p>And this is content</p>
    </section>
    <section>
      <h1>This is a heading</h1>
      <p>And this is content</p>
    </section>
    <section>
      <h1>This is a heading</h1>
      <p>And this is content</p>
    </section>    
  </div>
</div>

Upvotes: 3

Related Questions