Justify divs left in parent - html

I have the following requirement.
The green colored parent width will be varying depending on device width. I need all the boxes to be in the center of the parent.
I have tried the following things already, but it didnt help me.
Trial 1
Parent {text-align:center} box {display:inline-block}.
This resulted in following output
Trial 2
Parent {text-align:center} box{float:left}.
This resulted in following output
Trial 3
Parent {display:flex} box -> justify-around & justify-between also didn't work.
.parent {
text-align: center;
}
.item {
display: inline-block;
width: 100px;
height: 100px;
background: red;
}
<div class="parent">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
Any help on this will be appreciated.

Without Javascript this very hard using floats &/or inline-block.
Flexbox offers some hope but even then some creativity is required.
Essentially, provided the maximum number of elements "per row" is known you can create a required number of invisible elements which can be ustilised in conjunction with justify-content:center to acheieve the last line appearance you require by essentially pushing the last line content back over to the left.
Codepen Demo
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.parent {
text-align: center;
border: 1px solid grey;
width: 80%;
max-width: 800px;
margin: 1em auto;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.item {
width: 100px;
height: 100px;
margin: 10px;
background: red;
}
.balancer {
height: 0;
width: 100px;
margin: 0 10px;
visibility: hidden;
}
<div class="parent">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="balancer"></div>
<div class="balancer"></div>
<div class="balancer"></div>
<div class="balancer"></div>
</div>

Got it working by using jQuery and adding a #wrapper.
All you've got to do is calculate how many items will fit on one row. Then you set the wrapper to the exact width that is needed to fit these items.
I hoped it could be done in pure CSS, but as far as I know there is no Math.floor() equivalent for CSS.
Example:
function fitItemsOnRow() {
var windowWidth = $(window).width();
var itemWidth = $(".item").outerWidth(true);
var itemAmount = Math.floor((windowWidth / itemWidth));
if(itemAmount > $(".item").length) {
/* Set the maximum amount of items */
itemAmount = $(".item").length;
}
var rowWidth = itemWidth * itemAmount;
$("#wrapper").width(rowWidth);
}
$(window).resize(function() {
/* Responsive */
fitItemsOnRow();
});
$(document).ready(function() {
fitItemsOnRow();
});
body {
margin: 0px;
}
#parent {
background: #75DB3C;
min-width: 100vw;
min-height: 100vh;
text-align: center;
}
#wrapper {
display: inline-block;
text-align: left;
font-size: 0px; /* Removes default margin */
}
.item {
display: inline-block;
margin: 12px;
width: 100px;
height: 100px;
background: #0B56A9;
}
<div id="parent">
<div id="wrapper">
<!-- A wrapper is necessary to center the items -->
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

You can do this with css selector "nth-of-type(n)"
<div class="parent">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
and css here
.parent
{
display: block;
width: 980px;
padding: 10px 50px;
background: green;
box-sizing: border-box;
}
.parent::after
{
content: '';
display: block;
clear: both;
}
.item
{
float: left;
width: 24%;
margin-right: 1.25%;
margin-bottom: 1.25%;
/*
note
you may need min height , height or overflow:hidden
*/
}
.item:nth-of-type(4n)
{
float: right;
margin-right: 0;
}

Related

Horizontal list of items: the next to last item is centered, while the last item takes all of the remaining space

