HTML:
<body>
<div class="container">
<img width="200px" style="border: 2px solid;" src="../HTML/FCC Beta Curriculum/Sources/Naruto.jpg" alt="Placeholder Image">
<p>This is an example of text flowing around a floated image.</p>
</div>
</body>
CSS Fragment 1:
.container {
border: 1px solid black;
}
img {
float: left;
margin-right: 20px;
}
/* Clearfix CSS */
.container::after {
content: "";
display:inline;
clear: both;
}
CSS Fragment 2:
.container {
border: 1px solid black;
}
img {
float: left;
margin-right: 20px;
}
/* Clearfix CSS */
.container::after {
content: "";
display: block;
clear: both;
}
Preview for CSS Fragment 1:
Preview for CSS Fragment 2:
Question:
Why clear property is not worked for inline elements?
Why I ask this:
After checking and trying to understand the concepts of float property and clear property.My understanding of block elements and inline elements seems to have a conflict with these two new concepts.
- 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.-From MDN - The
clear
CSS property sets whether an element must be moved below (cleared) floating elements that precede it. Theclear
property applies to floating and non-floating elements..-From MDN - The main difference between block elements and inline elements is that block elements take up the whole width space of their parent elements by default,regardless of the content within it,while inline elements take up the space of their content only.-My understanding
My thought:
Originally,Both block ::after
elements and inline ::after
elements without clear
property will be added into the .container
div like these two pics below due to the influece of the floating img
.
Since the clear
property can do something like cleaing the effect of floating elements on other elements.
Then,I think,after adding the clear
property, both block ::after
elements and inline ::after
elements suppose to be moving like this and the border of the .container
div will wrap the floating img
into it.
(I just use drawing tool to show my thought,actually i did not set clear:both
with these two pics below).
But when I really add the clear
property to both block ::after
elements and inline ::after
elements,what I get are these.
I get confused about the results…
Pre-thanks for your reply and correcting!!!
If I do not express my idea clearly,Please tell me in the comment area!
Course Source:
What Are Common Use Cases for Using Floats, and How Do They Work?