Question regarding styling. I have a row of div elements. They're all the same size except one, which is double in height and width. I'd like the second row of div elements to line up with the second half of the bigger div but now sure how to do it.
.wrapper {
width: 800px;
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
.box {
height: 50px;
width: 50px;
margin: 5px;
border: 1px solid black;
}
.box-5 {
height: 100px;
width: 100px
}
<div class="wrapper">
<div class="box box-1">Box 1</div>
<div class="box box-2">Box 2</div>
<div class="box box-3">Box 3</div>
<div class="box box-4">Box 4</div>
<div class="box box-5">Box 5</div>
<div class="box box-6">Box 6</div>
<div class="box box-7">Box 7</div>
<div class="box box-8">Box 8</div>
<div class="box box-9">Box 9</div>
<div class="box box-10">Box 10</div>
<div class="box box-11">Box 11</div>
<div class="box box-12">Box 12</div>
<div class="box box-13">Box 13</div>
<div class="box box-14">Box 14</div>
<div class="box box-15">Box 15</div>
<div class="box box-16">Box 16</div>
<div class="box box-17">Box 17</div>
<div class="box box-18">Box 18</div>
<div class="box box-19">Box 19</div>
</div>
I'd like box 13 - 16 to be on the left side of the bottom half of box 5 and box 17 - 19 to be on the right side.
You could use CSS display: grid and its companion grid-template-columns to fix this. Updated answer thanks to Maaz Syed Adeeb's comment.
grid-template-columns: repeat(13, auto);
(It should be thirteen instead of 12 boxes).
Only box number 5 gets an upgrade in styling to expand its box:
grid-column: span 2;
grid-row: span 2;
Read more about grid at MDN.
Updated snippet:
.wrapper {
width: 100%;
height: 100%;
display: grid;
grid-template-columns: repeat(13, auto);
grid-template-rows: auto;
grid-gap: 0;
}
.box {
height: 50px;
width: 50px;
margin: 5px;
border: 1px solid black;
}
.box-5 {
height: 100px;
width: 100px;
grid-column: span 2;
grid-row: span 2;
}
<div class="wrapper">
<div class="box box-1">Box 1</div>
<div class="box box-2">Box 2</div>
<div class="box box-3">Box 3</div>
<div class="box box-4">Box 4</div>
<div class="box box-5">Box 5</div>
<div class="box box-6">Box 6</div>
<div class="box box-7">Box 7</div>
<div class="box box-8">Box 8</div>
<div class="box box-9">Box 9</div>
<div class="box box-10">Box 10</div>
<div class="box box-11">Box 11</div>
<div class="box box-12">Box 12</div>
<div class="box box-13">Box 13</div>
<div class="box box-14">Box 14</div>
<div class="box box-15">Box 15</div>
<div class="box box-16">Box 16</div>
<div class="box box-17">Box 17</div>
<div class="box box-18">Box 18</div>
<div class="box box-19">Box 19</div>
</div>
Just need to add 10 more pixel in width
.box-5 {
height: 100px;
width: 110px;
}
.wrapper{
width: 800px;
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
.box{
height: 50px;
width: 50px;
margin: 5px;
border: 1px solid black;
}
.box-5 {
height: 100px;
width: 110px;
}
<div class="wrapper">
<div class="box box-1">Box 1</div>
<div class="box box-2">Box 2</div>
<div class="box box-3">Box 3</div>
<div class="box box-4">Box 4</div>
<div class="box box-5">Box 5</div>
<div class="box box-6">Box 6</div>
<div class="box box-7">Box 7</div>
<div class="box box-8">Box 8</div>
<div class="box box-9">Box 9</div>
<div class="box box-10">Box 10</div>
<div class="box box-11">Box 11</div>
<div class="box box-12">Box 12</div>
<div class="box box-13">Box 13</div>
<div class="box box-14">Box 14</div>
<div class="box box-15">Box 15</div>
<div class="box box-16">Box 16</div>
<div class="box box-17">Box 17</div>
<div class="box box-18">Box 18</div>
<div class="box box-19">Box 19</div>
</div>
Related
I have a category menu and list of projects whereby on the main tag we have
margin-top: 250px;
height: calc(100vh - 250px);
From the above, if the projects' container has a height bigger than calc(100vh - 250px), then by default it's scrollable.
If the projects' container has a height smaller than calc(100vh - 250px), then we flex-end everything (menu, container) to the bottom of the page. The projects container has overflow-y: scroll;
This is working perfectly in all browsers except MacOS (Catalina) Safari 13 and below.
In Safari 13, all the projects that are supposed to be scrollable are reduced in height to fit the available height calc(100vh - 250px).
Normal and expected view
Faulty view Safari 13, using https://testingbot.com/ to test
You can view the page here https://category-scrollings.tiiny.site/ (Free trial expiring in 7 days).
const panels = Array.from(document.querySelectorAll(".projects-container"));
const menus = Array.from(document.querySelectorAll(".nav-item"));
menus.forEach(function (menu) {
menu.addEventListener("click", function (event) {
console.log("event:", event.target);
menus.forEach(function (_menu, index) {
const _panel = panels[index];
_menu.classList.remove("clicked");
_panel.classList.remove("active");
});
const panelId = `${event.target.id.split("-")[0]}-panel`;
const panel = document.getElementById(panelId);
event.target.classList.add("clicked");
panel.classList.add("active");
});
});
body {
height: 100vh;
overflow: hidden;
}
main {
display: flex;
-webkit-box-pack: start;
justify-content: flex-start;
align-items: flex-start;
width: 100%;
}
.container {
display: flex;
margin-top: 250px;
flex-direction: column;
-webkit-box-pack: end;
justify-content: flex-end;
align-items: flex-start;
width: calc(50vw - 25px);
height: calc(100vh - 250px);
}
.nav {
display: flex;
flex-direction: row;
-webkit-box-pack: start;
justify-content: flex-start;
top: 200px;
-webkit-box-align: center;
align-items: center;
padding: 6px 0px;
background-color: rgb(255, 255, 255);
position: sticky;
z-index: 1000;
width: 100%;
}
.nav-item {
cursor: pointer;
font-size: 0.688rem;
line-height: 1.334rem;
margin: 0px;
font-family: "ABC Favorit", sans-serif;
color: #ccc;
text-transform: uppercase;
letter-spacing: 0.6px;
font-weight: 600;
margin-right: 16px;
}
.nav-item.clicked {
color: #000;
}
.projects-container.active {
display: flex;
}
.projects-container {
display: none;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: end;
-ms-flex-pack: end;
-webkit-justify-content: flex-end;
justify-content: flex-end;
-webkit-align-items: flex-center;
-webkit-box-align: flex-center;
-ms-flex-align: flex-center;
align-items: center;
height: auto;
width: 100%;
overflow-y: scroll;
padding-bottom: 20px;
}
.projects {
display: flex;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: start;
-ms-flex-pack: start;
-webkit-justify-content: flex-start;
justify-content: flex-start;
-webkit-align-items: flex-start;
-webkit-box-align: flex-start;
-ms-flex-align: flex-start;
align-items: flex-start;
width: 100%;
overflow-y: scroll;
}
.project {
display: flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
justify-content: space-between;
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
width: 100%;
border-top: 0.9px solid #d9d9d9;
}
.boxes {
display: flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: start;
-ms-flex-pack: start;
-webkit-justify-content: flex-start;
justify-content: flex-start;
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
width: auto;
cursor: pointer;
min-width: 140px;
}
.box {
display: block;
height: 27px;
width: 7px;
}
.box.blue {
background-color: blue;
}
.box.white {
background-color: white;
}
.box.yellow {
background-color: yellow;
}
.box.red {
background-color: red;
}
.box.black {
background-color: black;
}
.title-container {
display: -ms-flexbox;
display: flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: start;
-ms-flex-pack: start;
-webkit-justify-content: flex-start;
justify-content: flex-start;
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
max-width: 355px;
width: 100%;
cursor: pointer;
padding: 9px 0;
}
.title {
margin: 0;
color: #000;
font-family: "ABC Favorit", sans-serif;
max-width: 255px;
width: 100%;
font-size: 0.875rem;
font-weight: 400;
text-transform: uppercase;
line-height: 0.875rem;
margin-top: 1.7px;
margin-left: 50px;
margin-right: 50px;
}
<main>
<div class="container">
<div class="nav">
<div id="all-menu" class="nav-item clicked">All</div>
<div id="cars-menu" class="nav-item">Cars</div>
<div id="fashion-menu" class="nav-item">Fashion</div>
</div>
<div id="all-panel" class="projects-container all active">
<div class="projects">
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">IULH VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ULHF VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">UYJHF VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">UYJHF VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">UYJHF VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">UYJHF VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">UYJHF VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ALIUIYF VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">HEYsh VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">YTKGUIY VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">rjke VERBINDEN</p>
</div>
</div>
</div>
</div>
<div id="cars-panel" class="projects-container cars">
<div class="projects">
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ZUKUNFT VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">AMDILK VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">IULH VERBINDEN</p>
</div>
</div>
</div>
</div>
<div id="fashion-panel" class="projects-container fashion">
<div class="projects">
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">UYJHF VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">ALIUIYF VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">HEYsh VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">YTKGUIY VERBINDEN</p>
</div>
</div>
<div class="project">
<div class="boxes">
<div class="box blue"></div>
<div class="box yellow"></div>
<div class="box white"></div>
<div class="box red"></div>
<div class="box black"></div>
</div>
<div class="title-container">
<p class="title">rjke VERBINDEN</p>
</div>
</div>
</div>
</div>
</div>
</main>
Click Expand Snippet to see all
I have figured out the answer myself. It appears I needed to assign a height to the project class
// either
.project {
...
height: 35px;
}
// Or
.project {
...
min-height: fit-content;
height: 100%;
}
I am building a Gantt chart using CSS. I calculate the 'offset' of a task from the first date in the header, and the width of the task based on the duration as a percentage of the total range between the dates in the header - see example. This largely works fine except for when there are too many months in the header, and so the overflow starts to scroll, because the percentage of the offset & width are applied to the container width excluding the scrollable portion. How can I fix this so the these values are applied to the actual scrollable width of the container? (Hopefully without using any JS)
In the example, the task should start at 50% (i.e. start of month 7) and run for 25% (i.e. to end of month 9). You can check this by removing the min-width:300px;
(Note that the red background will ultimately be transparent)
.container {
overflow-x: auto;
width:100%;
background-color:#eee;
}
.container .months {
display:flex;
flex-direction:row;
}
.container .months .month {
min-width:300px;
padding:5px 10px;
border:solid 1px black;
flex: 1 0 0%;
}
.container .bars .bar {
display:flex;
flex-direction:row;
}
.container .bars .bar .spacer {
background-color:red;
}
.container .bars .bar .task {
background-color:yellow;
}
<div class="container">
<div class="months">
<div class="month">Month 1</div>
<div class="month">Month 2</div>
<div class="month">Month 3</div>
<div class="month">Month 4</div>
<div class="month">Month 5</div>
<div class="month">Month 6</div>
<div class="month">Month 7</div>
<div class="month">Month 8</div>
<div class="month">Month 9</div>
<div class="month">Month 10</div>
<div class="month">Month 11</div>
<div class="month">Month 12</div>
</div>
<div class="bars">
<div class="bar">
<span class="spacer" style="width:50%"></span>
<span class="task" style="width:25%">Task 1</span>
</div>
</div>
</div>
display: grid on the container will fix the issue. The bars will be inside a track that has the same size as the months element
.container {
overflow-x: auto;
display: grid;
background-color:#eee;
}
.container .months {
display:flex;
flex-direction:row;
}
.container .months .month {
min-width:300px;
padding:5px 10px;
border:solid 1px black;
flex: 1 0 0%;
}
.container .bars .bar {
display:flex;
flex-direction:row;
}
.container .bars .bar .spacer {
background-color:red;
}
.container .bars .bar .task {
background-color:yellow;
}
<div class="container">
<div class="months">
<div class="month">Month 1</div>
<div class="month">Month 2</div>
<div class="month">Month 3</div>
<div class="month">Month 4</div>
<div class="month">Month 5</div>
<div class="month">Month 6</div>
<div class="month">Month 7</div>
<div class="month">Month 8</div>
<div class="month">Month 9</div>
<div class="month">Month 10</div>
<div class="month">Month 11</div>
<div class="month">Month 12</div>
</div>
<div class="bars">
<div class="bar">
<span class="spacer" style="width:50%"></span>
<span class="task" style="width:25%">Task 1</span>
</div>
</div>
</div>
Here's a codepen illustrating the issue: https://codepen.io/robertcooper_rc/pen/jOYbdbR
I'm using a CSS grid to create a table layout. It's been working well, but I'm having an issue with the behavior of position: sticky on one of the columns in my grid.
I have a horizontally scrollable table with 4 columns and the first column is sticky.
When I scroll to the right, the first column does stick to the left as is expected.
However, when scrolling starts nearing the end of the table's horizontal space, the first no longer maintains its sticky position to the left edge of the table.
I've noticed that if I remove the HTML markup for the <aside>, the sticky column behavior works as expected. However, I need the <aside> to be present.
Any ideas on how to fix this with CSS while maintaining the DOM structure?
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.container {
display: flex;
width: 300px;
padding: 0.5rem;
border: 1px solid red;
}
aside {
padding-right: 1rem;
width: 100px;
}
.table {
min-width: 0;
overflow: scroll;
}
.row {
display: grid;
grid-template-columns: repeat(4, 100px);
}
.col {
border: 1px solid black;
background: #eee;
}
.sticky {
position: sticky;
left: 0;
z-index: 1;
background: lightblue;
}
<div class="container">
<aside>
My aside
</aside>
<div class="table">
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
</div>
</div>
So the problem actually falls with the min-width set on .table. The width is not defined to the end of the row which is affecting the behavior of the sticky elements at row-end.
You'll notice if you exchange min-width: 0; to min-width: 100%; it functions as you would like, but then the table overflows outside of .container.
A stickily positioned element is treated as relatively positioned until its containing block crosses a specified threshold (such as setting top to value other than auto) within its flow root.
MDN CSS/Position
So with that said, the elements with the scroll need to have a defined width so the sticky element knows to stay sticky.
A simple solution would be to nest all of the .table elements in another wrapper that has a defined width. I chose 300px based on the rendered width of the content and the container.
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.container {
display: flex;
width: 300px;
padding: 0.5rem;
border: 1px solid red;
}
aside {
padding-right: 1rem;
width: 100px;
}
.table {
min-width: 0;
overflow: scroll;
}
.row {
display: grid;
grid-template-columns: repeat(4, 100px);
}
.col {
border: 1px solid black;
background: #eee;
}
.sticky {
position: sticky;
left: 0;
z-index: 1;
background: lightblue;
}
.wrapper {
width: 300px;
}
<div class="container">
<aside>
My aside
</aside>
<div class="table">
<div class="wrapper">
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
<div class="row">
<div class="col sticky">Col 1</div>
<div class="col">Col 2</div>
<div class="col">Col 3</div>
<div class="col">Col 4</div>
</div>
</div>
</div>
</div>
Here is some of my CSS code which I am using to create shadow boxes inside the grid view. I tried to find HTML something like rowspan and colspan but I did't got any idea.
.box{
width: 100%;
height: 240px;
background: white;
border-color: black;
border-width: 2px;
-webkit-box-shadow: -1px 2px 5px 1px rgba(10,0,10,1);
-moz-box-shadow: -1px 2px 5px 1px rgba(10,0,10,1);
box-shadow: -1px 2px 5px 1px rgba(10,0,10,1);
};
You can create columns with Bootstrap Grid system. You can try following structural.
<div class="container">
<div class="row">
<div class="col-sm-8">
<div class="box h-100">Box 1</div>
</div>
<div class="col-sm-4">
<div class="box mb-3">Box 2</div>
<div class="box mb-3">Box 3</div>
<div class="box mb-3">Box 4</div>
<div class="box">Box 5</div>
</div>
</div>
<div class="row my-3">
<div class="col-sm-4">
<div class="box">Box 6</div>
</div>
<div class="col-sm-4">
<div class="box">Box 7</div>
</div>
<div class="col-sm-4">
<div class="box">Box 8</div>
</div>
</div>
<div class="row my-3">
<div class="col-sm-4">
<div class="box">Box 9</div>
</div>
<div class="col-sm-4">
<div class="box">Box 10</div>
</div>
<div class="col-sm-4">
<div class="box">Box 11</div>
</div>
</div>
</div>
I'm trying to implement layout where on desktop screen size we have 2 columns, and one column on mobile/tablets
is it possible to make this code:
<div class="posts-2-col">
<div class="post">Post 1</div>
<div class="post">Post 2</div>
<div class="post">Post 3</div>
<div class="post">Post 4</div>
<div class="post">Post 5</div>
<div class="post">Post 6</div>
</div>
to render like this:
(knowing that height of each post can e different)
I just put the fixed height to image a higher post
Jsfiddle
*{
box-sizing: border-box;
}
.posts-2-col{
width: 300px;
margin: 0 -10px;
}
.posts-2-col .post{
border: 1px solid red;
width: 135px;
margin: 0 5px;
margin-bottom: 10px;
float: left;
}
<div class="posts-2-col">
<div class="post">Post 1</div>
<div class="post">Post 2</div>
<div class="post" style="height: 50px">Post 3</div>
<div class="post">Post 4</div>
<div class="post">Post 5</div>
<div class="post">Post 6</div>
<div class="post">Post 7</div>
</div>