将两个DIV彼此相邻放置

时间:2019-03-19 16:48:38

标签: html css

我有一个下拉菜单,其中包含几个DIV和一个我无法在该菜单旁边显示的促销图片。我正在使用内联块;在菜单中的图像和块中,其中一个显示在另一个下方。

 .menu{
    	background-color: white;
    	color: #707070;
    	height: 50px;
    	width: 200px;
    	cursor: pointer;
    	border: 1px solid;
    	text-align: middle;
    	margin-left: 13px;
    	margin-top: 30px;
    	display: block;
    }
    
    .collapsible {
    	background-color: white;
    	display: block;
    	color: #707070;
    	height: 50px;
    	width: 200px;
    	cursor: pointer;
    	border: 1px solid;
    	text-align: middle;
    	margin-left: 13px;
    }
    
    .content {
    	display: none;
    	text-align: center;
    	overflow: hidden;
    	background-color: white;
    	color: #707070;
    	height: 50px;
    	width: 200px;
    	cursor: pointer;
    	margin-left: 13px;
    }
    
    .advert{
    	height: 240px;
    	width: 800px;
    	margin-left: 500px;
    	display: block;
    	margin-left: auto;
    	margin-right: auto;
    	float: top;
    }
<div>
    <button class="menu">MENU</button>
    	<button class="collapsible">Categories &#x2BC6;</button>
    		<div class="content">
    			<p>1</p>
    		</div>
    	<button class="collapsible">Information &#x2BC6;</button>
    		<div class="content">
    			<p>2</p>
    		</div>
    	<button class="collapsible">Contact Us &#x2BC6;</button>
    		<div class="content">
    			<p>3</p>
    		</div>
    	<button class="collapsible">Follow Us &#x2BC6;</button>
    		<div class="content">
    			<p>4</p>
    		</div>
    </div>

    <div>
    <img src="img/promo_image.png" class="advert">
    </div>

   

添加更多详细信息,以便表格不会拒绝我的问题。

2 个答案:

答案 0 :(得分:0)

我通过使用div的内联flex将其水平排列。

.inline-flex{
  display: inline-flex;
}

.menu{
    background-color: white;
    color: #707070;
    height: 50px;
    width: 200px;
    cursor: pointer;
    border: 1px solid;
    text-align: middle;
    margin-left: 13px;
    /* margin-top: 30px; */
    display: block;
    }

.collapsible {
    background-color: white;
    display: block;
    color: #707070;
    height: 50px;
    width: 200px;
    cursor: pointer;
    border: 1px solid;
    text-align: middle;
    margin-left: 13px;
}

.content {
    display: none;
    text-align: center;
    overflow: hidden;
    background-color: white;
    color: #707070;
    height: 50px;
    width: 200px;
    cursor: pointer;
    margin-left: 13px;
}

.advert{
    height: 240px;
    width: 800px;
    margin-left: 500px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    float: top;
}
<div class="inline-flex">
<button class="menu">MENU</button>
    <button class="collapsible">Categories &#x2BC6;</button>
        <div class="content">
            <p>1</p>
        </div>
    <button class="collapsible">Information &#x2BC6;</button>
        <div class="content">
            <p>2</p>
        </div>
    <button class="collapsible">Contact Us &#x2BC6;</button>
        <div class="content">
            <p>3</p>
        </div>
    <button class="collapsible">Follow Us &#x2BC6;</button>
        <div class="content">
            <p>4</p>
        </div>
</div>

<div>
<img src="img/promo_image.png" class="advert">
</div>

答案 1 :(得分:0)

据我所知float: top在CSS中不存在。 有很多方法可以满足您的需求。例如,您可以简单地使广告位置绝对:

.menu {
  background-color: white;
  color: #707070;
  height: 50px;
  width: 200px;
  cursor: pointer;
  border: 1px solid;
  text-align: middle;
  margin-left: 13px;
  margin-top: 30px;
  display: block;
}

.collapsible {
  background-color: white;
  display: block;
  color: #707070;
  height: 50px;
  width: 200px;
  cursor: pointer;
  border: 1px solid;
  text-align: middle;
  margin-left: 13px;
}

.content {
  display: none;
  text-align: center;
  overflow: hidden;
  background-color: white;
  color: #707070;
  height: 50px;
  width: 200px;
  cursor: pointer;
  margin-left: 13px;
}

.advert {
  height: 240px;
  width: 800px;
  position: absolute;
  top: 30px;
  left: 230px;
}
<div>
  <button class="menu">MENU</button>
  <button class="collapsible">Categories &#x2BC6;</button>
  <div class="content">
    <p>1</p>
  </div>
  <button class="collapsible">Information &#x2BC6;</button>
  <div class="content">
    <p>2</p>
  </div>
  <button class="collapsible">Contact Us &#x2BC6;</button>
  <div class="content">
    <p>3</p>
  </div>
  <button class="collapsible">Follow Us &#x2BC6;</button>
  <div class="content">
    <p>4</p>
  </div>
</div>

<div>
  <img src="img/promo_image.png" class="advert">
</div>

但是我建议您使用CSS网格或Flex。例如:

.grid {
  display: grid;
  grid-template-columns: 213px 1fr;
  width: 100%;
}

.menu{
    background-color: white;
    color: #707070;
    height: 50px;
    width: 200px;
    cursor: pointer;
    border: 1px solid;
    text-align: middle;
    margin-left: 13px;
    display: block;
}

.collapsible {
    background-color: white;
    display: block;
    color: #707070;
    height: 50px;
    width: 200px;
    cursor: pointer;
    border: 1px solid;
    text-align: middle;
    margin-left: 13px;
}

.content {
    display: none;
    text-align: center;
    overflow: hidden;
    background-color: white;
    color: #707070;
    height: 50px;
    width: 200px;
    cursor: pointer;
    margin-left: 13px;
}

.advert{
    height: 240px;
    width: 800px;
    margin-left: 500px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    float: top;
}
<div class="grid">
<div>
<button class="menu">MENU</button>
    <button class="collapsible">Categories &#x2BC6;</button>
        <div class="content">
            <p>1</p>
        </div>
    <button class="collapsible">Information &#x2BC6;</button>
        <div class="content">
            <p>2</p>
        </div>
    <button class="collapsible">Contact Us &#x2BC6;</button>
        <div class="content">
            <p>3</p>
        </div>
    <button class="collapsible">Follow Us &#x2BC6;</button>
        <div class="content">
            <p>4</p>
        </div>
</div>

<div>
<img src="img/promo_image.png" class="advert">
</div>
</div>