CSS float, clear a "row" of floating elements - css-float

I want to create a "scorecard" grid to output some data. If the data in each div.item is all the same height, then a simple float left on each div.item gives a nice even layout which scales up and down nicely depending on browser size.
If the data however is variable, a different number of lines in each div, then the way elements float gives an uneven and messy output. See code sample below. If you create a page with the below, resize the browser to about 800px wide so that box 1, 2, and 3 create a "row" on top, followed by 4, 5 and 6. How do I get 7 to drop down to the next line so it creates a row along with 8 and 9?
Obviously if you resize the browser so that 4 divs appear in each row, number 9 is the element I want to break down below 5. Is there something obvious I am missing or do I need to use some Javascript to achieve this?
div.item{
float:left;
width:220px;
background-color:#DBDBDB;
margin:8px;
}
h1, p{
padding:4px;
margin:0;
}
<div class='item'>
<h1>1</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>
<div class='item'>
<h1>2</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>
<div class='item'>
<h1>3</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>

Change "float: left;" to "display: inline-block; vertical-align: top;"
and it will work the way you want.
Example: http://jsfiddle.net/yK9eY/2/
div.item {
display: inline-block;
*display: inline;
width:220px;
background-color:#DBDBDB;
margin:8px;
vertical-align: top;
zoom: 1;
}
h1, p {
padding: 4px;
margin: 0;
}
<div class='item'>
<h1>1</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>
<div class='item'>
<h1>2</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>
<div class='item'>
<h1>3</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>
<div class='item'>
<h1>4</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>
<div class='item'>
<h1>5</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
<p>More Content</p>
<p>More Content</p>
</div>
<div class='item'>
<h1>6</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>
<div class='item'>
<h1>7</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>
<div class='item'>
<h1>8</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>
<div class='item'>
<h1>9</h1>
<p>Content</p>
<p>Content</p>
<p>Content</p>
</div>

Wrap all divs with a container and give .container { overflow: hidden; } or .container { overflow: auto } to it.
The second way is wrap all divs with a container and put another div to bottom of all divs and give a class name such example"clear" and style it .clear { clear: both }

Related

CSS: Floating image inside left then right then left again inside even/odd <articles>

It's probably easiest to explain if I post some code:
<article>
<img class="service_pic" src="image.png" title="UNFILLED" /> <!-- I WANT THIS (ODD) IMAGE FLOATED RIGHT -->
<div class="service_text">
<h2>TITLE</h2>
<p>CONTENT</p>
</div>
</article>
<article>
<img class="service_pic" src="image.png" title="UNFILLED" /> <!-- I WANT THIS (EVEN) ONE FLOATED LEFT -->
<div class="service_text">
<h2>TITLE</h2>
<p>CONTENT</p>
</div>
</article>
<article>
<img class="service_pic" src="image.png" title="UNFILLED" /> <!-- I WANT THIS (ODD) ONE FLOATED RIGHT AGAIN) -->
<div class="service_text">
<h2>TITLE</h2>
<p>CONTENT</p>
</div>
</article>
Thanks!
I am aware of nth-child and nth-of-type but I don't know how to implement them. I know that I could theoretically add .left and .right to each service_pic and service_text but thats a lot of repetitive code. And its hard coded and must be edited every time a new is added.
article:nth-child(odd){
float: right;
}
article:nth-child(even){
float: left;
}
As #cloned suggested in comment, you can select odd-numbered articles with article:nth-child(odd) (or article:nth-child(2n+1)). If you want to select odd-numbered articles, you can use article:nth-child(even) (or article:nth-child(2n)).
Here I used a flex container to change the image order.
article {
display: flex;
align-items: center;
border: 1px solid black;
width: fit-content;
margin: 10px;
}
article .service_text {
padding: 10px;
}
article:nth-child(2n+1) img {
order: 2;
}
<article>
<img class="service_pic" src="https://via.placeholder.com/100x200" title="UNFILLED" />
<!-- I WANT THIS (ODD) IMAGE FLOATED RIGHT -->
<div class="service_text">
<h2>TITLE</h2>
<p>CONTENT</p>
</div>
</article>
<article>
<img class="service_pic" src="https://via.placeholder.com/50x50" title="UNFILLED" />
<!-- I WANT THIS (EVEN) ONE FLOATED LEFT -->
<div class="service_text">
<h2>TITLE</h2>
<p>CONTENT</p>
</div>
</article>
<article>
<img class="service_pic" src="https://via.placeholder.com/200x100" title="UNFILLED" />
<!-- I WANT THIS (ODD) ONE FLOATED RIGHT AGAIN) -->
<div class="service_text">
<h2>TITLE</h2>
<p>CONTENT</p>
</div>
</article>

