CSS and Flexbox layout issue - html

Created a codepen at http://codepen.io/cbireddy/pen/vmYxbz
.board {
display: flex;
width: 600px;
height: 400px;
justify-content: flex-start;
flex-flow: row wrap;
}
.square {
width: 200px;
height: 200px;
border: 5px solid black;
border-radius: 10px;
}
<div class="=board">
<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>
I was thinking the squares should be in a 2x3 matrix. Instead they are all stacked vertically. What am I missing? Thought that display: flex will align the items along main axis, which is horizontal.
Can someone please look into this and tell me what am I doing wrong? Goal is to visualize the squares in a 2x3 matrix.
-Thanks
Chakri

You need to fix the typo and add box-sizing: border-box to .square so that the borders don't push beyond the defined width.
.board {
display: flex;
width: 600px;
height: 400px;
justify-content: flex-start;
flex-flow: row wrap;
}
.square {
width: 200px;
height: 200px;
border: 5px solid black;
border-radius: 10px;
box-sizing: border-box;
}
<div class="board">
<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>

In row 24 of your HTML (in Codepen), you have typo. Just change <div class="=board">to <div class="board">

<div class="=board">
Replace this with
<div class="board">
Should do the trick.

Try This code
.board {
display: flex;
width: 600px;
height: 400px;
justify-content: flex-start;
flex-flow: row wrap;
}
.square {
width: 200px;
height: 200px;
border: 5px solid black;
border-radius: 10px;
}
<div class="board">
<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>

Related

How to space grid elements correctly

I want to apply this style to my current code but the spaces dont fit the grid div they spill out under it:
.square {
width: 20%;
height: 20%;
border: 1px solid black;
box-sizing: border-box;
margin: 1%;
border-radius: 5px;
}
Here is my current HTML/CSS:
.container {
display: flex;
justify-content: center;
align-items: center;
height: 500px;
}
.grid {
display: flex;
flex-wrap: wrap;
width: 500px;
height: 500px;
margin: 0 50px;
}
.square {
width: 20%;
height: 20%;
border: 1px solid black;
box-sizing: border-box;
}
.selector {
width: 200px;
height: 500px;
display: flex;
flex-direction: column;
align-items: center;
margin: 0 50px;
}
.bet-amount {
margin: 50px;
}
.button {
margin: 50px;
}
<html>
<head>
<link rel="stylesheet" href="index.css">
</head>
<body>
<div class="container">
<div class="grid">
<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 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 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 class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
<div class="selector">
<div class="amount">
<label for="amount-input">Amount:</label>
<input type="text" id="amount-input">
</div>
<div class="button">
<button>Play</button>
</div>
</div>
</div>
</body>
</html>
I also want to center the whole container vertically and horizontally in the center of the screen and convert the px to %.
I have tried editing the various heights and widths of the elements but can't seem to get it to work.

HTML/CSS Tic Tac Toe board: cells are moving around while they shouldn't

I am learning HTML/CSS/JavaScript and I am trying to make a Tic Tac Toe game in the browser. I thought I had the board set up, but noticed that the cells are changing position as soon as I remove one or more X/O tokens. I can't figure out what causes this bug and would really appreciate help.
.board {
display: block;
width: 350px;
height: 350px;
margin: 70px auto;
}
.cell {
display: inline-flex;
margin: 2px 0;
border: 3px solid black;
width: 100px;
height: 100px;
box-shadow: 2px 2px;
justify-content: center;
align-items: center;
font-size: 100px;
}
<section class="board">
<h2>Player X's turn</h2>
<div class="row0">
<div class="cell c00">X</div>
<div class="cell c01">O</div>
<div class="cell c02">X</div>
</div>
<div class="row1">
<div class="cell c10">O</div>
<div class="cell c11">X</div>
<div class="cell c12">O</div>
</div>
<div class="row2">
<div class="cell c20">X</div>
<div class="cell c21">O</div>
<div class="cell c22">X</div>
</div>
</section>
Thanks a lot in advance :)
Well it is because you are using inline-flex, which makes the div inline positiond and relative to the content it has, you can try to change the font-size and see how it affects all divs. I am not sure why you are trying to use inline flex but I would recommend you to read this guide I personally would just set the .row0, .row1 and .row2 to just display: flex and tweek the width of the .board a little
.board {
display: block;
width: 326px;
height: 350px;
margin: 70px auto;
}
.cell {
display: inline-flex;
margin: 2px 0;
border: 3px solid black;
width: 100px;
height: 100px;
box-shadow: 2px 2px;
justify-content: center;
align-items: center;
font-size: 100px;
}
.row0, .row1, .row2 {
display: flex;
justify-content: space-between;
}
<section class="board">
<h2>Player X's turn</h2>
<div class="row0">
<div class="cell c00">X</div>
<div class="cell c01">O</div>
<div class="cell c02"></div>
</div>
<div class="row1">
<div class="cell c10">O</div>
<div class="cell c11">X</div>
<div class="cell c12">O</div>
</div>
<div class="row2">
<div class="cell c20">X</div>
<div class="cell c21"></div>
<div class="cell c22">X</div>
</div>
</section>

