I'm experimenting with CSS properties of a two-level set of DOM elements to make it appear like it's a table. The topmost element is just one, it wraps its children, which, in turn, form a flat list of lookalike elements. Like this:
<div class="t">
<div class="c">First row</div>
<div class="c">2</div>
<div class="c">3</div>
<div class="c">4</div>
<div class="c">5</div>
<div class="c">Second row</div>
<div class="c">7</div>
<div class="c">8</div>
<div class="c">9</div>
<div class="c">0</div>
</div>
I'm trying to make this list form two rows, each containing 5 elements. So the CSS I'm using is like:
.t {
display: table;
width: 100%;
}
.c {
display: table-cell;
}
.c:nth-child(5n + 1):after {
content: '-';
display: table-row;
}
Which isn't working.
Is there a way to keep two levels of nesting and still have a list that appears as if it was a table?
If you can edit the HTML, just make the structure to like a complete table.
.t {
display: table;
width: 100%;
}
.r {
display: table-row;
}
.c {
display: table-cell;
}
<div class="t">
<div class="r">
<div class="c">First row</div>
<div class="c">2</div>
<div class="c">3</div>
<div class="c">4</div>
<div class="c">5</div>
</div>
<div class="r">
<div class="c">Second row</div>
<div class="c">7</div>
<div class="c">8</div>
<div class="c">9</div>
<div class="c">0</div>
</div>
</div>
Based on what/how you want them to behave, you can use float, (haven't tested for cross browser support on this one though) but they will not behave as a normal table.
Another option is to use flexbox (will still not behave as a normal table though)
.t {
display: table;
width: 100%;
}
.c {
display: table-cell;
float: left;
width: 20%;
}
<div class="t">
<div class="c">First row</div>
<div class="c">2</div>
<div class="c">3</div>
<div class="c">4</div>
<div class="c">5</div>
<div class="c">Second row</div>
<div class="c">7</div>
<div class="c">8</div>
<div class="c">9</div>
<div class="c">0</div>
</div>
To achieve your expected result, I have used position:relative option and display:table-cell
HTML:
<div class="t">
<div class="c">First row</div>
<div class="c">2</div>
<div class="c">3</div>
<div class="c">4</div>
<div class="c">5</div>
<div class="c">Second row</div>
<div class="c">7</div>
<div class="c">8</div>
<div class="c">9</div>
<div class="c">0</div>
</div>
CSS:
.t {
display: table;
width: 100%;
}
.c {
display: table-cell;
padding: 1%;
border: 1px solid black;
text-align: center;
width: 10%;
}
.c:nth-child(n+6) {
display: table-cell;
position: relative;
left: -50%;
top: 60px;
}
Codepen-http://codepen.io/nagasai/pen/mEOjLL
Related
I am unable to remove the spacing before the Top Left & Bottom Left cells and after the Right cell.
I would like the cells to use the full width of the screen with border spacing only in the middle.
JSFiddle link
#div-layout,
#div-layout-nested {
display: table;
width: 100%;
border-spacing: 5px;
}
#div-layout-nested {
margin-top: -5px;
margin-bottom: -5px;
}
.div-layout-row {
display: table-row;
}
.div-layout-cell {
display: table-cell;
width: 25%;
vertical-align: top;
border: 1px solid red;
background-color: #ffb2b2;
}
<div id="div-layout">
<div class="div-layout-row">
<div id="div-layout-nested">
<div class="div-layout-row">
<div class="div-layout-cell">Top Left</div>
<div class="div-layout-cell">Top Middle</div>
<div class="div-layout-cell">Top Right</div>
</div>
</div>
<div id="div-layout-nested">
<div class="div-layout-row">
<div class="div-layout-cell">Botom Left</div>
<div class="div-layout-cell">Bottom Middle</div>
<div class="div-layout-cell">Bottom Right</div>
</div>
</div>
<div class="div-layout-cell">Right</div>
</div>
</div>
You can get most of the way there, by adding:
body,
#div-layout,
.div-layout-row,
.div-layout-cell {
margin: 5px 0;
}
#div-layout,
.div-layout-row,
.div-layout-cell
#div-layout-nested {
border-spacing:0 0;
}
at the bottom of your styles.
I suspect you will find the table much easier to style if you rewrite it.
At present you have two .div-layout-rows, each nested within a #div-layout-nested, both of which themselves are nested within a .div-layout-row.
Three recommendations:
Reducing the nesting levels will make your table easier to style;
Consistently using .classes in favour of #ids - a good rule of thumb is never using #ids unless you absolutely have to - will also make your table easier to style;
Avoid giving different nesting levels the same .class - this will also make your table easier to style.
Here is your table again with the markup unchanged, but with the styles above added to the end of the stylesheet:
#div-layout,
#div-layout-nested {
display: table;
width: 100%;
border-spacing: 5px;
}
#div-layout-nested {
margin-top: -5px;
margin-bottom: -5px;
}
.div-layout-row {
display: table-row;
}
.div-layout-cell {
display: table-cell;
width: 25%;
vertical-align: top;
border: 1px solid red;
background-color: #ffb2b2;
}
body,
#div-layout,
.div-layout-row,
.div-layout-cell {
margin: 5px 0;
}
#div-layout,
.div-layout-row,
.div-layout-cell
#div-layout-nested {
border-spacing:0 0;
}
<div id="div-layout">
<div class="div-layout-row">
<div id="div-layout-nested">
<div class="div-layout-row">
<div class="div-layout-cell">Top Left</div>
<div class="div-layout-cell">Top Middle</div>
<div class="div-layout-cell">Top Right</div>
</div>
</div>
<div id="div-layout-nested">
<div class="div-layout-row">
<div class="div-layout-cell">Botom Left</div>
<div class="div-layout-cell">Bottom Middle</div>
<div class="div-layout-cell">Bottom Right</div>
</div>
</div>
<div class="div-layout-cell">Right</div>
</div>
</div>
Is it possible to fill an entire page with 16 divs but still have it responsive so it can be viewed on different devices. At the moment I have only used percentages but I am open to other solutions if there are any.
-How it is suppose to look.
The webpage has to contain 16 divs in total four spread across the top first quater of the webpage four spread across the second quarter of the page four spread across the third quarter of the page and four spread across the forth quarter of the page.
So overall it is suppose to look like a big cube or look like the 2408 game http://gabrielecirulli.github.io/2048/
-My code so far
***HTML***
<!doctype html>
<head>
<link rel="stylesheet" href="master.css">
</head>
<!-- ========================================================================================================================= -->
<div id="s1" class="divq"> </div> <div id="s2" class="divq"> </div> <div id="s3" class="divq"> </div> <div id="s4" class="divq"> </div>
<!-- ========================================================================================================================= -->
<div id="s5" class="divq"> </div> <div id="s6" class="divq"> </div> <div id="s7" class="divq"> </div> <div id="s8" class="divq"> </div>
<!-- ========================================================================================================================= -->
<div id="s9" class="divq"> </div> <div id="s10" class="divq"> </div> <div id="s11" class="divq"> </div> <div id="s12" class="divq"> </div>
<!-- ========================================================================================================================= -->
<div id="s13" class="divq"> </div> <div id="s14" class="divq"> </div> <div id="s15" class="divq"> </div> <div id="s16" class="divq"> </div>
<!-- ========================================================================================================================= -->
***CSS***
html {
height: 100%;
width: 100%;
margin: 0px;
}
body {
height: 100%;
width: 100%;
margin: 0px;
}
.divq {
height: 25%;
margin: 0px;
width: 25%;
}
#s1 {
background-color: rgb(100,100,100);
float: left;
}
#s2 {
background-color: rgb(120,100,100);
}
#s3 {
background-color: rgb(100,120,100);
}
#s4 {
background-color: rgb(100,100,120);
float: right;
}
#s5 {
background-color: rgb(140,100,100);
float: left;
}
#s6 {
background-color: rgb(100,140,100);
}
#s7 {
background-color: rgb(100,100,140);
}
#s8 {
background-color: rgb(160,100,100);
float: right;
}
#s9 {
background-color: rgb(100,160,100);
float: left;
}
#s10 {
background-color: rgb(100,100,160);
}
#s11 {
background-color: rgb(180,100,100);
}
#s12 {
background-color: rgb(100,180,100);
float: right;
}
#s13 {
background-color: rgb(100,100,180);
float: left;
}
#s14 {
background-color: rgb(200,100,100);
}
#s15 {
background-color: rgb(100,200,100);
}
#s16 {
background-color: rgb(100,100,200);
float: right;
}
Make them all float: left, and don't forget to add box-sizing: border-box to all elements (via .divq)
That way you can add margings and paddings without breakting your grid.
If you are fine with flexbox, you can span four rows inside a wrapper with display: flex and flex-direction: column, each including four columns.
Sample Fiddle:
http://fiddle.jshell.net/n50tnnka/2/
Maybe you could try using a Bootstrap grid? It's fairly easy to use!
Just give your div's the class col-md-3. That way, the div's will know they can take up 3/12th of the screen = 25% = 4 divs per row.
If you then contain all these divs in one parent div with fixed width and height, you should be fine.
<div id="cube">
<div class="col-md-3" id="s1"></div>
<div class="col-md-3" id="s2"></div>
<div class="col-md-3" id="s3"></div>
<div class="col-md-3" id="s4"></div>
<div class="col-md-3" id="s5"></div>
<div class="col-md-3" id="s6"></div>
<div class="col-md-3" id="s7"></div>
<div class="col-md-3" id="s8"></div>
<div class="col-md-3" id="s9"></div>
<div class="col-md-3" id="s10"></div>
<div class="col-md-3" id="s11"></div>
<div class="col-md-3" id="s12"></div>
<div class="col-md-3" id="s13"></div>
<div class="col-md-3" id="s14"></div>
<div class="col-md-3" id="s15"></div>
<div class="col-md-3" id="s16"></div>
</div>
By still using the id's you can give any square the color you like, but by using bootstrap you won't have to use float.
You can do this easily with Flexbox like this
DEMO
.content {
display: flex;
height: 100vh;
width: 100vw;
flex-wrap: wrap;
box-sizing: border-box;
}
.box {
flex: 25%;
border: 1px solid black;
padding: 5px;
box-sizing: border-box;
}
<div class="content">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
For better browser support (than flex) you can use display table-cell for your elements
But you will have to nest each "row" (four divs) in a parent element:
HTML:
<div class="row">
<div id="s1" class="divq"> </div>
<div id="s2" class="divq"></div>
<div id="s3" class="divq"> </div>
<div id="s4" class="divq"> </div>
</div>
CSS:
html {
height: 100%;
width: 100%;
margin: 0px;
}
body {
height: 100%;
width: 100%;
margin: 0px;
}
div {
box-sizing:border-box;
}
.row{
display: table;
table-layout: fixed;
border-spacing:0px;
width:100%;
height:25%;
}
.divq {
display:table-cell;
height: 25%;
width: 25%;
}
DEMO: https://jsfiddle.net/Nillervision/06z1L5tg/
I am using bootstrap and I have two container inside a bootstrap container. Like this:
<div class="container">
<div id="container-map">
aasdasd
</div>
<div id="container-buttons">
asdasda
</div>
</div>
What I am trying to do is center the two divs, #container-map and #container-buttons side by side, inside the main container.
This is my custom CSS for the two divs:
#container-map,
#container-buttons {
display: inline-block;
margin: 0 auto 0 auto;
width: 500px;
height: 500px;
}
Is there a reason you don't want to use the bootstraps built in gridsystem? Something like this?
<div class="container">
<div class="row">
<div class="col-md-3 col-md-offset-3">
<div class="container-map">
asdf
</div>
</div>
<div class="col-md-3">
<div class="container-buttons">
asdf
</div>
</div>
</div>
</div>
Just change your CSS to this
#container-map,
#container-buttons {
float: left;
margin-left: auto;
}
Both containers will be centered and side by side
You can try the code from this example (using text-align: center; on .container display:inline-block; for divs).
<style>
.container {
position:relative;
text-align:center;
}
#dv1, #dv2 {
display:inline-block;
width:100px;
margin:0 3px;
background:#33f;
}
</style>
<div class="container">
<div id="dv1">Div 1</div>
<div id="dv2">Div 2</div>
</div>
you make both your divs to take equal height using flex. You can refer the link to find out the browsers which support it. Have a look at this:
.container {
display: flex;
width: 400px;
background: #eee;
}
.column {
flex: 1;
background: #fff;
border: 1px solid #ccc;
margin: 1px;
}
<div class="container">
<div class="column">
<p>aasdasd</p>
</div>
<div class="column">
<p>asdasda</p>
<p>asdasda</p>
</div>
</div>
The best way to describe what I want to do is using this image. As you can see I have 3 different images as background (which are three different Divs) and I want to insert 3 List Points (here named as: "LV", "RP", "IP") with a centred description/heading below (here as: Lv: 15, RP: 16975 and so on).
My biggest problem is to handle the centred width of those elements. I have no idea how to solve this the best way regarding the CSS.
My HTML:
<div class="package">
<div class="item-description">
<div class="col-md-3"><span class="title">LV</span><span class="description">15</span></div>
<div class="col-md-3"><span class="title">RP<span><span class="description">16975</span></div>
<div class="col-md-3"><span class="title">IP<span><span class="description">40000</span></div>
</div>
</div>
Are you looking for something like this?
It's using quite a few elements (which could possibly be converted into pseudo elements), but it shows a general overview of what you might be looking for.
Also, with the id's and classes this shouldn't make it too hard to alter for your needs.
.container {
width: 32%;
height: 200px;
background: red;
display: inline-block;
}
.container .title {
margin-top: 100px;
width: 32%;
height: 100%;
display: inline-block;
vertical-align: middle;
text-align: center;
color: white;
}
#one {
background: url(http://placekitten.com/g/300/300);
}
#two {
background: url(http://placekitten.com/g/200/200);
}
#three {
background: url(http://placekitten.com/g/300/200);
}
<div id="one" class="container">
<div class="title">200
<div class="desc">I'm a very long description</div>
</div>
<div class="title">300
<div class="desc">desc</div>
</div>
<div class="title">400
<div class="desc">Be More Dog</div>
</div>
</div>
<div id="two" class="container">
<div class="title">200
<div class="desc">desc</div>
</div>
<div class="title">300
<div class="desc">I'm a tree.</div>
</div>
<div class="title">400
<div class="desc">What is a description?</div>
</div>
</div>
<div id="three" class="container">
<div class="title">200
<div class="desc">desc</div>
</div>
<div class="title">300
<div class="desc">a description of what?</div>
</div>
<div class="title">400
<div class="desc">Don't you like, er, trees?</div>
</div>
</div>
this might also work:
Note: I've used a pseudo effect here, but to keep my code 'minimal', rather than applying it to individual items (as you would for individual descriptions), i've just used an existing item.
html,
body {
margin: 0;
padding: 0;
}
.sec {
width: 33%;
height: 200px;
background: red;
background: url(http://placekitten.com/g/200/200);
position: relative;
text-align: center;
color: white;
vertical-align: middle;
display: inline-block;
line-height: 200px;
margin-left: -0.5%;
}
.col {
width: 32%;
margin-left: -0.5%;
font-size: 25px;
display: inline-block;
position:relative;
}
.col:after{
padding-top:25px;
font-size: 10px;
position:absolute;
content:"description";
left:25%;
}
<div class="sec">
<div class="col">
title
</div>
<div class="col">
title2
</div>
<div class="col">
title3
</div>
</div>
<div class="sec">
<div class="col">
title
</div>
<div class="col">
title2
</div>
<div class="col">
title3
</div>
</div>
<div class="sec">
<div class="col">
title
</div>
<div class="col">
title2
</div>
<div class="col">
title3
</div>
</div>
Try this in your css
.description, .title{float:left; width:100%; text-align:center;}
Help newbie in div for php or html without using css or table.
Trying to do this 4 x column, 2 x row using div with the following behavior
// [.....adjustable....][fixed][fixed][fixed]
// [.....adjustable....][fixed][fixed][fixed]
Below are my codes----------------------------------------
<form action="welcome.php" method="get">
<div style="position: relative; width: 100%; ">
<div id="left" style="position:relative;float:left;width:68%;"> left </div>
<div id="right" style="float:left;width:13%;"> Name: </div>
<div id="right2" style="float:left;width:13%;"> Age: </div>
<div id="right3" style="float:left;width:6%;"></div>
</div>
<div style="position: relative; width: 100%; ">
<div id="left2" style="position:relative;float:left;width:68%;"> left2 </div>
<div id="right4" style="float:left;width:13%;"> <input type="text" name="name"></div>
<div id="right5" style="float:left;width:13%;"> <input type="text" name="age"></div>
<div id="right6" style="float:left;width:6%;"> <input name="submit" type="submit"></div>
</div>
The 68% is equal to 860 pxl on my screen. It should change if it goes to other screen resolution. I tried making the 68% to 100% and the other div with id=right to style="position:fixed..." but it just mess up and puts everything on left side.
The easiest way of accomplishing this, is by using display: table and display: table-cell, to style divs as a table, without using an actual table:
Example on jsbin. See display on mdn.
I'll add both the CSS version and the, discouraged, inline style version to this post:
CSS version
<div class="parent">
<div class="left">ASDF</div>
<div class="right">asdf</div>
<div class="right">asdf</div>
<div class="right">asdf</div>
</div>
With CSS:
.parent {
display: table;
width: 100%;
}
.left {
width: auto;
border: 1px dotted blue;
display: table-cell;
}
.right {
display: table-cell;
width: 50px;
border: 1px dotted green;
}
Inline style version
<div style="display: table; width: 100%;">
<div style="display: table-cell;">ASDF</div>
<div style="display: table-cell; width: 150px;">asdf</div>
<div style="display: table-cell; width: 150px;">asdf</div>
<div style="display: table-cell; width: 150px;">asdf</div>
</div>