Alternating grid colours - html

I'm wanting to achieve a two column grid layout that has grid blocks with alternating colours. However, achieving this with a simple nth-child(odd) or nth-child(even)won't do the trick.
I'm pretty sure what I want can be achieved using a touch of nth-child trickery, rather than a JS solution, but not quite sure how.
Here's a pen of what I'm wanting to achieve: http://codepen.io/abbasinho/pen/Gbnze
Here's the HTML as it is in the pen, i'd like to avoid the additional class to add the colors:
<div class="grid-holder">
<div class="grid red"></div>
<div class="grid blue"></div>
<div class="grid blue"></div>
<div class="grid red"></div>
<div class="grid red"></div>
<div class="grid blue"></div>
<div class="grid blue"></div>
<div class="grid red"></div>
</div>
SASS:
.grid-holder {
width: 50%;
margin: 0 auto;
overflow: hidden;
}
.grid {
width: 50%;
height: 200px;
float: left;
&.red {
background: red;
}
&.blue {
background: blue;
}
}

As you repeat a pattern every 4 elements (red/blue/blue/red), you can achieve this with :nth-child(4n+x) variations :
<div class="grid-holder">
<div class="grid"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="grid"></div>
</div>
.grid-holder {
width: 50%;
margin: 0 auto;
overflow: hidden;
}
.grid {
width: 50%;
height: 200px;
float: left;
background: red;
// The 2nd and the 3rd element in our pattern will be blue
&:nth-child(4n+2),
&:nth-child(4n+3) {
background: blue;
}
}

Related

How to set three images horizontal in php

I am trying to set three images in horizontal but the images always comes out vertically
I have took reference from internet but still I cant solve this problem can someone please help me how to set it horizontally
.categories{
margin: 70px 0;
}
.col-3{
flex-basis: 30%;
min-width: 250px;
margin-bottom: 30px;
}
.col-3 img{
width: 100%;
}
.small-container{
max-width: 1080px;
margin: auto;
padding-left: 25px;
padding-right: 25px;
}
<div class="categories">
<div class="small-container">
<div class="row">
<div class="col-3">
<img src="assets/offer.jpg">
<div class="col-3">
<img src="assets/offer2.jpg">
<div class="col-3">
<img src="assets/offer3.jpg">
</div>
</div>
if you are not using Bootstrap try this code
.categories{
margin: 70px 0;
}
.col-3{
flex-basis: 30%;
min-width: 250px;
margin-bottom: 30px;
}
.col-3 img{
width: 100%;
}
.small-container{
max-width: 1080px;
margin: auto;
padding-left: 25px;
padding-right: 25px;
}
.row{
flex-direction: row!important;
display:flex;
}
<div class="categories">
<div class="small-container">
<div class="row">
<div class="col-3">
<img src="assets/offer.jpg">
</div>
<div class="col-3">
<img src="assets/offer2.jpg">
</div>
<div class="col-3">
<img src="assets/offer3.jpg">
</div>
</div>
</div>
</div>
I list out some changes in your code
Close Every Divisions <div></div>
add row class like this
.row{
flex-direction: row!important;
display:flex;
}
if you are using bootstrap
Close Every Divisions <div></div>
add class in your HTML d-flex flex-row
like this
<div class="categories">
<div class="small-container">
<div class="d-flex flex-row">
<div class="col-3">
<img src="assets/offer.jpg">
</div>
<div class="col-3">
<img src="assets/offer2.jpg">
</div>
<div class="col-3">
<img src="assets/offer3.jpg">
</div>
</div>
</div>
</div>
The problem: I see you have used the flex-basis property. This property belongs to Flexbox and only works with Flex elements.
The solution:
You need to make .row a flex container like so:
.row {
display: flex;
}
By the way, your 2nd and 3rd col-3 don't have an ending tag. Please close them using an ending tag.
Some resources to learn Flexbox:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
https://flexboxfroggy.com

Flexbox under overflow not taking full width

