How to put two divs side by side

前端 未结 8 1092
情深已故
情深已故 2020-12-07 17:20

So I\'m quite new to writing code (about a few weeks) and I\'ve hit a wall while writing code for my website. I want to have a layout like this:

相关标签:
8条回答
  • 2020-12-07 17:54

    This is just a simple(not-responsive) HTML/CSS translation of the wireframe you provided.

    HTML

    <div class="container">
    
      <header>
        <div class="logo">Logo</div>
        <div class="menu">Email/Password</div>
      </header>
    
      <div class="first-box">
        <p>Video Explaning Site</p>
      </div>
    
      <div class="second-box">
        <p>Sign up Info</p>
      </div>
    
      <footer>
        <div>Website Info</div>
      </footer>
    
    </div>
    

    CSS

    .container {
      width:900px; 
      height: 150px;
    }
    
    header {
      width:900px; 
      float:left; 
      background: pink; 
      height: 50px;
    }
    
    .logo {
      float: left;
      padding: 15px
    }
    
    .menu {
      float: right;
      padding: 15px
    }
    
    .first-box {
      width:300px; 
      float:left; 
      background: green; 
      height: 150px;
      margin: 50px
    }
    
    .first-box p {
      color: #ffffff;
      padding-left: 80px;
      padding-top: 50px;
    }
    
    .second-box {
      width:300px; 
      height: 150px; 
      float:right; 
      background: blue;
      margin: 50px
    }
    
    .second-box p {
      color: #ffffff;
      padding-left: 110px;
      padding-top: 50px;
    }
    
    footer {
      width:900px; 
      float:left; 
      background: black; 
      height: 50px;
      color: #ffffff;
    }
    
    footer div {
      padding: 15px;
    }
    
    0 讨论(0)
  • 2020-12-07 17:55

    This will work

    <div style="width:800px;">
      <div style="width:300px; float:left;"></div>
      <div style="width:300px; float:right;"></div>
    </div>
    <div style="clear: both;"></div>
    
    0 讨论(0)
  • 2020-12-07 17:56
    <div style="display: inline">
        <div style="width:80%; display: inline-block; float:left; margin-right: 10px;"></div>
        <div style="width: 19%; display: inline-block; border: 1px solid red"></div>
    </div>
    
    0 讨论(0)
  • 2020-12-07 17:58

    Have a look at CSS and HTML in depth you will figure this out. It just floating the boxes left and right and those boxes need to be inside a same div. http://www.w3schools.com/html/html_layout.asp might be a good resource.

    0 讨论(0)
  • 2020-12-07 18:00

    http://jsfiddle.net/kkobold/qMQL5/

    #header {
        width: 100%;
        background-color: red;
        height: 30px;
    }
    
    #container {
        width: 300px;
        background-color: #ffcc33;
        margin: auto;
    }
    #first {
        width: 100px;
        float: left;
        height: 300px;
            background-color: blue;
    }
    #second {
        width: 200px;
        float: left;
        height: 300px;
        background-color: green;
    }
    #clear {
        clear: both;
    }
    <div id="header"></div>
    <div id="container">
        <div id="first"></div>
        <div id="second"></div>
        <div id="clear"></div>
    </div>

    0 讨论(0)
  • 2020-12-07 18:02

    I am just giving the code for two responsive divs side by side

    *{
      margin: 0;
      padding: 0;
    }
    
    #parent {
      display: flex;
      justify-content: space-around;
    }
    
    #left {
      border: 1px solid lightgray;
      background-color: red;
      width: 40%;
    }
    
    #right {
      border: 1px solid lightgray;
      background-color: green;
      width: 40%;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <div id="parent">
            <div id="left">
            lorem ipsum dolor sit emet
            </div>
            <div id="right">
            lorem ipsum dolor sit emet
            </div>
        </div>
    </body>
    </html>

    0 讨论(0)
提交回复
热议问题