Flexbox - how to set equal spaces between elements - flexbox

I have unusual approach in using of Flexbox, it is "12 column" design, all works fine, exceptions are the spaces between flex elements, they look different while wrapped and aligned in row. How to fix this?
I play with margin, but it doesn't help:
margin: 1% 1% 1% 1%; ???????
https://jsfiddle.net/cmpt/j7tk30bx/7/
UPDATE:
Idea of this Flexbox is as follows:
if there is a big screen "desktop/pad" i.e. min screen width 841px - do FLEX in 12 column different width system,
for the rest "mobile/small screens" i.e. screen width <841px put wrapper (row) in one full screen width row - one by one.
This works fine, very flexible for web design and putting element where ever you want.
But the problem is that margins(spaces) between FLEXBOX elements (ROW and COL wrappers) are different in those 2 modes and CSS margin parameter top/bottom for example adjusts the view in different way in these 2 modes. I want to have spaces equal between FLEXBOX elements in the both modes.
NOTE:
margin: x 1% x 1%
is a strict rule to have perfect 12 column system
i.e.
.col_12 {width: 98%;}
1%+1%+98%=100%
but I do not know what to do with x (top/bottom) margins
body {
background-color: #998;
}
.row {
margin: 0;
padding: 0;
}
[class*="col_"] {
border-radius: 4px;
background-color: #eed;
margin: 1% 1% 1% 1%;
padding: 0em 0% 0em 0%;
}
#media all and (min-width: 841px) {
.row {
display: flex;
box-sizing: border-box;
flex-direction: row;
flex-wrap: nowrap;
}
.col_1 {width: 6.3%;}
.col_2 {width: 15.0%;}
.col_3 {width: 23.0%;}
.col_4 {width: 31.3%;}
.col_5 {width: 40.0%;}
.col_6 {width: 48.0%;}
.col_7 {width: 56.3%;}
.col_8 {width: 65.0%;}
.col_9 {width: 73.0%;}
.col_10 {width: 81.3%;}
.col_11 {width: 90.0%;}
.col_12 {width: 98%;}
[class*="col_"] {
flex: 0 auto;
}
}
<div class="row">
<div class="col_11">col_11</div>
<div class="col_1">col_1</div>
</div>
<div class="row">
<div class="col_2">col_2</div>
</div>
<div class="row">
<div class="col_12">col_12</div>
</div>
cleaned it up... fiddle updated too

UPDATE- FIX !!!
I have fixed it: margin at bottom 'auto' in #media section helps. In other places we indicate fixed margin (20px in this example), must be in px, not %. Looks fantastic... Tiny and reliable FLEXBOX... Thanks!
[class*="col_"] {
margin: 20px 1% 20% 1%;
}
...
#media all and (min-width: 841px) {
...
[class*="col_"] {
...
margin: 20px 1% auto 1%;
...
}
...
}
https://jsfiddle.net/cmpt/j7tk30bx/

Related

Flexbox gap workaround for Safari

I finished my website but I didn't realize that safari doesn't support the flexbox gap. Is there a way around this without having the mess anything up? This is mostly for my media queries.
<div class="social-media">
<a href="https://github.com/">
<img class="social-media__icon" src="img/github.png" alt="Github">
</a>
<a href="https://www.linkedin.com/">
<img class="social-media__icon" src="img/linkedin.png" alt="LinkedIn">
</a>
</div>
.social-media {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
gap: 8rem;
margin-top: 10rem;
padding-bottom: 2rem;
}
.social-media img {
width: 90px;
height: 90px;
}
#media only screen and (max-width: 400px) {
.social-media {
gap: 3rem;
margin-top: 5rem;
}
.social-media img {
width: 62px;
height: 62px;
}
}
Use the Lobotomized owl selector: .parent > * + * {} to add a margin-left that gives you space between the elements that come after another element, this way you eliminate the undesired margin it creates when you put the margin directly to the child's first element (.social-media img{})
.social-media > * + * { margin-left: 8rem;}
Here you can read more about the Lobotomized Owl Selector
Edit: Gap is now supported in safari so you should be able to use it no problem.
Property gap with display: flex is not supported by Safar version < 14 https://caniuse.com/flexbox-gap .
You might want to replace display flex with grid:
display: grid;
grid-gap: 8rem; /* Safari 10-11 */
gap: 8rem; /* Safari 12+ */
because grid's gap is supported in older Safari versions: https://caniuse.com/mdn-css_properties_gap_grid_context
The accepted answer has the problem, that you will have a wrong margin on the first element if when there is only one row. Also centered elements will always be 8rem too far the right.
Better solution that will always work with correct spacings:
.container {
display: flex;
// the trick is to set margins around boxes, but correct the margins around elements that are situated at the border of the container with negative margins
margin: 0 -10px -20px -10px;
}
.box {
min-width: 100px;
height: 100px;
background-color: deeppink;
margin: 0 10px 20px 10px;
}
<div class='container'>
<div class='box'>1</div>
<div class='box'>2</div>
<div class='box'>3</div>
<div class='box'>4</div>
</div>
You can remove the gap class and add another one to child elements
<div class="d-flex"> // it was "d-flex gap" previously
<div class="mx-2">
//content
</div>
<div class="mx-2">
//content
</div>
</div>
I think you could make a div container and put justify-content: space-between; then i think it should work

