如何创建一个元素向右浮动而另一个元素向左浮动的行

时间:2019-12-04 04:52:34

标签: html css

我试图让一个元素是左边的图像,右边的文本,但都在同一行中。当我尝试几种不同的方式时,有时我在不调整大小的情况下图像和文本会保留在图像的左侧。我在row_2元素中添加了背景色,并且该元素与图像或文本未对齐,该元素位于底部约200 px,图像和文本位于上方,可以得到一些帮助?

	#row_2{
		width: 60%;
		height: 35%;
		background-color: aqua;
	}

	#text_container2{
		position: relative;
		float: right;
		bottom: 25%;
		width: 800px;
		height: 100px;
	}
  
	#container_2{
		position: relative;
		margin-left: 10%;
		float: left; 
		width: 50%;
		height: 50%;
	}
  
  #balle_size{
		height: 100%;
		width: 100%;
	}
		<div id="row_2">
			<div id="container_2">
				<img src="https://hgtvhome.sndimg.com/content/dam/images/hgtv/stock/2018/2/27/0/iStock-purple-geraniums-542945456.jpg.rend.hgtvcom.966.725.suffix/1519754224444.jpeg" 	alt="image" id="balle_size">
			</div>

			<div id="text_container2">
				<p>words words more words and words </p>
			</div>
		</div>

2 个答案:

答案 0 :(得分:0)

尝试以下CSS代码。希望它对您有帮助。

#row_2{
    width: 60%;
    height: 35%;
    background-color: aqua;
    display: inline-block;
}

#text_container2{
    float: right;
    width: 50%;
}

#container_2{
    float: left;
    width: 50%; 
}

#balle_size{
    height: 100%;
    width: 100%;
}

答案 1 :(得分:0)

使用flexbox可以实现这一点(跨浏览器具有良好的兼容性,并且响应迅速)

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
}

.flex-item {
  background: tomato;
  padding: 5px;
  width: 200px;
  height: 150px;
  margin-top: 10px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
}
<ul class="flex-container">
  <li class="flex-item"><img src="https://hgtvhome.sndimg.com/content/dam/images/hgtv/stock/2018/2/27/0/iStock-purple-geraniums-542945456.jpg.rend.hgtvcom.966.725.suffix/1519754224444.jpeg" 	alt="image" id="balle_size" width="100%"></li>
  <li class="flex-item">Some text</li>
</ul>