I've simplified my HTML/CSS in a jsfiddle of what I'm trying to achieve without success.
I cannot make the .problem take full width. The content is represented by the blue background while the box itself is red.
I'm trying to make all the scrollable content have full width and a blue background since I guess the background not appearing after the scrollable content is a problem of width.
What I've taken a look so far:
Flexbox not full width
Flexbox: how to get divs to fill up 100% of the container width without wrapping?
Fill 100% width of scrolling flex container
To clarify: giving .problem a fixed width does give me the effect I want but not the solution since the content is dynamic and I cannot know its width.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
position: relative;
}
html,
body {
width: 100%;
height: 100%;
}
.bg {
display: flex;
background-color: gray;
width: 100%;
height: 100%;
}
.usable {
display: flex;
height: 90%;
width: 100%;
background-color: white;
}
.box {
position: absolute;
top: 0;
left: 0;
height: 200px;
width: 300px;
display: flex;
align-items: flex-start;
align-content: flex-start;
flex-direction: column;
background-color: red;
}
.top {
display: flex;
height: 20px;
background-color: yellow;
width: 100%;
}
.bottom {
width: 100%;
flex: 0 1 100%;
overflow: auto;
}
.contents {
height: 100%;
display: flex;
flex-direction: column;
}
.forms {
flex: 0 1 100%;
}
.problem {
display: flex;
height: 100%;
background-color: blue;
}
.row {
display: flex;
align-items: center;
}
.cell {
align-self: stretch;
flex: 1 0 100px;
}
<div class="bg">
<div class="usable">
<div class="box">
<div class="top">
Top
</div>
<div class="bottom">
<div class="contents">
<div class="forms">
<div class="problem">
<div class="data">
<div class="row">
<div class="cell">2020-06-28 01:34:46</div>
<div class="cell">2020-08-13 00:21:06</div>
<div class="cell">test1</div>
<div class="cell"></div>
<div class="cell">Basic User</div>
</div>
<div class="row">
<div class="cell">2020-06-28 01:34:46</div>
<div class="cell">2020-08-13 13:42:26</div>
<div class="cell">test2</div>
<div class="cell"></div>
<div class="cell">Basic User</div>
</div>
<div class="row">
<div class="cell">2020-06-28 01:34:46</div>
<div class="cell">2020-08-13 13:42:26</div>
<div class="cell">test3</div>
<div class="cell"></div>
<div class="cell">Basic User</div>
</div>
<div class="row">
<div class="cell">2020-06-28 01:34:46</div>
<div class="cell">2020-08-13 13:42:37</div>
<div class="cell">test4</div>
<div class="cell"></div>
<div class="cell">Basic User</div>
</div>
<div class="row">
<div class="cell">2020-06-28 01:34:46</div>
<div class="cell">2020-08-13 13:42:37</div>
<div class="cell">test5</div>
<div class="cell"></div>
<div class="cell">Basic User</div>
</div>
<div class="row">
<div class="cell">2020-06-28 01:34:46</div>
<div class="cell">2020-08-13 13:42:37</div>
<div class="cell">test6</div>
<div class="cell"></div>
<div class="cell">Basic User</div>
</div>
<div class="row">
<div class="cell">2020-06-28 01:34:46</div>
<div class="cell">2020-08-13 13:42:37</div>
<div class="cell">test7</div>
<div class="cell"></div>
<div class="cell">Basic User</div>
</div>
<div class="row">
<div class="cell">2020-06-28 01:34:46</div>
<div class="cell">2020-08-13 13:42:58</div>
<div class="cell">test8</div>
<div class="cell"></div>
<div class="cell">Basic User</div>
</div>
<div class="row">
<div class="cell">2020-06-28 01:34:46</div>
<div class="cell">2020-08-13 13:42:58</div>
<div class="cell">test9</div>
<div class="cell"></div>
<div class="cell">Basic User</div>
</div>
<div class="row">
<div class="cell">2020-06-28 01:34:46</div>
<div class="cell">2020-08-13 13:43:11</div>
<div class="cell">test10</div>
<div class="cell"></div>
<div class="cell">Basic User</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
The problem is that the container with the blue background is taking the width of the parent and not the content, to solve this you can change its property "display" to "inline-flex".
Then if you change the "flex" property of the .cell class to a "width" property the width you set will count for the parent and it will reach the blue container making it fill all the content.
.problem {
display: inline-flex;
height: 100%;
background-color: blue;
}
.cell {
align-self: stretch;
width: 100px;
}
To allow elements to grow according to content, you can replace 'width' by 'min-width'.
This makes sure your box is a certain size for styling purposes but allows it to grow.
Then you can use 'max-width' to limit the amount it can grow.
So, change 'width: 100%' by 'min-width: 100%'.
You can find more here: https://css-tricks.com/boxes-fill-height-dont-squish/

