如何将2个div彼此相邻放置

时间:2019-07-02 13:00:59

标签: html css

我尝试将2个div彼此相邻放置,但不知何故将它们放置在彼此的下方。

我已经在这个论坛上查找并复制了CSS代码,但是仍然无法正常工作,我认为这与我在其中使用的php代码有关。

<html>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
 <header>
<nav>
    <p><a href="index.php"><img src="img/handboeklogo.png"></a></p>
    <ul>
        <li><a href="index.php">Home</a></li>
        <li><a href="#">Checklist</a>
            <ul>
                <li><a href="#">Desktops</a></li>
                <li><a href="#">Laptops</a></li>
                <li><a href="#">Monitoren</a></li>
            </ul>
        </li>
        <li><a href="#">Handboek</a>
            <ul>
                <li><a href="#">Desktops</a></li>
                <li><a href="#">Laptops</a></li>
                <li><a href="#">Monitoren</a></li>
                <li><a href="#">Printers</a></li>
                <li><a href="#">Mobiele Telefoons</a></li>
            </ul>
        </li>
        <li><a href="#">Classificatielijst</a><ul>
                <li><a href="#">Desktops</a></li>
                <li><a href="#">Laptops</a></li>
                <li><a href="#">Monitoren</a></li>
                <li><a href="#">Printers</a></li>
                <li><a href="#">Mobiele Telefoons</a></li>
            </ul>
        </li>


<form class='logout' method='POST' action=''>
        <button type='submit' name='logoutSubmit'>Logout</button>
    </form>
</nav>
</header>
<br><br>


<div id='maindiv'><div id='first'>





<form method='POST' action=''>
            <input type='hidden' name='uid' value='1'> 
            <input type='hidden' name='date' value='2019-07-02'> 
            <textarea name='message'></textarea><br>
            <button type='submit' name='commentSubmit'> Comment </button>
        </form></div>

<div id='second'>



<form method='POST' action=''>
            <input type='hidden' name='uid' value='1'> 
            <input type='hidden' name='date' value='2019-07-02'> 
            <textarea name='message'></textarea><br>
            <button type='submit' name='Generalcommentsubmit'> Comment </button>
        </form></div></div>
</body>
</html>

CSS代码:

body {
    background-color: #edf0f2;
}

textarea {
    display: inline-block;
    width: 630px;
    height: 120px;
    background-color: #fff;
    border-color: #5bb112;
    margin-top: 10px;
    margin-left: 15px;
    position: relative;
    resize: none;
}

.Changelog {
    margin-left: 15px;
    width: 800px;
}

.logout {
    display: inline-block;
    float: right;
    position: absolute;
    margin-top: 27px;
    margin-right: 20px;
    margin-left: 680px;
}

.login {
    display: inline-block;
    float: right;
    position: absolute;
    margin-top: 27px;
    margin-right: 20px;
    margin-left: 330px;
}
/*
button {
    width: 100px;
    height: 30px;
    background-color: #5bb112;
    border: none;
    color: #e2e2e2;
    font-family: arial;
    font-weight: 400;
    cursor: pointer;
    margin-bottom: 19px;
    margin-left: 15px;
    margin-top: 3px;
}

.comment-box {
    width: 630px;
    padding-top: 5px;
    margin-bottom: 4px;
    background-color: #fff;
    border-radius: 1px;
    position: absolute;
    border: 1px solid #5bb112;
    margin-top: 8px;
    align-content: left;
}

.comment-box p {
    font-family: arial;
    font-size: 14px;
    line-height: 16px;
    color: #282828;
    font-weight: 100;
    margin-bottom: 10px;
    margin-left: 5px;
}

.edit-form {
    position: absolute;
    top: 3px;
    right: 3px;
}

.edit-form button {
    width: 40px;
    height: 20px;
    color: #282828;
    background-color: #fff;
    opacity: 0.7;
}

.edit-form button:hover {
    opacity: 1;
}

.delete-form {
    position: absolute;
    top: 3px;
    right: 58px;
}

.delete-form button {
    width: 40px;
    height: 20px;
    color: #282828;
    background-color: #fff;
    opacity: 0.7;
}

.delete-form button:hover {
    opacity: 1;
}*/

* {
    margin: 0;
    padding: 0;
}

header nav {
    width: 100%;
    height: 80px;
    background-color: #343131;
}

header nav p {
    font-family: arial;
    color: #b3b3b3;
    font-size: 24px;
    line-height: 55px;
    float: left;
    padding: 10px 20px;
}

header nav ul {
    float: left;
    z-index: 2;
}

header nav ul li {
    float: left;
    list-style: none;
    position: relative;
    margin-top: 15px;
    margin-right: auto;
    margin-left: 30px;
}

header nav ul li a {
    display: block;
    font-family: arial;
    color: #b3b3b3;
    font-size: 20px;
    padding: 21px 14px;
    text-decoration: none;
}

header nav ul li ul {
    display: none;
    position: absolute;
    background-color: #343131;
    border-radius: 0px 0px 4px 4px;
    padding: 8px;
}

header nav ul li:hover ul {
    display: block;
}

header nav ul li ul li  {
    width: 180px;
    border-radius: 4px;
}

header nav ul li ul li a {
    padding: 9px 14px;
}

header nav ul li ul li a:hover {
    background-color: #343131;
}

header nav p img {
    width: 210px;
    height: 55px;
    margin-top: 5px;
    margin-bottom: 5px;
}


header nav ul li a:hover {opacity: 1}

header nav ul li a {
    background-color: #343131;
    border: none;
    color: white;
    opacity: 0.6;
    transition: 0.3s;
}


.down {
  transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
}

i {
  border: solid white;
  border-width: 0 3px 3px 0;
  display: inline-block;
  padding: 3px;
  margin-left: 10px;
  margin-bottom: 3px;
  }

/*-------Changelog---------*/

#maindiv {
    display: flex;
    width: auto;
    border: 1px solid black;
    overflow: hidden;
}
#first {
    width: auto;
    float:left;

}
#second {
    float: left; 
}


我想将div彼此相邻放置,以便在彼此之间有2条注释区域。

4 个答案:

答案 0 :(得分:1)

要在彼此附近放置2个div,请将它们包裹在另一个div中,并将显示设置为“ flex”

<div id="maindiv'>
   <div id='first'>
      //content here
   </div>
   <div id='second'>
      //content here
   </div>
</div>

在CSS

#maindiv {
  display: flex;
  width: 500px;
  border: 1px solid black;
  overflow: hidden;
}
#first {
  width: 300px;
}
#second {

} 

答案 1 :(得分:0)

从您当前的代码中,我只能猜测显示问题,请尝试

#maindiv {
    display: inline-block;
    width: 500px;
    border: 1px solid black;
    overflow: hidden;
}

答案 2 :(得分:0)

您可以使用display:inline-block,因为div元素display默认设置为block

.parent {
  background: red;
  height: 50px;
  width: 50px;
  margin: 10px;
  display: inline-block
}
<div class="parent">

</div>
<div class="parent">

</div>
<div class="parent">

</div>
<div class="parent">

</div>
<div class="parent">

</div>
<div class="parent">

</div>
<div class="parent">

</div>
<div class="parent">

</div>

答案 3 :(得分:0)

#first, #second, #maindiv{
  display: inline-block;
}
#maindiv {
   width: 500px;
}
#first {
  width: 300px;
}
<div id='maindiv'>
  <div id='first'>first</div>
  <div id='second'>second</div>
</div>