Fixing the view of display: table property - html

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

Related

Getting an equal 15px border with a grid layout

I'm trying to make a grid layout that's responsive with a 15px coloured border, it works ok but it when there's multiple grids, it doubles up the border i.e 30px where it joins.
https://jsfiddle.net/exm8xsgx/
html,
body {
height: 100%;
width: 100%;
}
.container {
height: 100%;
width: 100%;
}
.one {
height: 50vh;
width: 25%;
border-style: solid;
border-color: red;
border-width: 15px;
float: left;
}
<div class="container">
<div class="row">
<div class="one">one</div>
<div class="one">two</div>
<div class="one">three</div>
<div class="one">four</div>
</div>
</div>
This is another method I've tried. When browser width is restricted, the grids start to stack up and the border doubles up again, it should always be 15px whether they are next to each other or stacked.
https://jsfiddle.net/7bxtt82r/24/
html,
body {
height: 100%;
width: 100%;
}
.container {
height: 100%;
width: 100%;
}
.one:first-child {
height: 50vh;
width: 20%;
border-style: solid;
border-color: red;
border-left-width: 15px;
border-top-width: 15px;
border-bottom-width: 15px;
border-right-width: 15px;
float: left;
}
.one:not(:first-child) {
height: 50vh;
width: 20%;
border-style: solid;
border-color: red;
border-left-width: 15px;
border-top-width: 15px;
border-bottom-width: 15px;
border-right-width: 15px;
float: left;
margin-left: -15px;
}
<div class="container">
<div class="row">
<div class="one">one</div>
<div class="one">two</div>
<div class="one">three</div>
<div class="one">four</div>
</div>
</div>
I also don't know how many grids there will be so they will just continue to stack up.
You can use CSS table, and set border-spacing to 15px, example:
body {
margin: 0;
}
.row {
display: table;
table-layout: fixed;
border-collapse: separate;
border-spacing: 15px;
width: 100%;
background: red;
}
.one {
display: table-cell;
background: white;
}
<div class="row">
<div class="one">one</div>
<div class="one">two</div>
<div class="one">three</div>
<div class="one">four</div>
</div>
EDIT
If you need the items to wrap for different viewport width, you can use flexbox + box-shadow + media queries.
body {
margin: 0;
}
.row {
display: flex;
flex-wrap: wrap;
padding: 15px;
}
.one {
flex-basis: 25%;
box-shadow: 0 0 0 15px red;
background: white;
}
#media (max-width: 992px) {
.one {
flex-basis: 50%;
margin-bottom: 15px;
}
}
#media (max-width: 768px) {
.one {
flex-basis: 100%;
}
}
<div class="row">
<div class="one">one</div>
<div class="one">two</div>
<div class="one">three</div>
<div class="one">four</div>
</div>
Your external borders can stay on 15px but the border which touches another one has to be devided by 2 if you want a clean look with a same border around.
I used a default border of 10px and 20px for those not touching each other. (You can do the opposite: default 20px and 10px for those touching)
I dealt with this while building my simon game here
.squareCircled {
height: 250px;
width: 250px;
border: 10px solid gray;
}
.green {
background-color:#01B600; /*green*/
border-top-left-radius: 100%;
border-top:20px solid gray;
border-left:20px solid gray;
cursor: pointer;
}

Placing two elements inline

