为什么段落不在HTML中的图像旁边对齐?

时间:2015-09-08 18:24:30

标签: html css css3

我正在学习前端网页设计,我正在尝试建立一个像静态页面一样的完全响应式博客。

但我的问题是图像与段落没有正确对齐。 enter image description here

作为“不要忘记跟进!”图像本来应该是第一篇文章内容,只是不要忘记跟进应该是内容什么是Lorem Ipsum ..但它出现的地方是它来自。

我的问题是为什么会发生这种情况?我在这里看到How to float paragraph next to image without wrapping the image?的解决方法,但我没有理解这个问题实际发生的原因。

body {
  background-color: #DCDBD9;
  color: #2C2C2C;
  font: normal 100% Cambria, Georgia, serif;
}
h1 {
  font-size: 1.5em;
  font-style: italic;
  font-weight: normal;
}
h1 a {
  color: #747474;
  font: bold 0.45833em Calibri, Optima, Arial, sans-serif;
  letter-spacing: 0.15em;
  text-transform: : uppercase;
  text-decoration: none;
}
#page {
  margin: 36px auto;
  width: 90%;
}
#nav {
  float: right;
  /*padding: 42px 0 0 30px;*/
}
#nav li {
  float: left;
  margin: 0 0 0 5px;
}
.blog {
  clear: both;
  margin: 50px auto 53px;
  width: 93.75%;
}
.blog .main {
  float: left;
  width: 62.88%;
  /* 566px / 900px */
}
.blog .other {
  float: right;
  width: 36.77%;
  /* 331px / 900px */
}
.lede {
  padding: 0.8em 5.333%;
}
.recent-entries {
  margin: 0 auto;
  width: 69.788%;
  /* 231 / 331px */
}
.article {
  padding: 40px 8.480%;
}
.date {
  float: left;
  margin-left: -17.088%;
  /* 81px / 474px */
  width: 14.55%;
  /* 69px / 474px */
  border: 1px solid black;
  border-radius: 2px;
  padding: 0.59%;
  transition-property: background;
  transition-duration: 0.3s;
  transition-timing-function: ease;
}
.date:hover {
  background: #9c3;
}
img,
embed,
object,
video {
  max-width: 100%;
}
.figure {
  float: right;
  margin-bottom: 0.5em;
  margin-left: 2.531%;
  /* 12px / 474px */
  width: 48.734%;
  /* 231px / 474px */
}
h2 {
  color: #F90B6D;
  font-family: 'Open Sans', sans-serif;
  font-size: 1.2em;
  font-weight: 300;
}
p {
  color: #222;
  font-family: 'Open Sans', sans-serif;
  font-size: 0.9em;
  font-weight: 400;
}
<!--  <h1> This is the top header! <a href="#"> Read More </a> </h1> -->

<div id="page">

  <ul id="nav">
    <li><a href="#">About me</a>
    </li>
    <li><a href="#">About Blog</a>
    </li>
    <li><a href="#">Stuff</a>
    </li>
    <li><a href="#">Junk Stuff</a>
    </li>
  </ul>
  <div class="blog section">
    <h1 class="lede">Responsive Blog </h1>
    <div class="main">

      <div class="article">
        <div>
          <div class="date">
            1 Sept 2015
          </div>
          <h2> What is Lorem Ipsum ?</h2>
          <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages , and more recently
            with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

          <div class="figure">
            <p>
              <img src="http://i.stack.imgur.com/I3hYQ.png" alt="">
              <b class="figcaption">Remember This</b>
            </p>
          </div>
        </div>

        <div class="date">
          5 Sept 2015
        </div>
        <h2> Where does it comes from ? </h2>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
          up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
          Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from
          a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original
          form, accompanied by English versions from the 1914 translation by H. Rackham.</p>


      </div>
    </div>
    <!--/end.main -->
    <div class="other">
      <p>Recent Enteries</p>
      <div class="recent-entries">
        <p>1.Simply First Entry</p>
        <p>2. Simply second Entry</p>
      </div>
    </div>
    <!-- /end .other -->
  </div>
  <!--/end .blog.section -->
</div>
<!-- /end #page-->

2 个答案:

答案 0 :(得分:2)

