We often find a problem when making web pages, that is, DIV covers DIV, so there is a problem of overlapping DIV boxes and causing the content to not appear. So today we will introduce to you the reasons and reasons. Solution.
Maybe you have encountered a layout with a top-down structure. The content of the lower DIV overlaps the content of the upper DIV. It is also possible that the lower content covers the upper DIV layout, forming a phenomenon of overlapping DIV and DIV coverage. You may also encounter I have seen overlapping coverage of two adjacent DIV boxes. What is the problem and how to solve it?
Next, we will use a case to demonstrate the overlapping phenomenon of these two compatible DIV coverages, and explain the reasons and solutions.
Top and bottom structure DIV box coverage First, use the example HTML code
<!DOCTYPE html>
<html>
<head>
<title>DIV与DIV覆盖</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
.boxa,.boxb{ margin:0 auto; width:400px;}
.boxa-l{ float:left; width:280px; height:80px; border:1px solid #F00}
.boxa-r{ float:right; width:100px; height:80px; border:1px solid #F00}
.boxb{ border:1px solid #000; height:40px; background:#999}
</style>
</head>
<body>
<div class="boxa">
<div class="boxa-l">内容左</div>
<div class="boxa-r">内容右</div>
</div>
<div class="boxb">boxb盒子里的内容</div>
</body>
</html>You can copy the code and discover the DIV coverage phenomenon yourself.
Example code description:
Set two large div boxes with CSS names ".boxa" and ".boxb", set the width to the same 400px, and set one for ".boxb" A black border with a height of 40px and a black background; then add two small boxes in boxa, one on the left and one on the right. The CSS names are ".boxa-l" and ".boxa-r", and they are set to red at the same time. The border and css height are 80px, and the width is 280px and 100px respectively.
Problem Analysis
Generally, it is necessary to layout ".boxa" and ".boxb" in a top-down structure. From the above picture, we find that the effect seen in the browser is that the contents of the two boxes are The top-bottom structure effect is achieved, but the DIV ".boxb" goes under ".boxa", but the content is not overwritten, only the DIV is overwritten.
This reason is because the child in the first big box uses floating floatattribute and floats, so ".boxa" is not opened, and the same level The ".boxb" box is closely connected to ".boxa", but ".boxa" has no height. The floating children of ".boxa" are not at the same level as ".boxb". The ".boxb" box still considers ".boxa" There is no height, so the ".boxb" DIV box runs under the ".boxa" sub-level DIV box, forming an overlapping phenomenon.
Solution to the problem
Eitherclear the float, or set the height of ".boxa". Generally, you cannot set a fixed height if the text content is uncertain, so generally The height of ".boxa" cannot be set (of course you can determine how high the content is. In this case, ".boxa" can set a height to solve the coverage problem.).
Here we use the CSS clear float method to solve the problem of overlapping DIVs in the upper and lower structures. There are two ways to clear float. The methods are as follows.
css clear clear float
Add clear style to clear float before closing the ".boxa" box .
Complete HTML source code:
<!DOCTYPE html>
<html>
<head>
<title>DIV与DIV覆盖</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
.boxa,.boxb{ margin:0 auto; width:400px;}
.boxa-l{ float:left; width:280px; height:80px; border:1px solid #F00}
.boxa-r{ float:right; width:100px; height:80px; border:1px solid #F00}
.boxb{ border:1px solid #000; height:40px; background:#999}
.clear{ clear:both}
</style>
</head>
<body>
<div class="boxa">
<div class="boxa-l">内容左</div>
<div class="boxa-r">内容右</div>
<div class="clear"></div>
</div>
<div class="boxb">boxb盒子里的内容</div>
</body>
</html>This method is simpler and simpler than the previous method. Just add overflow to ".boxa" (parent box with floating children) :hidden)
CSS DIV example code is as follows:
<!DOCTYPE html>
<html>
<head>
<title>DIV与DIV覆盖</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
.boxa{ overflow:hidden}
.boxa,.boxb{ margin:0 auto; width:400px;}
.boxa-l{ float:left; width:280px; height:80px; border:1px solid #F00}
.boxa-r{ float:right; width:100px; height:80px; border:1px solid #F00}
.boxb{ border:1px solid #000; height:40px; background:#999}
</style>
</head>
<body>
<div class="boxa">
<div class="boxa-l">内容左</div>
<div class="boxa-r">内容右</div>
</div>
<div class="boxb">boxb盒子里的内容</div>
</body>
</html>This solves the problem of DIV coverage for everyone. For more exciting content, please pay attention to other related articles on the php Chinese website!
Related reading:
How to use border-radius in CSS
The above is the detailed content of What to do if DIVs overlap each other in HTML. For more information, please follow other related articles on the PHP Chinese website!
The Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AMThe future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.
HTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AMThe roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.
The Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AMThe future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.
HTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AMThe roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.
HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AMHTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.
HTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AMHTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.
From Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AMHTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.
Understanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AMWebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.






