Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

html - floating elements are outside of containing blocks?

I'm a beginner in HTML/CSS, just some questions on floating elements, below is the code

section {
  border: 1px solid blue;
}

div {
  margin: 5px;
  width: 200px;
  height: 50px;
}

.left {
  float: left; 
  background: pink;
}

.left_second {
  position:relative;
  background: blue;
}

.right {
  float: right;
  background: cyan;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device_width,initial-scale=1.0">
<title>Page Title</title>
 <link rel="stylesheet" type="text/css" href="makeup.css">
</head>
<body>


<section>
  <div class="left">1</div>
  <div class="left_second">2</div>
  <div class="right">3</div>
</section>


</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It remains a part of the flow because text still wrap around float thus they are still considered a part of the flow unlike position:absolute elements that will no more affect the flow.

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).ref


To answer your question, let's add the properties step by step.

Intitially we have this:

section {
  border: 1px solid blue;
}

div {
  margin: 5px;
  width: 200px;
  height: 50px;
}

.left {
  /*float: left;*/ 
  background: pink;
}

.left_second {
  position:relative;
  background: blue;
}

.right {
  /*float: right;*/
  background: cyan;
}
<section>
  <div class="left">1</div>
  <div class="left_second">2</div>
  <div class="right">3</div>
</section>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...