Horizontal align text below flex-items

Image captions are aligned bottom by flexbox within a gallery where images have different heights. How can I achieve that first lines of text (titles) are aligned horizontally?
<style type="text/css">
* {
box-sizing: border-box; }
.flex-container {
display: flex;
flex-wrap: wrap; }
.flex-item {
width: 50%;
padding: 20px;
display: flex;
flex-direction: column; }
.flex-item img {
width: 100%;
height: auto; }
.flex-image {
flex: 1 0 auto; }
</style>
<div class="flex-container">
<div class="flex-item">
<div class="flex-image">
<img src="img-1.jpg" alt="" />
</div>
<p>title</p>
</div>
<div class="flex-item">
<div class="flex-image">
<img src="img-2.jpg" alt="" />
</div>
<p>title<br>Lorem ipsum dolor sit amet.</p>
</div>
</div>
Please check my codepen: https://codepen.io/tinusmile/pen/MoeORG
Many thanks!
With the existing markup, where the images can have different heights, you need to do something like this, where you give the p a height.
As an element's overflow defaults to visible, it will flow out of its boundaries, so I recommend to set the height so it can accommodate 2-3 lines of text.
On smaller screens you might need to add a #media query to adjust that height, as if the text is very long it might overflow any element below itself.
.flex-item p {
height: 50px; }
Updated codepen
Note, using min-height instead would solve the might overflow any element below itself issue, though it will make the first line to not align horizontally if the value does not accommodate all the possible amount of lines.
Another option is to remove the flex-item wrapper and use Flexbox's order property, as I suggested in a previous question of yours, align-horizontally-3-elements-over-3-divs

Chrome 43 Flexbox flex-grow issue

Before Chrome 43, div1 would take up 10% of the container height regardless of its childrens size, and div1 would overflow. As of Chrome 43 div1 doesnt follow flex-grow anyone more and instead grows to its childrens size. Is this supposed to work this way? How do i get div1 to overflow and follow its flex-grow property. Thanks!
Heres a jsfiddle: http://jsfiddle.net/HorseFace/xsbmmf4o/
<div id="container">
<div id="div1">
<div id="inner1"></div>
</div>
<div id="div2">
<div id="inner2"></div>
</div>
</div>
#container {
height: 500px;
display: flex;
flex-direction: column;
}
#div1 {
background: red;
flex-grow: 0.1;
}
#inner1 {
height: 200px;
background: lightcoral;
}
#div2 {
background: blue;
flex-grow: 0.9;
overflow: auto;
}
#inner2 {
height: 200px;
background: #ccccff;
}
body {
color: purple;
background-color: #d8da3d
}
You are misunderstanding flex-grow. It sets the flex grow factor,
which determines how much the flex item will grow relative to the rest of the flex items in the flex container when positive free space is distributed. When omitted, it is set to 1.
So only free space space is distributed. If you want that flex item to take up 10% of the flex container, you should set the flex-basis to 0:
the flex basis [is] the initial main size of the flex item, before free space is distributed.
Note you can use the shorthand flex property to set both flex-grow and flex-basis simultaneously (and flex-shrink too):
flex: 0.1; /*
flex-grow: 0.1;
flex-shrink: 1;
flex-basis: 0;
*/
Also note that the Flexbox spec changed the initial value of min-height and min-width to auto (previously it was 0). This may break your percentages, so either use overflow different than visible, or set min-height: 0.
body {
color: purple;
background-color: #d8da3d
}
#container {
height: 500px;
display: flex;
flex-direction: column;
}
#div1, #div2 {
min-height: 0; /* Or `overflow: hidden` */
}
#div1 {
background: red;
flex: 0.1;
}
#inner1 {
height: 200px;
background: lightcoral;
}
#div2 {
background: blue;
flex: 0.9;
overflow: auto;
}
#inner2 {
height: 200px;
background: #ccccff;
}
<div id="container">
<div id="div1">
<div id="inner1">Inner1</div>
</div>
<div id="div2">
<div id="inner2">Inner2</div>
</div>
</div>

