html - 即使向左浮动,元素也会向右移动

标签 html css

我是网络开发新手,我一生都无法弄清楚我的代码有什么问题。我想要一个长元素,在其左侧下方有一个短元素。然后我想要两个短元素与长元素并排。

所以为了给你画一张非常简单的 map ,这就是我想要的: 我所追求的图表

enter image description here

即使我将较大的元素和较小的元素设置为向左浮动,仍然有三个元素向右浮动。就像我说的,我对此很陌生,所以这可能很愚蠢,但如果有人可以提供帮助,那就太好了

fiddle :https://jsfiddle.net/es09vLc0/

HTML

html {
  background-color:black;
}
.blogPostsContainer {
  width:100%;
  border:solid;
  margin-top:100px;
}
.blogPost {
  display:inline-block;
  background-color:black;
  width:49%;
  border:solid;
  border-color:#FFF010;
  border-radius:1px;
  color:white;
  margin-top:10px;
  height:240px;
  overflow:hidden;
}
.blogPost h3 {
  color:white;
}
.blogPost h1 {    
  text-align:center;
  border-color:dodgerblue;
  border:solid;
  border-left:none;
  border-right:none;
}
.blogPost img {
  width:95%;
  height:200px;
  margin:10px;
}
<div class="BlogPostsContainer">
  <div class="blogPost"  style="height:700px; float:left">
    <img src="home_page/images/test/hqdefault-1.jpg" />
    <h1>This is the Title section</h1>
    <h3>This is the description.This is the description.This is the description.This is the description.
      This is the description.This is the description.This is the description.This is the description.
      This is the description.This is the description.This is the description.This is the description.
      This is the description.This is the description.This is the description.This is the description.
    </h3>
  </div>
  <div class="blogPost" style="float:right">
    <img src="home_page/images/test/hqdefault-2.jpg" />
    <h1>DESCRIPT</h1>
    <h3>THIS IS DESCRIPTIONNNN
    </h3>
  </div>
  <div class="blogPost" style="float:left">
    <img src="home_page/images/test/hqdefault-3.jpg" />
    <h1>This is the Title section</h1>
    <h3>This is the description.This is the description.This is the description.This is the description.
      This is the description.This is the description.This is the description.This is the description.
      This is the description.This is the description.This is the description.This is the description.
      This is the description.This is the description.This is the description.This is the description.
    </h3>
  </div>
  <div class="blogPost" style="float:right">
    <img src="home_page/images/test/hqdefault-3.jpg" />
    <h1>This is the Title section</h1>
    <h3>This is the description.This is the description.This is the description.This is the description.
      This is the description.This is the description.This is the description.This is the description.
      This is the description.This is the description.This is the description.This is the description.
      This is the description.This is the description.This is the description.This is the description.
    </h3>
  </div>
</div>

最佳答案

我将带有 float: left 的框移动到末尾,并使用 css clear: Both 添加了一个名为“clear”的 div。

html {
  background-color: black;
}
.blogPostsContainer {
  width: 100%;
  border: solid;
  margin-top: 100px;
}
.blogPost {
  display: inline-block;
  background-color: black;
  width: 49%;
  border: solid;
  border-color: #FFF010;
  border-radius: 1px;
  color: white;
  margin-top: 10px;
  height: 240px;
  overflow: hidden;
}
.blogPost h3 {
  color: white;
}
.blogPost h1 {
  text-align: center;
  border-color: dodgerblue;
  border: solid;
  border-left: none;
  border-right: none;
}
.blogPost img {
  width: 95%;
  height: 200px;
  margin: 10px;
}
.clear {
  clear: both;
}
<div class="BlogPostsContainer">
  <div class="blogPost" style="height:700px; float:left">
    <img src="home_page/images/test/hqdefault-1.jpg" />
    <h1>This is the Title section</h1>
    <h3>This is the description.This is the description.This is the description.This is the description.
                This is the description.This is the description.This is the description.This is the description.
                This is the description.This is the description.This is the description.This is the description.
                This is the description.This is the description.This is the description.This is the description.
            </h3>
  </div>
  <div class="blogPost" style="float:right">
    <img src="home_page/images/test/hqdefault-2.jpg" />
    <h1>DESCRIPT</h1>
    <h3>THIS IS DESCRIPTIONNNN
            </h3>
  </div>
  <div class="blogPost" style="float:right">
    <img src="home_page/images/test/hqdefault-3.jpg" />
    <h1>This is the Title section</h1>
    <h3>This is the description.This is the description.This is the description.This is the description.
                This is the description.This is the description.This is the description.This is the description.
                This is the description.This is the description.This is the description.This is the description.
                This is the description.This is the description.This is the description.This is the description.
            </h3>
  </div>
  <div class="clear"></div>
  <div class="blogPost" style="float:left">
    <img src="home_page/images/test/hqdefault-3.jpg" />
    <h1>This is the Title section</h1>
    <h3>This is the description.This is the description.This is the description.This is the description.
                This is the description.This is the description.This is the description.This is the description.
                This is the description.This is the description.This is the description.This is the description.
                This is the description.This is the description.This is the description.This is the description.
            </h3>
  </div>
</div>

关于html - 即使向左浮动,元素也会向右移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41712354/

相关文章:

html - 在固定高度的容器中垂直居中元素

html - 修复 CSS 右边框和左边框问题

javascript - 根据滚动期间当前可见的内容突出显示索引中的元素

jquery - 动画 CSS 生成 :after content?

javascript - JQUERY:为什么向下滚动时我的导航栏不固定在顶部?

javascript - 弹出模态窗体

css - 如何为css/js文件设置缓存

css - 如何将图像底部对齐到 h1?

javascript - 使用 JavaScript Var 作为 JS 和 HTML 的链接

html - 在页面上均匀居中 3 个图像