search
HomeWeb Front-endJS TutorialDetailed explanation of css float (with examples)

Detailed explanation of css float (with examples)

Oct 16, 2018 pm 03:36 PM
html5javascript

This article brings you a detailed explanation of css float (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

div css layout is the core skill of front-end developers and accounts for a large proportion of their work. A good front-end layout is the basis for JavaScript writing and interaction, which shows the importance of layout. Today we will talk about the cornerstone of CSS layout - float. It can be said that without floating, there would be no layout.

1. The emergence of float

From the beginning to its growth, the Internet drew on a large number of concepts and technologies from printing and typesetting. For example, the invention of the Internet at the beginning was to electronically exchange documents. Link.

The subsequent invention of table layout was also done by printing experts. Until the emergence of CSS, you can still see the shadow of printing. Of course, this is understandable. For example, the emergence of float was to cope with the emergence of typesetting with pictures and texts.

The following common layout of traditional printing and typesetting-text wrapping is one of the common application scenarios of float.

Detailed explanation of css float (with examples)

Let’s see how to achieve it,

nbsp;html>



    <meta>
    <meta>
    <meta>
    <title>JS暗黑编年史</title>
    <style>
        img{
            float:left;
        }
    </style>



    <p>
        <img  src="/static/imghwm/default1.png" data-src="https://jetorz.github.io/assets/Brendan-Eich.jpg" class="lazy" alt="Detailed explanation of css float (with examples)" >
        在一个一个炎热的下午,大家注意,一定要是炎热的下午,为什么要是炎热的下午呢,因为天气一热,人就容易烦躁,人一烦躁就不想工作,不工作就想看片儿(注意看片儿不是看电影),但是你知道的越看片儿越烦躁,正在这个时候老板娘进来来了,对js的作者布莱登·艾克说,小艾啊你看我们用猫上网的时候用户名密码填错了结果等一两分钟返回结果的时候才知道是错了,你看你能不能搞一个程序让我在请求之前就知道我写错了,其实小艾心里不想搞,但是大家都懂的,老板好拒绝,老板娘的需求是不好拒绝的,所以布莱登艾克心想赶紧随便糊弄一下算了,片儿还没看完呢,所以他就用了8天半(官方说)10天,其实另外的一天半被他用来看片儿了。大家懂的,你看片儿的时候有心思写代码吗?所以js的bug如山一样多,这个我们以后说。
    </p>

2. The classic scene of float

In addition to the above classics In addition to usage, float has several more complex and common scenarios. The first is the layout of the entire site.

Detailed explanation of css float (with examples)

The code is as follows,

nbsp;html>



    <style>
        #header {
            background-color: black;
            color: white;
            text-align: center;
            padding: 5px;
        }

        #nav {
            line-height: 30px;
            background-color: #eeeeee;
            height: 300px;
            width: 100px;
            float: left;
            padding: 5px;
        }

        #section {
            width: 350px;
            float: left;
            padding: 10px;
        }

        #footer {
            background-color: black;
            color: white;
            clear: both;
            text-align: center;
            padding: 5px;
        }
    </style>




    <div>
        <h1 id="我是头部">我是头部</h1>
    </div>

    <div>
        html教程<br>
        css教程<br>
        js教程<br>
    </div>

    <div>
        <h2 id="js暗黑编年史">js暗黑编年史</h2>
        <p>
            在一个一个炎热的下午,大家注意,一定要是炎热的下午,为什么要是炎热的下午呢,因为天气一热,人就容易烦躁,人一烦躁就不想工作,不工作就想看片儿(注意看片儿不是看电影),但是你知道的越看片儿越烦躁,正在这个时候老板娘进来来了,对js的作者布莱登·艾克说,小艾啊你看我们用猫上网的时候用户名密码填错了结果等一两分钟返回结果的时候才知道是错了,你看你能不能搞一个程序.
        </p>
       
    </div>

    <div>
        我是footer
    </div>

There are also Taobao-like product layouts that are also very common,

Detailed explanation of css float (with examples)

The implementation code is as follows,

nbsp;html>


    <meta>
    <meta>
    <meta>
    <title>Document</title>
    <style>
        #div2{
            background: red;
            margin-right: 10px;
            height: 300px;
        }
        #div3{
            padding: 5px;
        }
        #div2,#div3{
            float: left;
            width:300px;
           
        }
    </style>


    <div>
        <div>

        </div>
        <div>
            <h3 id="我是标题">我是标题</h3>
            <p>我是一段描述的文字我是一段描述的文字我是一段描述的文字我是一段描述的文字</p>
        </div>
    </div>

3. Common problems with float

1. The width is not enough and it will squeeze down

nbsp;html>


    <meta>
    <meta>
    <meta>
    <title>Document</title>
    <style>
        #wrap{
            width:1000px;
            margin: 0 auto;
        }
        div{
            border:5px solid #000;
        }
        #left{
            width:200px;
            background:red;
            height: 300px;
            float: left;
        }
        #right{
            width:800px;
            background:green;
            height: 300px;
            float: left;

        }
    </style>


    <div>
        <div>左侧</div>
        <div>右侧</div>
    </div>

