I am trying to create the following table layout, but I want to use DIV instead of TABLE:
------------------
| | |
| CELL1 | CELL2 |
| | |
| |--------|
| | |
| | CELL3 |
| | |
------------------
I want the height of all the cells to be set by their content (i.e. no height: style)
I have tried using float:left on cell1, but can't seem to get cells 2 and 3 to behave.
EDIT
JS Fiddle here: http://jsfiddle.net/utZR3/
HTML:
<div class="list-row">
<div class="list-left">CELL1
</div>
<div class="list-right">
<div class="list-title">CELL2</div>
<div class="list-filters">CELL3
</div>
</div>
</div>
<div class="list-row">
<div class="list-left">CELL1
</div>
<div class="list-right">
<div class="list-title">CELL2</div>
<div class="list-filters">CELL3
</div>
</div>
</div>
<div class="list-row">
<div class="list-left">CELL1
</div>
<div class="list-right">
<div class="list-title">CELL2</div>
<div class="list-filters">CELL3
</div>
</div>
</div>
CSS:
.list-row {
background:#f4f4f4;
border:2px solid red;
}
.list-left {
width:auto;
padding:10px;
top:0px;
left:0px;
border: 2px solid blue;
}
.list-right {
top:0px;
left:60px;
padding:10px;
border:2px solid green;
}
.list-title {
font-size:18px;
padding:8px;
}
.list-filters {
padding:8px;
}
You need inline-block and float: here's the jsFiddle
.list-row {
background:#f4f4f4;
display:inline-block;
border:2px solid red;}
.list-left {
width:auto;
padding:10px;
float:left;
border: 2px solid blue;}
.list-right {
padding:10px;
float:right;
border:2px solid green;}
Also, since you're not using relative or absolute positioning, you don't need to specify top and left.
Try the following: (working jsFiddle)
HTML:
<div class="container">
<div class="cell" id="cell1"></div>
<div class="cell" id="cell2"></div>
<div class="cell" id="cell3"></div>
</div>
CSS:
.container{overflow:hidden; width:100%;}
.cell{width:50%; float:right;}
#cell1{float:left;}
Your approach (which places the divs in rows) is not a good choice in this case.. mine separates them by columns.
You could use display:inline-block instead of float. Just set widths of about 50% (adjust depending on padding, margins and borders) for the left and right containers and make them inline-block.
Here is my jsFiddle
rowspan is not avalaible for display:table,
but you can still use it to get close to what you are looking for.
http://codepen.io/anon/pen/kqDab
display:inline-table used for the show , so they stand aside each others. you can turn it back to display:table.
Options i see here is to set an height to parent container to have height:XX% avalaible for direct childs element (if it is: float, inline-block, table ...) .
Other option is vertical-align middle for the cell if display:table-cell;.
You HTML with the same CSS of first demo : http://codepen.io/anon/pen/dvlsG
edit display:flex is also a good option nowdays :http://codepen.io/anon/pen/aBjqXJ
<style>
.grid-container {
display: grid;
}
</style>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
</div>
use display grid for using div instead of table
Related
I am having hard time to adjust floating div with dynamic heights (with CSS only) if you see the bottom gap i want them to stick together having dynamic height
please check the code here any help would be highly appreciated
.c {
display: inline-block;
width: 100%;
}
.D {
float: left;
}
<div class="c">
<div class="D" style="height:100px; background:blue; width:25%;">
</div>
<div class="D" style="height:200px; background:green; width:25%;">
</div>
<div class="D" style="height:100px; background:pink; width:25%;">
</div>
<div class="D" style="height:120px; background:red;width:25%;">
</div>
<div class="D" style="height:200px; background:red; width:25%;">
</div>
<div class="D" style="height:150px; background:blue; width:25%;">
</div>
<div class="D" style="height:100px; background:green; width:25%;">
</div>
<div class="D" style="height:100px; background:pink;width:25%;">
</div>
</div>
Could this be what you're trying to achieve?
If you end up using this approach, make sure to set the height of .c the sum of the two (or more) tallest, and stacked, .D elements.
https://jsfiddle.net/axelboberg/5pdpf6zh/
Also, try to use as little inline css as possible. It will get messy when your project grows and violates the MVC concept.
I have in my layout three elements like this:
<div id="container">
<div id="element1"/>
<div id="element2"/>
<div id="element3"/>
</div>
shown this way:
| element1 | element2 | element3 |
I want them to show like this:
element1 | element2
element3 |
The closest thing I've achieved to do is this:
element1 |
element3 | element2
I can't achieve to align element1 and element2
Does anybody knows how to do it only with CSS ?
Here is the working example
<div id="container">
<div id="element1" class="boxes">
This is elem1
</div>
<div id="element2" class="boxes">
This is the elem2
</div>
<div id="element3">
This is elem3
</div>
</div>
<style>
.boxes{
border:1px solid black;
box-sizing:border-box;
width:50%;
float:left;
}
</style>
This is how you use div tags :
<div class="exampleclass">Example Text</div>
An example made by Sören Kuklau can be seen here
Here is an example Fiddle using the float: left css property
Your question is quite open-ended (no context whatsoever), there's a very simple solution, which is to use float: left...
#element1, #element2, #element3 {
float: left;
width: 50%;
box-sizing: border-box; // <- Not necessary for this basic example unless you add padding, etc.
}
<div id="container">
<div id="element1">El 1</div>
<div id="element2">El 2</div>
<div id="element3">El 3</div>
</div>
define them all as float: left and add clear: both to the third one.
<div id="container">
<div id="element1">El 1</div>
<div id="element2">El 2</div>
<div id="element3">El 3</div>
</div>
#container > div {
float: left;
}
#container{
position:relative;
}
#element3{
bottom: -40px;
display: block;
position: absolute;
}
In my explaination the #container #element3 and #container > div are ids of div so please dont read as comment thanks
You can't do it only using CSS because CSS can't do real DOM modifications for that you have to use jQuery.
I hope this link can help you:
What is the easiest way to order a <UL>/<OL> in jQuery?
This is difficult to explain so please see this fiddle: http://jsfiddle.net/vhfhzgfz/
Say I have a row of 4 columns using Bootstrap. I want the content within these cols to be equal distances apart. The content has a fixed width in px.
The content in the first column needs to be aligned to the left (edge of the page) and the content in the last col aligned to the right (the other edge of the page). This is fine.
But how do I align the content in the middle two columns to ensure the space between the content is equal.
Here's the code form the fiddle:
<div class="container">
<div class="row">
<div class="col-xs-3 left">
<div class="wrapper"></div>
</div>
<div class="col-xs-3 center">
<div class="wrapper"></div>
</div>
<div class="col-xs-3 center">
<div class="wrapper"></div>
</div>
<div class="col-xs-3 right">
<div class="wrapper"></div>
</div>
</div>
</div>
.col-xs-3{
background:lightblue;
outline:solid 1px black;
}
.wrapper{
width:100px;
height:100px;
background:pink;
position:relative;
display:inline-block;
}
.left{
text-align:left;
}
.right{
text-align:right;
}
.center{
text-align:center;
}
I am trying to setup a responsive design for my divs and I also want to maintain the left and the right margin to 5px no matter the screen width. The problem I have is I have 5 div inside a parent div and I am not sure how to set the grid.
My previous pose for the similar question. It's not taking account of child divs.
How to setup margin responsively?
For example:
<div class="container">
<div class="row">
<div class="col-md-2">
//contents...
</div>
<div class="col-md-2">
//contents...
</div>
<div class="col-md-2">
//contents...
</div>
<div class="col-md-2">
//contents...
</div>
<div class="col-md-2">
//contents...
</div>
</div>
</div>
This will produce:
-------------------------------------
|5px+ ---- ---- ---- ---- ---- 5px+
| div div div div div
|
|
|
I am not sure how to maintain the 5px for every screen. Can anyone help me about it?
Thanks!
As I post on the other answer you can use custom class definitions. In this case if the number of col doesn't fit the request of 12 you can do this:
<div class="container-fluid">
<div class="row margin">
<div class="col-xs-12">
//contents...
</div>
..........
</div>
</div>
.margin {
padding:0 5px;
}
#media (min-width:768px) {
.margin > div {
background:red;
width:20%;
}
}
Check this BootplyDemo
You will give yourself a headache if you use margins on grids. Switch to padding and it won't affect the grid:
.container {
padding-left: 5px;
padding-right: 5px;
}
Padding can be added to any column without having to worry about it breaking.
I am trying to achieve what is shown in the below picture.
Found a few links online, where people have suggested to use table-cell. Tried it, but not getting the exact result. Are there any better standard ways to achieve this ? Please suggest.
Tried code:
CSS
.left, .right{
display:table-cell;
HTML
<div class=left>
<div> Cell A text ... </div>
<div> Cell B text ... </div>
</div>
<div class=right>
<div> Cell C text ... </div>
</div>
Why not do:
Demo Fiddle
HTML
<div class='table'>
<div class='cell'>
<div class='table'>
<div class='cell'>Cell A</div>
</div>
<div class='table'>
<div class='cell'>Cell B</div>
</div>
</div>
<div class='cell'>Cell C</div>
</div>
CSS
.table {
display:table;
width:100%;
}
.cell {
display:table-cell;
border:1px solid black;
width:50%;
height:100px;
vertical-align:middle;
}
Add right column vertical-align: middle; property to your existing solution. Demo