Align bottom not works in flex box

I am trying to align the footer at the bottom, leave the space at the top. but not works. any one help me to know the correct way?
.parent {
border: 1px solid red;
display: flex;
}
.child {
border: 1px solid green;
min-height: 100px;
flex: 1;
align-items:bottom;
}
.footer{
background:#808080;
}
<div class="parent">
<div class="child">
<div class="footer">Footer</div>
</div>
<div class="child">
<div class="footer">Footer</div>
</div>
<div class="child">
<div class="footer">Footer-will be in bottom!!</div>
</div>
</div>
First of all, to align items to the bottom the correct way is align-items: flex-end;
I also declared display:flex; on .child, and gave it a width.
.parent {
border: 1px solid red;
display: flex;
}
.child {
border: 1px solid green;
min-height: 100px;
display: flex;
width: 33.333%;
align-items: flex-end;
}
.footer{
background:#808080;
width: 100%;
}
<div class="parent">
<div class="child">
<div class="footer">Footer</div>
</div>
<div class="child">
<div class="footer">Footer</div>
</div>
<div class="child">
<div class="footer">Footer-will be in bottom!!</div>
</div>
</div>
In this example your .child also needs to have display: flex and .footer needs align-items: bottom.
Why? This is actually multiple nested flex layouts.
As per the code you have provided. This can be a possible solution.
.parent {
border: 1px solid red;
display: flex;
}
.child {
border: 1px solid green;
min-height: 100px;
flex: 1;
display: inline-flex;
align-items: flex-end;
flex-direction: row;
}
.footer {
display: inline-block;
width: 100%;
background:#808080;
}
<div class="parent">
<div class="child">
<div class="footer">Footer</div>
</div>
<div class="child">
<div class="footer">Footer</div>
</div>
<div class="child">
<div class="footer">Footer-will be in bottom!!</div>
</div>
</div>
But in a more optimized way. Let me show you another sample:
.parent {
border: 1px solid red;
display: flex;
}
.child {
flex: 1;
border: 1px solid green;
min-height: 100px;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
}
.footer{
background:#808080;
}
<div class="parent">
<div class="child">
<div class="content">Content</div>
<div class="footer">Footer</div>
</div>
<div class="child">
<div class="content">Content</div>
<div class="footer">Footer</div>
</div>
<div class="child">
<div class="content">Content</div>
<div class="footer">Footer-will be in bottom!!</div>
</div>
</div>
If you don't want to change the HTML tags and use the same as in the question, you can go with the first solution. Otherwise, the second one will serve better.
Remove line-height from the .child and add height for the .parent.
Updated :
.parent {
border: 1px solid red;
display: flex;
align-items: flex-end;
height: 100px;
}
.child {
border: 1px solid green;
flex: 1;
align-items:bottom;
}
.footer{
background:#808080;
}
<div class="parent">
<div class="child">
<div class="footer">Footer</div>
</div>
<div class="child">
<div class="footer">Footer</div>
</div>
<div class="child">
<div class="footer">Footer-will be in bottom!!</div>
</div>
</div>