Flexbox items with min width and adaptive to content

I want to make flexbox with responsive to content items.
Here is example code:
HTML:
.container {
width: 200px;
height: 400px;
display: flex;
flex-flow: row wrap;
}
.item {
flex-basis: 40px;
background-color: orange;
margin: 0 10px 10px 0;
}
<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">this is text content and again and again this is text content and again and again</div>
</div>
And this is what I want to achieve:
How can I achieve that behavior? Thanks!)
If you want your page to be responsive to different resolution then u shouldn't adopt the habit of defining height and width in terms of px.
instead try to put width and height interms of % , width defined in % tend to adjust with resolution but width defined in terms of px tend to remain static and insensitive to changing resolution.
try this instead,
.container {
width: 800px;
height: 400px;
display: flex;
flex-flow: row wrap;
}
.item {
flex-basis: 25%;
background-color: orange;
margin: 0 10px 10px 0;
height: 50%;
}
.items {
flex-basis: 51%;
background-color: orange;
margin: 0 10px 10px 0;
height: 50%;
}
<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="items">this is text content and again and again this is text content and again and again</div>
<div class="item"></div>
</div>
This Works fine. All the best
Any reason you have a strong attachment to flexbox? This would be super easy using the bootstrap grid. (I see you have the container class, so you likely already have bootstrap installed)
<div class="container">
<div class="item col-md-3"></div>
<div class="item col-md-3 col-md-offset-1"></div>
<div class="item col-md-3 col-md-offset-1"></div>
<div class="item col-md-3"></div>
<div class="item col-md-3 col-md-offset-1"></div>
<div class="item col-md-3 col-md-offset-1"></div>
<div class="item col-md-6">this is text content and again and again this is text content and again and again</div>
<div class="item col-md-3 col-md-offset-1"></div>
</div>
If you're not into using bootstrap, CSS3 now has a native grid layout which should also suit your purpose.
In your situation, I think this would be the way to go. Flexbox is a great layout, but it's meant more for dynamic displays where you don't need precise control over the size/dimension of the items.
You can add a class, let's say extra.
And add assign property of max-width:max-content; to it.
.container {
display: flex;
width: 300px;
height: 300px;
flex-wrap: wrap;
border: 2px solid red;
}
.item {
flex: 1;
min-width: calc(100px - 10px);
max-width: calc(100px - 10px);
height: calc(100px - 10px);
margin: 5px;
background: green;
}
.extra {
max-width: max-content !important;
max-width: -moz-fit-content !important;
color: white;
}
<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 extra">this is extra text and expands</div>
<div class="item"></div>
</div>

Center align HTML Elements, while adding new items from left

I have a parent div containing an unknown number of smaller divs that are used like large icon-like buttons. If a row of child divs is full, I would like them to have equal margins on each side (ie. centered), but if a row is not full I would like them to be filled in from the left side (but still in columns with the elements above). Is there a way to do this with CSS? Resizing the window should maintain the centering and add/remove columns as necessary. All the child div widths are known.
Here's a crappy image of the behavior I'm trying for:
Okay I figured out a solution for this that both allows for equal margins and ALSO aligns divs left in the last row. The caveat is that it uses hidden elements, so the container is taller than the visible elements it contains.
Also it adds a bunch of extra markup to your code. If I thought of any other way to do this, I would do it differently. Sorry.
JSFiddle: https://jsfiddle.net/88qadmo3/2/
#container > div {
margin: 10px;
width: 100px;
height: 100px;
}
.floatleft {
background-color: red;
}
.invis {
visibility: hidden;
}
#container {
display: flex;
flex-wrap: wrap;
justify-content: center;
background-color: #DDDDDD;
}
.clearfix {
clear: both;
}
<div id="outer">
<div id="container">
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="floatleft"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
<div class="invis"></div>
</div>
</div>
You can do this by wrapping each of the child elements in an element and using CSS media queries to change the size of the wrappers. Then, just use % width on the wrappers based on what you want the column sizes to be.
When you run the example, shrink it to <400px so you can see it move from 6 columns to 3 (you'll need to run the code snippet on a full-page to see this).
.parent {
border: 2px solid red;
}
.child {
width: 100px;
display: block;
margin: 0 auto;
border: 1px solid black;
}
.wrapper {
display: inline-block;
text-align: center;
float: left;
width: 16.66%;
margin: 0;
padding: 0;
box-sizing: border-box;
}
.clear {
clear: both;
}
#media (max-width: 800px) {
.wrapper {
width: 20%;
}
}
#media (max-width: 600px) {
.wrapper {
width: 25%;
}
}
#media (max-width: 400px) {
.wrapper {
width: 33.33%;
}
}
<div class="parent">
<div class="wrapper">
<div class="child">1</div>
</div>
<div class="wrapper">
<div class="child">2</div>
</div>
<div class="wrapper">
<div class="child">3</div>
</div>
<div class="wrapper">
<div class="child">4</div>
</div>
<div class="wrapper">
<div class="child">5</div>
</div>
<div class="wrapper">
<div class="child">6</div>
</div>
<div class="clear"></div>
</div>
Just use float left, and items of the width (the original fiddle):
<div class="container">
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
<div class="item">Cats</div>
</div>
.container {
overflow: auto;
}
.item {
width: 50px;
float: left;
margin: 0 10px;
}