I'm building a horizontal list of items:
Once scrolled to the end of the list, the second last item needs to be in center, and the last item takes all of the remaining space after it.
I've had a success in making the list of items, but not sure what's the best way to implement the desired behaviour of having second last item centered and last item taking remaining space. Here is my current code:
I would appreciate any help or advice for guiding me in the right direction. Thank you!
/* layout */
.container {
display: flex;
overflow-x: auto;
gap: 21px;
}
.item {
min-width: 212px;
height: 254px;
}
/* visuals */
.container {
padding-left: 11px;
}
.item {
background: #D9D9D9;
}
.item.green {
background: #099F9F;
}
.item.red {
background: #FF6262;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item green"></div>
<div class="item red"></div>
</div>
You can define the width as calc((100% - item_wdith - 2*gap)/2)
.container {
display: flex;
overflow-x: auto;
padding-left: 10px;
gap: 20px;
}
.item {
width: 220px;
flex-shrink: 0;
height: 254px;
background: #D9D9D9;
}
.item.green {
background: #099F9F;
}
.item.red {
background: #FF6262;
width: calc((100% - 220px - 2*20px)/2);
}
/* to illustrate the center */
html {
background: linear-gradient(red 0 0) 50%/2px 100% no-repeat;
min-height: 100%;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item green"></div>
<div class="item red"></div>
</div>

Orphaned Inline Divs

I have a series of inline-block divs that can wrap onto a second line depending on how wide the browser is at a given time. Here's a fiddle example. For example, it might look like this:
However if the viewport is of a certain width, it can end up with just one orphaned inline-block on the next line:
Is there a CSS method that is essentially some form of "orphan control" (CSS orphan doesn't seem to apply in this situation) so that if there are fewer than a certain number of elements on the second line, it'd wrap more of them down there to even things out?
.container {
width: 100%;
}
.item {
display: inline-block;
width: 50px;
background-color: blue;
}
<div class="container">
<div class="item">
Item 1
</div>
<div class="item">
Item 2
</div>
<div class="item">
Item 3
</div>
<div class="item">
Item 4
</div>
<div class="item">
Item 5
</div>
<div class="item">
Item 6
</div>
<div class="item">
Item 7
</div>
</div>
Here is an idea using CSS grid where the trick is to make sure your div has a width expressed as a multiple of a fixed amount to make sure you never have an orphan element.
.wrapper {
--w: 100px; /* width of one item */
display: grid;
grid-template-columns: repeat(auto-fit, calc(2*var(--w))); /* the item will break two by two*/
justify-content:center;
}
.container {
grid-column: 1/-1;
display:flex;
flex-wrap:wrap;
justify-content:center;
}
.item {
width: calc(var(--w) - 10px);
margin: 5px;
}
/* irrelevant styles */
.container {
counter-reset: num;
}
.item {
height: 150px;
background-color: blue;
color: #fff;
}
.item::before {
color: #fff;
content: attr(class) " " counter(num);
counter-increment: num;
}
<div class="wrapper">
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
If we use 3
.wrapper {
--w: 100px; /* width of one item */
display: grid;
grid-template-columns: repeat(auto-fit, calc(3*var(--w)));
justify-content:center;
}
.container {
grid-column: 1/-1;
display:flex;
flex-wrap:wrap;
justify-content:center;
}
.item {
width: calc(var(--w) - 10px);
margin: 5px;
}
/* irrelevant styles */
.container {
counter-reset: num;
}
.item {
height: 150px;
background-color: blue;
color: #fff;
}
.item::before {
color: #fff;
content: attr(class) " " counter(num);
counter-increment: num;
}
<div class="wrapper">
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
You could use flex and flex-wrap for this with break points so you can decide how many items are in the top row on each width. Where it is inevitable that there is a single item on the last row this snippet goes for minimising the number of rows.
It would have been nice to use CSS variables for all of this so the choice is easily altered, but you can't use variables in media queries so we have to put them in by hand.
.container {
width: 104px; /* item width plus 2 lots of padding */
margin: 0;
padding: 0;
justify-content: center;
display: flex;
flex-wrap: wrap;
}
#media (min-width: 312px) {
.container {
width: 312px;
}
}
#media (min-width: 416px) {
.container {
width: 416px;
}
}
#media (min-width: 728px) {
.container {
width: 728px;
}
}
.item {
r-sizing: border-box;
width: 100px;
background-color: cyan;
text-align: center;
flex: 0 0 100px; /* made 100px so easier to test on different widthe */
margin: 2px;
}
<div class="container">
<div class="item">
Item 1
</div>
<div class="item">
Item 2
</div>
<div class="item">
Item 3
</div>
<div class="item">
Item 4
</div>
<div class="item">
Item 5
</div>
<div class="item">
Item 6
</div>
<div class="item">
Item 7
</div>
</div>

flexbox child margin right doesn't applied