Flex-wrap property not responsive when parent has a set width

In the example below, when I set a width for a wrapper, the parent flex container can no longer use the flex-wrap property. The top two boxes won't wrap, but the bottom ones will.
<div class="wrapper">
<div class="parent">
<div class="child"></div>
<div class="child"></div>
</div>
</div>
<div class="parent">
<div class="child"></div>
<div class="child"></div>
</div>
.wrapper {
width: 700px;
margin: 0 auto;
border: solid cadetblue 5px;
}
.parent {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.child {
height: 250px;
min-width: 250px;
max-width: 300px;
flex: 1;
background: mistyrose;
border: solid goldenrod 2px;
margin: 30px;
}
The 'issue' you raise is by design; you're specifying a width for the parent that is wide enough for your children to be wholly contained within (a 700px container for two 300px children). flex-wrap only causes elements to overflow when there's not enough space for the container to hold them. In your example, there is.
To force an overflow responsively, you could either specify a narrow width on the parent(which will cause an overflow for all viewports):
.wrapper {
width: 400px;
margin: 0 auto;
border: solid cadetblue 5px;
}
.parent {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.child {
height: 250px;
min-width: 250px;
max-width: 300px;
flex: 1;
background: mistyrose;
border: solid goldenrod 2px;
margin: 30px;
}
<div class="wrapper">
<div class="parent">
<div class="child"></div>
<div class="child"></div>
</div>
</div>
<div class="parent">
<div class="child"></div>
<div class="child"></div>
</div>
Or use max-width instead(which will only overflow on narrow viewports):
.wrapper {
max-width: 700px;
margin: 0 auto;
border: solid cadetblue 5px;
}
.parent {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.child {
height: 250px;
min-width: 250px;
max-width: 300px;
flex: 1;
background: mistyrose;
border: solid goldenrod 2px;
margin: 30px;
}
<div class="wrapper">
<div class="parent">
<div class="child"></div>
<div class="child"></div>
</div>
</div>
<div class="parent">
<div class="child"></div>
<div class="child"></div>
</div>

Centering the container of divs

I'm trying to center container of divs (gray boxes) so that A=B. This equality should be preserved during resizing of the browser windows. Also, when the window is smaller then the width of current number of divs in a row, div arrangement should change to less number of columns.
I was trying to find some code, but no luck. Hope that someone could help.
And this is the code I have for now
HTML:
<div id="Area">
<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 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>
CSS:
#Area {
padding: 30px 50px; /* top and bottom 25px, left and right 50px */
}
#container{
width: 90%;
margin: 0 auto;
}
.square {
display: inline-block;
margin: 7 auto;
width: 350px;
height: 200px;
background-color: gray;
border: 1px black solid;
}
UPDATE.
After Shai's help, I've come up to a new problem. Divs are now stacked liked this:
But I would like to have them like this:
Add text-align: center; to the #container.
Working JSFiddle
A quick fix without changing your markup and styles would be to give your #area the position:relative; and width:100% attributes as well as changing the width:90% to a full width:100% of your #container
#Area {
padding: 30px 50px; /* top and bottom 25px, left and right 50px */
width: 100%;
position: relative;
}
#container{
width: 100%;
margin: 0 auto;
}
http://jsfiddle.net/8a0035ep/
Another solution(which i prefer most) is to remove dsiplay: inline-block(so keep default block) and use margin: 0 auto. Also add margin-bottom at x px according to your needs.
#Area {
padding: 30px 50px;
/* top and bottom 25px, left and right 50px */
}
#container {
width: 90%;
margin: 0 auto;
}
.square {
margin: 7 auto;
width: 350px;
height: 200px;
background-color: gray;
border: 1px black solid;
margin: 0 auto;
margin-bottom: 5px;
}
<div id="Area">
<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 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>
Could your heard about media query. I use the attribute to achieve your purpose.
This is my JSFiddle. The main code:
#media screen and (min-width:352px){
#container{
text-align: center;
}
}