How can I shrink a 4-column flexbox layout to 2 columns in Polymer? - css

I have a dashboard I made with Polymer and the flex layout, however there's some issues. Basically I need it to be 4 columns long, but when the screen shrinks I need it to go to 2 columns, and then finally to 1 column. (basically laptop - 4, tablet - 2, phone - 1). Right now it goes 4 to 3 to 2 to 1. How can I achieve the 4 to 2 to 1?
Here's what I currently have:
<style>
#container {
display: flex;
-webkit-flex-wrap: wrap; /* Safari */
flex-wrap: wrap;
-webkit-flex-direction: row; /* Safari */
flex-direction: row;
-webkit-justify-content: space-between; /* Safari */
justify-content: space-between;
}
</style>
and
<div id="container">
<paper-card heading="ep-gw-ops-node1">
<div class="card-content">
<paper-card heading="Temperature" id="temperature" class="blue" style="width: 275px; margin: 2px">
<div class="card-content">
<div>
<h3 id="tv_temperature" style="color:#fff; text-align: center"> 0 </h3>
</div>
<div style="" id="chart_div_0"></div>
</div>
</paper-card>
...
...
...
</paper-card>
</div>
I've attached a basic wireframe of what I am trying to accomplish.
If there is a better way to do it without paper-cards I'm more than open to suggestions.
Note* the last column will be lowered so the 'node 1' header will stretch all the way across and the columns will be the same size.

I do something like this
<dom-module id="my-element">
<template>
<style>
.grouph[wide-layout] {
#apply(--layout-horizontal);
#apply(--layout-wrap);
#apply(--layout-center-justified);
}
.grouph:not([wide-layout]). .groupv {
#apply(--layout-vertical);
}
</style>
<iron-media-query query="(min-width: 600px)" query-matches="{{wide}}"></iron-media-query>
<div class="grouph" wide-layout$="[[wide]]">
<paper-card>...</paper-card>
<paper-card>...</paper-card>
</div>
</template>
<script>
Polymer ({
is: 'my-element'
});
</dom-module>
This doesn't match your case exactly but the principal is the same. Use a media query to define a boolean property (in my case wide) that can be set for one of your two cases and unset for the other.
Use wide-layour$="[[wide]]" on an element that you want to alter dependant on the width.
Use CSS with a selector of [wide-layout] or :not([wide-layout]) to define how a container lays out its children.
You can do it at multiple levels to get whatever re-arrangement you want.

Related

Bulma level item height is too small

How could i make sure my <p class="title"></p> and
<p class="subtitle"></p> are displayed underneath of eachother because right now they are being displayed next to eachother
You can make use of flexbox, it'll definitely solve your problem.
Example
If you want to arrange two items underlying with each other:
.container {
display: flex;
flex-direction: column;
}
<div class="container">
<p class="title">Hello</p>
<p class="subtitle">World</p>
</div>
If you want to arrange two items next to each other.
.container {
display: flex; /*by default it will align items in one row*/
}
<div class="container">
<p class="title">Hello</p>
<p class="subtitle">World</p>
</div>

is it possible to conditionally right align an element using CSS?

I'm trying to left align B and then right align C only if there is space, if there is not enough space, how do I make just position itself after B instead of right aligning? is this only doable with javascript or can I do it from CSS?
<div class='A'>
<div class='B'/>
<div class='C'/>
<div>
I actually have it working using javascript to apply css classes and update based on those, but I was wondering if it was possible without doing that.
You can use flexbox. By using justify-content:space-between; items takes space in between of them
.A{
display:flex;
justify-content:space-between;
}
<div class="A">
<div class="B">B</div>
<div class="C">C</div>
</div>
Try out display: flex
like this:
<div class=‚flex a‘>
<div class=‚b‘></div>
<div class=‚spacer‘></div>
<div class=‚c‘></div>
</div>
And use this CSS
.flex {
display: flex;
flex-wrap: wrap;
}
.spacer {
flex: 1 1 auto
}
For more information how flex works look here
Yes you can use
flex-wrap: wrap;
remove display flex in your style

Firefox does not scale images inside flexbox

