How can I make responsive grid layout in html css - html

I searched over internet and couldn't find this kind of grid layout.
"grid-template-columns" doesnt do the thing, cant do one box bigger then the others. I want 4 equal boxes and 1 box with equal height and width = square box * 2 + gridgap.
here is the image I've illustrated to make you understand what i ment.
I also tried to use display flex but I didnt get the Idea of it. Please, help me. Thanks!
Illustation of my idea

The answer to the question ("How can I make responsive grid layout in html css") is this:
.wrapper {
display: grid;
justify-content: center;
}
.box {
border: 2px solid #000;
width: 128px;
min-height: 128px;
justify-content: center;
margin: 10px;
display: grid;
align-items: center;
}
.box5 {
grid-column: 2/5;
max-width: 280px;
width: 100%;
}
/*for this to be visible, the screen-width has to be under 600px*/
#media (max-width: 600px) {
.box5 {
grid-column: 2/1;
}
}
<div class="wrapper">
<div class="box1 box">128x128</div>
<div class="box2 box">128x128</div>
<div class="box3 box">128x128</div>
<div class="box4 box">128x128</div>
<div class="box5 box">280x128</div>
</div>

Like in the image
.container {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 100%;
height: 180px;
}
.row {
display: flex;
flex-direction: row;
justify-content: center;
}
.box {
width: 10vw;
height: 10vw;
margin: 12px;
border: 1px solid black;
}
.box.big {
width: calc(20vw + 24px);
}
<div class="container">
<div class="row">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div class="row">
<div class="box big"></div>
</div>
</div>
Fields 1-5 directly below each other are centered to the middle at the current width
.container {
display: flex;
flex-direction: column;
align-items: center;
width: 100vw;
height: 100vw;
}
.box {
width: 10vw;
height: 10vw;
margin: 12px;
border: 1px solid black;
}
.box.big {
width: calc(20vw + 24px);
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box big"></div>
</div>

Related

How can I move the title to the center

I want to move the title to green area. If I carry to the container it puts blue area. If I decrease container height title getting closer to the squares. But I want the boxes in the center and the title little above of them. How can I do it?
#container {
width: 1200;
height: 600px;
margin: auto;
border: 1px solid grey;
display: flex;
align-items: center;
justify-content: center;
}
.box {
height: 250px;
width: 250px;
margin-left: 25px;
margin-right: 25px;
background-color: red;
}
h1 {
text-align: center;
}
<h1>TITLE!!!</h1>
<div id="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
my page
It's the display: flex; that's causing the issue. Here's a working model:
#container {
width: 1200;
height: 600px;
margin: auto;
border: 1px solid grey;
}
.box-wrapper {
display: flex;
align-items: center;
justify-content: center;
}
.box {
height: 250px;
width: 250px;
margin-left: 25px;
margin-right: 25px;
background-color: red;
}
h1 {
text-align: center;
}
<div id="container">
<h1>TITLE!!!</h1>
<div class="box-wrapper">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
I added a extra wrapper to wrap all the elements and added a display flex to it. Also, You can make use of a gap css property in wrapper class to add extra space between them.
.wrapper {
display: flex;
flex-direction: column;
border: 1px solid grey;
width: 1200px;
height: 600px;
}
#container {
margin: auto;
//border: 1px solid grey;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
}
.box {
height: 250px;
width: 250px;
margin-left: 25px;
margin-right: 25px;
background-color: red;
}
h1 {
text-align: center;
}
<div class="wrapper">
<h1>TITLE!!!</h1>
<div id="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
Also, the other way to solve this would be to use absolute positioning and adding a top padding to allow space for the div header if extra div is really not required.

how to use css flex to have a row of squares that scales and keeps aspect ratios?

I am pulling my hair out over this, I feel like it should be simple and I get close but can't get this perfect. I am trying to create a row of squares that will keep their aspect ratio but just get smaller as their container size shrinks. The way that I have gotten closest is having my square have a height of 100% with an aspect-ratio: 1/1; and then its container has a flex display. this works but only for the height, if I scale the width, the flex container and it's content does not scale.
I edited this to be easier to reproduce and used one of the provided solutions which got closest to what I am after.
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
width: 100%;
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.game {
width: 100%;
max-width: 500px;
height: 100%;
display: flex;
flex-direction: column;
}
.header-container {
background-color: grey;
width: 100%;
height: 15vh;
max-height: 100px;
}
.row-container {
background-color: green;
width: 100%;
height: 20vh;
max-height: 150px;
display: flex;
align-items: center;
justify-content: center;
}
.row {
display: flex;
height: 100%;
width: 100%;
flex-direction: row;
justify-content: center;
align-items: center;
}
.square {
aspect-ratio: 1/1;
flex: 0 1 100px;
background-color: blue;
margin: 5px;
}
.board-container {
background-color: aquamarine;
width: 100%;
height: 45vh;
}
.lower-container {
background-color: black;
width: 100%;
height: 30vh;
max-height: 250px;
}
<div class="game">
<div class="header-container">
</div>
<div class="row-container">
<div class="row">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
</div>
<div class="board-container">
</div>
<div class="lower-container">
</div>
</div>
I provided all the css but what i am trying to fix is in .square and .row and .row-container
#container {
display: flex;
height: 100%;
width: 100%;
flex-direction: row;
justify-content: center;
align-items: center;
}
.square {
aspect-ratio: 1/1;
flex: 0 1 55px;
background-color: blue;
margin: 10px;
}
<div id="container">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
Not quite sure if that's what you are looking for. Can you just provide a brief example of your use-case?
I may have misunderstood your question, but are you sure it doesn't scale? Here it is when I set the width to 100%:
#container {
display: flex;
height: 100%;
width: 100%;
flex-direction: row;
justify-content: center;
align-items: center;
}
.square {
background-color: orange;
width: 100%;
height: 100%;
max-height: 55px;
aspect-ratio: 1/1;
}
<div id="container">
<div class="square">a</div>
<div class="square">a</div>
<div class="square">a</div>
<div class="square">a</div>
<div class="square">a</div>
<div class="square">a</div>
</div>

