I am trying to have a div then inside the division have some different places where I can place stuff. For example.
<div blah>
<table blah>
content...
</table>
<table blah>
content...
</table>
<table blah>
content....
</table>
</div>
I am not really a web developer so I know this question might seem simple but any help is greatly appreciated. Oh and I am using Macromedia Dreamweaver 8.
Thanks.
I would suggest using three different divs and then using CSS to place the divs. Check out this w3 schools article on CSS positioning: http://www.w3schools.com/css/css_positioning.asp
This probably isn't the place for tutorials and "hello world"s, but here's something to get you started:
http://jsfiddle.net/rBNUy/
HTML:
<div class="main">
<div class="sidebar"></div>
<div class="top"></div>
<div class="bottom"></div>
</div>
CSS:
.main{ /* this makes the containing box 200x200 */
width: 200px;
height:200px;
background: #eee;
}
.sidebar{ /* this makes the sidebar 100x200 and makes it stick to as far left as it can */
background:blue;
float:left;
height:100%;
width: 100px;
}
.top, .bottom{ /* this makes the boxes 96x96 and makes them stick to as far left as they can , eventually till they hit the sidebar */
width: 96px;
height:96px;
float:left;
background:red;
border:solid 2px green;
}
comments are obviously over-simplified, but in essence, "floating" stacks elements onto one another very much the same as you would stack letters in a word.
When a floated element runs out of space to be stacked in the same line, it just hops to the next line, and finds the closest "wall" to stack to.
In general, try not to use tables for non-tabular data - in other words, don't try to build a webpage using tables. Here is a good article which might get you thinking: http://www.alistapart.com/articles/practicalcss/
On a side note, a lot of people try to use w3schools for learning HTML and CSS. w3schools is an excellent reference for more experienced programmers, but I feel like it fails to teach novice programmers the big picture. You actually learn a lot from trial and error, and from looking at other people's work (source code). http://www.csszengarden.com/ has a great collection of webpages which will certainly help you pick up some new HTML and CSS techniques.
I implemented a rough solution for you of how (horrendous) your markup would have to look if you were to use tables.
http://jsfiddle.net/Wexcode/vMXQe/
HTML:
<table id="container">
<tr>
<td id="sidebar"></td>
<td id="main">
<table>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
CSS:
table { background: #000; }
td { background: #fff; }
#container {
width: 600px;
height: 400px;
margin: 0 auto; }
#sidebar { width: 35%; }
#main { width: 65%; }
#main table { width: 100%; height: 100%; }
#main table tr { height: 50%; }
Related
I have 3 div-s inside a div, I want to put a picture in red area which have a 500px height, I want to fix red height area to 500px and want to stretch other div-s to fill the pages, I don't know hot fit that, also tests put 50% to each blue div-s but don't worked.
<html>
<head></head>
<body>
<div>
<div style="background-color: blue; height:100%"></div>
<div style="background-color: red;height:760px"></div>
<div style="background-color: blue;height:100%"></div>
</div>
</body>
</html>
My solution:
<style>
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
zoom: 1;
}
.main{
background: yellow;
height: 100%;
display: table;
min-width: 100%;
}
.box{
background: blue;
width: 100%;
height: 100%;
display: table;
}
.image{
background: red;
width: 100%;
height: 100px;
display: table-row;
}
</style>
<div class="main">
<div class="box">#1</div>
<div class="image">#2</div>
<div class="box">#1</div>
</div>
Easiest solution: Use a table (they are valid again for layout thanks to html5 and it's presentational role attribute)
Works in "modern" browsers (IE 8 at least): use css display table + display: table-cell etc to use table rendering on other elements
Works when you have javascript: Use javascript. This will lag behind most of the time and might file when the JS engine encounters an error, so it should be your last resort (or actually past that). Example (with explanation) can be found here: http://nicholasbarger.com/2011/08/04/jquery-makes-100-height-so-much-easier/
If it's only about a visual effect: Use a container div for the blue color and 100% height and then center the red one in it (again, multiple ways to achieve that, e.g. tables, table display + css vertical align, ...)
Regarding a correct HTML5 solution (using role attribute marks table as presentational according to W3C spec)
<table style="width:100%;height:100%;border-collapse:collapse" role="presentation">
<tr>
<td style="background-color: blue;"></td>
</tr>
<tr>
<td style="background-color: red;height:760px"></td>
</tr>
<tr>
<td style="background-color: blue;"></td>
</tr>
</table>
Complete working fiddle here (css part is also important for it to work correctly): http://jsfiddle.net/8jFan/
Additional info regarding HTML5 + Tables:
http://www.w3.org/TR/2011/WD-html5-20110525/tabular-data.html#table-layout-techniques
On one hand tables are disencouraged to fix layouting ("Tables should not be used as layout aids."), on the other hand the spec goes on to describe how to use tables for layouting. As tables are - in contrast to css alternatives - backwards compatible really far (they are used for most newsletters because of that), they still seem to be the better choice to me than css "display attribute hacks".
I want to display a list of complex records and trying to simply fit it into a table doesn't seem to be working very well.
I'd like a layout that goes something like this:
Each whole record could be put into a table cell (one cell per row), and then I could use <div> tags within each cell. Is putting divs into table cells likely to cause display problems? It could be simply done with divs anyway, so perhaps that's a bad idea.
Within each record, there are quite a number of components. If I lay these out with divs, what do I need to do to ensure each label/value pair is in the right position? Or is there some better way to ensure a consistent layout?
Obviously the values for each record will be different so to maintain a consistent look I would need the labels to all line up vertically.
If the question seems a bit vague, then it's because my understanding of how to do it is vague... even some help clarifying the question would be great!
Using divs in table cells is fine. Shouldn't cause issues.
Although looking at your mockup, semantically I would say its not a table. No columns, column headings etc.
It looks more like a list of items with more details in them.
I'd use a ul with li's and divs inside to lay things out further.
Also if you need the ID sitting exactly like that you could use a legend element inside each li.
If you are looking at HTML 5, the article tag might fit here. The fact that you have an "Author Element" seems to make it a good fit. If you are not looking at HTML 5 just use a div instead of article. Or as #Moin Zaman mentioned use ul and use li in place of article in my example below.
As for ensuring your labels etc line up vertically this is fairly easy to achieve. Just explicitly set the widths via css.
Here is a quick example:
HTML
<article>
<h2>ID: 123</h2>
<div class="actions">
<input type="button" value="Do Someting" />
<input type="button" value="Do Someting Else" />
<input type="button" value="And Maybe something Else" />
</div>
<div class="description">A fairly long description that takes up basically the entire width of the section, maybe even longer still</div>
<div class="details">
<div class="author"><span>Author:</span>Douglas Adams</div>
<div class="created"><span>Created:</span>1 Jan 2012</div>
<div class="label first"><span>Label 1:</span>Value</div>
<div class="label"><span>Label 2:</span>Value</div>
<div class="label"><span>Label 3:</span>Value</div>
<div style="clear:both; line-height:0px;"> </div><!-- Use A Clear Fix Instead of this, I got Lazy!! -->
</div>
</article>
CSS
article
{
border: solid 2px black;
margin:20px;
padding:5px;
position:relative;
}
article h2
{
background:#FFF;
padding:5px 8px;
position:relative;
top:-15px;
left:5px;
display:inline;
}
article .actions
{
float:right;
width:25%;
text-align:right;
position:relative;
}
article .actions input
{
display:block;
float:right;
clear:both;
}
article .details
{
position:releative;
width:70%;
}
.author
{
float:left;
width:60%;
}
.created
{
float:left;
width:40%
}
.label
{
float:left;
width:30%;
}
.label span, .author span, .created span
{
font-weight:bold;
padding-right:3px;
}
See this fiddle
Note: Use a clear fix instead of having the clearing div.
How about something like this.
HTML
<div>
<span class="label">ID 345</span>
<table>
<tr>
<td class="desc" colspan="3">A fairly long description that takes up basically the entire width of the section, maybe even longer still</td>
<td rowspan="3" class="doStuff">Do Something<br />Do another thing<br />Maybe 1 more thing</td>
</tr>
<tr>
<td class="author"colspan="2"><span>Author: </span>Joe Bloggs</td>
<td class="created"><span>Created: </span>19th June 2012</td>
</tr>
<tr>
<td class="label3"><span>Label 3: </span>Value</td>
<td class="label4"><span>Label 4: </span>Value</td>
<td class="label5"><span>Label 5: </span>Value</td>
</tr>
</table>
</div>
CSS
div{
margin:17px 10px;
border:2px solid #000;
}
span.label{
background:#FFF;
padding:5px 8px;
position:relative;
top:-10px;
left:5px;
}
table{
width:100%;
}
tr{
background:#ccc;
}
td{
padding:5px 7px;
}
span{
font-weight:bold;
}
jsFiddle - http://jsfiddle.net/QActK/
Simple problem, though apparently not a simple solution.
Example here: http://myhideout.eu/new/
Basically the site consist of two columns, though with no wrappers or anything like it, as I'd really like to do with as little of the sort as possible, partly for the sake of simplicity, but also to make use of the HTML5 semantics, which in my mind don't really include divs, no matter how appropriately they are be named.
However, I'd like to have the sidebar fill up the full height of the adjacent column, which is not as easy as I first thought it would be. I know it's an old problem, but I was sure I had solved it before.
Anyhow, I tried to figure out how to do it without using wrappers or JavaScript. JavaScript is a no go, but that's another story. I was sure that there would be some sort of smart CSS3 feature or something similar, that would solve my problem, without the need for wrappers, but my search for this much need feature was a failure of epic proportions.
So I said to my self: "Damn it! Oh well, just have to use wrappers then."
I was sure it would work. I tried different configurations, but no matter what I did, I couldn't get it to work without setting an absolute height of the surrounding wrapper. Just imagine my disappointment, failing once again when I was sure I had done it before. So again I went searching for a solution to suit my needs. Though a lot more material turned up this time, it was still a failure. The few solutions I found was questionable to say the least.
So, now I'm here again, asking yet another one of those questions which undoubtedly have been asked a quadrillion times before. I am sorry about this, but I really don't know where else to go.
I do hope you can help.
Thanks in advance and best regards.
edit:
This works exactly as I want it too:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
margin: 0 auto;
width: 800px;
}
#wrapper {
position: relative;
width: 800px;
}
body > header, body > footer {
background-color: red;
width: 800px;
}
#wrapper > article {
margin-right: 200px;
width: 600px;
background-color: blue;
}
#wrapper > aside {
position: absolute;
top: 0;
bottom: 0;
right: 0;
width: 200px;
background-color: green;
}
</style>
</head>
<body>
<header>This is a header</header>
<div id="wrapper">
<article>
This is the content<br /><br /><br /><br />left<br /><br /><br /><br />left
</article>
<aside>
And this is the sidebar! I dynamically make myself bigger based on the content on the left!
</aside>
</div>
<footer>This is a footer</footer>
</body>
</html>
Only problem left is getting rid of that damn div tag ;)
edit:
the css table display properties have been pointed out to me, and it really seems to be what I'm looking for, as the smart solution, but with multiple elements in one row, and only one in the other, I can't figure out how it should be done.
If IE6 compatibility is not a requirement, then I usually will usually do the following html:
<div class="container">
<div class="content">
This is the content
</div>
<div class="sidebar">
And this is the sidebar! I dynamically make myself bigger based on the content on the left!
</div>
</div>
And this is the CSS:
.container { position:relative; }
.content {margin-right:<SIDEBAR WIDTH HERE>;}
.sidebar {position:absolute; top:0; bottom:0; right:0; width:???; }
JSFiddle example: http://jsfiddle.net/geC3w/
This works in all modern browsers and Internet Explorer 7 and above, it's also immensely simple, as long as IE6 compatibility isn't a requirement
If IE7 compatibility is not a requirement, use display: table-cell;:
body {
margin: 0 auto;
width: 800px;
}
#wrapper {
position: relative;
width: 800px;
}
body > header, body > footer {
background-color: red;
}
#wrapper > * {
display: table-cell;
}
#wrapper > article {
width: 600px;
background-color: blue;
}
#wrapper > aside {
width: 200px;
background-color: green;
}
Working example.
Is there anything I can do to make IE display table cells as actual blocks?
Given this style:
table,tbody,tr,td,div {
display: block;
border: 1px solid #0f0;
padding: 4px;
}
And this html:
<table>
<tbody>
<tr>
<td>R1C1</td>
<td>R1C2</td>
<td>R1C3</td>
</tr>
</tbody>
</table>
<div>
<div>
<div>
<div>R1C1</div>
<div>R1C2</div>
<div>R1C3</div>
</div>
</div>
</div>
The table renders exactly the same as the nested divs in both Firefox and Safari/Chrome. But in Internet Explorer (8) the property display: block has no effect. The table renders exactly as if I don't set that property.
My main problem is that the cells don't break; They all render on one line. (The tbody and tr elements don't get any borders nor padding. That is not a problem for me right now, though.)
I haven't found any information on the problem when searching. Compatibility charts on quirksmode and elsewhere states that IE supports display: block since v. 5.5. Any discussion on table display problems seems to be when doing the reverse - giving non-table elements any of the display: table-* properties.
So once again, is there anything I can do to make IE render table cells as block?
(The real table is really a table, with tabular data. I would like to keep it that way, and restyle it unobtrusively.)
I applied float: left to stuff. It kinda works.
Live Demo
The biggest problem is width: 100% combined with the padding is making things too wide.
So:
Live Demo (without the problematic padding)
That looks a bit better, but I'm not sure how you can easily add padding everywhere if you need it.
This fails --> miserably <-- in IE7 (it just won't get over the fact that it's a <table>), and even if you don't care about IE7, it will need tweaking for your use case (if it's usable at all).
IE7:
The following worked for me for IE6+:
tr {
display: block;
position: relative
}
td.col1 {
display: block;
left: 0;
top: 0;
height: 90px;
}
td.col2 {
display: block;
position: absolute;
left: 0;
top: 30px;
}
td.col3 {
display: block;
position: absolute;
left: 0;
top: 60px;
}
Assumptions:
cell height 30px
Drawbacks:
Fixed cell height
Cumbersome specification of top property (maybe generate)
Only works when HTML provides classes for columns
Advantage:
Works in all browsers.
When to use:
When you have no control over HTML, but have control over CSS. Some hosted payment solutions come to mind that display in an IFRAME and offer a custom style sheet.
Just figured it out with a collegue of mine.
ALTHOUGH I STRONGLY RECOMMEND TO NOT SUPPORT IE8 AT ALL ANYMORE!
Since you are facilitating the use of an unsupported and currently unsafe product that is not up to par with current standards and techniques. It would be way better to tell your users to upgrade and give them some browser downloadlinks to choose from.
That being said. The CSS below is the minimum css you need to fix it in Internet Explorer 8.
table {
width: 100%;
}
td {
float: left;
width: 100%;
}
<table>
<tbody>
<tr>
<td>cell-1</td>
<td>cell-2</td>
</tr>
</tbody>
</table>
add this code:
<!DOCTYPE html>
我这里是这么解决的,加上上面那条声明语句,display:block对td就会有效。
you need add this code in the top.
<!DOCTYPE html>
<html>
<head>
<style>
td {
display: block;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Job Title</td>
</tr>
</thead>
<tbody>
<tr>
<td><div>James</div></td>
<td><div>Matman</div></td>
<td><div>Chief Sandwich Eater</div></td>
</tr>
<tr>
<td><div>The</div></td>
<td><div>Tick</div></td>
<td><div>Crimefighter Sorta</div></td>
</tr>
</tbody>
</table>
</body>
</html>
Add this line of code in the top, but use 'float' and 'width' is very good.
sorry, my english so poor.
make it display:table-row; instead of display:block
It will work like it is supposed to
Why doesn't the "aaaaaaaaa..." go to a new line and go out of div?
<head>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
#container {
background: #888888;
color: white;
width: 200px;
padding: 20px;
}
#container li {
list-style: none;
}
#container td {
padding-right: 20px;
}
</style>
</head>
<div id="container">
<table>
<tbody>
<tr>
<td>one</td>
<td>
<ul>
<li>two</li>
<li>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</li>
</ul>
</td>
</tr>
</tbody>
</table>
</div>
If there aren't any spaces in your word then your table will expand to fit the word. There isn't much you can do about that (not with css anyway). Word wrap only works with actual word (i.e. with spaces in them).
A solution that will work for you though is to use the (soft hyphen)
<li>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</li>
will break in the middle. It will only break there if it needs to - if it doesn't fit in the parent container.
There are a number of other solutions available, most of them are unreliable cross-browser or break your design:
The word break tag : <wbr> is unreliable.
The overflow CSS statement will either break your design (overflow:auto) or hide content (overflow:hidden)
So basically, no easy solution. Soft-hyphen will work best if you can use it.
You could look into hyphenator, a way to automate work breaks like that on your website.
For your edit, if it's a variable then I would definitely with hyphenator.
What you are looking for is css3 feature word wrap: http://www.css3.info/preview/word-wrap/
This have some browser compatibility issues but I think if you are determined to use such long words, this is an option.
This is a sample: http://jsfiddle.net/VXgdS/2/