I am attempting to place an iframe and img inline in a div. The code that I have posted puts the img below the iframe. I have tried to position and float the elements but nothing seems to work. I have also checked out other posts on SO, but nothing seems to work. I am willing to start from scratch if required.
I would be grateful if someone could point out my error and show me the corrections to make to get this to work. I have looked at other posts but nothing seems to be working.
Thank you.
#media (min-width: 1200px) {
.container {
max-width: 1080px;
}
}
.hero-unit {
background-color: #000000;
border: 1px solid #252525;
margin-bottom: 30px;
}
.hero-unit img {
display: inline-block;
}
.fp-block {
padding: 5px !important;
padding: 0;
}
/*** CUSTOM CODE FOR YOUTUBE VIDEO DISPLAY ***/
.inner {
float: left;
width: 49%;
margin: 0;
}
.holder {
height: 0px;
width: 100%;
}
<div class="container">
<div class="hero-unit fp-block">
<div id="ut-wrap">
<div class="inner">
<div class="holder">
<iframe src="https://www.youtube.com/embed/_VRXrp_AfMM" frameborder="0" allowfullscreen></iframe>
<img src="http://placehold.it/150x150">
</div>
</div>
</div>
</div>
</div>
The problem
The issue is that .inner has a width of 49% which is pushing the image onto a new line. This can be seen if you add a background color and height to .inner.
#media (min-width: 1200px) {
.container {
max-width: 1080px;
}
}
.hero-unit {
background-color: #000000;
border: 1px solid #252525;
margin-bottom: 30px;
}
.hero-unit img {
display: inline-block;
}
.fp-block {
padding: 5px !important;
padding: 0;
}
/*** CUSTOM CODE FOR YOUTUBE VIDEO DISPLAY ***/
.inner {
float: left;
width: 49%;
margin: 0;
background-color: red;
height: 100px;
}
.holder {
height: 0px;
width: 100%;
}
iframe {
opacity: 0.5;
}
<div class="container">
<div class="hero-unit fp-block">
<div id="ut-wrap">
<div class="inner">
<div class="holder">
<iframe src="https://www.youtube.com/embed/_VRXrp_AfMM" frameborder="0" allowfullscreen></iframe>
<img src="http://placehold.it/150x150">
</div>
</div>
</div>
</div>
</div>
How to fix
Option 1
Add whitespace: nowrap; to .holder to stop the image from being able to wrap onto the next line
#media (min-width: 1200px) {
.container {
max-width: 1080px;
}
}
.hero-unit {
background-color: #000000;
border: 1px solid #252525;
margin-bottom: 30px;
}
.hero-unit img {
display: inline-block;
}
.fp-block {
padding: 5px !important;
padding: 0;
}
/*** CUSTOM CODE FOR YOUTUBE VIDEO DISPLAY ***/
.inner {
float: left;
width: 49%;
margin: 0;
}
.holder {
height: 0px;
width: 100%;
white-space: nowrap;
}
<div class="container">
<div class="hero-unit fp-block">
<div id="ut-wrap">
<div class="inner">
<div class="holder">
<iframe src="https://www.youtube.com/embed/_VRXrp_AfMM" frameborder="0" allowfullscreen></iframe>
<img src="http://placehold.it/150x150">
</div>
</div>
</div>
</div>
</div>
Option 2
Set a larger width on .inner.
#media (min-width: 1200px) {
.container {
max-width: 1080px;
}
}
.hero-unit {
background-color: #000000;
border: 1px solid #252525;
margin-bottom: 30px;
}
.hero-unit img {
display: inline-block;
}
.fp-block {
padding: 5px !important;
padding: 0;
}
/*** CUSTOM CODE FOR YOUTUBE VIDEO DISPLAY ***/
.inner {
float: left;
width: 100%;
margin: 0;
}
.holder {
height: 0px;
width: 100%;
}
<div class="container">
<div class="hero-unit fp-block">
<div id="ut-wrap">
<div class="inner">
<div class="holder">
<iframe src="https://www.youtube.com/embed/_VRXrp_AfMM" frameborder="0" allowfullscreen></iframe>
<img src="http://placehold.it/150x150">
</div>
</div>
</div>
</div>
</div>

Parent block doesn't shrink after children wrap

What it looks like:
When I decrease the browser's window size I get:
BUT I WANT IT TO LOOK LIKE THIS WITHOUT USING FLEXBOX:
Here's codepen
CODE:
#one {
background-color: grey;
text-align: center;
}
#two {
display: inline-block;
text-align: left;
padding: 10px;
background-color: lightgrey;
}
.square {
display: inline-block;
width: 300px;
padding-bottom: 300px;
background-color: black;
}
<div id="one">
<div id="two">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
</div>
Without using Flexbox, you need #media queries for that.
changed your values to make it responsive
*,
*::before,
*::after {
box-sizing: border-box
}
body {
margin: 0
}
#one {
background-color: grey;
max-width: 900px;
margin: 0 auto;
font-size: 0 /* inline-block gap fix */
}
#two {
padding: 10px;
background-color: lightgrey;
}
.square {
display: inline-block;
width: calc((100% / 3) - 10px);
padding-bottom: 300px;
background-color: black;
margin:5px
}
#media (max-width: 768px) {
.square {
width:calc((100% / 2) - 10px)
}
}
<div id="one">
<div id="two">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
</div>
Try this,
#two {
display: inline-block;
text-align: left;
padding: 10px;
background-color: lightgrey;
width:100%;
}
.square {
display: inline-block;
width: 32.5%;
padding-bottom: 300px;
background-color: black;
}
Here's the solution using Media queries codepen
* {
margin: 0px;
}
#one {
background-color: grey;
text-align: center;
font-size: 0;
}
#two {
display: inline-block;
text-align: left;
padding: 10px;
background-color: lightgrey;
}
.square {
display: inline-block;
width: 300px;
padding-bottom: 300px;
background-color: black;
}
#media screen and (max-width: 920px) {
#two {
width: 600px;
}
}
#media screen and (max-width: 600px) {
#two {
width: 300px;
}
}
<div id="one">
<div id="two">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
</div>

Different width divs in the same row

