reposition different divs differently - css

I want to stack div differently for small screens and I want to use css for it.
What I want to achieve is following:
for one page, the div class="three has to go UNDER the .header
for another page (uses the same code), the div class="two" has to go ABOVE the .header
I only managed to make .two go above .header, but the result is that I cannot make the .three go below the .header on my other page (the actual result is that the .three is also placed ABOVE the .header because of my css code). How to fix?
#media(max-width: 460px) {
.container {
display: flex;
flex-direction: column;
}
.header {
order: 2;
}
}
<div class="body">
<div class='container'>
<div class='header'>
<div class="one">
one
</div>
hello
</div>
<div class='sidebar'>
<div class="two">
two
</div>
<div class="three">
three
</div>
</div>
</div>
</div>

Your two and three are within in a div called sidebar. You can't remove them from this div and order them with header using CSS because header is not the same level as them. You should possibly consider re-structuring your HTML?
I have amended your example slightly to show you what I mean.
.container {
display: flex;
flex-direction: column;
}
.header {
order: 2;
}
.two {
order: 1;
}
.three {
order: 3;
}
<div class="body">
<div class='container'>
<div class='header'>
<div class="one">
one
</div>
hello
</div>
<div class="two">
two
</div>
<div class="three">
three
</div>
</div>
</div>

What you want is tricky because your HTML is grouping items two and three.
The very best solution is to rearrange your HTML.
Just in case that it is not possible, you can set a workaround with display: contents on the sidebar element (this makes the element disappear from the flow)
#media(max-width: 4600px) {
.container {
display: flex;
flex-direction: column;
background-color: lightblue;
}
.header {
order: 2;
}
.three {
order: 3;
}
.sidebar {
display: contents;
}
}
<div class="body">
<div class='container'>
<div class='header'>
<div class="one">
one
</div>
hello
</div>
<div class='sidebar'>
<div class="two">
two
</div>
<div class="three">
three
</div>
</div>
</div>
</div>

Related

Is there a way to make this using bootstrap 4?

I currently have something like this https://jsfiddle.net/qjs5vzL2/32/. I would like it if the display is smaller than md the card to be displayed right bellow the button it corresponds to and not at the bottom of the page. Is there a way to make this using bootstrap 4.
My code looks like
<div class="row">
<div class="col-md-3"><a>Foo</a></div>
<div class="col-md-3"><a>Bar</a></div>
<div class="col-md-3"><a>Foobar</a></div>
<div class="col-md-3"><a>Barfoo</a></div>
</div>
<div>
My Content
</div>
Something like this:
desired layout for screens smaller than md
Yes, You can use CSS order property for that but all boxes should have the same first parent. For that you can bring your card inside the links row and give it the order 2 in the small screens.
You can update your HTML with the following code
<div class="container my-4">
<div class="row">
<div class="col-md-3 nav-button text-center selected">Events</div>
<div class="col-md-3 nav-button text-center">Foo</div>
<div class="col-md-3 nav-button text-center">Bar</div>
<div class="col-md-3 nav-button text-center">Profile</div>
<div class="col-12 mt-3">
<div class="card">
<div class="card-header">
<h1>
My Events
</h1>
</div>
<div class="card-body">
...
</div>
</div>
</div>
</div>
</div>
And update Your CSS with follwing Code
.nav-button {
border: 1px solid black;
}
.nav-button.selected {
background-color: red;
color: black !important;
}
a {
color: inherit;
}
#media (max-width: 992px){
.container .row > div:first-of-type{
order: 1;
}
.container .row > div:nth-of-type(2){
order: 3;
}
.container .row > div:nth-of-type(3){
order: 4;
}
.container .row > div:nth-of-type(4){
order: 5;
}
.container .row > div:nth-of-type(5){
order: 2;
}
}

Adding a class "collapse" to flex grid creates uneven spacing

So, I am creating a grid system based on flexbox and everything is going quite swimmingly. The basics of my grid are:
<div class="row">
<div class="column"><p>Column</p></div>
<div class="column"><p>Column</p></div>
<div class="column"><p>Column</p></div>
</div>
And in my css:
.row {
margin: 10px 0;
display: flex;
flex-wrap: wrap;
}
.column {
padding: 10px;
flex: 1 1 0%;
}
Essentially, this makes the columns quite fluid, and they shrink/grow to fill all available space. This is great for me as I need to use this throughout various projects where I can't quite customize the grid for every single one. However, I have run into a small "issue". I was going to create a class called ".collapse" so I could collapse the left/right padding to have some columns fit right next together (for example: If I wanted a div with a background color (by adding a color class to the column=> .column .green) flush to an image in the next column). However, the spacing is all out of wack compared to row/columns above it.
<div class="row">
<div class="column purple collapse"><p>Column</p></div>
<div class="column red collapse"><p>Column</p></div>
<div class="column purple collapse"><p>Column</p></div>
<div class="column red collapse"><p>Column</p></div>
</div>
example screenshot here
As you can see in my little example mockup, they do kinda line up, but the right and left margins have "decreased". Is there any smart way around this? I tried adding "left/right margins" to the first-of-type and last-of-type, but this just gets a bit hacky as then anything added in between start having odd alignment issues.
For this kind of grid system, you usually would discourage using structural styling on the grid cells directly, and it lets you do something like this:
.row {
display: flex;
flex-flow: row wrap;
list-style: none;
padding: 0;
margin-left: -10px;
}
.column {
flex: 1 0 0;
padding-left: 10px;
}
.collapse { margin-left: 0; }
.collapse > .column { padding-left: 0; }
.red,
.purple {
padding: 10px;
margin-bottom: 10px;
}
.red { background-color: red; }
.purple { background-color: purple; }
<div class="row">
<div class="column">
<div class="purple">
<p>Column</p>
</div>
</div>
<div class="column">
<div class="red">
<p>Column</p>
</div>
</div>
<div class="column">
<div class="purple">
<p>Column</p>
</div>
</div>
<div class="column">
<div class="red">
<p>Column</p>
</div>
</div>
</div>
<div class="row collapse">
<div class="column">
<div class="purple">
<p>Column</p>
</div>
</div>
<div class="column">
<div class="red">
<p>Column</p>
</div>
</div>
<div class="column">
<div class="purple">
<p>Column</p>
</div>
</div>
<div class="column">
<div class="red">
<p>Column</p>
</div>
</div>
</div>
This approach uses no margins on the outer ends, which I find way more convenient.
It's worth noting that this kind os system is not all that useful anymore, with the advent of CSS Grid Layout, but there you have it.
On a side note, 0 is always 0, and it never needs a unit.

