Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 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 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...