2017-02-25 62 views
0

下面是代碼,有解決方案可用於解決我真正感興趣的是爲什麼會發生,什麼是幕後?HTML和CSS:圖像浮動與下面的ul重疊 - 爲什麼

<!DOCTYPE html> 

<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title>Demo CSS3</title> 
     <style> 
     p 
     { 
      color: #000000; 
      font-size: medium; 
      font-family: 'Segoe UI'; 
     } 
     h1 
     { 
      color: #ff0000; 
      font-size: x-large; 
      font-family: 'Segoe UI'; 
     } 
     h2 
     { 
      color: #0000ff; 
      font-size: larger; 
      font-family: 'Segoe UI'; 
     } 

     footer p 
     { 
      font-size: x-small; 
     } 
     .entry-content :first-child {color:brown} 
     ul :nth-child(odd) {background-color : yellow } 
     ul :nth-child(even) {background-color : blue} 
     .aside-body p:nth-of-type(odd) {background-color : yellow } 
     .aside-body p:nth-of-type(even) {background-color : pink } 

     .wholePage{ 
      font-family: 'Segoe UI', Arial, Helvetica, san-serif; 
      font-size: 18px; 
      color: #0000ff; 
      width: 700px; 
      margin: 5 auto; 
     } 
     .aside{ 
      float:right; 
      width:170px; 
      margin-top:50px; 

     } 
     .mainsection{ 
      margin:180px; 
      background:gray; 
     } 
     .mainsection img{ 
      float:right; 
      width:150px; 
      height:150px; 
      margin:5px; 

     } 



     </style> 
    </head> 
    <body class="wholePage"> 
     <aside class="aside"> 
      <section class="aside-header"> 
      <h2>About HTML5</h2> 
      </section> 

      <section class="aside-body"> 
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
      Fusce porta lorem rhoncus, rhoncus orci sed, auctor nisl. 
      Etiam rutrum risus tellus, eu interdum tortor porttitor ut. 
      Praesent et nisl tincidunt lorem suscipit laoreet vel non turpis. 
      Vivamus hendrerit vel neque at varius. 
      Donec tempus, sapien at fermentum pulvinar, orci mauris tempor dolor, 
      non mattis magna lectus et augue. 
      Vivamus ut diam enim. Fusce nec dui arcu. 
      Duis luctus odio ligula, vitae pharetra nisi cursus ut. 
      Nam id neque pulvinar tortor laoreet laoreet sed at sem.</p> 
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
      Fusce porta lorem rhoncus, rhoncus orci sed, auctor nisl. 
      Etiam rutrum risus tellus, eu interdum tortor porttitor ut. 
      Praesent et nisl tincidunt lorem suscipit laoreet vel non turpis. 
      Vivamus hendrerit vel neque at varius. 
      Donec tempus, sapien at fermentum pulvinar, orci mauris tempor dolor, 
      non mattis magna lectus et augue. 
      Vivamus ut diam enim. Fusce nec dui arcu. 
      Duis luctus odio ligula, vitae pharetra nisi cursus ut. 
      Nam id neque pulvinar tortor laoreet laoreet sed at sem.</p> 
      </section> 
     </aside> 
     <div class="mainsection"> 
     <article id="post-5946"> 
      <section id="post-5946-header"> 
        <h1 class="entry-title">8th Pluralsight Course: 
        HTML5 From Scratch</h1> 
        <div class="entry-meta"> 
         <span class="meta-prep">Posted on July 14, 2013</span> 
        <span class="meta-sep">by</span> 
        <span class="author">Jesse Liberty</span>   
       </div><!-- .entry-meta --> 
      </section> 
      <section class="entry-content"> 
       <p> 
        <a href="http://blog.pluralsight.com/2013/07/10/new-course-html5-from-scratch/"> 
        HTML5 From Scratch</a> 

        <img alt="html5" title="html5" src="http://jesseliberty.com/wp-content/uploads/2013/07/html5_thumb.jpg" width="134" height="136"/> 
        Very proud to announce my latest Pluralsight Course: 
       </p> 
       <p><a href="http://www.html5rocks.com/en/">HTML5f</a> 
       is the markup language of the web that has taken the world by storm. 
        In this 「from scratch」 course, 
        I demonstrate all you need to know about what’s new in HTML5 to get started 
       creating world-class web sites 
       </p> 
       <p> 
       asdf 
       <a href="http://blog.pluralsight.com/2013/07/10/new-course-html5-from-scratch/"> 
        HTML5 From Scratch</a> 
       sdf 
       </p> 
       <p> 
        <a href="http://blog.pluralsight.com/2013/07/10/new-course-html5-from-scratch/"> 
        HTML5 From Scratch</a> 

        <img alt="html5" title="html5" src="http://jesseliberty.com/wp-content/uploads/2013/07/html5_thumb.jpg" width="134" height="136"/> 
        Very proud to announce my latest Pluralsight Course: 
       </p> 
       <p>here you go!</p> 
       <ul> 
        <li>option 1</li> 
        <li>asdfasf</li> 
        <li>sdfasdfa</li> 
        <li>asdfasdf</li> 
        <li>asdfasf</li> 
       </ul> 
      </section> 

     </article> 
     </div> 
    <footer class="footer"> 
     <p>Copyright 2013 Jesse Liberty</p> 
    </footer> 
    </body> 
</html> 

我已經看了很多帖子,每個人都似乎有自己的變通,但沒有人解釋爲什麼會出現這種情況,我是這樣做的真正的方法是什麼?

回答

0

任何與定位具有效果,所以我不會用浮子或位置爲此

- 試穿父用 - 顯示:撓曲;撓曲纏繞:包裝;寬度:100%; - 並設置具有指定寬度的孩子,例如。寬度:55%;

什麼是你想要彼此相鄰的帶寬必須是100%或更少,100% 因此,如果容器左側的寬度爲40%,圖像的60%圖像轉到旁邊其它內容沒有覆蓋它 -

現在非內容的覆蓋別的

0

嘗試.entry-content p { overflow: hidden; }(即含有浮動元素的元素被強制這種方式也包是裏面的花車)

0

向UL添加float屬性(右側或左側)和100%寬度。看小提琴。

HTML:

 <ul class="floatme"> 
      <li>option 1</li> 
      <li>asdfasf</li> 
      <li>sdfasdfa</li> 
      <li>asdfasdf</li> 
      <li>asdfasf</li> 
    </ul> 

CSS:

ul.floatme { float: left; 
    width: 100%; 
} 

https://jsfiddle.net/mediaguru/8gk7nkyp/

0

我相信Luke帶來了答案,在這個其他#1回答Which is the load, rendering and execution order of elements in a HTML page?你的問題。

他把它像這樣在他的回答:

...

  • 開始HTML解析

  • 開始下載外部文件(JavaScript,CSS,圖片),因爲它們遇到HTML

  • 解析外部文件一旦被下載(或如果他們是內聯,不需要下載)

    • ...
    • 如果他們的圖像,然後顯示他們
  • ...

  • 結束HTML解析
  • 因此,瀏覽器首先渲染頁的正文部分和僅在之後繪製的圖像(吸引過來上的文本的頂部)。

    我肯定會推薦查看盧克的答案,因爲它比我的信息量更高,描述性更強。

    0

    發生這種情況的原因是<img><p>標記內浮動,因此<p>標記不考慮圖像高度。你沒有清理浮球。

    p {overflow: auto} 
    

    您可以設置溢出爲<p>自動清除浮動。

    檢查jsfiddle

    檢查本文https://css-tricks.com/all-about-floats/