Grid columns without float

I'm trying to place seven columns of the same size in one row:
.seven-cols .col-hard-1 {
width: 14.28%;
*width: 14.28%;
float: left;
}
However, float:left screws up the all the next blocks on the page. Is there a way to make this grid without using float?
Codepen
Use display: flex; on the container and flex-grow: 1; and flex-basis: 0; on the items to make them all take up the same amount of available space.
.seven-cols {
display: flex;
}
.seven-cols .col-hard-1 {
flex-grow: 1;
flex-basis: 0;
}
<div class="seven-cols">
<div class="col-hard-1">
one
</div>
<div class="col-hard-1">
two
</div>
<div class="col-hard-1">
three
</div>
<div class="col-hard-1">
four
</div>
<div class="col-hard-1">
five
</div>
<div class="col-hard-1">
six
</div>
<div class="col-hard-1">
seven
</div>
</div>

Have div take up 1/6th of remaining parent div height

I am attempting to make a simple calendar using css.
I have a parent div that will contain the calendar, and I have a div within that that contains the header with "Monday", "Tuesday", etc and is of fixed height. I now want to add divs that represent the rows of the calendar and split the remaining space into six even rows. However, I can't figure out how to divide the REMAINING space into 6 parts. Everything I try makes the div 1/6th of the parent div.
Any tips would be appreciated.
HTML:
<div id="parent>
<div id="header">
ST
</div>
<div class="row">
hi
</div>
</div>
CSS:
.row{
width:100%;
height: 16.66%;
background-color:red;
}
When you want to distribute remaining space left by a flexible element, flexbox is the answer.
html, body, #parent {
margin: 0;
height: 100%;
}
#parent {
display: flex;
flex-direction: column;
}
#header {
background-color: green;
}
.row {
width: 100%;
flex: 1; /* Distribute remaining space equally among the rows */
background-color: red;
}
.row:nth-child(odd) {
background-color: blue;
}
<div id="parent">
<div id="header">Header</div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
</div>
There are several ways to do that, and to pick one I need to know more how it should be used.
This sample simply use CSS calc() and subtract 1/6 of the header from 1/6 of the parent.
html, body {
margin: 0;
}
#parent {
height: 100vh;
}
#header {
height: 60px;
background-color:green;
}
.row{
height: calc(16.66% - 10px);
background-color:red;
}
.row:nth-child(odd){
background-color:blue;
}
<div id="parent">
<div id="header">
Header
</div>
<div class="row">
</div>
<div class="row">
</div>
<div class="row">
</div>
<div class="row">
</div>
<div class="row">
</div>
<div class="row">
</div>
</div>

What is the proper arrangement for table-header-group?

I'm trying to build a footer that has two title columns and three content columns.
I'd like to use display: table-cell for the content columns, and believe I need to use display: table-header-group for the title columns.
While researching display: table-header-group, I could not find any documentation at all on how to use this CSS property. W3Cschools says the following.
table-header-group: Let the element behave like a thead element.
(source)
That unfortunately doesn't tell me how I should arrange my divs
So far I've got the following code, but I'm not sure if I'm using table-header-group correctly
.footer {
display: table;
}
.footer-box {
display: table-cell;
}
.footer-title-box {
display: table-header-group;
}
<div class="footer">
<div class="footer-title-box">
Title
</div>
<div class="footer-title-box">
Title
</div>
<div class="footer-box">
Content
</div>
<div class="footer-box">
Content
</div>
<div class="footer-box">
Content
</div>
</div>
If anyone has any experience with table-header-group and could shed some light on it, I would be incredibly grateful.
I don't have any experience with it but logic dictates that you can only have one thead so you can only have one table-header-group
So your structure should, perhaps, look more like this:
JSfiddle Demo Backup Link
.footer {
display: table;
width: 50%;
table-layout: fixed;
}
.footer-title-box {
display: table-header-group;
font-weight: bold;
}
.footer-row-box {
display: table-row-group;
}
.footer-box {
display: table-cell;
text-align: center;
}
<div class="footer">
<div class="footer-title-box">
<div class="footer-box">Title</div>
<div class="footer-box caption">Title</div>
</div>
<div class="footer-row-box">
<div class="footer-box">Content</div>
<div class="footer-box">Content</div>
<div class="footer-box">Content</div>
</div>
<div class="footer-row-box">
<div class="footer-box">Content</div>
<div class="footer-box">Content</div>
<div class="footer-box">Content</div>
</div>
<div class="footer-row-box">
<div class="footer-box">Content</div>
<div class="footer-box">Content</div>
<div class="footer-box">Content</div>
</div>
</div>

Resources