CSS Flexbox aspect ratio - html

I hope someone can help me to end my 16 hour search. I have to make
six responsive squares in a 2 x 3 raster.
Inside the square there has to be a responsive circle.
Inside the circle there has to be a centered letter.
I have to use Flexbox.
I am not allowed to use Grid and/or tables.
The squares and circles need to keep their aspect ratio and must fill the container.
CSS-only
div {
display: flex;
flex: 1;
justify-content: space-between;
font-size: 10rem;
}
.blokrij {
flex-direction: column;
width: 50%;
}
.vierkant {
background-color: hsla(26, 100%, 50%, 1.00);
flex: 1 1 auto;
max-width: 100%;
border-radius: 10%;
margin 1px;
}
.rond {
background-color: blue;
border-radius: 50%;
}
<div class="container">
<div class="blokrij">
<div class="vierkant">
<div class="rond"></div>L</div>
<div class="vierkant">
<div class="rond"></div>O</div>
<div class="vierkant">
<div class="rond"></div>I</div>
</div>
<div class="blokrij">
<div class="vierkant">
<div class="rond"></div>L</div>
<div class="vierkant">
<div class="rond"></div>O</div>
<div class="vierkant">
<div class="rond"></div>I</div>
</div>
</div>
I'm so frustrated I cant figure out what the hell I have to do. Especially because I know how to fix it with GRID.
Thansk for helping in Advance!

It is better to assign different codes to their own sections
The text is not written inside the circle
The ratios are not set correctly
Sizes are not set correctly
I set margin: 10px; and padding: 10px; for better and more clarity.
You might say that circles are not precise and are more like ellipses
This is true because they are measured relative to the screen
They may have different sizes in different sizes
.container{
width:100%;
height: 100vh;
display: flex;
}
.blokrij {
display: flex;
flex-direction: column;
width: 50%;
height: 100%;
}
.vierkant {
background-color: hsla(26, 100%, 50%, 1.00);
border-radius: 10%;
margin 1px;
height: 100%;
margin: 10px;
padding: 10px;
}
.rond {
height: 100%;
background-color: blue;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 10rem;
}
<div class="container">
<div class="blokrij">
<div class="vierkant">
<div class="rond">L</div>
</div>
<div class="vierkant">
<div class="rond">O</div>
</div>
<div class="vierkant">
<div class="rond">I</div>
</div>
</div>
<div class="blokrij">
<div class="vierkant">
<div class="rond">L</div>
</div>
<div class="vierkant">
<div class="rond">O</div>
</div>
<div class="vierkant">
<div class="rond">I</div>
</div>
</div>
</div>
If you want the circles and squares to be exactly the exact circle and square, you can measure exactly with px , rem, ...
For example:
.container{
width:400px;
height: 400px;
display: flex;
}
.blokrij {
display: flex;
flex-direction: column;
width: 50%;
height: 100%;
}
.vierkant {
background-color: hsla(26, 100%, 50%, 1.00);
border-radius: 10%;
margin 1px;
height: 100%;
margin: 5px;
padding: 5px;
}
.rond {
height: 100%;
background-color: blue;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 10rem;
}
<div class="container">
<div class="blokrij">
<div class="vierkant">
<div class="rond">L</div>
</div>
<div class="vierkant">
<div class="rond">O</div>
</div>
<div class="vierkant">
<div class="rond">I</div>
</div>
</div>
<div class="blokrij">
<div class="vierkant">
<div class="rond">L</div>
</div>
<div class="vierkant">
<div class="rond">O</div>
</div>
<div class="vierkant">
<div class="rond">I</div>
</div>
</div>
</div>

You have to give the circles a dedicated width and height, put the letters inside them and define for all flex-items the centering with justify-content and align-items. For the responsiveness you should define width and height of the circle and the font-size with the same responsive dimension, for example vw. The width for the columns isn't necessary.
Working example:
div {
display: inline-flex;
flex: 1;
justify-content: center;
align-items: center;
font-size: 10vw;
}
.blokrij {
flex-direction: column;
}
.vierkant {
background-color:hsla(26,100%,50%,1.00);
border-radius: 10%;
margin: 1px;
}
.rond {
width: 20vw;
height: 20vw;
background-color: blue;
border-radius: 50%;
}
<div class="container">
<div class="blokrij">
<div class="vierkant">
<div class="rond">L</div>
</div>
<div class="vierkant">
<div class="rond">O</div>
</div>
<div class="vierkant">
<div class="rond">I</div>
</div>
</div>
<div class="blokrij">
<div class="vierkant">
<div class="rond">L</div>
</div>
<div class="vierkant">
<div class="rond">O</div>
</div>
<div class="vierkant">
<div class="rond">I</div>
</div>
</div>
</div>

