CSS Flexbox: align flex item with different width - html

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

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>

CSS Flexbox aspect ratio

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>

How to add text at the bottom of each flexbox

I have a flexbox container with 4 flex items. I need output like the image below
I have done the coding part but I am stuck with adding the text below the flexbox
My Code:
.flex-container {
margin: 40px auto 0 auto;
margin-top: 0;
display: flex;
justify-content: center;
background-color: white;
}
.flex-container > div {
margin: 40px auto 0 auto;
color: white;
background-color:lightgray;
width: 140px;
margin: 20px;
text-align: center;
line-height: 140px;
font-size: 20px;
}
<body>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
This is how I would do it.
.flex-container {
margin: 40px auto 0 auto;
margin-top: 0;
display: flex;
justify-content: center;
background-color: white;
}
.box {
margin: 40px auto 0 auto;
color: white;
background-color:lightgray;
width: 140px;
margin: 20px;
text-align: center;
line-height: 140px;
font-size: 20px;
}
.text{
text-align:center;
}
<div class="flex-container">
<div class="container">
<div class="box">1</div>
<div class="text">BOX1</div>
</div>
<div class="container">
<div class="box">2</div>
<div class="text">BOX2</div>
</div>
<div class="container">
<div class="box">3</div>
<div class="text">BOX3</div>
</div>
<div class="container">
<div class="box">4</div>
<div class="text">BOX4</div>
</div>
</div>
Simply do this:
(The more appropriate way to do it with table)
.box{
background-color: #7c7cab;
height: 80px;
display: flex;
margin: 10px;
color: white;
}
.box,.textbox{
width: 20%;
float: left;
}
.textbox{
color: black;
margin-left: 10px;
margin-right: 10px;
text-align: center;
}
<div class="boxes">
<div class="box">box 1</div>
<div class="box">box 2</div>
<div class="box">box 3</div>
<div class="box">box 4</div>
</div>
<div class="text-box">
<div class="textbox">text 1</div>
<div class="textbox">text 2</div>
<div class="textbox">text 3</div>
<div class="textbox">text 4</div>
</div>
Hopefully this works for you.
.flex-container {
margin: 40px auto 0 auto;
margin-top: 0;
display: flex;
justify-content: center;
background-color: white;
text-align: center;
}
.container {
margin: 40px auto 0 auto;
color: white;
background-color:lightgray;
width: 140px;
margin: 20px;
text-align: center;
line-height: 140px;
font-size: 20px;
}
<div class="flex-container">
<div>
<div class='container'>1</div>
<span>Box 1</span>
</div>
<div>
<div class='container'>1</div>
<span>Box 2</span>
</div>
<div>
<div class='container'>1</div>
<span>Box 3</span>
</div>
<div>
<div class='container'>1</div>
<span>Box 4</span>
</div>
</div>

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>

overflow-y either auto or scroll bar does not allow showing all overflow content

I have a dummy screen with background image. The background image correctly expands or contracts with the size of the viewport. I have dummy flexbox content that I intentionally make overflow the bottom of the page.
The scrollbar correctly appears, but it will not scroll far enough to show all the content that overflowed.
Apologies: the content is varied so I can tell what IS NOT showing.
Dummy header menu bar is fine.. it is supposed to scroll up - and it does.
CSS:
html {
box-sizing: border-box;
}
body {
box-sizing: border-box;
overflow-y: auto;
min-height: 100vh;
width: 100%;
margin: 0; padding: 0; border: 0; cursor: default
}
ul {
list-style: none;
li {
display: inline;
margin-left: 50px;
margin-top: 50px;
}
}
.blockStyleEven {
display: flex;
flex-direction: row;
color: white;
justify-content: space-evenly;
}
.blockSpaceAround {
display: flex;
flex-direction: row;
color: white;
justify-content: space-around;
}
.blockSpaceBetween {
display: flex;
flex-direction: row;
color: white;
justify-content: space-between;
}
.blockStart {
display: flex;
flex-direction: row;
color: white;
justify-content: flex-start;
}
.blockEnd {
display: flex;
flex-direction: row;
color: white;
justify-content: flex-end;
}
.blockCenter {
display: flex;
flex-direction: row;
color: white;
justify-content: center;
}
.maroon {
width: 100px;
height: 100px;
background-color: maroon;
}
.red {
width: 100px;
height: 100px;
background-color: red;
}
.green {
width: 100px;
height: 100px;
background-color: green;
}
.blue {
width: 100px;
height: 100px;
background-color: blue;
}
.workspace {
background: url("././Features.jpg") no-repeat center center fixed;
background-size: cover;
height: 100vh;
width: 100vw;
overflow: hidden;
h1 {
color: #E0B228;
}
h2 {
color: #2856E0;
}
}
HTML:
<div [ngStyle]="{'width': '100vw', 'height': '100vh'}">
<div [ngStyle]="{'display': 'flex', 'flex-direction': 'column'}">
<div [ngStyle]="{'background-color': 'beige'}">
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
</div>
<div class="workspace">
<div class="blockStyleEven">
<div class="maroon">space-evenly</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockSpaceAround">
<div class="maroon">space-around</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockSpaceBetween">
<div class="maroon">space-between</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockStart">
<div class="maroon">start</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockEnd">
<div class="maroon">end</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockCenter">
<div class="maroon">center</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockStyleEven">
<div class="maroon">space-evenly</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockSpaceAround">
<div class="maroon">space-around</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockSpaceBetween">
<div class="maroon">space-between</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockStart">
<div class="maroon">start</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockEnd">
<div class="maroon">end</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
<div class="blockCenter">
<div class="maroon">center</div>
<div class="red">RED</div>
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
</div>
</div>
</div>
</div>
Thanks in advance.
Yogi
Finally figured it out. The problem was the scrollbar was "too short" to show all the content. This was because my workspace class was on a div below the dummy menu bar. When I put the workspace (containing Margin: auto) on the outer move div, it worked perfectly.
Thanks for all your help.
Apologies for the earlier poor formatting, which has now been cleaned up.
Yogi