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?
Related
I have four divs inside a container like this:
<div class="container">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
I would like to keep the first and last div aligned to the edges of the screen and only center the two divs in the middle. I tried display inline block and adjusting the margins, but i just can't figure it out. Please try and enlighten me!
Kind Regards
Use this HTML
<div class="container">
<div class="block left">1</div>
<div class="block">2</div>
<div class="block">3</div>
<div class="block right">4</div>
</div>
and then this CSS
.container {
display:block;
text-align:center
}
.block {
display:inline-block;
border:1px solid #000;
padding:10px;
margin:auto
}
.left {
float:left
}
.right {
float:right;
}
you can also use first-child and last-child, but it's easier to add a class to first and last div
See fiddle here
Try using :nth-of-type() pseudo-class allows you to select elements with a formula,like this: http://jsfiddle.net/csdtesting/11fh7suy/
//second div
div.container div:nth-of-type(2) {
background-color: #9999ff;
text-align:center;
}
//third div
div.container div:nth-of-type(3) {
background-color: #9999ff;
text-align:center;
}
Hope it helps!
Adding an extra container will give you more control:
<div class="container">
<div class="block">1</div>
<div class="wrap">
<div class="block">2</div>
<div class="block">3</div>
</div>
<div class="block right">4</div>
</div>
.wrap{width:50px;margin:0 auto}
.block{float:left;width:25px;text-align:center}
.right{float:right}
I am using HTML 5 and CSS 3.
Here is my html code:
<div style="float:left; width:100px; height:150px;">Column 1</div>
<div style="float:left; width:100px;">Column 2</div>
<div style="float:left; width:100px;">Column 3</div>
I am trying to position the second and third div at bottom irrespective of the height of first div. Whatever may be the height of first div but the second and third div should be at bottom like following.
Test
Test
Test Test Test
I tried position absolute inside divs but not working. I need to achieve this using div not table. Any help would be appreciated. Thanks.
You don't need to use tables to take advantage of table-layouts:
You'll probably need to wrap the divs in another div to act like a table-row. These columns will grow so they are each as tall as the tallest:
html
<div class='table-row'>
<div>Column 1<br>with<br>more<br>text</div>
<div>Column 2</div>
<div>Column 3</div>
</div>
css
.table-row{
display:table-row;
}
.table-row > div{
display:table-cell
;width:100px;
vertical-align:bottom;
}
demo here: http://jsfiddle.net/mhfaust/CV33q/
update
Acutally, you don't need the .table-row wrapper at all.
You could remove it in the above code, and change the selector from .table-row > div to jsust div and it will still work (though with other markup on the page that wouldn't be the best way to do it -- you'd want a classname on the div like .table-cell and use that selector instead.)
Working example http://jsfiddle.net/cYbW7/
<div style="background: yellow; float: left; display: block; position: relative;">
<div style="width:100px;display: inline-block; height:150px; background: red;">Column 1</div>
<div style="width:100px;display: inline-block; vertical-align: bottom;background: blue;">Column 2</div>
<div style="width:100px;display: inline-block; vertical-align: bottom; background: green;">Column 3</div>
</div>
try setting bottom:0 to both div:
<div style="float:left; width:100px; height:150px;">Column 1</div>
<div style=" bottom:0px; width:100px;">Column 2</div>
<div style=" bottom:0px; width:100px;">Column 3</div>
Here is demo
or if you just want to see in next line try this:
<div style="float:left; width:100px; height:100px;">Column 1</div>
<div style=" display:block; width:100px;">Column 2</div>
<div style=" display:block; width:100px;">Column 3</div>
I have a PHP code which generates a dynamic questionnaire. To make the story short, i must keep the structure of the DIVs exactly as it its. I cannot modify, add or delete the DIVs, they must stay exactly as they are now. But I can modify the CSS and the CLASSES. With that being said, how can i display Question 2 and Question 3 to be inline?
I made this simple example here, it will be easier for you to see it what i mean:
http://jsfiddle.net/radusl/4H68P/
<style type="text/css">
.bigclass {
margin-left: 10px;
}
.smallclass {
margin-left: 20px;
}
.question {
font-weight: bold;
margin-left: 10px;
}
</style>
<div class="question">Question 1</div>
<div class="bigclass">
<div class="smallclass">
Text1_a
</div>
<div class="smallclass">
Text1_b
</div>
</div>
<div class="question">Question 2</div>
<div class="bigclass">
<div class="smallclass">
Text2_a
</div>
<div class="smallclass">
Text2_b
</div>
</div>
<div class="question">Question 3</div>
<div class="bigclass">
<div class="smallclass">
Text3_a
</div>
<div class="smallclass">
Text3_b
</div>
</div>
<div class="question">Question 4</div>
<div class="bigclass">
<div class="smallclass">
Text4_a
</div>
<div class="smallclass">
Text4_b
</div>
</div>
<div class="question">Question 5</div>
<div class="bigclass">
<div class="smallclass">
Text5_a
</div>
<div class="smallclass">
Text5_b
</div>
</div>
As others have stated, this is not ideal markup for what you need, but it is technically possible by adding an extra class to question2, in my example .inline, and doing some tricks with float: left; and clear: left;
Check out this updated fiddle
.inline {
float: left;
}
.inline + .bigclass {
float: left;
clear: left;
}
You can't do this, without wrapping question and bigclass in another div
HTML
<div class="inline-question">
<div class="question">...</div>
<div class="bigclass">...</div>
</div>
CSS
.inline-question {
display: inline-block;
}
Without wrapping, you can only show question and bigclass side by side
Q1 T1 Q2 T2 Q3 T3 ...
CSS
.question, .bigclass {
display: inline-block;
}
You ment something like this?
http://jsfiddle.net/4H68P/1/
use the float function:
float: left;
Hope this helps you out.
I'm not sure to understood what you want but if it's what I think you can add
display:inline;
in your smallclass.
If you want this for only some questions, create a new class :
.inlineClass {
display:inline;
}
and call this class for the answers you want :
<div class="smallclass inlineClass">
***** EDIT *********
And if you want answers be side by side the question number, add the class like this :
<div class="question inlineClass">Question 3</div>
<div class="bigclass inlineClass">
<div class="smallclass inlineClass">
Text3_a
</div>
<div class="smallclass inlineClass">
Text3_b
</div>
</div>
I have this result :
Question 1
Text1_a
Text1_b
Question 2
Text2_a
Text2_b
Question 3 Text3_a Text3_b
Question 4
Text4_a
Text4_b
Question 5
Text5_a
Text5_b
Is it not what you want ?
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
I have a list of couples (name, value) and I would like to display them vertically aligned as in a table:
aaaaaaaa first_value
bbbb second_value
ccc third_value
ddd fourth_value
Is there a way to do so in HTML/CSS, without using tables (I suppose I should use UL/LI tags in some way)?
Yep. Use a definition list.
<dl>
<dt>aaa</dt>
<dd>111</dd>
<dt>bbb</dt>
<dd>222</dd>
<dt>ccc</dt>
<dd>333</dd>
</dl>
and then
<style>
dl dt { width: 100px; float: left; }
dl dd { margin-bottom: 10px; }
</style>
http://jsfiddle.net/wr6wL/
This looks like a definition list to me:
<dl>
<dt>aaaaaaaa</dt>
<dd>first_value</dd>
<dt>bbbb</dt>
<dd>second_value</dd>
<dt>ccc</dt>
<dd>third_value</dd>
<dt>ddd</dt>
<dd>fourth_value</dd>
</dl>
Some CSS to style it is in a this question.
.name { display: inline-block; width: 100px }
Do consider using a table though if you have tabular data. That's what tables were made for.
You can certainly do it with just CSS, but why not use a table? This is a perfectly valid use of one.
That said, an example with just CSS:
<ul>
<li><div>name</div><div>value</div></li>
</ul>
li {overflow: auto;}
li div {width: 50%; float: left;}
try something like this :
<ul>
<li><span class="fixedWidth">aaaaaaaa</span>first_value
<li><span class="fixedWidth">bbbb</span>second_value
<li><span class="fixedWidth">ccc</span>third_value
<li><span class="fixedWidth">ddd</span>fourth_value
</ul>
in your css add
.fixedWidth { display: inline-block; width: 100px }
You can create a group of div tags that have a table style.
Create your html divs like so:
<div class="div-table">
<div class="div-table-caption">This is a caption</div>
<div class="div-table-row">
<div class="div-table-col">1st Column</div>
<div class="div-table-col">2nd Column</div>
</div>
</div>
Then you can add css styling to get the look you want:
.div-table{display:table; border:1px solid #003399;}
.div-table-caption{display:table-caption; background:#009999;}
.div-table-row{display:table-row;}
.div-table-col{display:table-cell; padding: 5px; border: 1px solid #003399
View this jsFiddle for the end result.
You could do something like this
HTML
<div id="main">
<div class="row">
<p class="cell">aaaaaa</p><p class="cell">first_value</p>
</div>
<div class="row">
<p class="cell">bbbb</p><p class="cell">second_value</p>
</div>
<div class="row">
<p class="cell">ccc</p><p class="cell">third_value</p>
</div>
<div class="row">
<p class="cell">ddd</p><p class="cell">fourth_value</p>
</div>
</div>
CSS
#main{display:table;}
.row{display:table-row;}
.cell{display:table-cell; padding:10px;}
http://jsfiddle.net/jasongennaro/cHhTE/