I'm trying to put 3 divs(with different widths respectively : 10%,70% & 20%) in the same row but the middle one always go full width of the page.
Here is my code:
#left-bar {
width: 10%;
background-color: #FF0000;
}
#middle-bar {
width: 70%;
background-color: #6600FF;
}
#right-bar {
width: 20%;
background-color: #99FF99;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
By default div is a block level element that's why they aren't in the same row.
You have a few options to fix this:
option with CSS flexbox:
.row {
display: flex;
width: 100%
}
.row>div {
/*demo purposes */
height: 30px;
}
#left-bar {
flex: 0 10%;
background-color: #F00;
}
#middle-bar {
flex: 1;
background-color: #60F;
}
#right-bar {
flex: 0 20%;
background-color: #9F9;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
(old options)
option with display:inline-block
.row {
/*fix inline-block gap*/
font-size: 0;
}
.row>div {
display: inline-block;
/*demo purposes */
height: 30px;
}
#left-bar {
width: 10%;
background-color: #F00;
}
#middle-bar {
width: 70%;
background-color: #60F;
}
#right-bar {
width: 20%;
background-color: #9F9;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
option with display:table-[cell]
.row {
display: table;
width: 100%
}
.row>div {
display: table-cell;
/*demo purposes */
height: 30px;
}
#left-bar {
width: 10%;
background-color: #F00;
}
#middle-bar {
width: 70%;
background-color: #60F;
}
#right-bar {
width: 20%;
background-color: #9F9;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
The table-cell option actually doesn't work in some internet explorer versions. But the same result can be achieved with the property float:
#left-bar{
width:10%;
background-color: #FF0000;
}
#middle-bar{
width:70%;
background-color: #6600FF;
}
#right-bar{
width:20%;
background-color: #99FF99;
}
.row > div {float:left;}
<div class="row">
<div id="left-bar">a</div>
<div id="middle-bar">b</div>
<div id="right-bar">c</div>
</div>
#left-bar{
width:10%;
background-color: #FF0000;
float:left;
}
#middle-bar{
width:70%;
background-color: #6600FF;
float:left;
}
#right-bar{
width:20%;
background-color: #99FF99;
float:left;
}
If that doesn't work, please provide more html and css because the problem will be somewhere else. Also, verify that you have heights set for your divs.

CSS background is less than div hight

Help me please, I can't understand result of my simply code:
<div id="wrapper-top">
<div class="wrapper">
<div id="logo">logo</div>
<div id="menu">menu</div>
<div id="content">
<div class="block-1-1">text</div>
<div class="block-3-1">text</div>
<div class="block-3-2">text</div>
<div class="block-3-3">text</div>
</div>
</div>
</div>
and css file:
#wrapper-top
{
width: 100%;
background-color: gray;
}
.wrapper
{
margin: 0 150px 0 150px;
}
#logo
{
width: 100%;
height: 50px;
}
#menu
{
width: 100%;
height: 50px;
background-color: navajowhite;
}
#content
{
width: 100%;
background-color: white;
}
.block-1-1
{
width: 100%;
text-align:center;
background-color: pink;
}
.block-3-1
{
float:left;
width:33%;
text-align:center;
background-color: violet;
}
.block-3-2
{
float:left;
width:34%;
text-align:center;
background-color: blueviolet;
}
.block-3-3
{
float:left;
width:33%;
text-align:center;
background-color: yellowgreen;
}
Why divs .block-3-1, .block-3-2 and .block-3-3 seem to be outside of div .wrapper.
I don't expected that because I want this blocks inside .wrapper.
http://jsfiddle.net/4yvLv853/1/
You need to contain the floated items in the #content div
One method (there are others as detailed here) is to use overflow:hidden
#content
{
width: 100%;
background-color: white;
overflow: hidden;
}
JSfiddle Demo
use clearfix
.clearfix {
*zoom: 1;
}
.clearfix:before,
.clearfix:after {
display: table;
line-height: 0;
content: "";
}
.clearfix:after {
clear: both;
}
#wrapper-top
{
width: 100%;
background-color: gray;
border: solid blue 1px;
}
.wrapper
{
margin: 0 150px 0 150px;
border: solid brown 1px;
}
#logo
{
width: 100%;
background-color: white;
}
#menu
{
width: 100%;
background-color: navajowhite;
}
#content
{
width: 100%;
background-color: white;
}
.block-1-1
{
width: 100%;
text-align:center;
background-color: pink;
}
.block-3-1
{
float:left;
width:33%;
text-align:center;
background-color: violet;
}
.block-3-2
{
float:left;
width:34%;
text-align:center;
background-color: blueviolet;
}
.block-3-3
{
float:left;
width:33%;
text-align:center;
background-color: yellowgreen;
}
<div id="wrapper-top">
<div class="wrapper clearfix">
<div id="logo">logo</div>
<div id="menu">menu</div>
<div id="content">
<div class="block-1-1">block-1-1</div>
<div class="block-3-1">block-3-1</div>
<div class="block-3-2">block-3-2</div>
<div class="block-3-3">block-3-3</div>
</div>
</div>
</div>
Try
<div id="wrapper-top">
<div class="wrapper" style="height: 400px"> //You can add this in CSS if you want.
<div id="logo">logo</div>
<div id="menu">menu</div>
<div id="content">
<div class="block-1-1">text</div>
<div class="block-3-1">text</div>
<div class="block-3-2">text</div>
<div class="block-3-3">text</div>
</div>
</div>
</div>
I think the wrapper height is too small.
Alternatively, if you want the .wrapper div to stay the height it is, try changing the #content to
#content {
overflow-y: scroll;
overflow-x: hidden; //this gets rid of the pesky bottom scrollbar
}