Related

how to center the first item in a flexbox slider/carousel

I want to build a simple scroll slider with flexbox with three items. I want the first item to be centered in the page (under the headline) but the following items only should have a less margin.
HTML:
<div class="page-width">
<h1>Headline</h1>
<div class="container">
<div class="flex-item">
<div class="flex-wrapper">
Center me
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
Hello World
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
Hello World
</div>
</div>
</div>
</div>
CSS:
.page-width {
max-width: 500px;
border: 5px solid green;
}
h1 {
text-align: center;
}
.container {
display: flex;
justify-content: flex-start;
overflow: scroll;
}
.flex-item {
margin-left: 20px;
}
.flex-wrapper {
background: blue;
width: 250px;
height: 250px;
}
How can I achieve to center the first item, while the second and third only remain with a margin of 20px? Also, it should be responsive, for example when the page width is smaller, the first item should still be centered.
I tried to use
.flex-item {
flex: 0 0 100%
}
and center the wrapper inside, so the box would be in the center, but then the second and third item are outside of the screen.
Codepen: https://codepen.io/ascena/pen/wvqZgzg
.page-width {
max-width: 500px;
border: 5px solid green;
}
h1 {
text-align: center;
}
.container {
display: flex;
justify-content: flex-start;
overflow: scroll;
padding-left:20%;
}
.flex-item {
margin-left: 20px;
}
.flex-wrapper {
background: blue;
width: 250px;
height: 250px;
}
<div class="page-width">
<h1>Headline</h1>
<div class="container">
<div class="flex-item">
<div class="flex-wrapper">
Center me
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
Hello World
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
Hello World
</div>
</div>
</div>
</div>

Flex nowrap container not wrapping child. when the width of those are in mentioned in percentage

I have a flex container that wraps some cards. I have implemented a horizontal scroller with flex(using flex-nowrap). The flex wrapper container is subjected to have a 36px left spacing and a 0px right spacing initially. The catch here is after the last card is scrolled I need to have a 36px right spacing.
here is what I did so far
*{
box-sizing: border-box;
}
h2 {
margin: 0;
}
body {
background: cadetblue;
}
.container {
width: 500px;
margin: 0 auto;
background: #000;
padding: 20px 36px;
}
.scroll-wrapper {
overflow: hidden;
margin-right: -36px;
}
.scroll-inner {
display: flex;
flex-flow: row nowrap;
overflow: auto;
margin: 0px -16px;
}
.card {
height: 250px;
width: 75%;
flex: 1 0 75%;
padding: 0px 16px;
}
.inner-wrapper {
background: #fff;
height: 250px;
}
<div class="container">
<div class="scroll-wrapper">
<div class="scroll-inner">
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
</div>
</div>
</div>
I have given a negative right margin for the wrapper to make it look like it's flowing from the right.
Once the last element is scrolled/reached I want it to look something like this
enter image description here
One thing I noticed is my scroll-inner(flex-nowrap) is not wrapping up the entire children. I presumed if we have five children each having width 50px. The scroll-inner should show a scrollable width of 250px, Unfortunately, it's not how flex is behaving. Any help or suggestion is much appreciated.
Updating few images that show what I'm really looking for
During scrolling
After scrolling till the last card
Try
.scroll-wrapper {
overflow: hidden;
margin: auto;
}
.scroll-inner {
display: flex;
flex-flow: row nowrap;
overflow: auto;
margin: auto;
}
I have found a fix and hope this help someone.
*{
box-sizing: border-box;
}
h2 {
margin: 0;
}
body {
background: cadetblue;
}
.container {
width: 500px;
margin: 0 auto;
background: #000;
padding: 20px 36px;
}
.scroll-wrapper {
overflow: hidden;
margin-right: -36px;
}
.scroll-inner {
display: flex;
flex-flow: row nowrap;
overflow: auto;
margin: 0px;
}
.card {
height: 250px;
width: 75%;
flex: 1 0 75%;
padding-right: 40px;
margin-right: -8px;
}
.inner-wrapper {
background: #fff;
height: 250px;
}
<div class="container">
<div class="scroll-wrapper">
<div class="scroll-inner">
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
<div class="card">
<div class="inner-wrapper"><h2>Card</h2></div>
</div>
</div>
</div>
</div>
Please do answer if someone has a better solution!! Thanks

How to get nested css flex to have the right width after wrap in this setup?

