Web front-end development learning----5. Understanding CSS Floating Float

Source: Internet
Author: User

First you need to understand two concepts: inline elements, block elements.

Inline elements: Also called inline elements, which can hold text or other inline elements. The height width of an inline element is not set, and its width is the width of its own text or picture, and does not wrap. Common inline elements: <span></span>, <a></a>, </img>, <font></font>, < Strong></strong>, etc.

Block elements: can hold text, other inline elements, and block elements. You can set its height width. Repulsion is on the same line as other elements. Common block elements:

Then there is a problem, if we set up 3 div blocks, it will be arranged vertically according to the order in which the HTML documents are written.

The block element wraps automatically, and if we want it to be displayed in a row, how do we fix it? Of course, there are more ways than one. Today talk about the most common setup method, floating.

If the block element is set to float, it will float to the parent element's bounding box to the left or right as we specify, and it will not be in the standard flow (writing order) of the HTML document. Thus, the positioning of block elements is achieved.

For example, if we set the box 1 to the right float (float:lright), it will move out of the standard flow and float to the upper-right corner of the border. Because you are out of the standard stream, you can treat it as if it does not exist. Then box 2, Box 3 will move up. As shown in the following:

Note: If the inclusion box is too narrow to accommodate three floating elements arranged horizontally, the other floating blocks move down until there is enough space. If the height of the floating elements is different, they may be "stuck" by other floating elements when they move down.

So if we want 3 boxes to be displayed on the same line, we need to set them all to the left float and have the appropriate size of the parent element border.

Give a common case, this is the NetEase picture News in the tab bar,

The top label needs to use JavaScript, I will be learning behind, today only need to implement the news picture and the following link, note: When the mouse moves to the bottom of the link is, the font changed to orange, and there are underscores appear.

Effect:

Full code

[HTML]View Plaincopy
  1. <head>
  2. <meta http-equiv= "content-type" content="Text/html;charset=utf-8">
  3. <title> NetEase Social news </title>
  4. <style type="Text/css">
  5. body,ul,li,p{margin:0; padding:0; font-size:12px;}
  6. img,p{width:170px; height:128px; margin-left:20px;}
  7. p{width:170px;height:30px;}
  8. ul{
  9. width:1000px;
  10. height:200px;
  11. List-style:none;
  12. border:1px solid Gray;
  13. padding-top:40px;
  14. padding-left:45px;
  15. margin:0 Auto;
  16. }
  17. li{
  18. Float:left;
  19. }
  20. a:link{
  21. Color:black;
  22. Text-decoration:none;
  23. }
  24. a:hover{
  25. Color:orange;
  26. Text-decoration:underline;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <ul>
  32. <li>
  33.             < a href= "#"  title=" hornbills ">< img src= "img/1.jpg" /><p> our country experts first observed the field propagation process of crown-spotted hornbills </p></ a>  
  34. </li>
  35. <li>
  36. <a href="#" title="Earthquake"><img src="img/2.jpg"/> <p> Ludian earthquake-stricken children </P></a>
  37. </li>
  38.         <li >  
  39.             < a href= "#"  title=" drought ">< img src= "img/3.jpg" /><p> Henan lost more than 4 billion yuan due to drought </p></ a>    
  40. </li>
  41. <li>
  42.             < a href= "#"  title=" Xi ' an >< img src= "img/4.jpg" /><p> Xi ' an long drought every "dew"   People enjoy cool </p></ a>  
  43. </li>
  44. <li>
  45.             < a href= "#"  title=" Metro ">< img src= "img/5.jpg" /><p> Shanghai Metro three line early Peak Qi FA fault span class= "tag" ></p></ a>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
  46. </li>
  47. </ul>
  48. </body>
  49. </html>

Web front-end development learning----5. Understanding CSS Floating Float

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.