Why is the background of my box not shown? - html

I am at a loss at the current problem. I wanted to position three boxes with HTML/CSS, two at the left, one at the right. To mark the area of the boxes, I have chosen three different backgrounds: red, green, and blue. Everything works, but the blue background is not shown. I think I must have made a really stupid mistake, but I can't find it.
Here is the HTML:
.boxupperleft {
float: left;
width: 50%;
height: 100px;
background: red;
}
.boxright {
float: right;
width: 50%;
height: 200px;
background: green;
}
.boxlowerleft {
width: 50%;
height: 100px;
background: blue;
}
<div class='boxupperleft'>
<p>Text1</p>
</div>
<div class='boxright'>
<p>Text3</p>
</div>
<div class='boxlowerleft'>
<p>Text2</p>
</div>
Bonus points for explaining why Text1 and Text3 are not at the top of the box, but Text2 is.
Edit: I just found out that the background is there when I add "float: left" to the definition of "boxlowerleft", but this still doesn't make sense to me. Why does the background need a float?

#Temani Afif made a comment with very useful links to explain float behavior (this and this). There are examples in those post to demonstrate how float works. You should check them out.
As for your specific problem:
Why doesn't the blue background appear?
To cite the docs again:
The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow.
What happens here is that because the red and green boxes are floated, the blue div (which is a display: block element) will be rendered as if those 2 boxes are not there (it ignore them). Coincidentally you have set it to have the exact same size as the red box, so the blue box is rendered behind and is hidden by the red box.
The text inside it, though, is an inline text, so it is wrapped around the red and green boxes, and thus get pushed down (out of) the blue box, to below the red box.
When you add float: left to the blue box, it now recognize the red box (they're both floated) and get pushed down by it, making it visible.
Why do Text1 and Text3 not at the top, but Text2 is?
That's because all of your texts are also inside a <p> element. <p> has a default top and bottom margin so they have some spacing from the top of the boxes.
But <p> itself is also a display: block; element, so it is also rendered as if the red box is not there. The result is that its top margin is at the top, above the red box, creating an empty white space. The text inside it (Text2), is wrapped by the red box to the next line, and since there's no margin there, it touches the red box.

Related

CSS Float and Width

I'm wondering why adding a "width" element to a box is destroying the effect of "float".
For example, when I have
.login form {
float: right;
background-color: green;
}
All elements shifts to the right and the background color only encircles the elements that are there (it does not create a green bar at the top of the screen as I want). I figured I could ameliorate this problem by setting a width,
… width: 800px; …..
but although I get a green by striping the top of the screen, all of my box elements seem to float to the left, so I have a green bar with login elements at the left and not the right.
Could someone please tell me how to take my .login box element, justify all of the five attributes that it has to the right (textfields, boxes, and a button), and still have a green bar at the top of the screen even where there is just blank space.
trying it with padding-left:(something px) ; instead of width: 800px; will probably fix this problem
What is likely causing this problem is that you're using the full capacity of the overlaying div, this makes it useless to float-right since there is nothing left to float away from
You can even put to the container: overflow:auto; or you can you the clearfix method found here.

Image Produces top border line

I have a li tag that when it's hovered over with the mouse the background image url is set to a dark brown arrow as seen in this image below.
As you can see, a black line is produced on the top right of the image. I have no idea how this is produced and I want the dark line removed. How do I fix it?
The CSS:
#topcol1navtall ul.contactsleftnav li.dash.selected {
background:#7c6a54 image-url('contact-arrow-nav.png') right no-repeat;
border-bottom: 2px dotted #f17ca6;
margin-top: 5px; padding-top: 8px;
}
The HTML:
<li class="contact">
<div class="text">
<span class="name">
<!-- / Dynamically loads edit form into right column -->
asdf, Billy 3
</span>
</div>
</li>
That line isn't black. It's the same color as your background, #7c6a54. Your image is either transparent in the first row of pixels, or it's one pixel too short and somehow anchored to the bottom of the containing li.
I would guess it's probably the latter. Your li is one pixel taller than the background image you're using. I would recommend making your background image taller than you need it be and then explicitly anchoring it either to the top or bottom of the containing element like so:
background: #7c6a54 image-url(your-url) right top no-repeat;. Then make the background image taller by a few pixels. Make sure the bottom angle doesn't suddenly turn into a vertical line or else you'll have a weird one-pixel bump there. Actually continue the angle so that you retain a smooth line on that end.
The other option is to explicitly declare the height of the li (making adjustments for your padding), but then you run into trouble if someone's browser renders text larger than you expect it to. If you're going to explicitly declare the height of the li, you should also explicitly declare the font-size and line-height of the text in the li (or they should inherit that from a parent element, but don't leave it to chance).

Make all elements of a column the same width, without `display:block` or `width`

I have a webpage which has three green boxes, each on their own line. Each green box also has an associated red box, which appears when you hover over the green box. All of the green boxes are contained within a black box, which is just wide enough to contain the widest green box.
JSFiddle
Question
How can I widen the shorter green boxes so they match the width of the widest green box? Here is an MSPaint rendition of what I would like to see:
Browsers I Am Testing With
At a minimum, I want to support Internet Explorer 7, 8, and 9. Firefox is optional. I am not interested in supporting any other browsers.
What I've Tried So Far
I have made a few attempts to attain this, but I can't get it to work the way I want it to. (Feel free to skip these next parts if they're boring.)
Attempt 1
I gave each green block the display:block style and removed the <br/>s between them.
JSFiddle
The green boxes are successfully resized! However, now each red box appears below its respective green box, rather than to the right. I don't know if there's any way to put the red box on the same line as a block element, so I abandoned this approach.
Attempt 2.0
I gave each green box a width of 100% and removed the <br/>s between them.
JSFiddle
(Ignore the gap between the green and red box. That's there because there's whitespace between the two spans, and is easy to repair.)
The green boxes are all the same size, but they are about 50px too wide and they extend past the end of the black box.
Additional browser-specific problems:
In IE7, the boxes all appear on one line.
In Firefox, the red boxes appear below their respective green box.
Attempt 2.1
From Attempt 2.0, I removed the green boxes' padding. Padding is a very strong "it would be nice to have" feature, but I'll ditch it if there's literally no way to have it.
JSFiddle
The green boxes extend 1px past the end of the black box, which is annoying but not unacceptable. The boxes are still too wide. All of the browser-specific problems from 2.0 are still in effect.
Attempt 2.2
From Attempt 2.1, I re-added the <br/>s.
JSFiddle
The green boxes are the right width, give or take a pixel!
Browser-specific updates:
The boxes appear on their own lines in IE7, but they are not all the same width.
Firefox still shows the red boxes in the wrong position. In addition, there are big gaps between neighboring green boxes.
Attempt 2.3
From Attempt 2.2, I changed width to 99%.
JSFiddle
The green boxes are a little shorter than the black box, which is acceptable. As long as they are the same width.
Browser-specific updates:
In IE7, the boxes are still not the same width.
Firefox is working perfectly.
So I've got 75% browser compatibility at this point. However, It doesn't seem like any amount of tweaking will get IE7 to work, so I gave up with this technique.
TL;DR
resizing boxes to fit a column is normally not too difficult. But normal methods fail when the boxes have fixed-pixel-width padding and absolutely positioned siblings that must appear to their right. I am seeking a method that will work under these particular circumstances.
I verified that this demo works in IE7+ and modern browsers:
http://jsfiddle.net/thirtydot/APVuq/4/
CSS:
.hoverBox {
position: relative;
}
.visiblePart {
display: block;
border: 1px solid green;
padding: 5px;
white-space: nowrap;
}
.hiddenPart {
display: none;
border: 1px solid red;
position: absolute;
left: 100%;
top: 0;
white-space: nowrap;
}
.hoverBox:hover > .hiddenPart{
display: block;
}
.enclosingBox {
border: 1px solid black;
display: inline-block;
*display: inline;
zoom: 1;
}
HTML:
<div class="enclosingBox">
<div class="hoverBox">
<span class="visiblePart">
Box 1
</span>
<span class="hiddenPart">
Hidden part of Box 1
</span>
</div>
<div class="hoverBox">
<span class="visiblePart">
Box 2, which is much wider than box 1
</span>
<span class="hiddenPart">
Hidden part of Box 2
</span>
</div>
<div class="hoverBox">
<span class="visiblePart">
Box 3, which <br/> is split into two lines.
</span>
<span class="hiddenPart">
Hidden part of Box 3
</span>
</div>
</div>​
I just can't believe what I am about to write, but why not use a good old <table> ? With 2 columns and 3 rows, the second column containing your red boxes... That should do it pretty easily.

