Examples of common float are provided on this page.
It is simple to float content boxes next to one another using the float property:
* {
box-sizing: border-box;
}
.box {
float: left;
width: 33.33%; /* three boxes (use 25% for four, and 50% for two, etc) */
padding: 50px; /* if you want space between the images */
}
Three floating boxes next to each other are simple to make. Nevertheless, the box will shatter if you include anything that makes each box wider, such padding or borders. In order to ensure that the padding remains inside the box and does not break, we can include the padding and border in the box’s total width (and height) using the box-sizing property.
It is also possible to display images side by side using the box grid:
.img-container {
float: left;
width: 33.33%; /* three containers (use 25% for four, and 50% for two, etc) */
padding: 5px; /* if you want space between the images */
}
You discovered how to float identically widened boxes side by side in the preceding example. Nevertheless, making floating boxes of uniform height is not simple. Setting a fixed height, as in the example below, is a simple workaround, though:
.box {
height: 500px;
}
That being said, this lacks flexibility. If you can ensure that the boxes will always have the same quantity of content, it’s OK. However, the content is frequently different. The content of the second box will appear outside of the box if you try the aforementioned example on a mobile device. Because CSS3 Flexbox can automatically expand boxes to match the longest box’s length, it is useful in this situation:
Using Flexbox to create flexible boxes:
Tip: You can read more about the Flexbox Layout Module in our CSS Flexbox Chapter.
Additionally, use the float attribute for complete web layouts is common:
.header, .footer {
background-color: grey;
color: white;
padding: 15px;
}
.column {
float: left;
padding: 15px;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
.menu {
width: 25%;
}
.content {
width: 75%;
}
Property | Description |
---|---|
box-sizing | Defines how the width and height of an element are calculated: should they include padding and borders, or not |
clear | Specifies what should happen with the element that is next to a floating element |
float | Specifies whether an element should float to the left, right, or not at all |
overflow | Specifies what happens if content overflows an element's box |
overflow-x | Specifies what to do with the left/right edges of the content if it overflows the element's content area |
overflow-y | Specifies what to do with the top/bottom edges of the content if it overflows the element's content area |
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.