I have this mockup, there are some nested containers. some of the link-class have multiple elements (par and ref) and I want them to display next to each other if there's space, but responsively move them below each other when total width gets smaller.
It works somewhat, but I expect (want) the link-element containing two childs to return to the same width as the link-element with one single child as it wraps.
For some reason, it remains wider than the single-child ones.
Any hints appreciated!
Code:
let name = 'world';
:global(body) {
margin: 0;
padding: 0
}
.main {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
background-color: gray;
}
.Container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
background-color: lightblue;
padding: 3px
}
.linkContainer {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 3px;
background-color: salmon;
}
.par {
width: 80vw;
max-width: 300px;
background-color: red
}
.links {
display: flex;
flex-flow: row wrap;
padding: 3px;
background-color: orange
}
.ref {
background-color: olive;
width: 30vw;
max-width: 100px
}
.item {
width: 80vw;
max-width: 300px;
background-color: steelblue
}
<div class="main">
<div class="Container">
<div class="item">
header
</div>
<div class="linkContainer">
<div class="links">
<div class="par">
some text
</div>
</div>
<div class="links">
<div class="par">
some text
</div>
</div>
<div class="links">
<div class="par">
some text
</div>
</div>
<div class="Container">
<div class="item">
another header
</div>
<div class="linkContainer">
<div class="links">
<div class="par">
some text
</div>
<div class="ref">
a ref
</div>
</div>
<div class="links">
<div class="par">
some text
</div>
</div>
<div class="links">
<div class="par">
some text
</div>
<div class="ref">
a ref
</div>
</div>
</div>
</div>
</div>
</div>
</div>
you could simply add a max-width:300px; to .links and have the box in size but then you couldn't have the desired stacking effect you wanted so i went a bit further and with the help of css variables and media queries and adding a class .single to single .pars which didn't have a .ref after them, i came up with this:
:root {
--ref-size: 100px;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.main {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
background-color: gray;
}
.Container {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
background-color: lightblue;
padding: 3px
}
.links {
min-width: 300px;
display: flex;
flex-flow: row wrap;
padding: 3px;
background-color: orange;
}
.par {
width: calc(100% - var(--ref-size));
background-color: red;
}
.ref {
background-color: olive;
width: var(--ref-size);
}
.item {
width: 80vw;
max-width: 300px;
background-color: steelblue
}
#media all and (max-width:300px){
.par{
width: 100%;
}
}
#media all and (min-width: 300px){
.par.single{
width: 100%;
}
}
<div class="main">
<div class="Container">
<div class="item links">
header
</div>
<div class="links">
<div class="par single">
some text
</div>
</div>
<div class="links">
<div class="par single">
some text
</div>
</div>
<div class="links">
<div class="par single">
some text
</div>
</div>
<div class="item links">
another header
</div>
<div class="links">
<div class="par">
some text
</div>
<div class="ref">
a ref
</div>
</div>
<div class="links">
<div class="par single">
some text
</div>
</div>
<div class="links">
<div class="par">
some text
</div>
<div class="ref">
a ref
</div>
</div>
</div>
</div>

CSS Flexbox: align flex item with different width

