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
424 views
in Technique[技术] by (71.8m points)

css - Does width property affects clear?

I was trying to understand float and clear. I have DIV with float left and then

element cleared right.

.container {
  border: solid thin #ccc;
}

.left-segment {
  float: left;
  background-color: #8FC9FF;
  height: 200px;
  width: 200px;
}


.text-clear {
  clear: right;
  
  background-color: red;
}
<div class='container'>
  <div class='left-segment'>I am LEFT DIV</div>
  
  <p class='text-clear'>Hey There I am P with some so called DUMMY data. And Dummy data again</p>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

To start, the use of clear here is useless simply because you are clearing the right and you have used float:left. So you will have the same output if you remove the clear.

To better understand what is happening, let's make the float element a bit transparent:

.container {
  border: solid thin #ccc;
}

.left-segment {
  float: left;
  background-color: #8FC9FF;
  height: 200px;
  width: 200px;
  opacity: 0.5
}

.text-clear {
  background-color: red;
  width: 200px;
  margin: 0; /*let's remove margin to avoid confusion*/
}
<div class='container'>
  <div class='left-segment'>I am LEFT DIV</div>

  <p class='text-clear'>Hey There I am P with some so called DUMMY data. And Dummy data again</p>
</div>

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

...