Solution:

nbsp;html>


    <meta>
    <meta>
    <meta>
    <title>Document</title>
    <style>
        #wrap{
            width:1000px;
            margin: 0 auto;
            border:5px solid yellow;
        }
        div{
            border:5px solid #000;
        }
        #left{
            width:200px;
            background:red;
            height: 300px;
            float: left;
            box-sizing: border-box;
        }
        #right{
            width:800px;
            background:green;
            height: 300px;
            float: left;
            box-sizing: border-box;
            }
    </style>


    <div>
        <div>左侧</div>
        <div>右侧</div>
    </div>

As shown in the picture, you will find that the height of the outer div is not stretched. I don’t want to explain the reason here. What are the BFC and document flow? Why? ?

Programming is like learning to ride a bicycle. You have seen how other people ride, and you also know that the pedals are for pedaling, the saddle is for sitting on it, and the handlebars are for steering. It is enough. There is no need to study from the beginning:

The 30-speed mountain bike corresponds to: The transmission kit of this mountain bike is composed of a 3-piece chainring and a 10-piece flywheel, which can change 30 transmission ratios, that is gear ratio. The detailed explanation is: there are three chainrings on the front and 10 flywheels on the back. 3x10 is 30 speed. If there are 9 flywheels on the back, it is 27 speed. The speed ratio is the number of front chainrings multiplied by the number of rear wheels. Under normal circumstances There are three discs in the front, and the main difference is the small flywheel in the back. The common speeds are 18, 21, 24, 27, and 30.

After all, you can’t just start learning to ride a bike and think about becoming the Akina mountain bike god in the future.

Even if you learn these, it will not help you learn to ride a bicycle. Just like even if you know that the height is not supported because BFC is not triggered, even if you know the document flow and regular flow, you know float. The default value is none. How helpful is it for your smooth layout?

I am not denying the value of this knowledge, but I want you to understand your priorities. After all, if you can't even write the most basic float layout, what's the point of studying it? Memorize the typical layout first, learn it, and use it familiarly, and then talk about other things. Moving on to the second question,

2. The level is not high enough. Just say one sentence and remember it.

nbsp;html>


    <meta>
    <meta>
    <meta>
    <title>Document</title>
    <style>
        #wrap{
            width:1000px;
            margin: 0 auto;
            border:5px solid yellow;
            overflow: hidden;
        }
        div{
            border:5px solid #000;
        }
        #left{
            width:200px;
            background:red;
            height: 300px;
            float: left;
            box-sizing: border-box;
        }
        #right{
            width:800px;
            background:green;
            height: 300px;
            float: left;
            box-sizing: border-box;
        }
    </style>


    <div>
        <div>左侧</div>
        <div>右侧</div>
    </div>

Actually, it’s just one sentence.

overflow: hidden;

Just remember it first. You'll understand when you grow up.

As for the third question,

It is obvious that the footer position is wrong. This is a feature. The elements behind the float will automatically follow and try to be as high and to the left as possible. The problem is that the footer is obviously not willing to follow the elements on the right, he should be below. How to do it? Clear float.

nbsp;html>



    <meta>
    <meta>
    <meta>
    <title>Document</title>
    <style>
        #wrap {
            width: 1000px;
            margin: 0 auto;
            border: 5px solid yellow;
            overflow: hidden;
        }

        div {
            border: 5px solid #000;
        }

        .main {
            border: 0;
        }

        #left {
            width: 200px;
            background: red;
            height: 400px;
            float: left;
            box-sizing: border-box;
        }

        #right {
            width: 800px;
            background: green;
            height: 200px;
            float: left;
            box-sizing: border-box;
        }

        .clearFix:after {
            content: &#39;&#39;;
            display: block;
            clear: both;
        }

        .clearFix {
            zoom: 1;
        }
    </style>



    <div>
        <div>
            <div>左侧</div>
            <div>右侧</div>
        </div>

        <div>我是帅气的footer</div>
    </div>


Note that a layer of div is wrapped around left and right, and then class is added.

        .clearFix:after {
            content: '';
            display: block;
            clear: both;
        }

        .clearFix {
            zoom: 1;
        }

It doesn’t matter if you don’t know how to use it. Just remember to use it first. Memorizing code is like riding a bicycle. The more you ride it, the more you will naturally know how to ride it. Once you become proficient, you can ride a mountain bike or a road bike, and you will get started quickly.

The above is the detailed content of Detailed explanation of css float (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault思否. If there is any infringement, please contact admin@php.cn delete
Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Is Python or JavaScript better?Is Python or JavaScript better?Apr 06, 2025 am 12:14 AM

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

How do I install JavaScript?How do I install JavaScript?Apr 05, 2025 am 12:16 AM

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.