Div spacing offset using calc()

I have 3 vertical rows which are divided on my screen using calc()
I'm using JS to set the height for each box type
width: calc(1/3 * 100%);
This is the javascript to set the height of each block, It's setting the height equal to the width
and in the case of a long box it's setting the height to half the width.
$('.box1').height( $('.box1').width() );
$('.box2').height( $('.box2').width() / 2 );
$('.box4').height( $('.box4').width() );
I have a weird offset between the columns in the row ( see screenshot )
You can view the page here http://cloudlabme.webhosting.be/4sr
This is the HTML of the two vertical rows
<div class="container">
<div class="row">
<div class="box box4 box-event" style="background-image: url(build/img/events/2.jpg)"><h1>II</h1></div>
<div class="box box2 box-medium"></div>
</div>
<div class="row">
<div class="box box2 box-light"></div>
<div class="box box1 box-dark"><h3>Hier</h3></div>
<div class="box box1 box-medium"></div>
<div class="box box2"></div>
</div>
</div>
This is my CSS
.container {
position: relative;
width: 100%;
max-width: $breakpoint;
margin: 0 auto;
z-index: 0;
}
.row {
float: left;
width: calc(1/3 * 100%);
background: #f2f2f2;
}
/* BOX */
.box {
&.box-light {background: #fff;}
&.box-medium {background: #666;}
&.box-dark {background: #111;}
}
.box1 {
float: left;
width:50%;
background: #ff4444;
}
.box2 {
clear: left;
width: 100%;
background: #ff6666;
}
.box4 {
clear: left;
width: 100%;
background: #ff8888;
}
Thanks! This is killing my brain!
Most probably, the one pixel gap is caused by rounding off error. The only solution I could think of is to force the container width to be a multiple of 3 using JavaScript.
A better solution is to use CSS table display. Set 33.33333333% width on the first two "cells" and let the third one autosize itself. The heights will still be off by one or two pixel but this is better than using calc() and fighting with rounding issues.
$(window).on("load resize", function() {
$('.box1').height($('.box1').width());
$('.box2').height($('.box2').width() / 2);
$('.box4').height($('.box4').width());
});
.container {
display: table;
width: 100%;
}
.row {
display: table-cell;
vertical-align: top;
background: #CCC;
}
.row:first-child, .row:first-child + .row {
width: 33.33333333%;
}
.box1 {
float: left;
width: 50%;
background: #C99;
}
.box2 {
clear: left;
background: #C66;
}
.box4 {
clear: left;
background: #C33;
}
h1, h3 {
margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="box box4">
<h1>II</h1>
</div>
<div class="box box2"></div>
</div>
<div class="row">
<div class="box box2"></div>
<div class="box box1">
<h3>Hier</h3>
</div>
<div class="box box1"></div>
<div class="box box2"></div>
</div>
<div class="row">
<div class="box box2"></div>
<div class="box box4"></div>
</div>
</div>