I got this flex-box problem,
i got 2 divs contained in 1 div, each one of the is a flexbox too, the Divs pop out of the parent div,
i want them to never move out the parent divs dimensions,
the divs go over the table below them at the moment,
how do i accomplish this?
Another Question: I would like to ask is, in the div named container i got a table,
i want that table to be dead center of the div, and of course never collide with the footer or header
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0
}
.main {
display: flex;
flex-direction: column;
text-align: center;
height: 100vh;
}
.header {
display: flex;
flex-direction: column;
justify-content: space-evenly;
/* flex-wrap: wrap; */
height: 20%;
}
.header h1 {
width: 100%;
}
.secondHeader {
display: flex;
flex-direction: row;
justify-content: space-evenly;
}
.levels {
display: flex;
flex-direction: row;
justify-content: space-evenly;
background: green;
margin: 5px;
}
.main-Game-Body {
display: flex;
flex-direction: row-reverse;
height: 72%;
margin: 5px;
padding: 4px;
}
.container {
flex-grow: 1;
}
.footer {
height: 8%;
margin: 4px;
}
<div class="main">
<div class="header">
<h1>Welcome To Minesweeper</h1>
<div class="secondHeader">
<button class="hints" onclick="activateHintMode()">
You Have: <span></span> Hints Left
</button>
<button class="safe" onclick="safeClick()">
You Have: <span></span> safe clicks Left
</button>
<button class="lives">You Have: <span></span> Lives Left</button>
<button class="backUp" onclick="stepBack()">stepBack</button>
</div>
<div class="levels">
<button class="manual" onclick="manualMode()">
Manual Mode
</button>
<button class="level" onclick="easyMode()">Easy Mode</button>
<button class="level" onclick="hardMode()">Hard Mode</button>
<button class="level" onclick="expertMode()">EXPERT</button>
</div>
</div>
<div class="main-Game-Body">
<!-- <button class="icon" onclick="restartLevel()"></button> -->
<div class="container"></div>
</div>
<div class="footer">
<div class="timer" onclick="timer()">
Your Score: <span class="stopwatch"></span> Best Score At Difficulty
<span class="difficulty"></span> is: <span class="results"></span>
</div>
</div>
</div>
This is not the issue with levels div. You have given static height to each div that's why the content is trying to fit in that height. When you try to increase the margin the element treis to reside inside the container height resulting in moving the items above levels div outside the container.
Solution: Remove static height for header, main-Game-Body and footer, use flex-grow: 1 for main-Game-Body this will help fitting the content in remaining height.
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0
}
.main {
display: flex;
flex-direction: column;
text-align: center;
height: 100vh;
}
.header {
display: flex;
flex-direction: column;
justify-content: space-evenly;
/* height: 20%; */
}
.header h1{
width: 100%;
}
.secondHeader {
display: flex;
flex-direction: row;
justify-content: space-evenly;
}
.levels {
display: flex;
flex-direction: row;
justify-content: space-evenly;
background: green;
margin: 5px;
}
.main-Game-Body {
display: flex;
flex-direction: row-reverse;
/* height: 72%; : Do not give height, use flex grow*/
flex-grow: 1;
margin: 5px;
padding: 4px;
}
.container {
flex-grow: 1;
}
.footer {
/* height: 8%; */
margin: 4px;
}
<div class="main">
<div class="header">
<h1>Welcome To Minesweeper</h1>
<div class="secondHeader">
<button class="hints" onclick="activateHintMode()">
You Have: <span></span> Hints Left
</button>
<button class="safe" onclick="safeClick()">
You Have: <span></span> safe clicks Left
</button>
<button class="lives">You Have: <span></span> Lives Left</button>
<button class="backUp" onclick="stepBack()">stepBack</button>
</div>
<div class="levels">
<button class="manual" onclick="manualMode()">
Manual Mode
</button>
<button class="level" onclick="easyMode()">Easy Mode</button>
<button class="level" onclick="hardMode()">Hard Mode</button>
<button class="level" onclick="expertMode()">EXPERT</button>
</div>
</div>
<div class="main-Game-Body">
<!-- <button class="icon" onclick="restartLevel()"></button> -->
<div class="container"></div>
</div>
<div class="footer">
<div class="timer" onclick="timer()">
Your Score: <span class="stopwatch"></span> Best Score At Difficulty
<span class="difficulty"></span> is: <span class="results"></span>
</div>
</div>
</div>
Related
This question already has answers here:
How to center an element horizontally and vertically
(27 answers)
Best way to center a <div> on a page vertically and horizontally? [duplicate]
(30 answers)
Closed 25 days ago.
centering the div to be center to the webpage.
This has been asked a hundred times before, and yet I'm still having trouble finding the exact answer.
I'm trying to center this div exactly center of the page. I've set justify-content and align-items to center, but it not showing the expected result. I'm sure I'm missing something, but what?
body {
display: flex;
justify-content: center;
}
.box {
display: flex;
justify-content: center;
align-items: center;
border: 5px solid black;
width: 50vh;
height: auto;
}
.content {
display: flex;
flex-direction: column;
align-items: center;
}
.buttons {
display: flex;
padding: 10px;
margin-top: 10px;
}
<div class="box">
<div class="content">
<h1 class="header">BLACKJACK</h1>
<p id="message-el" class="message">What to play a round? </p>
<p id="cards-el" class="cards">Cards:</p>
<p id="sum-el" class="sum">Sum:</p>
<div class="buttons">
<button id="button-start-el" class="button-start">START GAME</button>
<button id="button-draw-el" class="button-draw">DRAW CARD</button>
<button id="button-new-el" class="button-new">NEW GAME</button>
</div>
</div>
</div>
<script src="/js/javascript.js"></script>
</body>
Set body to 100vh and add align-items: center;.
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.box {
display: flex;
justify-content: center;
align-items: center;
border: 5px solid black;
width: max-content;
padding: 1em 2em;
height: auto;
}
.content {
display: flex;
flex-direction: column;
align-items: center;
}
.buttons {
display: flex;
padding: 10px;
margin-top: 10px;
}
<body>
<div class="box">
<div class="content">
<h1 class="header">BLACKJACK</h1>
<p id="message-el" class="message">What to play a round? </p>
<p id="cards-el" class="cards">Cards:</p>
<p id="sum-el" class="sum">Sum:</p>
<div class="buttons">
<button id="button-start-el" class="button-start">START GAME</button>
<button id="button-draw-el" class="button-draw">DRAW CARD</button>
<button id="button-new-el" class="button-new">NEW GAME</button>
</div>
</div>
</div>
<script src="/js/javascript.js"></script>
</body>
I was trying to achieve 0 and C button as twice the size of normal button and = button as twice in height (taller) as height of normal button.
What I've tried -
Div1 - Main Divison display flex and direction column.
Div2 - The input bar inside Div1.
Div3 - The whole divison of buttons.
Div3 - Contains Two Div as flex layout in row manner (Div4, Div5).
Div4 - Contains rows of divs which contain button except the buttons present inside last column.
Inside Div4 I was able to do flex:2 for the button I need twice as width.
Div5 - Last column of buttons But when I set flex:2 for "=" button It's height doesn't change.
CodePen Link
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> calculator </title>
<script src="./calculator.js"> </script>
<link rel="stylesheet" href="calculator.css">
</head>
<body>
<div class="calculator">
<div id="textbox">
<span type="text" id="tagone"></span>
</div>
<div id="buttondivision">
<div class="calc-col1">
<div class="calc-row">
<button id="clearbutton" onclick="clearfun()"> C</button>
<button id="percentbutton" onclick="percentfun()">%</button>
</div>
<div class="calc-row">
<button onclick="setvalue('7')"> 7 </button>
<button onclick="setvalue('8')"> 8 </button>
<button onclick="setvalue('9')"> 9</button>
</div>
<div class="calc-row">
<button onclick="setvalue('4')"> 4</button>
<button onclick=" setvalue('5')"> 5 </button>
<button onclick="setvalue('6')"> 6</button>
</div>
<div class="calc-row">
<button onclick="setvalue('1')"> 1 </button>
<button onclick="setvalue('2')"> 2 </button>
<button onclick="setvalue('3')"> 3</button>
</div>
<div class="calc-row">
<button id="zerobutton" onclick="setvalue('0')"> 0 </button>
<button id="dotbutton" onclick="dotfun()"> .</button>
</div>
</div>
<div class="calc-col2">
<button onclick="dividefun()"> /</button>
<button onclick="multiplyfun()"> * </button>
<button onclick="minusfun()"> - </button>
<button onclick="plusfun()"> + </button>
<button id="equalbtn" onclick="equalfun()"> = </button>
</div>
</div>
</div>
</body>
</html>
CSS -
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
padding: 20px;
margin: 0px;
font-family: "sourcesans";
}
.calculator {
display:flex;
flex-direction: column;
width: 100%;
height: 100%;
align-items: center;
}
#textbox {
flex: 4;
width: 25%;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
margin-bottom: 10px;
}
#textbox > #tagone {
display: flex;
flex-direction: row;
justify-content: flex-end;
width: 100%;
height: 50px;
border: 1px solid black;
font-size: 1.5em;
padding: 0 10px;
}
#buttondivision {
width: 25%;
align-items: center;
display: flex;
flex-direction: row;
/* break-inside: avoid; */
}
.calc-col1 {
display: flex;
flex-direction: column;
flex:2;
width:100%;
}
.calc-col2 {
display: flex;
flex-direction: column;
flex:1;
height:100%;
}
/* .calc-col2:last-child {
flex:2;
} */
.calc-row {
display: flex;
flex-direction: row;
width:100%;
/* flex:1; */
}
.calc-row > button {
flex:1;
}
.calc-col2 {
width:100%;
height:100%;
flex:1;
}
#clearbutton, #zerobutton, #equalbtn {
flex: 2;
}
button {
background-color: rgb(199, 255, 192);
cursor: pointer;
margin: 0px;
padding: 0px;
box-sizing: content-box;
border: 0px solid black;
border-top:none;
border-left:none;
font-size:2em;
margin: 1px;
}
.calc-row > button:hover {
background-color: rgb(199, 255, 141);
}
.calc-row2 > button:hover {
background-color: rgb(199, 255, 141);
}
Try to set height for #buttondivision. This will allow flexbox to calculate relative height for buttons inside .calc-col1
E.g. something like here - https://codepen.io/AlphaOmegaBetaGamma/pen/abYaKLo
I've tried using text-align and justify-content but both of them refuse to work. I think I've made a mistake with all the spacings.
Here's the HTML:
.container1 {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.placeholder {
display: flex;
justify-content: center;
align-items: center;
background-color: #6D747D;
width: 400px;
height: 200px;
}
.contents1 {
display: flex;
margin: 0 auto;
}
<div class="contents1">
<div class="container1">
<h1>This website is awesome</h1>
<p>This website has some subtext that goes hear under the main title. It's a smaller font and the color is lower contrast</p>
<div class="button">Sign up</div>
</div>
<div class="image">
<div class = "placeholder">this is a placeholder for an image</div>
</div>
</div>
Put the justify-contents and align-items into class image
.container1 {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.image{
display: flex;
justify-content: center;
align-items: center;
background-color: #6D747D;
width: 400px;
height: 200px;
}
It is a good practice to first add some background-color to better see where each element is and then remove these dumb colors.
For the container which has a display:flex, please add
width:100%;
.container1 {
display: flex;
flex-direction: column;
flex-wrap: wrap;
width: 75%;
margin: auto;
}
.placeholder {
display: flex;
justify-content: center;
align-items: center;
background-color: #6D747D;
width: 75%
height: 200px;
margin: auto;
}
.contents1 {
display: flex;
flex-direction: column;
margin: 0 auto;
}
<div class="contents1">
<div class="container1">
<h1 class="center">This website is awesome</h1>
<p class="center">This website has some subtext that goes hear under the main title. It's a smaller font and the color is lower contrast</p>
<div class="button">Sign up</div>
</div>
<div class="image">
<div class = "placeholder">this is a placeholder for an image</div>
</div>
</div>
Assuming you want to center the container1 div with the image placeholder div, the issue is that your divs don't have an assigned with. Set their width to a percentage less than the parent and then use the CSS property margin: auto. You will also need to apply flex-direction: column to your parent div, contents1
.container1 {
display: flex;
flex-direction: column;
flex-wrap: wrap;
width: 400px;
margin: auto;
}
.placeholder {
display: flex;
justify-content: center;
align-items: center;
background-color: #6D747D;
width: 400px;
height: 200px;
margin: auto;
}
.contents1 {
display: flex;
flex-direction: column;
margin: 0 auto;
}
<div class="contents1">
<div class="container1">
<h1 class="center">This website is awesome</h1>
<p class="center">This website has some subtext that goes hear under the main title. It's a smaller font and the color is lower contrast</p>
<div class="button">Sign up</div>
</div>
<div class="image">
<div class = "placeholder">this is a placeholder for an image</div>
</div>
</div>
This question already has answers here:
Centered elements inside a flex container are growing and overflowing beyond top [duplicate]
(3 answers)
Closed 2 years ago.
I created this css snippet.
.container {
display: flex;
justify-content: center;
width: 100%;
align-items: center;
height: calc(100vh - 50px);
}
.container__img {
display: flex;
flex-direction: column;
}
.header {
background-color: red;
padding: 25px;
}
<div class="header">HEADER</div>
<div class="container">
<div class="container__inner">
<div class="container__img">
HELLO
<img src="https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569__340.jpg" />
</div>
</div>
</div>
I set 100vh to have the page 100% of page height, but appears the next issue when i resize the page vertically:
How you can see the image goes over the header, but it should stop when is near header like:
Question: Why it is happening and how to solve this?
Make it a habit for yourself to wrap all the divas in the main div:
<div class="main">
...
</div>
When this main div is absent in the structure of html, then problems may arise in the future.
I made some changes to the css. If you have any questions, then ask me.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.main {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.container {
display: flex;
justify-content: center;
width: 100%;
align-items: center;
flex: auto;
}
.container__img {
display: flex;
flex-direction: column;
}
.header {
background-color: red;
padding: 25px;
}
<div class="main">
<div class="header">HEADER</div>
<div class="container">
<div class="container__inner">
<div class="container__img">
HELLO
<img src="https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569__340.jpg" />
</div>
</div>
</div>
</div>
When you resize the page vertically, the image goes over the header because the div "container__inner" and the div "container__img" have height: auto (by default). That means their height will be equal to the maximum height of the elements inside (here is the img).
To solve this problem, you just need to set height: 100% for one of them. Then its height will not exceed the height of the parent element (the div "container")
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.main {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.container {
display: flex;
justify-content: center;
width: 100%;
align-items: center;
flex: auto;
}
.container__img {
display: flex;
flex-direction: column;
text-align: center;
}
.header {
background-color: red;
padding: 25px;
text-align: center;
}
<div class="main">
<div class="header">HEADER</div>
<div class="container">
<div class="container__inner">
<div class="container__img">
HELLO
<img src="https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569__340.jpg" />
</div>
</div>
</div>
</div>
Basically the title. I have a flex container, that has its height set to a specific value (100% but its parent is a specific one. I want the items to fill the available space, regardless of the content in them (specifically the second item, the treeview1, which has its overflow set to scroll. However, the items simply don't stretch to fill. Do you have any suggestions for me? Thanks :)
html, body, .container-fluid, .row, #left-panel {
height: 100%;
}
.thumbnail-view {
margin: 2px;
padding: 2px;
width: 100%;
overflow-y: scroll;
}
.buttons-placeholder {
padding-right: 15px;
padding-left: 15px;
display: flex;
justify-content: center;
flex-flow: row wrap;
}
.buttons-placeholder button {
margin: 2px;
flex-grow: 1;
}
.btn-success {
width: 100%;
margin-bottom: 10px;
}
<div class="container-fluid">
<div class="row">
<div class="col-md-2" id="left-panel">
<div class="buttons-placeholder">
<button type="button" class="btn btn-primary">Load Batch</button>
<button type="button" class="btn btn-primary">Save Batch</button>
</div>
<div class="thumbnail-view"></div>
<button type="button" class="btn btn-success">Submit</button>
</div>
</div>
</div>
Edit: Sorry if the formatting is off, i'm kind of new to this.
Edit 2: Cleaned up the code a bit.
We can set display: flex; flex-direction:column; height: 100vh; to the row class and set flex: 1 for the element that needs to fill the remaining space. In addition, try to set flex-direction: column for the flex container:
html, body, .container-fluid, .row, #left-panel {
display: flex;
flex-direction: column;
height: 100vh;
}
.thumbnail-view {
margin: 2px;
padding: 2px;
width: 100%;
overflow-y: scroll;
background-color: lightpink;
flex: 1;
}
.buttons-placeholder {
padding-right: 15px;
padding-left: 15px;
display: flex;
justify-content: center;
flex-flow: row wrap;
}
.buttons-placeholder button {
margin: 2px;
flex-grow: 1;
}
.btn-success {
width: 100%;
margin-bottom: 10px;
}
and html:
<div class="container-fluid">
<div class="row">
<div class="buttons-placeholder">
<button type="button" class="btn btn-primary">Load Batch</button>
<button type="button" class="btn btn-primary">Save Batch</button>
</div>
<div class="thumbnail-view"></div>
<button type="button" class="btn btn-success">Submit</button>
</div>
</div>
I guess that "treeview1" element that you've mentioned is actually element with class "thumbnail-view". That element doesn't want to stretch because it is not a flex element. The only flex wrapper that you have is the one with "buttons-placeholder" class. So, what you need to do is to add
style="display: flex; flex-direction: column; justify-content: space-between;"
to element that currently has "col-md-2" class.