Edit: Here is a codepen and a screenshot showcasing the issue
Codepen
I 'm trying to center align an image using flexbox, maintain its aspect ratio, and also have transparent background below it (check images below).
I approached the problem by creating a full-height-width parent container, containing 4 rows (first is title, second is logo, third are social links, fourth is a chevron-down-icon)
So parent container has flex-direction: row containing these 4.
Inside the logo container I have a parent column container with flex-direction: column, containing 2 empty divs with flex: 1 and my app logo.
Mixins
=full-height-width
width: 100%
height: 100%
=fill($background-color: null)
flex: 1
display: flex
height: 100%
=flex-center-contents
display: flex
justify-content: center
align-items: center
// grid mixins
=row($z-index: auto)
display: flex
flex-direction: column
z-index: $z-index
flex-wrap: nowrap
=column($z-index: auto)
display: flex
flex-direction: row
z-index: $z-index
flex-wrap: nowrap
Styles
// Grid implementation
.fill
+fill($logo-background-color)
z-index: $not-animated-background-z-index
.parent-row-container
+full-height-width()
+row($not-animated-background-z-index)
.parent-column-container
+full-height-width()
+column($not-animated-background-z-index)
#parent-container
+full-height-width()
#social-container, #name-container, #logo-container
flex: 1
#social-container, #name-container
background-color: $logo-background-color
+flex-center-contents()
z-index: $not-animated-background-z-index
#name-container
> #name
color: $home-primary-text-color
font-family: $font-stack-sci-fi
font-size: $name-font-size
text-align: center
user-select: none
#logo-container
display: flex
> #logo-img-wrapper
> #logo
max-height: 100%
max-width: 100%
object-fit: contain
#social-container
display: flex
flex-wrap: wrap
> .social-icon
font-size: $social-icon-font-size
min-width: 85px
+media("<=tablet")
min-width: 45px
text-align: center
#bottom-arrow-container
background-color: $logo-background-color
padding-bottom: 1rem
+flex-center-contents()
z-index: $not-animated-background-z-index
Html
<div class="parent-row-container">
<!-- Row -->
<div class id="name-container">
<p id="name">
Artist name
</p>
</div>
<!-- End Row -->
<!-- Row -->
<div id="logo-container" class="parent-column-container">
<!-- Column -->
<div class="fill"></div>
<!-- Column -->
<div id="logo-img-wrapper">
<img id="logo" src="#img/home/logo.png" />
</div>
<!-- Column -->
<div class="fill"></div>
</div>
<!-- End Row -->
<!-- Row -->
<div class="fill">
<!-- Column -->
<div id="social-container">
</div>
</div>
<div id="bottom-arrow-container">
<font-awesome-icon class="selectable" :icon="['fa', 'chevron-down']" :style="{ color: 'white'}" />
</div>
</div>
This results in the following when run on Chromium/opera (expected)
But firefox produces this
Has this something to do with how firefox treats percentage widths?
If i amend the img container and put flex: 1 on the image container firefox is fixed, but chromium/opera break, and they center the image (because of object-fit: contain) leaving spaces at the edges (also expected behavior) Also, if I remove completely the img from the html, the grid is the same as with chrome/opera
Have any of you experienced anything similar?
I think you use oddly complex approach (using extra empty columns, object-contain and hard way). Here I've just used align-items,text-align and justify-content in one or two cases, and problem is solved:
https://codepen.io/anon/pen/NOOZqb
Flexbox not working anymore after firefox 34.0.5 but you get it to fix by a CSS trick as:
This worked for me:
img {max-width:100%; width:100%;}

Flexbox toolbar with a ton of nesting

I am trying to build a flexbox type container with a search and filters and buttons. I am having trouble getting the desired behaviour. The toolbar-container class is the main flex container, which holds search-bar and filter-group as the top-level flex-items. I am getting confused as to how approach setting up filter-group as a nested flex-container to which the desired behavior is to push the buttons to the end of the row (far right, equivalent to float: right) and give the filters the largest amount of space, wrapping below as the window resizes but maintaining the positions of search and buttons on either side. I have tried using the below css the seperate the fitlers from the buttons but there filters and buttons stay grouped together like this:
search-bar-filters-buttons---------------------------------------------------
Below is desired layout, with only the filters wrapping to the space below when the space decreases.
search-bar-filters-----------------------------------------------------buttons
<div class="toolbar-container">
<div class="search-bar"></div>
<div class="filter-group">
<div class="filters"></div>
<div class="buttons"></div>
</div>
</div>
.toolbar-container {
display: flex;
}
.search {
}
.filter-group {
display: flex;
justify-content: space-between;
}
.filters {
flex-wrap: wrap;
}
.buttons {
}
You need to add flex: 1 for you .filter-group to take all remaining space. Demo:
.toolbar-container {
display: flex;
}
.filter-group {
display: flex;
flex: 1; /* new */
justify-content: space-between;
}
.filters {
flex-wrap: wrap;
}
<div class="toolbar-container">
<div class="search-bar">Search bar</div>
<div class="filter-group">
<div class="filters">Filters</div>
<div class="buttons">Buttons</div>
</div>
</div>

Bulma: Change stack order of columns

How can I change the stack order of columns on mobile or tablet?
For example, the code below shows elements horizontally on wide screens, but when it's shrinked I want 2 to be on top. I don't want to change the html structure to do it.
The example is below:
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.3.1/css/bulma.css" rel="stylesheet"/>
<div class="columns">
<div class="column box">
1
</div>
<div class="column box">
2
</div>
</div>
As of the current bulma version v0.3.1, there is no feature
for the changing the order of columns.
However, you can define custom styles to change the order for mobile, tablet or whatever resolution that you want.
You can define a custom class .reverse-columns for example and add it on parent with following styles:
#media(max-width: 767px) { /* <== You can change this break point as per your needs */
.reverse-columns {
flex-direction: column-reverse;
display: flex;
}
}
#import url("https://cdnjs.cloudflare.com/ajax/libs/bulma/0.3.1/css/bulma.css");
#media(max-width: 767px) {
.custom-columns {
flex-direction: column-reverse;
display: flex;
}
}
<div class="columns custom-columns">
<div class="column box">
1
</div>
<div class="column box">
2
</div>
</div>
#media(max-width: $desktop) {
.columns.is-reversed-touch {
flex-direction: column-reverse;
display: flex;
}
}
#media(max-width: $tablet) {
.columns.is-reversed-mobile {
flex-direction: column-reverse;
display: flex;
}
}
You can always add more rules for widescreen etc., but this is what you usually need.
flex-direction: row-reverse; is what I would use for .columns.is-mobile.is-reversed-mobile. So you can add that rule too.

Resources