Alternative for absolute?

1)
<div class="testimonials">
<div class="content">
<p>Content ONE</p>
<p>Content TWO</p>
<p>Content THREE</p>
</div>
</div>
<div class="other-content">
<p>Content Here</p>
</div>
2)In above code i have give position:absolute to <p></p> tag, so the other-content class content is not displayed under the content class.
3)I need other-content content will be displayed below the other-content class.
4)please don't use height, solve the issue.
5)Please check the below link.
6)Here is the fiddle.
Thanks in advance.
remove the position from .content p
try this..
.content p{
width: 50%;
background-color: #eee;
float: left;
padding: 15px;
}
FIDDLE
Wrap the both divs (testimonials and other-content) in an extra wrapper div and then set position:absolute on that wrapper.
FIDDLE
.wpr {
position: absolute;
}
<div class="wpr">
<div class="testimonials">
<div class="content">
<p>Content ONE</p>
<p>Content TWO</p>
<p>Content THREE</p>
</div>
</div>
<div class="other-content">
<p>Other Content Here</p>
</div>
</div>

Place 3 HTML div; 1 left 2 right

following problem:
I have 3 divs.
<div id="div1"> </div>
<div id="div2"> </div>
<div id="div3"> </div>
And I want to place them in the following order:
DIV1 DIV2
DIV1 DIV2
DIV1 DIV2
........DIV3
........DIV3
........DIV3
One div on the left and 2 divs on the right underneath.
When I give the first two divs a float: left it looks like this:
DIV1 DIV2
DIV1 DIV2
DIV1 DIV2
DIV3
DIV3
DIV3
With "clear: left; float: right;" on the third div i get this:
DIV1 DIV2
DIV1 DIV2
DIV1 DIV2
.....................................DIV3
.....................................DIV3
.....................................DIV3
div 3 is here on the bottom of the page;
How can I place the last div underneath the second one?
Kai
Working jsFiddle Demo
Change your markup. Add div1 to one element, and div2 and div3 to another one:
<div class="left">
<div id="div1">
<p>DIV 1</p>
<p>DIV 1</p>
<p>DIV 1</p>
<p>DIV 1</p>
</div>
</div>
<div class="right">
<div id="div2">
<p>DIV 2</p>
<p>DIV 2</p>
<p>DIV 2</p>
<p>DIV 2</p>
</div>
<div id="div3">
<p>DIV 3</p>
<p>DIV 3</p>
<p>DIV 3</p>
<p>DIV 3</p>
</div>
</div>
Then float left and right:
.left {
float: left;
width: 50%;
}
.right {
float: right;
width: 50%;
}
Try this jfiddle, 2 columns with float and divs in it
http://jsfiddle.net/AnFPa/1/
<div style="float: left;">
<div>div1</div>
</div>
<div style="float: left;">
<div>div2</div>
<div>div3</div>
</div>
<div id="div4">
<div id="div1"> </div>
<div id="div2"> </div>
<div id="div3"> </div>
</div>
Give div 4 a width and float div 3 right.
try using clear left :
<div 3 style="clear:left" ></div>
or you can have both divs inside a container and float the bottom row to the right:
<div>
<div id="1" style="float:left"></div>
<div id="2" style="float:left"></div>
<div style="clear:both"></div>
<div id="3" style="float:right"></div>
</div>
<html>
<body>
<div style="height: 100px; Width: 200px;"> <!-- Container -->
<div style="float:left; height: 50px; Width:100px; background-color:red;">
</div>
<div style="float:left; height: 50px; Width:100px; background-color:blue;">
</div>
<div style="float:right; height: 50px; Width:100px; background-color:green;">
</div>
</div>
</body>
</html>

Responsive Design, how to change order from div containers?

