It's my first time here. A friend suggested this as a good source of knowledge, and I am in dire need of that!
I am trying to write HTML and CSS using a viewport, media queries and a fluid grid to create a page which scales from desktop (desktop example ) to mobile (mobile example).
I have tried to cobble it together with help from the w3c site, but someone who wasn't particularly helpful said I had bootstrap in there (I didn't intend to use that, and want it taken out but I have no idea what it is).
What I've managed to achieve is to have the mobile size work, but for some reason the colour area of the boxes that should be 50% and 25% of the width of the rows in desktop don't stretch as they should.
What did I do wrong? Help would be most, most appreciated!
This is the html I had written:
And this is the CSS: #charset "utf-8";
/* CSS Document */
* {
box-sizing: border-box;
}
.row::after {
content: "";
clear: both;
display: table;
}
[class*="col-"] {
float: left;
padding: 8px;
border: thin rgba(0, 0, 0, 1.00);
}
.col-1 {
width: 8.33%;
}
.col-2 {
width: 16.66%;
}
.col-3 {
width: 25%;
}
.col-4 {
width: 33.33%;
}
.col-5 {
width: 41.66%;
}
.col-6 {
width: 50%;
}
.col-7 {
width: 58.33%;
}
.col-8 {
width: 66.66%;
}
.col-9 {
width: 75%;
}
.col-10 {
width: 83.33%;
}
.col-11 {
width: 91.66%;
}
.col-12 {
width: 100%;
}
html {
font-family: Constantia, "Lucida Bright", "DejaVu Serif", Georgia, "serif";
}
.navspot {
width: 100%;
max-width: 100%;
padding: 10px;
height: auto;
vertical-align: middle;
background: #d9cf91;
text-align: right;
}
.maincontent {
/*properties for cell*/
/*Nothing is required here
because we can use default styles*/
background: #30332c;
color: #FFFFFF;
padding: 10px;
}
.box1 {
background-color: #dfcf91;
color: #FFFFFF;
padding: 10px;
text-align: left;
float: left;
}
.box2 {
background-color: rgba(107, 35, 36, 1.00);
color: #FFFFFF;
text-align: center;
padding: 10px;
float: left;
}
.box3 {
background-color: #8D9981;
text-align: center;
padding: 10px;
float: left;
}
.box4 {
background-color: #606956;
text-align: center;
padding: 10px;
float: left;
}
.box5 {
background-color: #8D9981;
text-align: center;
padding: 10px;
float: left;
}
.box6 {
background-color: #606956;
text-align: center;
padding: 10px;
float: left;
}
.footer {
/*properties for cell*/
/*Nothing is required here because we can use default styles*/
/*background: #d9cf91;*/
padding: 10px;
}
/*for phone*/
#media only screen and (max-width: 768px) {
[class*="col-"] {
width: 100%;
}
}
<div class="row">
<div class="col">
<div class="navspot">Navigation Spot
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="maincontent">Main Content Spot
</div>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="box1">Box 1
</div>
</div>
<div class="col-6">
<div class="box2">Box 2
</div>
</div>
</div>
<div class="row">
<div class="col-3">
<div class="box3">Box 3
</div>
</div>
<div class="col-3">
<div class="box4">Box 4
</div>
</div>
<div class="col-3">
<div class="box5">Box 5
</div>
</div>
<div class="col-3">
<div class="box6">Box 6
</div>
</div>
<div class="row">
<div class="col">
<div class="footer">Footer Area
</div>
</div>
</div>
Sorry if I'm being a dunce. Really struggling with the concepts of CSS and fluidity. Thanks in advance!
Fix 1: Colored content boxes not stretching
Do as ovokuro said, and remove float:left; line from ALL of your ".box" classes.
Fix 2: Column behavior and wrapping in your grid
Some div elements in your grid are not inheriting the border-box model, as you expected. To fix this for all browsers add:
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
For more about this see Box Sizing > Universal Box Sizing with Inheritance
Fix 3: Guides
While you are learning add some guides to your grid so you can more easily see what you are doing, and how things relate. For example:
/* temporary orange column guides */
[class*="col-"] {
border: 1px dotted orange;
}
Related
I'm not sure how I can explain this the best.
but I'm working on a layout right now, and I've created an okay code.
However when I test the website the 3 DIV's I did create show up different in the mobile layout then in the PC layout.
Well here is the mobile code
.container1 {
margin: 5px auto;
width: 98%;
text-align:center;
}
.container1 > div {
width: 100%;
}
.col1 {
float: auto;
}
.col2 {
float: auto;
}
.col3 {
margin: auto;
}
div.panel {
width: 100%;
}
And this is the PC CODE
.container1 {
margin: 5px auto;
width: 98%;
text-align:center;
}
.container1 > div {
width: 33%;
}
.col1 {
float: left;
}
.col2 {
float: right;
}
.col3 {
margin: auto;
}
div.panel {
width: 100%;
}
HTML IS:
<div class="container1">
<div class="col1"></div>
<div class="col2"></div>
<div class="col3"></div>
</div>
However, the mobile version shows them as
Col1 - Col3 - Col2
So I'm not sure how to fix this that mobile also shows it as col1,col2,col3.
If I move the div, it doesn't show good in the PC version.
I hope someone can help out.
So, it are 3 div's horizontally aligned.
There is no float: auto. Simply make them all float: left in the mobile version. Then they will appear in the order they are in the HTML code.
Try this use display: flex; property to the parent element, and we have an property called order give to the child element with this we can achieve easily your requirement.
working codepen
Try this for the mobile:
.container1 {
margin: 5px auto;
width: 98%;
text-align:center;
}
.container1 > div {
width: 100%;
}
.col1 {
float: left;
}
.col2 {
float: left;
}
.col3 {
margin: auto;
}
div.panel {
width: 100%;
}
float have no auto property
in the end this is my CSS and HTML which is working
.card {
box-shadow: 0px 4px 8px 0px rgba(0,255,24,0.3);
transition: 0.3s;
float: left;
border-radius: 5px;
background-color:#111;
width: 97%;
}
.container1 {
padding-top: 5px;
margin: 0px auto;
width: 98%;
text-align:center;
}
.container1 > div {
width: 33%;
}
.col1 {
float: left;
}
.col2 {
float: right;
}
.col3 {
margin: auto;
}
div.panel {
width: 100%;
}
Also HTML:
<div class="container1">
<div class="col1 ">
<div class="card " id="left">
</div></div>
<div class="col3 ">
<div class="card " id="center">
</div></div>
<div class="col2 ">
<div class="card " id="right">
</div></div>
</div>
I've tried to add a display:table to a parent element (rowcontainer) and display:table-cell; to the child element (div1, div2) for over 500px width on screen. This worked, but the padding on the child elements now have to left or right padding, and the bottom row has something off with it, any ideas on how to fix this? :
Here is also a codepen of the problem:
http://codepen.io/anon/pen/MYxegN
#media screen and (max-width: 499px) {
.div1,
.div2 {
color: blue;
}
}
#pagewrap {
padding: 10px;
background-color: rgba(21, 21, 21, 0.75);
border: 1px red solid;
max-width: 1024px;
min-height: 87.5% height: inherit;
margin: 0 auto;
}
h1,
h2 {
color: white;
}
p {
color: silver;
}
#tablecontainer {
padding: 5px 5px 5px 0px;
width: 100%;
min-height: 100%;
color: white;
font-size: 20px;
}
.div1,
.div2 {
padding: 5px;
margin: 5px;
}
.div1 {
background-color: darkgreen;
padding: 5px;
}
.div2 {
background-color: green;
}
#media screen and (min-width: 500px) {
.rowcontainer {
padding: 2.5px;
display: table;
width: 100%;
}
.rowcontainer div {
display: table-cell;
}
.div1 {
padding: 1.25px;
}
}
<div id="pagewrap">
<div id="tablecontainer">
<div class="rowcontainer">
<div class="div1">1</div>
<div class="div2">2</div>
</div>
<div class="rowcontainer">
<div class="div1">3</div>
<div class="div2">4</div>
</div>
<div class="rowcontainer">
<div class="div1">5</div>
<div class="div2">6</div>
</div>
<div class="rowcontainer">
<div class="div1">7</div>
<div class="div2">9</div>
</div>
<div class="rowcontainer">
<div class="div1">9</div>
<div class="div2">10</div>
</div>
</div>
I think the correct structure is like this for CSS table.
#tablecontainer {
display:table; /*behaves like <table>*/
}
.rowcontainer {
display:table-row; /*behaves like <tr>*/
}
.rowcontainer div {
display:table-cell; /*behaves like <td>*/
}
As far as I can tell, all that is needed is to add
table-layout:fixed
in the media query
#media screen and (min-width: 500px){
.rowcontainer {
padding:2.5px;
display:table;
table-layout: fixed;
border-spacing: 5px 0;
border-collapse: separate;
width:100%;
}
}
Codepen Updated.
#media screen and (max-width: 499px) {
.div1,
.div2 {
color: blue;
}
}
html {
background: url(img/nature.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
#pagewrap {
padding: 10px;
background-color: rgba(21, 21, 21, 0.75);
border: 1px red solid;
max-width: 1024px;
min-height: 87.5% height: inherit;
margin: 0 auto;
}
h1,
h2 {
color: white;
}
p {
color: silver;
}
#tablecontainer {
padding: 5px 5px 5px 0px;
width: 100%;
min-height: 100%;
color: white;
font-size: 20px;
}
.div1,
.div2 {
padding: 5px;
margin: 5px;
}
.div1 {
background-color: darkgreen;
padding: 5px;
}
.div2 {
background-color: green;
}
#media screen and (min-width: 500px) {
.rowcontainer {
padding: 2.5px;
display: table;
table-layout: fixed;
width: 100%;
}
.rowcontainer div {
display: table-cell;
}
.div1 {
padding: 1.25px;
}
}
<div id="pagewrap">
<div id="tablecontainer">
<div class="rowcontainer">
<div class="div1">1</div>
<div class="div2">2</div>
</div>
<div class="rowcontainer">
<div class="div1">3</div>
<div class="div2">4</div>
</div>
<div class="rowcontainer">
<div class="div1">5</div>
<div class="div2">6</div>
</div>
<div class="rowcontainer">
<div class="div1">7</div>
<div class="div2">9</div>
</div>
<div class="rowcontainer">
<div class="div1">9</div>
<div class="div2">10</div>
</div>
</div>
Are you looking for something like this?
#media screen and (max-width: 499px) {
.div1,
.div2 {
color: blue;
}
}
#pagewrap {
padding: 10px;
background-color: rgba(21, 21, 21, 0.75);
border: 1px red solid;
max-width: 1024px;
min-height: 87.5% height: inherit;
margin: 0 auto;
}
h1,
h2 {
color: white;
}
p {
color: silver;
}
#tablecontainer {
padding: 5px 5px 5px 0px;
width: 100%;
min-height: 100%;
color: white;
font-size: 20px;
display: table;
border-spacing: 5px;
}
.div1,
.div2 {
padding: 5px;
margin: 5px;
}
.div1 {
background-color: darkgreen;
padding: 5px;
}
.div2 {
background-color: green;
}
#media screen and (min-width: 500px) {
.rowcontainer {
padding: 2.5px;
display: table-row;
}
.rowcontainer div {
display: table-cell;
}
.div1 {
padding: 1.25px;
}
}
<div id="pagewrap">
<div id="tablecontainer">
<div class="rowcontainer">
<div class="div1">1</div>
<div class="div2">2</div>
</div>
<div class="rowcontainer">
<div class="div1">3</div>
<div class="div2">4</div>
</div>
<div class="rowcontainer">
<div class="div1">5</div>
<div class="div2">6</div>
</div>
<div class="rowcontainer">
<div class="div1">7</div>
<div class="div2">9</div>
</div>
<div class="rowcontainer">
<div class="div1">9</div>
<div class="div2">10</div>
</div>
</div>
You are almost correct in making a table with div. The above problem is because of the text. (Last cell has 2 digit text, if you make it one digit it works fine ). To fix this issue use below code. In the rowcontainer just add width.
.rowcontainer div{
display:table-cell;
width:50%
}
Your complete code can be seen on http://codepen.io/gauravshankar/pen/OPqXga
I'm trying to create a css grid system with six columns. You can see the output of the html and css at this jsfiddle. The problem appears to be that the columns are overflowing the rows they are supposed to be contained in. For example, even though a row should be able to hold six 'size 1' columns, the sixth column in my example is spilling over into another row. How would I fix this so that the desired number of columns for each row doesn't exceed the width of the row?
<div class="grid-container outline">
<div class="row">
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
<div class="col-1"><p>col-1</p></div>
</div>
<div class="row">
<div class="col-2"><p>col-2</p></div>
<div class="col-2"><p>col-2</p></div>
<div class="col-2"><p>col-2</p></div>
</div>
<div class="row">
<div class="col-3"><p>col-3</p></div>
<div class="col-3"><p>col-3</p></div>
</div>
</div>
css
.grid-container{
width: 100%;
max-width: 1200px;
}
/*-- cleafix hack -- */
.row:before,
.row:after {
content:"";
display: table ;
clear:both;
}
[class*='col-'] {
float: left;
min-height: 1px;
width: 16.66%;
/*-- gutter -- */
padding: 12px;
background-color: #FFDCDC;
}
.col-1{ width: 16.66%; }
.col-2{ width: 33.33%; }
.col-3{ width: 50%; }
.col-4{ width: 66.66%; }
.col-5{ width: 83.33%; }
.col-6{ width: 100%; }
.outline, .outline *{
outline: 1px solid #F6A1A1;
}
/*-- some extra column content styling --*/
[class*='col-'] > p {
background-color: #FFC2C2;
padding: 0;
margin: 0;
text-align: center;
color: white;
}
I believe that you're looking for:
div {
box-sizing:border-box;
}
jsFiddle example
See: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing
I'm designing a basic, two-column fluid responsive layout that linearizes for mobile-- as depicted in my attached diagram-- using floated elements; and I'm having an issue with certain elements dropping, as can be viewed in the fiddle I've set up here. For some reason, "block 7" aligns with the top of "block 6" instead of flowing below "block 3" as desired.
I have two questions with respect to this layout: (1) how can I get the blocks to align as intended; and (2) is it possible, say using jQuery, to re-arrange the order of blocks for mobile-- for instance, at a certain breakpoint-- I'm using 678px in the fiddle-- I could have, say, "block 6" appear under "block 3"?
For the first question, I have been reading articles and other threads about using inline-block instead of float although I would prefer to not have to deal with the whitespace issue that seems to occur. If this is the only viable route, however, can it be implemented in a way that minimizes these sorts of quirks? (I've always used floats for layout...)
Thanks very much for any feedback here.
Please take a look at this, I have modified your CSS a little bit:
/*-------------------- clearfix --------------------- */
.cf:before,
.cf:after {
content: "";
display: table;
}
.cf:after {
clear: both;
}
.cf {
*zoom: 1;
}
/*-------------------- main --------------------- */
.container {
max-width: 960px;
padding: 2%;
}
.block {
font-family: sans-serif;
color: #fff;
background-color: #7f7f7f;
font-weight: bold;
text-align: center;
margin-bottom: 20px;
}
.one {
float: left;
width: 30%;
height: 150px;
margin-right: 2%;
}
.two {
float: right;
width: 67%;
height: 250px;
}
.three {
float: left;
clear: left;
width: 30%;
height: 150px;
margin-right: 2%;
}
.four {
float: right;
width: 67%;
height: 250px;
}
.seven {
float: right;
width: 67%;
height: 250px;
}
.six {
float: right;
width: 67%;
height: 200px;
}
.five {
float: left;
clear: left;
width: 30%;
height: 450px;
margin-right: 2%;
}
.eight {
float: left;
width: 30%;
height: 200px;
margin-right: 2%;
}
/* 678 breakpoint ----------- */
#media only screen and (max-width: 678px) {
.block {
width: 100% !important;
float: none !important;
}
}
<div class="container cf">
<div class="block one">1</div>
<div class="block two">2</div>
<div class="block three">3</div>
<div class="block four">4</div>
<div class="block five">5</div>
<div class="block six">6</div>
<div class="block seven">7</div>
<div class="block eight">8</div>
</div>
First of all, in your original fiddle, the styles which had to be assigned to .five div, i.e. float: left; width: 30%; height: 150px; margin-right: 2%;, were assigned to .seven div and the styles which had to be assigned to .seven div, i.e. float: right; width: 67%; height: 250px;, were assigned to .five div. Moreover, I added clear: left; to .five div and increased its height.
Secondly, as far changing the order of boxes is concerned at a certain break-point, you can achieve that using CSS only by adding another div of .six after .three div and hiding it on desktop and showing it only at the break-point, here's an example (view the code snippet in full page and then resize your browser):
/*-------------------- clearfix --------------------- */
.cf:before,
.cf:after {
content: "";
display: table;
}
.cf:after {
clear: both;
}
.cf {
*zoom: 1;
}
/*-------------------- main --------------------- */
.container {
max-width: 960px;
padding: 2%;
}
.block {
font-family: sans-serif;
color: #fff;
background-color: #7f7f7f;
font-weight: bold;
text-align: center;
margin-bottom: 20px;
}
.one {
float: left;
width: 30%;
height: 150px;
margin-right: 2%;
}
.two {
float: right;
width: 67%;
height: 250px;
}
.three {
float: left;
clear: left;
width: 30%;
height: 150px;
margin-right: 2%;
}
.four {
float: right;
width: 67%;
height: 250px;
}
.seven {
float: right;
width: 67%;
height: 250px;
}
.six {
float: right;
width: 67%;
height: 200px;
}
.five {
float: left;
clear: left;
width: 30%;
height: 450px;
margin-right: 2%;
}
.eight {
float: left;
width: 30%;
height: 200px;
margin-right: 2%;
}
.show {
display: none;
}
/* 678 breakpoint ----------- */
#media only screen and (max-width: 678px) {
.block {
width: 100% !important;
float: none !important;
}
.hide {
display: none;
}
.show {
display: block;
}
}
<div class="container cf">
<div class="block one">1</div>
<div class="block two">2</div>
<div class="block three">3</div>
<div class="block six show">6</div>
<div class="block four">4</div>
<div class="block five">5</div>
<div class="block six hide">6</div>
<div class="block seven">7</div>
<div class="block eight">8</div>
</div>
As you can see, there are two instances of the .six div in the HTML structure above. First is <div class="block six show">6</div> which is after the .three div and the other is <div class="block six hide">6</div>before the .seven div. For the desktop view, I am hiding the first instance of .six div by setting display: none on .show and inside the media-query, I am hiding the second instance of .six div by setting display: none on .hude and showing the first instance of .six div by setting display: block on .hide.
You can do it but you have to be careful that your right boxes don't exceed the height of the left or it won't tuck under and you may have other alignment problems. Put your content in small viewport order, or the order that it is numbered.
I would suggest, if you want to only have a css way of doing this, using flexbox. It's not fully supported in legacy browsers, but you can have a fallback if you use modernizr. Google "flexbox css" w/o the quotes and there's lots of tutorials.
Edit: I just noticed that 7 is not in the even odd order. I'll leave this for now but will probably delete it tomorrow.
DEMO:http://jsfiddle.net/aut5haxv/1/
CSS
.clearfix:before,
.clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both
}
.container {
max-width: 960px;
padding: 2%;
box-sizing:border-box;
border:1px solid blue;
}
.container .column:nth-child(odd) {
width: 30%;
float: left;
}
.container .column:nth-child(even) {
width: 68%;
float: right;
}
.column {
font-family: sans-serif;
color: red;
font-weight: bold;
text-align: center;
padding: 15px;
box-sizing: border-box;
border: 1px solid red;
margin-bottom:2%;
}
.one {
height: 150px
}
.two {
height: 250px
}
.three {
height: 250px;
clear:left;
}
.four {
height: 450px
}
.five {
height: 450px;
}
.six {
height: 350px
}
.seven {
height: 250px
}
.eight {
height: 200px;
}
#media only screen and (max-width:678px) {
.container .column:nth-child(odd),
.container .column:nth-child(even) {
width: 100%;
float: none;
}
}
HTML:
<div class="container clearfix">
<div class="column one">1</div>
<div class="column two">2</div>
<div class="column three">3</div>
<div class="column four">4</div>
<div class="column five">5</div>
<div class="column six">6</div>
<div class="column seven">7</div>
<div class="column eight">8</div>
</div>
How to stretch parent div to fit children div?
I tried to add element with clear: both; after content but it didn't work for me.
HTML:
<div id="wrapper">
<div class="left-menu">
</div>
<div class="right-bar">
<div class="right-content">
<div class="content">
<div class="content-wrapper">
<div class="content-body">
Here is content
</div
</div>
</div>
</div>
</div>
</div>
CSS:
.left-menu {
background-color: #0B0C0E;
width: 20%;
height: 100%;
float: left;
}
.right-bar {
background-color: #F0F0F0;
height: 100%;
width: 100%;
}
.right-content {
float: left;
width: 80%;
}
.right-content > .content {
padding: 21px 0 0 42px;
}
.right-content > .content > .content-wrapper {
width: 98%;
height: 70%;
}
.right-content > .content .content-body {
background-color: #FAFAFA;
padding: 20px;
font-size: 24px;
border: 1px solid #D0D0D0;
}
sandbox for test: http://roonce.com/en/room/SwZuEJYB
Thanks in advance.
Use "clear-fix" technique. http://css-tricks.com/snippets/css/clear-fix/
This will allow the parent div to be the appropriate size of the floated elements within. Note this works specifically on #wrapper. (http://jsbin.com/huqehuta/1/edit)
.clear-fix:after {
content: "";
display: table;
clear: both;
}