Css grid and flexbox
Introduction and Setup
One thing I have learned in this introduction is that, Flexbox alone was never intended to be used to layout Web pages. It was intended to layout galleries and similar stuff.
The 3 Characteristics of Responsive Design
- Flexible grid-based layout
- Media queries
- Images that resize
Floats
- These are basically an Hack from the start, right after table-based layout!
- Features rows and cells.
- Rows clear the floats on the cells.
- Source ordering determines the display, though some(minor) rearrangement is possible.
- Major disadvantages: equal column heights
Here is a interesting learning I wish to share when naming the elements. Atleast while using floats.
Learning how to name css when using floats
fem-layout 👉 /Users/saifas/Masters/Layout/fem-layout/day-1-flexbox/1-intro-floats/end/floats.html
- This file will essentially a series of articles that developer is expected to float and build a 4 rowed layout.
- First row is should have 4 articles on desktop viewport and 2 articles on mobile viewport.
- Second row should have 2 articles with each article layed out side by side. Desktop Viewport.
- Third row should have 2 articles. 1st article should take up of the space and 2nd article should take 3/4th of the space.
- Row 4 should contain 1 article taking up entire entire space.
- To achieve these goals, the articles need to be smartly named.
- Notice
<article class="col-1-2">
this means the article can take up one column or two columns.
Similarly in the rest of the code,
Additional code to note is,
The above line talks about normalize.css
which usually seems to be stylesheet that will help the web app to be consistent across browsers. For now, that’s all I know.
Leveraging this class naming in CSS
Before diving deep into it lets begin to understand some initial parts of the code,
The above is optimal way of box-sizing. This essentially helps the developer to have his HTML elements which follow box model to have combined width and height along the element’s content. So it takes the burden off the developer to combine border + padding + content just to adjust the width.
The above is some special piece of code.
By default when developer applies float:left
to an element. Next element to is wraps around. If developer wants to let this next element to not wrap around, he has to apply clear:left
In the above piece, [class*="col-"]
is particularly interesting to us. Because
- This selector actually selects all the elements which contains “col-” in their class names. In this case, it essentially selects all the articles.
Flexbox
- The first layout elements — but not designed to layout whole web pages
- Features flex containers(row) and flex-items(cells). Both are required to work.
- Excels at vertical centering and equal heights
- Very easy to reorder boxes
- Major disadvantages:
- Wasn’t designed to be locked down for layouts! Works in 1 dimension only.
- Browser support and syntax is challenging.
cheatsheet
Flexbox Grid
Flexbox Parent vs Flexbox Items
Parent: Don’t consider parent as the official name
- It has more to do with how items are arranged
- How much space is inside or around it.
- How the items inside are aligned
Children
- This has more to do with what order they are laid out.
- What is the width of each element. If that can be put in that way.
-
Why shouldn’t we use
width
css rule inside of flex child?Because
width
is strict property. That means if you assign it with pixels or percentages, browser makes sure it is strictly mentioned pixel or percentage wide. But usingflex-basis
will make the the item to be adaptive along side other siblings. It make it seem just OKAY.
🗒️
flex: 2 1 25%
occupies25%
width and grows at the speed of2x
and shrinks at the rate of1x
when resizing.- Default values are ****
- 0 for flex-grow
- 1 for flex-shrink
- auto for flex-basis
- 1 for order
Flexbox Exercises
How do I name the classes in the right way?
First of all there is no right way. But I’d try to put down the way I understood.
💁Look the flexbox.html and flexbox.css in folder flexbox-grid
Understand the CSS Cascade
- We wrote mobile first
- Them Mobile landscape with media query break point.
- Then Desktop version
Okay getting back to nameing:
-
I will first look at how many flex container are there and how many flex items are there in the each row.
-
I will try to imagine the build in mobile layout first. Because it will have more common css rules come early. More you go down the cascade you can apply more specific rules tablet and desktop layouts.
-
Have a mind map of CSS to be as following:
- A Border Sizing CSS rules which is common.
- CSS rules that apply to all the body
- anchor tags
- font-family
- wrap the entire body inside a div if necessary and apply general rules to them.
- Any common behaviour for all the images/spans in your body
- The comes the mobile first css
- Then media query for tablet size
- Then media query for Desktop size
-
Once you have the cascade mechanics set up. You need to have parllel thinking of nameing classes to flex items and flex containers.
-
I would start by imagining how many rows webpage may have.
- Then row would be container class
-
Once done, I will start to imagine number of items I want to the row to contain.
- This will help decide the what percentages I need to give in flex basis. For example if 4 items, I can give
flex: 0 0 25%
. - But Ideally you will also want take margin into account. If
flex-flow: row
, then along the main axis(runs left to right), you should plan to leave may bemagin-left: 4%
so that items have space between them. This will turn give ideall each itemflex: 0 0 20%
to contain the row.
- This will help decide the what percentages I need to give in flex basis. For example if 4 items, I can give
-
For the flex items in the first row, if it has 4 items in it
- I would call each as
col-1
. My interpretation is since I decided to have 4 columns, I will give each item acol-1
and add nothing in mobile first css but next in the cascade of desktop:flex: 0 0 20%
. Mobile:flex: 0 0 44%
. - Now I understand that
col-1
has to increasingly to be changed to tablet and desktop. But Ideally the goal is stack 2 items in the top and 2 items in the bottom for tablet. and all 4 items in a row in desktop. That means, it can take 1 column in desktop. - So change the class name for each of these
col-1-2
- I would call each as
To make a image seem responsive with a hack
Responsive Images
The ways of loading the images in a webpage:
Load a big image and let it scale
This is not a very good approach. The reason is no matter the size of the client device, application is still trying to request a very big image and use it. Not a optimised image for the receiver’s device.
Server-side
This is a good approach. Application when it makes requests to the server, it will tell the server of the client screen size. Server will to the checking work and send the right image size to the browser.
Client-side:
Not a good approach. Load several images on the client side and pick the image that suits the client. The worst part is that, application downloads all the image sizes nevertheless causing data costs.
Client-side with JS:
Let the JavaScript decide on the client side what to do. perform it. It’s better solution.
Solving with <picture>
tag in HTML
This possibly the cheap and best solution out there.
For example, if your code is something like following,
But with picture tag,
- The meta tag helps the browser to become aware to leverage
<picture>
tag. - You can add how many source tags ever you may want.
- Browsers checks with media attribute to load the src that is suitable.
Media Query & Asset Downloading Results
Why do we need to add height?
Most of the places for elements app needs to be specifed width css rule. May be in Flex form, flex-basis rule. How ever, consider a situation where app with want to load a background image.
And the best css would be,
Observations here,
height
css rule is applied. The reason is, default, the height of an element is determined by the browser when element is rendered by the browser. If you look at this case, thediv
element doesn’t actually have an content in it.- In that case, as a developer you can approximate the height of the image as you already know it (because you will tend to maintain all
srcset
to support picture tag). And set that in the CSS as shown above. - Lastly, if you look at the
min-width
attribute of media query, it’s just 1px diff. The reason for this is, when CSS rule like#background-image{ background-image:url('images/test5-desktop')}
is used directly, browser loads all the variants, which is bad performance(by loading unnecessary image size for particular screen)
CSS Grid
- Built into CSS spec (now a recommendation)
- No “row” markkup required.
- Grid is designed to work in 2 dimensions
- use Flexbox for UI elements, but use Grid for major layout.
Mobile browser doesn’t necessarily require CSS Grid.
Can I use… Support tables for HTML5, CSS3, etc
Approach
- Figure out number of rows and colums
- Then accordingly try to add
grid-row: x / y;
andgrid-column: x / y;
- Grid will start re arranging the items as specified automatically.
- Now if you want to have each item do have different size. Go back to
.wrapper
and addgrid-template-rows: Apx Bpx Cpx Dpx;
and similarlygrid-template-columns: Apx Bpx Cpx;
. Assign the values (A B C Ds) to this rule based on number of rows or columns app requires.
🗒️
Notes
- You can use percentages and ems in
grid-template-rows:;
orgrid-template-columns:;
- Or you can use the same in this way,
grid-template-rows: repeat(4, 1fr);
orgrid-template-columns: repeat(5, auto);
. 1fr just means that it is a fraction or a cell. and auto means that CSS Grid will automatically handle, adding rows for you.