How to move div lowered from edge of page

Here is a jsfiddle http://jsfiddle.net/Jw6kU/2/ of what i have right now. The thing i need to do is drop the white box and the text in that div tag ("left") down so it is center in the green bar at the top (67px tall). How can i easily do this? Sorry this is sloppy, the work was done fast.
This is a second post. The first one i messed up by pasting the wrong link. Sorry.
Using the margin-top css property would give the div space from the top of the page. You'd have to find the right amount of pixels and test it out fully but I think that will help.
I added this to the css of your fiddle and got the white div at the top in the center of the green div under it.
#left {
margin-top: 9px;
padding-top: 12px;
}

Transparent text box underlay

I'm looking to clone the Google Instant "underlay/type ahead" type look, where what the system is predicting is grayed out and infront of what you are typing.
The technical part of it I am completely sorted, as well as representing the text. I simply am unable to work out how to do the CSS positioning and transparent textbox over the top of the gray text.
Anyone know how to simply do this?
I've tried to adapt code from other sources, but been unable to get the text with the gray text underneath a transparent textbox.
I believe you're looking for something like this. Keep in mind they need to be posiitoned together, so it's probably a good idea to wrap this in a div together.
HTML
<div class='top'>
<input type='text' id='gray'/>
</div>
<div>
<input type='text' id='type'/>
</div>​
CSS
.top {
background:transparent;
position:relative;
}
input {
font-size: 14px;
width: 200px;
}
#type {
background: transparent;
z-index: 1;
}
#gray {
position: absolute;
z-index: -1;
color: silver;
}​
Live Example
http://jsfiddle.net/r4jSR/
Edit
This positioning works by stacking a position:relative div on top of another block level element, then setting the div's contents to absolute, but with no positioning. This causes the div to collapse as it has no contents, and - as long as neither block element has a margin - the 0,0 coordinates for absolute positioning should put it right on top of the block element below. Presto. This is the way Google does it.