I'm trying to align some flex items with different widths. I can not hit some elements.
https://jsfiddle.net/sistel/m1vu9exf/2/
I cannot align the texts and colored squares as shown in this figure http://www.farmacom.it/baseball.jpg
.flex-container-4 {
display: flex;
height: 70px;
align-items: center;
background-color: Black;
justify-content: space-around;
position:relative;
}
.flex-container-4 > div {
background-color: BLACK;
width: 500px;
margin: 5px;
text-align: center;
line-height: 50px;
font-size: 40px;
color: white;
}
I have re-written your code using flex css and removed some unwanted styles. Hope this helps your problem
HTML
<div class="flex-container">
<div class="flex-column">
<div class="flex-row">
<div class="number">18</div>
<div class="number">18</div>
<div class="number">18</div>
</div>
<div class="flex-row">
<div class="roman-numeric">I</div>
<div class="roman-numeric">II</div>
<div class="roman-numeric">III</div>
</div>
<div class="flex-row">
<div class="number">18</div>
<div class="number">18</div>
<div class="number">18</div>
</div>
<div class="flex-row">
<div class="text">BALL</div>
</div>
<div class="flex-row">
<div class="green"></div>
<div class="green"></div>
<div class="green"></div>
</div>
</div>
<div class="seperator"></div>
<div class="flex-column">
<div class="flex-row">
<div class="number">18</div>
<div class="number">18</div>
<div class="number">18</div>
</div>
<div class="flex-row">
<div class="roman-numeric">IV</div>
<div class="roman-numeric">V</div>
<div class="roman-numeric">VI</div>
</div>
<div class="flex-row">
<div class="number">18</div>
<div class="number">18</div>
<div class="number">18</div>
</div>
<div class="flex-row">
<div class="text">STRIKE</div>
</div>
<div class="flex-row">
<div class="red"></div>
<div class="red"></div>
</div>
</div>
<div class="seperator"></div>
<div class="flex-column">
<div class="flex-row">
<div class="number">18</div>
<div class="number">18</div>
<div class="number">18</div>
</div>
<div class="flex-row">
<div class="roman-numeric">VII</div>
<div class="roman-numeric">VIII</div>
<div class="roman-numeric">IX</div>
</div>
<div class="flex-row">
<div class="number">18</div>
<div class="number">18</div>
<div class="number">18</div>
</div>
<div class="flex-row">
<div class="text">OUT</div>
</div>
<div class="flex-row">
<div class="orange"></div>
<div class="orange"></div>
</div>
</div>
</div>
CSS
.flex-container {
display: flex;
background-color: Black;
justify-content: start;
padding: 10px 10px;
width: fit-content;
}
.flex-column {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.flex-row {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.flex-row>.number {
background-color: #444;
width: 100px;
margin: 5px;
text-align: center;
line-height: 75px;
font-size: 60px;
color: yellow;
}
.flex-row>.roman-numeric {
width: 100px;
margin: 5px;
text-align: center;
line-height: 45px;
font-size: 50px;
color: white;
}
.flex-row>.text {
background-color: BLACK;
margin: 5px;
text-align: center;
line-height: 50px;
font-size: 40px;
color: white;
}
.green {
background-color: green;
width: 40px;
height: 40px;
margin: 25px;
}
.red {
background-color: red;
width: 40px;
height: 40px;
margin: 25px;
}
.orange {
background-color: orange;
width: 40px;
height: 40px;
margin: 25px;
}
.seperator {
background: #a5a2a2;
width: 10px;
margin: 0px 15px;
}
JS Fiddle Link : https://jsfiddle.net/SJ_KIllshot/2ymefx81/
This is because you created rows and cells from these rows doesn't know about other rows' dimensions - this is why in the last part, "VIII" takes more width than other elements.
One solution is to create columns instead of rows, because in this way, flex container will be of the width of the widest element.
To get exact result you need to change your bit of html structure. If not i have created similar pattern using your current code. please check this:
Baseball Flex

position part of the div underneath another

I'm trying to hide part of the div underneath another.
https://jsfiddle.net/71obhkzh/7/ shows what I have now.
<div>
some stuff here
</div>
<div id="container">
<div id="top">
</div>
<div id="bottom">
</div>
</div>
<div>
some stuff here
</div>
#container {
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
width: 300px;
}
#top {
height:100px;
width: 100px;
background-color: red;
}
#bottom {
height:100px;
width: 120px;
background-color: blue;
margin-top: -40px;
}
In the fiddle I used the negative margin-top to move the blue div up a bit, but it covers the bottom of the red div. I need the red one to be on top of blue one like this https://awwapp.com/b/unzo2gs6g/
Ok, if I add the z index as suggested it works on the fiddle, but in real app the colors are mixed like here
http://i67.tinypic.com/34pets8.png
I'm using bootstrap and the reality is bit more complicated (flex boxes in the top and bottom div with more content). I tried to set opacity on the top div but it did not help
real code (login container is the red one, info the blue one)
<div class="container">
<div class="row justify-content-center login-container">
<div class="col-6 login-box">login form here</div>
<div class="col-4 register-box">register box here</div>
</div>
<div class="d-flex justify-content-center info-container">
<div id="advantages" class="d-flex flex-column justify-content-center align-items-center">
some text
</div>
<div id="image" class="d-flex flex-column justify-content-center align-items-center">
<img src="some image"/>
</div>
</div>
</div>
https://jsfiddle.net/71obhkzh/31/
Make use of z-index.
#container {
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
width: 300px;
}
#top {
height:100px;
width: 100px;
background-color: red;
z-index: 2
}
#bottom {
height:100px;
width: 200px;
background-color: blue;
margin-top: -40px;
z-index: 1
}
<div>
some stuff here
</div>
<div id="container">
<div id="top">
</div>
<div id="bottom">
</div>
</div>
<div>
some stuff here
</div>
Use z-index to do that:
#container {
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
width: 300px;
}
#top {
height:100px;
width: 100px;
background-color: red;
z-index:2;
}
#bottom {
height:100px;
width: 120px;
background-color: blue;
margin-top: -40px;
z-index:1;
}
<div>
some stuff here
</div>
<div id="container">
<div id="top">
</div>
<div id="bottom">
</div>
</div>
<div>
some stuff here
</div>