I'm trying to learn how to put table content into css floats.
My approach has been to use a div at the "table" level, at the "row" level, and the "cell" level. Not sure this is a good strategy.
Anyway, when I set a background style at the "table" or "cell" level I can see the color change. When I set it at the row level it stays white.
Any guesses what's going on? Is there a better way to do this?
<h2>"Tables" work</h2>
<div style="width: 455px; background-color:#a4c4fc">
<div>
<div style="width: 70px; float: left">ID</div>
<div style="width: 220px; float: left">Lemons</div>
<div style="width: 50px; float: left">Horseradish</div>
</div>
<br clear: both>
<div>
<div style="width: 70px; float: left">1<LEFT></div>
<div style="width: 220px; float: left">3</div>
<div style="width: 50px; float: left">4</div>
</div>
</div>
<br>
<h2>"Row" divs do not seem to work</h2>
<div style="width: 455px">
<div style="background-color:#a4c4fc">
<div style="width: 70px; float: left">ID</div>
<div style="width: 220px; float: left">Lemons</div>
<div style="width: 50px; float: left">Horseradish</div>
</div>
<br clear: both>
<div style="background-color:#a4c4fc">
<div style="width: 70px; float: left">0</div>
<div style="width: 220px; float: left">0</div>
<div style="width: 50px; float: left">1</div>
</div>
</div>
<br>
<h2>Individual cell divs work</h2>
<div style="width: 455px">
<div style="background-color:#a4c4fc">
<div style="width: 70px; float: left; background-color:#a4c4fc">ID</div>
<div style="width: 220px; float: left; background-color:#a4c4fc">Lemons</div>
<div style="width: 50px; float: left; background-color:#a4c4fc">Horseradish</div>
</div>
<br clear: both>
<div style="background-color:#a4c4fc">
<div style="width: 70px; float: left; background-color:#a4c4fc">0</div>
<div style="width: 220px; float: left; background-color:#a4c4fc">0</div>
<div style="width: 50px; float: left; background-color:#a4c4fc">1</div>
</div>
</div>
This looks like tabular data.
If it is, you should use a HTML table element to display this information.
Too many people make the mistake of hearing "tables are bad", and try and do work arounds using div's, even when tables are most appropriate.
When people say you shouldn't use tables, they are referring to layout structure. It is still fine to use it in this case, they are not evil!
Then you'll be able to do:
<tr style="background-color:#a4c4fc;">
</tr>
If you are certain on using div's your issue is because the "cell" divs are all float:left;. Therefore your "row" div doesn't have a height.
You could add the following to the bottom of the "cell"'s to fix this:
<div style="background-color:#a4c4fc">
<div style="width: 70px; float: left">ID</div>
<div style="width: 220px; float: left">Lemons</div>
<div style="width: 50px; float: left">Horseradish</div>
<div style="clear:both;overflow:hidden;height:0;"></div>
</div>
Alternatively, use display:inline-block;
<div style="background-color:#a4c4fc">
<div style="width: 70px; display:inline-block;">ID</div>
<div style="width: 220px; display:inline-block;">Lemons</div>
<div style="width: 50px; display:inline-block;">Horseradish</div>
</div>
http://jsfiddle.net/hRCWk/2/
But don't do that, use a table :)
Clear your floats. Add overflow:hidden; to your row div.
Also, <br clear: both> makes no sense. Remove it or use <div style="clear:both;"></div>
Demo: http://jsfiddle.net/RX8Pq/
The row element isn't stretching because all the sub elements are floated:
http://www.quirksmode.org/css/clearing.html
Instead of floating you DIV elements you could set them to display as inline blocks...
display:inline-block;
then you dont need to clear the DIV floats afterwards.
Of course, you should just use a table anyway
NOTE: Apparently this will not work in IE8 and earlier versions.
Related
I have this code below. As you can see the width of the div container is 40%. When I reduce the width, the green div goes down keeping foobar's in the same line. I expected the words (foobar2, foobar3) go down below foobar1 instead. Why this behaviour?
This is the jsfiddle.
<div style="width: 40%; background-color: blue">
<div style="display: inline-block; background-color: red">hello</div>
<div style="display: inline-block; background-color: green">foobar1 foobar2 foobar3 </div>
</div>
It's the use of inline-block. For this purpose you might be better using a <span> element (the equivalent of setting it to display: inline).
<div style="width: 40%; background-color: blue">
<span style="background-color: red">hello</div>
<span style="background-color: green">foobar1 foobar2 foobar3 </div>
</div>
Because you are using style="display: inline-block; This displays the div in the same line.
It's because foobar1, foobar2, foobar3 are inside the same div. So they will be on the same line. If you do something like this, you'll get the desired results.
<div style="width: 40%; background-color: blue">
<div style="display: inline-block; background-color: red">hello</div>
<div style="display: inline-block; background-color: green">foobar1</div>
<div style="display: inline-block; background-color: green">foobar2</div>
<div style="display: inline-block; background-color: green">foobar3 </div>
</div>
PS. There are better ways to do this. I am just modifying your question to help you understand better.
A fiddle to demonstrate this FIDDLE
Please bear with me - CSS and RWD is quite new to me (a javascript, c++, python person).
I'm trying to center a nested DVI on a RWD (responsive web design page) for a friend.
The code looks like:
<div id="homewrapper" style="margin: 0 auto;">
<div class="section" id="homeleftcol" style="overflow: hidden; margin-left: 7px; margin-right: 7px; text-align: left; clear: left; float: left; cursor: pointer;">
<a title="365x522" href="/a/"><img style="padding: 0px;" alt="" src="black.png" width="356" height="522" /></a>
<div class="hometitle" id="imgtitle">365x522</div>
</div>
<div id="homerightcol" style="clear: none; float: left;">
<div class="section" style="margin-left: 7px; margin-right: 7px; margin-bottom: 0px; overflow: hidden; cursor: pointer;">
<a title="365x254" href="/b/"><img style="padding: 0px;" alt="" src="yellow.png" /></a>
<div class="hometitle" id="imgtitle">365x254</div>
</div>
<div id="learncontact" style="overflow: hidden;">
<div class="section" id="learn" style="margin-left: 7px; margin-right: 7px; overflow: hidden; float: left; cursor: pointer;">
<a title="172x254" href="/c/"><img style="padding: 0px;" alt="" src="blue.png" width="171" height="254" /></a>
<div class="hometitle" id="imgtitle">172x254</div>
</div>
<div class="section" id="contact" style="margin-left: 7px; margin-right: 7px; overflow: hidden; float: left; cursor: pointer;">
<a title="172x254" href="/d/"><img style="padding: 0px;" alt="" src="red.png" width="171" height="254" /></a>
<div class="hometitle" id="imgtitle">172x254</div>
</div>
</div>
</div>
Currently I have a mostly ok solution but when the page is enlarged the DIVs hug the left side.
Wide: Hugs the margin-left:7px
Here are a few other views (you can see it still hugs to the left but the images scale to fit for the most part.
Your help is greatly appreciated! Thank you so much!!
The layout hugs the right side of the page because you have not set a width-value to the "homewrapper" div. The question How to center a div in a div - horizontally? covers this problem in more detail.
This could be fixed by giving it a total width value like so:
<div id="homewrapper" style="width: 744px; margin: 0 auto;">
However there are some other issues with your code. I don't know if you already are aware of this, but you say that you are new to CSS, so I'll include it anyway.
Writing all of your styling directly in the style attribute of a html tag is considered very bad style and counteracts the benefits of separating content and presentation. You should instead include it in a separate style sheet and include it in the header of the html-document. w3schools will show you the way to enlightenment.
I am having an issue with my webpage.
My div tags on the right side are overlapping onto my center column... I have set a min-width to my parent div tag but it did nothing to help elevate the problem.
Plus my navigation bar is giving me a little issue.. ENGAGEMENT will fall under the ABOUT tab when I re-size my web page. I have display: block; and display: inline; set in my CSS. It works fine just the re-sizing is hurting my web-page..
My layout is basic..
<body>
<div style="width:100%; margin-top: -18px; clear: both;">
<div style="width: 100%; height: 100px; background-color: white;">
<p style="padding-top: 3%;font-size:30px; font"><i>Welcome</i></p>
</div>
<div style="width: 100%; display: block; overflow: hidden; ">
<div><li>About</li></div>
<div><li>Books</li></div>
<div><li>Electronics</li></div>
<div><li>Apparel</li></div>
<div><li>Activities</li></div>
<div><li>Engagement</li></div>
</div>
<div style="float: left; background-color: white; margin-left: 80px;">
</div>
<div id="left-col" style="clear: left;">
</div>
<div id="central-col">
</div>
<div id="right-col" style="text-align: center; clear: right; ">
</div>
<div id="footer"><p style="text-align: center;">KNOWLEDGE IS POWER</p> </div>
</div>
</body>
It would be better if you have given full code (may be on jsfiddle) because it is hard to understand what these divs are doing by looking at your code. (You have made so many of them.)
For your solution, I think you have to remove
width: 100%
from second and third div.
You have set margin-left to 80px, so whenever you resize the window, it will always have a margin of 80px from left.
Also I want to know the use of
overflow: hidden;
in your code.
Usual CSS centering issue, just not working for me, the problem is that I don't know the finished width px
I have a div for the entire nav and then each button inside, they dont center anymore when there is more than one button. :(
.nav {
margin-top: 167px;
width: 1024px;
height: 34px;
}
.nav_button {
height: 34px;
margin: 0 auto;
margin-right: 10px;
float: left;
}
<div class="nav">
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Home</div>
<div class="b_right"></div>
</div>
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Contact Us</div>
<div class="b_right"></div>
</div>
</div>
Any help would be greatly appreciated. Thanks
Result
If the width is unknown, I did find a way a center the buttons, not entirely happy but doesnt matter, it works :D
The best way is to put it in a table
<table class="nav" align="center">
<tr>
<td>
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Home</div>
<div class="b_right"></div>
</div>
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Contact Us</div>
<div class="b_right"></div>
</div>
</td>
</tr>
</table>
I stumbled across this problem today and I got it to work with
<div style="text-align:center;">
<button>button1</button>
<button>button2</button>
</div>
Consider adding this to your CSS to resolve the problem:
button {
margin: 0 auto;
display: block;
}
Another nice option is to use :
width: 40%;
margin-left: 30%;
margin-right: 30%
The problem is with the following CSS line on .nav_button:
margin: 0 auto;
That would only work if you had one button, that's why they're off-centered when there are more than one nav_button divs.
If you want all your buttons centered nest the nav_buttons in another div:
<div class="nav">
<div class="centerButtons">
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Home</div>
<div class="b_right"></div>
</div>
<div class="nav_button">
<div class="b_left"></div>
<div class="b_middle">Contact Us</div>
<div class="b_right"></div>
</div>
</div>
</div>
And style it this way:
.nav{
margin-top:167px;
width:1024px;
height:34px;
}
/* Centers the div that nests the nav_buttons */
.centerButtons {
margin: 0 auto;
float: left;
}
.nav_button{
height:34px;
margin-right:10px;
float: left;
}
Consider adding this to your CSS to resolve the problem:
.btn {
width: 20%;
margin-left: 40%;
margin-right: 30%;
}
In HTML you can write,
<div id="btn">
<button>Click</button>
</div>
if you work with JS instead of CSS, you can write this to move the button to center.
const bu = document.getElementById("btn");
bu.style.cursor="pointer";
bu.style.justifyContent="center";
bu.style.display="flex";
when all else fails I just
<center> content </center>
I know its not "up to standards" any more, but if it works it works
I want to create a simple box with a header bar containing a title and some tool buttons. I have the following markup:
<div style="float:left">
<div style="background-color:blue; padding: 1px; height: 20px;">
<div style="float: left; background-color:green;">title</div>
<div style="float: right; background-color:yellow;">toolbar</div>
</div>
<div style="clear: both; width: 200px; background-color: red;">content</div>
</div>
This renders fine in Firefox and Chrome:
http://www.boplicity.nl/images/firefox.jpg
However IE7 totally messes up and puts the right floated element to the right of the page:
http://www.boplicity.nl/images/ie7.jpg
Can this be fixed?
Specify width in outermost div.
If that width in your content div means this is the total width of your box, simply add it to the outermost div, and (optionally) remove it from content, like this:
<div style="float:left; width: 200px;">
<div style="background-color:blue; padding: 1px; height: 20px;">
<div style="float: left; background-color:green;">title</div>
<div style="float: right; background-color:yellow;">toolbar</div>
</div>
<div style="clear: both; background-color: red;">content</div>
</div>
This is just a quick answer, so I hold my hands up if it doesn't quite work. I think Marko's solution will probably work if you just add min-width rather than width. If you are trying to cater for ie6 as well, you may need to use a hack, as min width is not supported by ie6 and it's descendants.
So this should work with IE7 and other modern browers. Set the min-width to whatever is appropriate.
<div style="float:left; min-width: 200px;">
<div style="background-color:blue; padding: 1px; height: 20px;">
<div style="float: left; background-color:green;">title</div>
<div style="float: right; background-color:yellow;">toolbar</div>
</div>
<div style="clear: both; background-color: red;">content</div>
</div>
I fixed it using jQuery to emulate the behaviour of the non-IE browsers:
// IE fix for div widths - size header to width of content
if (!$.support.cssFloat) {
$("div:has(.boxheader) > table").each(function () {
$(this).parent().width($(this).width());
});
}
Try putting position:relative; to parent-element and to the child-element. It solved the problem for me.
Got to know recently that the right floated elements need to be appended with the divs before the other elements. This is the easiest fix without adding a line of change.
<div style="background-color:blue; padding: 1px; height: 20px;">
<div style="float: right; background-color:green;">title</div>
<div style="float: left; background-color:yellow;">toolbar</div>
</div>
Make this <div style="background-color:blue; padding: 1px; height: 20px;> the parent of the 2 floating divs also clear:all