2015-09-08 358 views
1

我正在學習前端網頁設計,並且正在嘗試構建一個類似靜態頁面的完全響應式博客。爲什麼段落不會在HTML中與圖像對齊?

但我的問題是圖像沒有正確對齊段落。 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-->

+0

你能不能上傳圖片,通過編輯你的問題,並使用所產生的鏈接而不是'img''src'?它只是讓我們看到,你看到的更清楚。 – jaunt

+1

@jaunt我上傳了圖片:) –

回答

2

floatting元件應該先/提前在代碼從元件在自然通量。

看到(除其他鏈接,您可以找到關於搜索引擎)https://css-tricks.com/all-about-floats/

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> 
 
      <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-->

+0

非常感謝你的回答:)但是,你能否給出一些見解推理,爲什麼這應該首先在HTML呈現方式上有什麼不同? –

+1

@AnkurAnand。浮動元件從流量中被排出到右側或左側。如果在它不是一個浮動元素之前,它首先打破這條線,否則只要它有空間就擱置。元素在非浮動狀態下站立後會在其下面展開,只有內容會被推動(它被包裹在旁邊和下方)。如果你觸發佈局,它會放在一邊或在沒有足夠空間的情況下看到,以便更好地理解:https://css-tricks.com/all-about-floats/ –

0

每個HTML元素得到了限定元素是如何顯示的顯示值。大多數元素的這個值是塊或內聯的;塊元素總是從新行開始,佔用全部可用寬度,而行內元素保持在同一行上,並且只佔用他們需要的空間量。 <p>元素屬於塊元素類,而<img>元素屬於內聯元素。

爲了進一步解釋和示例採取這裏看看W3C School - HTML Block and Inline Elements