I am just starting with responsive webdesign. I have a 2 column layout (sidebar and content).
<div class="main-container">
<div class="main wrapper clearfix">
<div class="con1">
<header>
<h1>container 1 h1</h1>
<p>text1</p>
</header>
<section>
<h2>overview section h2</h2>
<p> test</p>
</section>
</div>
<div class="con2">
<header>
<h1>container 2 article header h1</h1>
<p></p>
</header>
<section>
<h2>article section h2</h2>
<p>text</p>
</section>
</div>
<aside>
<h3>aside</h3>
<p>text</p>
</aside>
</div> <!-- #main -->
</div> <!-- #main-container -->
The content got 2 div container. On a small screen I want
Div1
Div2
On a large screen I want them swapped,
Div2
Div1
In my CSS I now got the following Media Query:
#media only screen and (min-width: 768px) {
.main div.con1 {
float: right;
width: 57%;
}
.main div.con2 {
float: right;
width: 57%;
}
Is it possible to swap the order around and if so, how can I do it? Anyone got maybe a link to a good tutorial?
Thanks a lot in advance !
The method used in the StackOverflow question "CSS positioning div above another div when not in that order in the HTML" seems to be your best bet.
with js:
//on load
responsive_change_box_order();
//on resize
window.addEventListener('resize', responsive_change_box_order );
function responsive_change_box_order() {
if (window.matchMedia("(max-width: 1118px)").matches) {
jQuery("#block-menu-block-1").remove().insertBefore(jQuery("#content"));
}
else{
jQuery("#block-menu-block-1").remove().prependTo(jQuery(".region-sidebar-second .region-inner"));
}
}
This snippet uses flexbox and related properties to meet your end requirement.Also kindly note that you use use appropriate vendor prefixes for flexbox when you implement or use something like autoprefixer or prefixfree.
.main{
display:flex;
flex-direction:column
}
.con1{
order:2;
background-color: green;
}
.con2{
order:1;
background-color: red;
}
/*For Large screen only*/
#media(min-width:1200px){
.con1{
order:1;
}
.con2{
order:2;
}
}
<div class="main-container">
<div class="main wrapper clearfix">
<div class="con1">
<header>
<h1>container 1 h1</h1>
<p>text1</p>
</header>
<section>
<h2>overview section h2</h2>
<p> test</p>
</section>
</div>
<div class="con2">
<header>
<h1>container 2 article header h1</h1>
<p></p>
</header>
<section>
<h2>article section h2</h2>
<p>text</p>
</section>
</div>
<aside>
<h3>aside</h3>
<p>text</p>
</aside>
</div>
<!-- #main -->
</div>
<!-- #main-container -->

Definitive way to do three-column layout in CSS

I have a really, really simple CSS question that has already been asked here a thousand times already in different forms, and seems to have no definitive answer.
I just want to create three columns on an HTML page, using CSS. Doesn't matter about fixed-width versus liquid: just need three columns.
Here's a complete HTML page:
<html>
<body>
<div id="left" style="float:left; width:300px;">
<h3>Column 1</h3>
</div>
<div id="right" style="float:right; width:300px;">
<h3>Column 3</h3>
</div>
<div id="middle">
<h3>Column 2</h3>
</div>
</body>
</html>
In Chrome, at least, this is pushing the left & right columns down below the middle. What is wrong?
like this?: http://jsfiddle.net/SebastianPataneMasuelli/Xu5c6/
just float everything left, and have the columns flow in the normal order in your HTML.
<div id="left">
<h3>Column 1</h3>
</div>
<div id="middle">
<h3>Column 2</h3>
</div>
<div id="right">
<h3>Column 3</h3>
</div>
css:
#left {
background-color: red;
float:left;
width:200px;
}
#middle {
background-color: salmon;
float:left;
width:200px;
}
#right {
background-color: pink;
float:left;
width:200px;
}
if you don't want them to wrap, you can wrap a container div around them, or use
body {
width: 600px; /*combined width of three columns*/
margin: 0 auto;
}
http://jsfiddle.net/SebastianPataneMasuelli/Xu5c6/1/
float is sensitive to order. Put the left, then middle, then right.
Have you tried floating the middle section too?
You might try this
<html>
<body>
<div id="left" style="float:left; width:300px;border:1px solid black;">
<h3>Column 1</h3>
</div>
<div id="middle" style='float:left;width:600px;border:1px solid black;'>
<h3>Column 2</h3>
</div>
<div id="right" style="float:left; width:300px;border:1px solid black;">
<h3>Column 3</h3>
</div>
</body>
</html>

Resources