浮动元素应该来自自然通量中元素的代码中的第一个/前面。

请参阅(您可以在搜索引擎上找到的其他链接)https://css-tricks.com/all-about-floats/

&#13;
&#13;
body {
  background-color: #DCDBD9;
  color: #2C2C2C;
  font: normal 100% Cambria, Georgia, serif;
}
h1 {
  font-size: 1.5em;
  font-style: italic;
  font-weight: normal;
}
h1 a {
  color: #747474;
  font: bold 0.45833em Calibri, Optima, Arial, sans-serif;
  letter-spacing: 0.15em;
  text-transform: : uppercase;
  text-decoration: none;
}
#page {
  margin: 36px auto;
  width: 90%;
}
#nav {
  float: right;
  /*padding: 42px 0 0 30px;*/
}
#nav li {
  float: left;
  margin: 0 0 0 5px;
}
.blog {
  clear: both;
  margin: 50px auto 53px;
  width: 93.75%;
}
.blog .main {
  float: left;
  width: 62.88%;
  /* 566px / 900px */
}
.blog .other {
  float: right;
  width: 36.77%;
  /* 331px / 900px */
}
.lede {
  padding: 0.8em 5.333%;
}
.recent-entries {
  margin: 0 auto;
  width: 69.788%;
  /* 231 / 331px */
}
.article {
  padding: 40px 8.480%;
}
.date {
  float: left;
  margin-left: -17.088%;
  /* 81px / 474px */
  width: 14.55%;
  /* 69px / 474px */
  border: 1px solid black;
  border-radius: 2px;
  padding: 0.59%;
  transition-property: background;
  transition-duration: 0.3s;
  transition-timing-function: ease;
}
.date:hover {
  background: #9c3;
}
img,
embed,
object,
video {
  max-width: 100%;
}
.figure {
  float: right;
  margin-bottom: 0.5em;
  margin-left: 2.531%;
  /* 12px / 474px */
  width: 48.734%;
  /* 231px / 474px */
}
h2 {
  color: #F90B6D;
  font-family: 'Open Sans', sans-serif;
  font-size: 1.2em;
  font-weight: 300;
}
p {
  color: #222;
  font-family: 'Open Sans', sans-serif;
  font-size: 0.9em;
  font-weight: 400;
}
&#13;
<!--  <h1> This is the top header! <a href="#"> Read More </a> </h1> -->

<div id="page">

  <ul id="nav">
    <li><a href="#">About me</a>
    </li>
    <li><a href="#">About Blog</a>
    </li>
    <li><a href="#">Stuff</a>
    </li>
    <li><a href="#">Junk Stuff</a>
    </li>
  </ul>
  <div class="blog section">
    <h1 class="lede">Responsive Blog </h1>
    <div class="main">

      <div class="article">
        <div>
          <div class="date">
            1 Sept 2015
          </div>
          <div class="figure">
            <p>
              <img src="http://dummyimage.com/290x400&text=Dont-Forget-To-Follow-Up" alt="">
              <b class="figcaption">Remember This</b>
            </p>
          </div>
          <h2> What is Lorem Ipsum ?</h2>
          <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages , and more recently
            with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

          
        </div>

        <div class="date">
          5 Sept 2015
        </div>
        <h2> Where does it comes from ? </h2>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
          up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
          Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from
          a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original
          form, accompanied by English versions from the 1914 translation by H. Rackham.</p>


      </div>
    </div>
    <!--/end.main -->
    <div class="other">
      <p>Recent Enteries</p>
      <div class="recent-entries">
        <p>1.Simply First Entry</p>
        <p>2. Simply second Entry</p>
      </div>
    </div>
    <!-- /end .other -->
  </div>
  <!--/end .blog.section -->
</div>
<!-- /end #page-->
&#13;
&#13;
&#13;

答案 1 :(得分:0)

每个HTML元素都有一个显示值,用于定义元素的显示方式。大多数元素的值是块或内联;块元素始终从一个新行开始并占据整个可用宽度,而内联元素保持在同一行,并且只占用他们需要的空间量。 <p>元素属于块元素类,而<img>元素属于内联元素。

有关详细说明和示例,请查看W3C School - HTML Block and Inline Elements