I am building a website layout as an exercise, using Flexbox. But elements collapse into each other when I put the website through responsive test.
Using Flexbox, I tried to build a responsive website, where it would self-adjust ratios when you play with the dimensions of the browser. The horizon test was a pass, but when I opened the inspect tools, the "vertical view port" became smaller and the blue 'Section' and the fuchsia 'Footer' all collapsed into each other, because the vertical aspect of the website is not working as expected.
I tried to change and play around with different size units such as {%, em, vh, vw} to get the elements to readjust their sizes in relation to each other, but it still collapses into each other when I
I uploaded the code: {HTML & CSS} to Codepen as well.
:root{
text-align: center;
}
body{
background-color: gray;
height: 100vh;
}
.container{
margin-left: 1em;
margin-right: 1em;
background-color: gray;
display: flex;
flex-direction: column;
}
.headerAndNav{
}
.header{
background-color: navajowhite;
border: 0.5em solid gray;
}
.nav{
background-color: powderblue;
border: 0.5em solid gray;
}
.sectionAndAside{
display: flex;
flex-direction: column;
align-items: stretch;
text-align: center;
}
.header{
height: 8vh;
}
.nav{
height: 8vh;
}
.middle{
display: flex;
flex-direction: row;
/* background-color: red; */
border: 0.5em solid gray;
height: 40vh;
}
.aside{
background-color: powderblue;
width: 50%;
margin: 0.5em 0 0.5em 0.5em;
}
.section{
color:red;
display: flex;
flex-direction: column;
background-color: blue;
width: 50%;
margin: 0.5em 0.5em 0.5em 0;
padding-top: 3em;
}
.article{
background-color: yellow;
height: 100vh;
margin: 10px;
}
h1{
background-color: bisque;
}
h2{
background-color:gold;
}
h3{
background-color: khaki;
}
.hClass{
background-color: gray;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
height: 10vh;
margin: 1em;
}
.p{
background-color: lightcoral;
height: 10vh;
margin: 1em;
}
.footer{
background-color: fuchsia;
border: 0.5em solid gray;
height: 10vh;
}
<div class='container'>
<div class='sectionAndAside'>
<div class='header'>header</div>
<div class='nav'> nav</div>
</div>
<div class='middle'>
<div class='section'>section
<div class='article'>article
<div class='conta'>
<div class='hClass'>
<h1>h1</h1>
<h2>h2</h2>
<h3>h3</h3>
</div>
<div class='p'>
<p>this is p</p>
</div>
</div>
</div>
</div>
<aside class='aside'>aside</aside>
</div>
<div class='footer'>footer</div>
</div>
I was hoping to find a way/solution/method-of-approach and learn how to how to effectively control the layout of a website when I build them from scratch. This image here (bottom), is exactly what I am trying to replicate. I eventually did, but it was not responsive. Initially I had trouble with controlling div s positions, but flexbox helped me with that. The objective now is to learn how to get the vertical properties of the elements to behave as well as the horizontal ones.
use property flex wrap :wrap; it helps in small screen.
https://scrimba.com/g/gflexbox
Related
So I am trying to create a logo and a menu icon in the header but for some reason, they are always overflowing the height of the header which I have strictly specified! Why is that ?
And I know I can hide out the overflowing items by using overflow:hidden; property but it is not always a good case.
For example, I tried to create a hamburger icon but I could not because of this overflow issue. The menu lines were working as if the entire element is shown but I had to hide it out so that it could fit into the header.
Here is the code -
<header>
<div class="logo">
Elvis
</div>
<div class="menu">
Hamburger Menu
</div>
</header>
In CSS -
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
display: flex;
justify-content: space-between;
align-items: center;
border: 2px solid red;
}
.logo {
font-size: 33px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
color: white;
}
.menu {
height: 100px;
width: 100px;
background-color: #bd4439;
}
Here is the codepen link -
https://codepen.io/raghav-sharma333/pen/eYeZYGO
Here is the image of the issue -
Overflowing content
So I just want to know :
Why is it happening?
&
How can it be prevented?
Basically you are forcing your elements to be higher than the header itself by giving them static heights (height 100px on the menu and padding-top/bottom 30px on the logo)
I updated your pen: https://codepen.io/penmasterx/pen/wvPGaGz
Using height 100%, so the elements adapt to the header.
Let me know if this solves your problem. If not, let me know in more detail what you're trying to accomplish.
What I added to the pen:
.logo {
height: 100%;
display: flex;
align-items: center;
/* removed padding top/bottom */
}
.menu {
height: 100%;
}
In such cases, it is better to use the position to manage the inheritance of the elements
I modified your code:
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
align-items: center;
border: 2px solid red;
position: relative;
}
.wrapper{
display: flex;
justify-content: space-between;
width: 100%;
height: 100%;
position: absolute;
}
.logo {
font-size: 30px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
max-height: 100%;
color: white;
}
.menu {
height: 100%;
width: 100px;
background-color: #bd4439;
}
<header>
<div class="wrapper">
<div class="logo">Elvis</div>
<div class="menu">Hamburger Menu</div>
</div>
</header>
First: the reason you use a 33px font which adds padding, then you use a height:100px on the menu while on your header you put a height:60px
you also need to add align-self: center on your flex-box
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
display: flex;
justify-content: space-between;
align-items: center;
align-self: center;
border: 2px solid red;
}
.logo {
font-size: 17px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
color: white;
}
.menu {
height: 60px;
width: 100px;
background-color: #bd4439;
}
I did it like 'Ali Memar' answer but the difference is the position of the texts. they are now in the middle of the div.
*{
padding:0px;
margin:0px;
}
header{
height: 60px;
align-items: center;
border: 2px solid red;
position: relative;
}
.wrapper{
display: flex;
justify-content: space-between;
width: 100%;
height: 100%;
position: absolute;
}
.logo {
font-size: 30px;
text-decoration: none;
padding: 20px 30px;
background-color: green;
max-height: 100%;
color: white;
text-align: center;
display: flex;
align-items: center
}
.menu {
height: 100%;
width: 100px;
background-color: #bd4439;
text-align: center;
display: flex;
align-items: center
}
<header>
<div class="wrapper">
<div class="logo">Elvis</div>
<div class="menu">Hamburger Menu</div>
</div>
</header>
I'm trying to give style to this div, however, no CSS works on besides height and width. I am using bootstrap.
Although the border is red in this code editor, it is not when I'm editing in Brackets. Added some more code.
body{
padding: 100px 10% 0 10%;
}
#hero {
display: flex;
flex-direction: row;
width: 100%;
height: 750px;
justify-content: space-around;
}
.card {
height: 100%;
width: 25%;
display: flex;
flex-direction: row;
align-items: center;
border: 1px solid red;
border-radius: 7px;
}
.card-title-span {
height: 50px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
font-size: 28px;
font-weight: bold;
border-bottom: 1px solid grey;
}
.card-image {
width:100%;
height: 250px;
background-color: dodgerblue;
}
.card-pricing-span {
height: 50px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
font-size: 24px;
border-bottom: 1px solid grey;
}
.card-description-span {
padding: 3%;
font-style: italic;
}
<div id="hero">
<div class="card">
<span class="card-title-span">Beginner</span>
<img src="" alt="Placeholder image" class="card-image">
<span class="card-pricing-span">pricing</span>
<span class="card-description-span"></span>
</div>
<div class="card"></div>
<div class="card"></div>
</div>
[IGNORE THIS TEXT - Need to put it here so there's enough words to meet the auto-mods standards.]
Because .card is a standard BootStrap class. Use another class name or overwrite it.
First of all you should pay attention to the overrides that bootstrap or any template may apply to your DIV.
To do so, press F12 on your browser and go to Elements tab, select your div, watch for Style and you should be able to see if your style gets striked.
If that isn't your case, more likely you are using a bunch of CSS properties that conflict with display:flex and other properties.
HTML:
<div id="wrap">
<div id="block1"></div>
<div id="block2"></div>
</div>
CSS:
div#wrap{
margin-top: 3em;
border: solid 1px black;
text-align: center;
}
div#wrap *{
display: inline-block;
width: 12.5em;
margin-top: 1em;
height: 8em;
}
div#wrap *:not(:last-child){
margin-right: 8em;
}
#block1{
background: orange;
}
div#wrap #block2{
background: magenta;
}
These 2 blocks are supposed to be centered in responsive design mode. When the screen is wide enough to have 2 blocks in a row, the code works. But when I narrow the screen down, the top block is shifted to the left because of the margin:
fiddle
Is it possible to fix this without media queries?
Edit
I tried flex-box:
div#wrap{
margin-top: 3em;
border: solid 1px black;
display: flex;
flex-flow: row wrap;
justify-content: center;
}
fiddle2
A solution is to use flex and justify-content:space-around and remove margin:
div#wrap {
margin-top: 3em;
border: solid 1px black;
justify-content:space-around;
display: flex;
flex-wrap: wrap;
}
div#wrap * {
display: inline-block;
width: 12.5em;
margin-top: 1em;
height: 8em;
}
#block1 {
background: orange;
}
#block2 {
background: magenta;
}
<div id="wrap">
<div id="block1"></div>
<div id="block2"></div>
</div>
If you use a container with negative margin, you don't need to vary the margin for the endpoints of the rows at different breakpoints and you can just go with inline-block. I set font-size to zero in the container so I can calculate my widths using percents without worrying about white space.
div#wrap {
margin-top: 3em;
border: solid 1px black;
padding: 20px;
text-align: center;
}
.block {
display: inline-block;
width: 12.5em;
margin: 20px;
height: 8em;
font-size: 16px;
}
.block-container {
margin: -20px;
font-size: 0;
}
#block1 {
background: orange;
}
#block2 {
background: magenta;
}
<div id="wrap">
<div class="block-container">
<div class="block" id="block1"></div>
<div class="block" id="block2"></div>
</div>
</div>
I want "About This Page" and "Around the web" to be horizontally aligned.
Also, open to any suggestions for improving this snippet of code. I just want to have a responsive / simple two column layout behind a wide colored background.
.footer-above {
width: 100%;
height: 200px;
background-color: #aaa;
padding-top: 30px;
padding-bottom: 30px;
color: white;
font-size: 20px;
}
.footer-links,
.built-with {
display: inline-block;
max-width: 40%;
height: 100%;
border: 2px solid black;
}
.container {
margin: 0 auto;
width: 500px;
height: 100%;
}
<div class="footer-above">
<div class="container">
<div class="built-with">
<h3>ABOUT THIS PAGE</h3>
<p> Made with HTML5Boilerplate</p>
</div><!--built-with-->
<div class="footer-links">
<h3>AROUND THE WEB</h3>
</div><!--footer-links-->
</div><!--container-->
</div><!-- footer-above -->
I would recommend using flexbox for this, which can be achieved by simply adding:
.container {
display: flex;
align-items: center;
justify-content: center;
}
If you want to have both boxes occupy the same height, you'll need a fixed height on .footer-links and .built-with. I've gone with 150px in the following example:
.footer-above {
width: 100%;
height: 200px;
background-color: #aaa;
padding-top: 30px;
padding-bottom: 30px;
color: white;
font-size: 20px;
}
.footer-links,
.built-with {
display: inline-block;
max-width: 40%;
height: 150px;
border: 2px solid black;
}
.container {
display: flex;
align-items: center;
justify-content: center;
}
<div class="footer-above">
<div class="container">
<div class="built-with">
<h3>ABOUT THIS PAGE</h3>
<p> Made with HTML5Boilerplate</p>
</div><!--built-with-->
<div class="footer-links">
<h3>AROUND THE WEB</h3>
</div><!--footer-links-->
</div><!--container-->
</div><!-- footer-above -->
Flexbox has support in every browser apart from Internet Explorer (though it's coming to IE as well). If you'd like to support Internet Explorer as well, you can use vertical-align: middle along with display: inline-block, as is demonstrated in this answer.
Hope this helps! :)
Simply use flex. Read about it here
.footer-above {
width: 100%;
height: 200px;
background-color: #aaa;
padding-top: 30px;
padding-bottom: 30px;
color: white;
font-size: 20px;
}
.footer-links,
.built-with {
display: inline-block;
max-width: 40%;
height: 100%;
border: 2px solid black;
}
.container {
margin: 0 auto;
width: 500px;
height: 100%;
display: inline-flex;
}
<div class="footer-above">
<div class="container">
<div class="built-with">
<h3>ABOUT THIS PAGE</h3>
<p> Made with HTML5Boilerplate</p>
</div><!--built-with-->
<div class="footer-links">
<h3>AROUND THE WEB</h3>
</div><!--footer-links-->
</div><!--container-->
</div><!-- footer-above -->
Use table-cell as display property
.footer-links,
.built-with {
display: table-cell;
max-width: 40%;
height: 100%;
border: 2px solid black;
}
I think , it'll horizontally aligned these boxes
this is based off of obsidian ages answer since it wasn't updated to match my exact question.
I edited .footer-above to 1400px since width:100% with code doesn't scale as viewport width changes.
Also, it should be align-items: flex-start; on container class, since i want a baseline at the top of parent div
.footer-above {
width: 1400px;
height: 200px;
background-color: #aaa;
padding-top: 30px;
padding-bottom: 30px;
color: white;
font-size: 20px;
}
.footer-links,
.built-with {
display: inline-block;
width: 40%;
height: 150px;
border: 2px solid black;
}
.container {
margin: 0 auto;
width: 960px;
display: flex;
align-items: flex-start;
justify-content: center;
}
<div class="footer-above">
<div class="container">
<div class="built-with">
<h3>ABOUT THIS PAGE</h3>
<p> Made with HTML5Boilerplate</p>
</div><!--built-with-->
<div class="footer-links">
<h3>AROUND THE WEB</h3>
</div><!--footer-links-->
</div><!--container-->
</div><!-- footer-above -->
I'm trying to make a board-game adoption for web.
I want to use Bootstrap to make the elements responsive.
The main element of the game is a rectangle (the game board). This shall appear centered in all display-sizes and with a bit of margin to all sides.
Which attributes and CSS-rules do I have to apply?
Shall I use a normal container or container-fluid?
Would it be enough to make one column within the container / row and give it a class of "col-xs-12"?
As far as I know this would be applied to all devices beginning from the smallest to the largest upwards.
What I have tried so far:
html, body {
height: 100%;
}
.container {
margin: 10px auto;
height: 100%;
display: flex;
justify-content: center;
background-color: #ababab;
}
.row {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.board {
margin: 10px;
height: 400px;
width: calc(100% - 40px);
background-color: teal;
border: 1px solid black;
font-size: 2.5em;
font-weight: bold;
color: white;
text-align: center;
line-height: 400px;
}
<div class="container">
<div class="row">
<div class="col-xs-12 board">The game-board</div>
</div>
</div>
Here you can see an example : you dont need col-xs-12 . You have only to set contaier-fluid in the parent and some padding in div wrapper with box-sizing:border-box . Row is for reset the standard padding of container and col classes with negative margin. It's your decision if you want to .
html,body{height:100%;margin:0;padding:0;}
.container-fluid,.row{height:100%;background-color:grey}
.board-container{padding:40px;box-sizing:border-box}
.board{background-color:teal;height:100%;padding:40px;}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<div class="container-fluid">
<div class="row board-container">
<div class="board">Board Game</div>
</div>
</div>
html, body {
height: 100%;
}
.container {
margin: 10px auto;
height: 100%;
display: flex;
justify-content: center;
background-color: #ababab;
}
.row {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.board {
margin: 10px;
height: calc(100% - 40px);;
width: calc(100% - 40px);
background-color: teal;
border: 1px solid black;
font-size: 2.5em;
font-weight: bold;
color: white;
text-align: center;
}
<div class="container">
<div class="row">
<div class="col-xs-12 board">The game-board</div>
</div>
</div>
html,body{
height:100%;
background:#FF3366;
}
.container {
width:100%;
height:100%;
}
.row{
height:100%;
margin:30px 30px 30px 30px;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
background-color: teal;
border: 1px solid black;
font-size: 2.5em;
font-weight: bold;
color: white;
}
<div class="container">
<div class="row">
<div class="col-xs-12">The game-board</div>
</div>
</div>
Try this one.