I want to make equal gap for the children of flexbox. I used margin-right zero for each child, then applied the margin right back on the last child. But the margin right somehow doesn't applied. You can see after you scrolled to the end of the child.
see demo below
.flex {
display: flex;
background: pink;
width: 80px;
overflow: auto;
}
.item {
min-width: 20px;
height: 20px;
background: black;
margin: 8px;
margin-right: 0;
}
.item:last-child {
margin-right: 4px;
}
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
add another parent div and give overflow for parent div. For .flex class apply display:inline-flex; then it will works
.flex-parent{
overflow: auto;
width: 80px;
}
.flex {
display: flex;
background: pink;
min-width:100%;
float:left;
}
.flex-parent{
overflow: auto;
width: 80px;
}
.flex {
display: inline-flex;
background: pink;
/*min-width:100%;
float:left;*/
}
.item {
min-width: 20px;
height: 20px;
background: black;
margin: 8px;
margin-right: 0;
}
.item:last-child {
margin-right: 4px;
}
<div class="flex-parent">
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
.flex {
display: flex;
background: pink;
width: 200px;
overflow: auto;
justify-content: space-between;
}
.item {
min-width: 20px;
height: 20px;
background: black;
}
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>

How can I do two columns layout with flexbox and same spacing between items in column? [duplicate]

This question already has answers here:
CSS-only masonry layout
(4 answers)
Closed 4 years ago.
Hello! Can you help me? I'm trying to do two column layout with same spacing between items in same column.
.flex {
margin: 0;
padding-left: 0;
list-style: none;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-content: center;
}
.item {
width: 45%;
height: 200px;
margin-bottom: 10px;
background-color: navy;
}
.item:nth-child(1) {
height: 210px;
}
.item:nth-child(2) {
height: 500px;
}
.item:nth-child(3) {
height: 360px;
}
.item:nth-child(4) {
height: 400px;
}
.item:nth-child(5) {
height: 150px;
}
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
Thanks for every good advice & your help!
You could use column-count instead of flexbox. In this situation, there's no need to implement a flexible box layout. You're not trying to maintain identical sizes or adjust for any odd alignment.
.flex
{
margin: 0;
padding-left: 0;
list-style: none;
column-count: 2;
column-gap: 10px;
}
.item{
height: 200px;
margin-bottom: 10px;
background-color: navy;
break-inside: avoid;
}
.item:nth-child(1){height: 210px;}
.item:nth-child(2){height: 500px;}
.item:nth-child(3){height: 360px;}
.item:nth-child(4){height: 400px;}
.item:nth-child(5){height: 150px;}
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
Here's a way to use percentages for the column gap. It uses 0px column-gap and a negative margin on the container to adjust for the excess margins on the left and right.
.flex
{
margin: 0;
padding-left: 0;
list-style: none;
column-count: 2;
column-gap: 0;
margin: 0 -1.25%;
}
.item{
height: 200px;
margin-bottom: 10px;
background-color: navy;
break-inside: avoid;
margin: 0 2.5% 5%;
}
.item:nth-child(1){height: 210px;}
.item:nth-child(2){height: 500px;}
.item:nth-child(3){height: 360px;}
.item:nth-child(4){height: 400px;}
.item:nth-child(5){height: 150px;}
<div class="flex">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>

CSS Float elements

I guess this may have been asked a lot of times, but I've searched accross the forum and haven't find the answer for this case.
I've some divs "container" and some divs "item" all of them floated elements, and I want each "container" below the previous one.
I know I could achieve that without using floats on the containers. But I thought that using :after and clear: would be enough.
Why this doesn't work?
My code:
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="./style2.css">
</head>
<body>
<header></header>
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</body>
</html>
My CSS:
* {
margin: 0;
padding: 0;
}
header {
width: 100%;
min-height: 25px;
background-color: #444;
position: fixed;
overflow: auto;
}
.container {
float: left;
padding: 10px;
margin: 10px;
}
.container:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.item {
min-width: 50px;
min-height: 50px;
float: left;
background-color: lightblue;
border: 1px solid red;
margin: 5px;
}
Thank you
Just add clear:left to your .container rules
.container {
clear:left;
float: left;
padding: 10px;
margin: 10px;
}
jsFiddle example
You can use the clearfix approach by adding the .clearfix class to your .container divs:
.clearfix:before,
.clearfix:after{
content: " ";
display: table;
}
.clearfix:after{
clear: both;
}
<div class="container clearfix">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
With this approach, you don't need to float: left your .container, and also,
Clearfix it's going to help you each time a div has floated children by making it's height variable according to its children.
Another approach would be adding clear: left to your .container but I don't recommended it since it would be an specific approach solution.