floating an image over other divs, from the right

I've read many other posts regarding floating divs, but haven't been able to find success yet with the things I've tested, so here I am... (I'm still new to this, so apologies if my code isn't super clean!)
I have an image that I'd like to float over several others. My goal (if it is attainable) is to have it in a fixed position from the upper right corner of .container. I'm close... but I can't get the image to move in from the right, and as it sits now, it is bumping the other photo out of the header (without the .crosses added, it sits in the green, right-aligned.)
The project requires that it still looks good (or degrades nicely) in IE7.
I've set up a fiddle here: (can't figure out what the red error means by "links to jsfiddle.net must be accompanied by code") so, if you could just go there and visit:
http://jsfiddle.net/cathi/VAkk5/5/
HTML (extract):
<div class="container">
<div class="crosses"><img src="/img/common/crosses-motif.png" width="213" height="118" alt="crosses-motif" /></div>
<div class="header"></div>
<div class="hero">
<div class="herophoto">photo</div>
</div>
</div>
CSS (extract):
.container {
width: 80%;
margin: 0 auto;
}
.crosses {
float:right;
margin-right:0;
margin-top:130px;
}
.header {
height: 150px;
}
.headerlogo {
width: 250px;
padding-top: 80px;
padding-left: 20px;
float:left;
}
.headerlogo2 {
float:right;
}
.hero {
height: 205px;
}
.heroheadline {
height: 0;
width: 450px;
padding-top: 45px;
padding-left: 70px;
float:left;
}
.herophoto {
height: 205px;
width: 333px;
float: right;
}

No scrollbar for DIV wider than browser

I'm doing some tests on a website using Wordpress as a CMS. In the example below the top left of the page has an "S" graphic outside of the main content area, clipped accordingly depending on the browser width. I would like to do something similar with an "L" graphic to the right in the footer.
The page width is set to 960px, and I've made the footer container DIV 1088px so that the "L" appears outside the content area. The trouble is this makes a scrollbar appear when it exceeds the current width of the browser.
I've tried overflow:hidden on the footer container DIV but this doesn't seem to work. I've also tried overflow:hidden on the BODY element and this works ok in IE, but not in other browsers.
Example: http://unclemort.com/wp/
I really hope there is away to do this, any help gratefully received.
I was trying to figure this out myself today and stumbled upon the answer.
What you need is a surrounding element around everything that has this:
#wrapper {
min-width: 600px; //whatever width you want
overflow-x: hidden;
}
Your main content should have that same width, and the things that need to jut out should have a negative margin.
Here's a complete example:
HTML:
<div id="outer">
<div id="container">
<div class="row">
<div class="inner">Hello World</div>
</div>
</div>
</div>
CSS:
#outer {
min-width: 300px;
overflow-x: hidden;
}
#container {
width: 300px;
margin: 0px auto;
background: gray;
height: 500px;
}
.row {
width: 350px;
background: blue;
margin-left: -25px;
}
.inner {
background: yellow;
margin-left: 25px;
width: 300px;
height: 100px;
}
#media screen and (min-width: 301px) {
body {
//overflow-x: hidden;
}
}
jsfiddle:
http://jsfiddle.net/aaronjensen/9szhN/
Try in style.css, line 65, adding:
#footer-container {
border: none;
overflow: hidden;
}
Explanation:
#footer-container #footer {
background: #f5e8f7 url('images/slobraico-footer-pink-full.gif') no-repeat top left;
width: 1088px;
height: 217px;
overflow: hidden;
}
The scrollbar you're hiding is effectively not there.
The scrollbar you're seing is another one.
The problem is that the footer is 1088px wide, and that's causing a scrollbar to appear.
As long as the footer has fixed width and it's parent doesn't have overflow: hidden, you'll get a scroll if there's not enough width for the footer to fit.
Same goes for any other container.

Resources