Table of Contents
一, what is float?
2. How to generate float?
3. What is the role of floating?
css div Floating other application cases
Home Web Front-end CSS Tutorial Detailed explanation of CSS using float attribute to control div floating left and right

Detailed explanation of CSS using float attribute to control div floating left and right

Aug 03, 2022 am 09:59 AM
css

This article brings you relevant knowledge about css, which mainly introduces the issues related to the float attribute controlling the left and right floating of divs. Floating, as the name suggests, means floating. It refers to the phenomenon that an element is separated from the document flow and suspended above the parent element. Let’s take a look at it. I hope it will be helpful to everyone.

Detailed explanation of CSS using float attribute to control div floating left and right

(Learning video sharing: css video tutorial, html video tutorial)

一, what is float?

Floating, as the name suggests, means floating. Refers to the phenomenon of an element breaking away from the document flow and floating above the parent element.

Css style float attribute, used to set label objects (such as:

label box, label, label, label and other html tags) Floating layout, floating is what we call label objects floating to the left (float:left) and floating to the right (float:right).

The horizontal floating direction of the element means that the element can only move left and right but not up and down.

A floating element will try to move left or right until its outer edge touches the border of the containing box or another floating box.

Elements after the floated element will surround it.

Elements before the floated element will not be affected.

If the image is floated right, the text flow below will wrap to the left of it.

2. How to generate float?

Add the float attribute to the element itself

float value:

3. What is the role of floating?

Function: In the HTML document flow, it is divided into line elements, block elements and inline block elements.

Line elements and inline block elements are arranged horizontally, and blocks The elements are arranged from top to bottom in the form of a flow. When we want to arrange the block elements horizontally, we use our float.

When a float is added to a block-level element, the elements that should have been arranged vertically begin to be arranged horizontally, as shown in the following figure:

.box {border: 1px solid #666;height: 700px;width: 700px;color: #fff;}
.box1 {float: left; width: 100px; height: 100px; background: #000;}
.box2 {float: left; width: 100px; height: 100px; background: red; }
.box3 {width: 200px; float: left; height: 200px; background: yellow; }
.box4 {width: 300px; float: left; height: 300px; background: blue; }
.box5 {float: left; width: 300px; height: 400px; background: green;}
<p class="box"> <p class="box1"> box1 </p> <p class="box2"> box2 </p> <p class="box3"> box3 </p> <p class="box4"> box4 </p> <p class="box5"> box5 </p> </p>
Copy after login

css div Floating other application cases

DIV CSS Experiment 1

Css style example content, we put the text and pictures in a fixed-width div layer, let the blue background text content be on the right, and the small picture on the left .

www.divcss5.com The final rendering of the CSS case demonstration is as follows

Detailed explanation of CSS using float attribute to control div floating left and right

1. First, we set the width of the outermost layer to 300px and the height to 200px The css selector code named box is as follows (knowledge point px means)

.box{width:300px; height:200px;}
Copy after login

2. Set the css style of the text content part in the box to yangshi, and set the background to blue and the width to 120px, float on the right

.yangshi{ width:120px; float:right; background:#0066FF;}
Copy after login

3. Set the css style of the image to float on the left div

img { float: left;}
Copy after login

4. The div layout within the body, the code is as follows

 <div class="box">
<div class="yangshi">我是www.divcss5.com 网站,测试内容</div>
<img  src="/static/imghw/default1.png"  data-src="demo.gif"  class="lazy"   / alt="Detailed explanation of CSS using float attribute to control div floating left and right" >
</div>
Copy after login

Description: Here is the img tag It is a link to an external image, and the image name is demo.gif

Screenshot of the final demonstration result

Detailed explanation of CSS using float attribute to control div floating left and right

##CSS Experiment 2

Next we will demonstrate the use The div css allows the small image here to be on the right (the previous example was on the left), and the blue background text content area is on the left (the previous example was on the right) (extended css center). Here we only need to change yangshi's float:right; to float:left and the image css style img { float: left;} to img { float: right;}

CSS code is as follows:

 .box{width:300px; height:200px;}
.yangshi{ width:120px; float:right; background:#0066FF;}
img{ float:left;}
Copy after login
The css code and content in html remain unchanged

The screenshot of the final demonstration result is as follows:

Detailed explanation of CSS using float attribute to control div floating left and right

I hope that the above two css examples will help you understand float . I hope you all can try it out in practice!

css floating summary

We need to distinguish between text content left (text-align:left) and right (text-align:right) styles. Floating is only set to the left for html tags. Right float style. The float style does not have a centered (floating centered) style. If you need to center the label object, we will explain it in detail (css margin) in the text related to css layout centering. Remember here to use float:right when floating to the right, and float:left when floating to the left.

(Learning video sharing:

css video tutorial, html video tutorial)

##left

Elements float to the left.

right

The element floats to the right.

none

Default value. The element is not floated and appears where it appears in the text.

inherit

Specifies that the value of the float attribute should be inherited from the parent element.

The above is the detailed content of Detailed explanation of CSS using float attribute to control div floating left and right. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

See all articles