How to create a table with flex-direction: column;?

My goal is to create a table with flex-direction: column;.
Ticker Price --.--
-- Volume --
index.html
<div class="d-flex">
<div class="p-1">
Ticker
<div id="stockSymbol" class="font-weight-bold display-4">--</div>
</div>
<div class="d-flex flex-column p-1">
<div class="d-flex">
Price
<div id="stockPrice" class="p-1 font-weight-bold display-4">--.--</div>
</div>
<div class="d-flex">
Volume
<div id="stockVolume" class="p-1">--</div>
</div>
</div>
</div>
styles.css
.p-1 {
padding: 1rem;
}
.d-flex {
display: flex;
}
.flex-column {
flex-direction: column;
}
.font-weight-bold {
font-weight: bold;
}
.display-4 {
font-size: 2rem;
}
My expected result: I can use the text-align: center; to make the stockPrice and stockVolume looks aligned.
My actual result: the text-align: center; does not affect the view.
What I've considered:
Use the HTML tables. Per my knowledge, it's not mobile friendly, especially if the first column direction is to below, and the 2nd and 3rd column direction is to the right.
Here you go! I used flex-direction column
I added quite a bit to the CSS, but that was just to demonstrate what the table is doing, so if you need any of the colors/margins removed; or anything changed let me know.
html, body {
height: 100%;
margin: 0;
}
.grid2x2 {
min-height: 60%;
width: 60%;
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
.grid2x2 > div {
display: flex;
flex-basis: calc(50% - 40px);
justify-content: center;
flex-direction: column;
}
.grid2x2 > div > div {
display: flex;
justify-content: center;
flex-direction: row;
}
.box { margin: 3px; }
.box1 { background-color: red; }
.box2 { background-color: orange; }
.box3 { background-color: purple; }
.box4 { background-color: grey; }
<div class="grid2x2">
<div class="box box1"><div>Price</div></div>
<div class="box box2"><div>--.--</div></div>
<div class="box box3"><div>Volume</div></div>
<div class="box box4"><div>--</div></div>
</div>
Here's my take on your problem of using flex-direction: column to create a table. Through this approach you can use the div class="col" to append data columns to the right of Price-Volume column.
.table {
display: flex;
column-gap: 5px;
margin: 10px;
border: 1px solid black;
width: fit-content;
width: -moz-fit-content;
}
.col {
display: flex;
flex-direction: column;
justify-content: center;
row-gap: 5px;
}
.col div {
background: beige;
}
.align-center {
/* align-self: center; */
text-align: center;
}
.align-right {
/* align-self: flex-end; */
text-align: right;
}
<div class="table">
<div class="col">
<div class="align-center">Ticker</div>
<div class="align-center">--</div>
</div>
<div class="col">
<div class="align-center">Price</div>
<div class="align-center">Volume</div>
</div>
<div class="col">
<div class="align-right">--.--</div>
<div class="align-right">--</div>
</div>
</div>
<div class="table">
<div class="col">
<div class="align-center">Ticker</div>
<div class="align-center">--</div>
</div>
<div class="col">
<div class="align-center">Price</div>
<div class="align-center">Volume</div>
</div>
<div class="col">
<div class="align-right">98.56</div>
<div class="align-right">20</div>
</div>
<div class="col">
<div class="align-right">72.03</div>
<div class="align-right">13</div>
</div>
</div>

Dynamic centered header bar with CSS

currently I have this header bar
#header {
display: flex;
align-items: center;
height: 60px;
background: gray;
}
.box {
width: 32px;
height: 32px;
margin: 0 5px;
background: red;
}
<div id="header">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
and I want the containers align themselves in the center of the parent.
As you can see I always want the bar being centered when adding more divs or remove some of them.
Add the justify-content: space-around rule:
#header {
display: flex;
align-items: center;
height: 60px;
background: gray;
justify-content: space-around;
}
.box {
width: 32px;
height: 32px;
margin: 0 5px;
background: red;
}
<div id="header">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
You could also use justify-content: space-evenly;. The difference between space-around and space-evenly is that space around has less space towards the (left and right) edges of the parent container, whereas the space in space-evenly is similar everywhere.
#header {
justify-content: space-evenly;
display: flex;
align-items: center;
height: 60px;
background: gray;
}
.box {
width: 32px;
height: 32px;
margin: 0 20px;
background: red;
}
<div id="header">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
Here is a good resource for flexbox.

Use flexbox to center content while not making elements side by side

I am using flexbox to center content with justify-content: center which works as intended but flexbox also moves divs to be side by side which I don't want.
Here is an example
.flex {
display: flex;
justify-content: center;
}
.content {
width: 100px;
height: 100px;
background-color: #000;
margin: 10px;
}
<div class="flex">
<div class="content">
</div>
<div class="content">
</div>
</div>
How can I use flexbox while retaining the default one div on top of the other positioning.
You can set flex-direction: column and then you have to use align-items: center. Default flex-direction is row.
.flex {
display: flex;
align-items: center;
flex-direction: column;
}
.content {
width: 100px;
height 100px;
color: #000;
border: 1px solid black;
padding: 5px;
margin: 5px;
}
<div class="flex">
<div class="content"></div>
<div class="content"></div>
</div>
Try following code,
.flex {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
}
.content {
width: 100px;
height: 100px;
background-color: #000;
margin: 10px;
}
<div class="flex">
<div class="content">
</div>
<div class="content">
</div>
</div>