Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a simple layout with a sidebar in HTML & CSS

Tags:

html

css

I have been trying to make a simple HTML layout with a sidebar on the left and a container at it's right. The CSS Codes i wrote are these:

body{
    margin: 0;
    padding:0;
}
.sidebar,.content{
    background: #333;
    color: #fff;
    height: 500px;
    border-radius: 4px;
    margin: 20px;
    border:1px solid #777;
}
.sidebar{
    width: 300px;
    float: left;
    position: absolute;
}
.content{
    width: 630px;
}

And the simple HTML Follows:

<body>
  <div class="sidebar"> </div>

  <div class="content">  </div>
</body>

But It doesn't seem to work though i thought using float:left solves the problem, but it seems that the right one is going on top of another.

JSFiddle: Click here to see the JS Fiddle

like image 274
Blaise M. Avatar asked Oct 31 '25 06:10

Blaise M.


1 Answers

A modern and elegant solution to this would be to use CSS Grid.

.container {
  width: 100%;
  display: grid;
  grid-template-areas: "sidebar main";
  grid-template-columns: 200px auto;
}

.sidebar {
  grid-area: sidebar;
  height: 100vw;
  background-color: lightgray;
}

.main {
  grid-area: main;
  height: 100vw;
  background-color: gray;
}
<div class='container'>
  <div class='sidebar'>
    // sidebar content
  </div>
  <div class='main'>
    // main page content goes here
  </div>
</div>
like image 66
Nathan Fries Avatar answered Nov 02 '25 22:11

Nathan Fries