Home Web Front-end HTML Tutorial What to do if DIVs overlap each other in HTML

What to do if DIVs overlap each other in HTML

Nov 23, 2017 pm 06:12 PM
div html

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>
Copy after login

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>
Copy after login

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>
Copy after login

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

HTML table style

html editing converter

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!

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)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles