Something like margin or padding except where background doesn't draw - css

Is there something that will do what margin does but without the background drawing in that area? For instance, when you give an element margin: 1em you get a 1em border of blank space around the element, but the background draws in that area. Is there something similar to that except where the background doesn't draw?
My problem is I'm trying to put something below three float: lefted divs and right now I can't get any spacing between that and the floated divs above it. They just abut directly against each other.
The div that is going below the float: lefted divs has the property clear: both. If there was something that made that div have space between it and that floated div above it then that would work too.

Maybe this example will help explain (and solve) your problem?
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<style>
* { margin: 0; padding: 0; }
h1, p { background-color: #eee; margin: 10px 0; }
div { background-color: pink; float: left; width: 100px; height: 100px; margin-right: 1px; }
br { clear: both; display: block; }
</style>
</head>
<body>
<h1>Hello, World!</h1>
<div></div>
<div></div>
<div></div>
<br />
<p>Lorem ipsum dolor set amit...</p>
</body>
</html>

Margins are supposed to be transparent. I think what you're seeing here is collapsing margins. Try putting a 1px border around your divs and see what happens.
Check out the CSS 2.1 spec:
http://www.w3.org/TR/CSS21/box.html#collapsing-margins

Perhaps you're looking for:
border: 4px white; /* replace with your color */

With floated elements the margin around elements next to them is ignored. I think you will have to create an additional element between the floated element and the item you want.

Because the element is floated margin space won't always be properly respected. Use a margin/border hack where you simply set the element's color to the same color as your page background and its thickness to whatever you desire. Such as in the following post:
http://socialstreams.co/41/CSS_MarginBorder_Hack

Related

I'd like to apply margin-top inside the box [duplicate]

This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
Closed 3 years ago.
Apply this code and you can see it. The margine-top of the internal box is applied to the outside. How can inBox not affect the box?
<!DOCTYPE html>
<html>
<head>
<style>
.box{
width: 400px;
height: 400px;
background-color: red;
}
.inBox{
width: 200px;
height: 200px;
background-color: white;
margin-top: 100px;
}
</style>
<meta charset="utf-8">
</head>
<body>
<div class = "box">
<div class = "inBox">
</div>
</body>
</html>
So you actually can have margin-top on the child element. As mentioned by others, vertical margins can collapse. This is one of the least intuitive instances, where the margin-top of the child is apparently going right through the parent.
You can counter this by adding any amount of padding to the parent. I added 1px of padding-top and the margin suddenly appears to behave as originally expected:
.box {
width: 400px;
height: 400px;
background-color: red;
padding-top: 1px;
}
.inBox {
width: 200px;
height: 200px;
background-color: white;
margin-top: 100px;
}
<div class="box">
<div class="inBox">.inBox has margin-top</div>
</div>
As recommended by others I would also probably just use padding-top on the parent to create all of the space but this trick exposes an interesting nuance of margin collapse.
You can’t do margins inside an element. You can use padding. If you don’t want the padding to affect the side of the box, also set box-sizing: border-box.
Vertical margins have some unintuitive characteristics, like collapsing. There are ways to prevent vertical margin collapse that may solve your issue.

A CSS absolute positioning mystery

Consider the webpage below. The <img> is positioned absolutely relative to its parent, and when I load this page on Safari or Firefox, the <img> appears in the top-right, as expected (see first image). However, when the border is removed from from the <div>, for example, by setting border-width: 0, the <img> positions itself absolutely relative to the <p> tag, its sibling! See picture #2. Why is this? What difference should the border make?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body {
margin: 0;
}
div {
position: relative;
left: 0;
top: 0;
border: 1px solid red;
}
img {
position: absolute;
right: 0;
top: 0;
}
p {
margin: 20px;
}
</style>
</head>
<body>
<div>
<img src="content/en/flag.png" />
<p>This is a test</p>
</div>
</body>
</html>
Your image is always at the top-right. It has to do with collapsing margins.
Try to do it with a background color. You will see that your div is moving away from the top of the body a few pixels. If you delete p's margin everything would be fine, or setting p as an inline element or floating it, or even setting an overflow to auto, hidden or scroll to the parent. Another way to fight the collapsed margin is to add a border to the container element. So you really was solving this with that border.
But image is always where it is supposed to be.
Its really strange indeed but let me try to explain this actually the elements are not float and you are using margin on p tag which the div is taking properly when it has border and failed to implement it when its removed if add float property than the div will also gain its height
add overflow:auto; to div it will fix the problem

Background shows to the right of the border in IE9

I have a div with a background-color, and a 3 pixel white border.
When I set the html element to direction:rtl and overflow-y: scroll, I get a pixel of the background to the right of the border - only in IE9:
I'm pasting my code here, because on JsFiddle I can't replicate the bug.
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
html {
overflow-y: scroll;
direction:rtl;
}
.main {
margin: 0 auto;
width: 960px;
}
.sld-menu-item {
height: 85px;
border: 3px solid #fff;
background-color: #d25188;
}
</style>
</head>
<body>
<div class="main" role="main">
<div class="sld-menu-item sld-menu-item-2">
</div>
</div>
</body>
Has anyone run into this problem, and/or can someone suggest a solution? I can't give up the scroll and rtl rules...
I was only able to fix it by setting overflow: hidden on containing element and doing a negative margin hack:
.main {
overflow: hidden;
}
.sld-menu-item {
margin-right: -1px;
}
You might also want to set width of sld-menu-item to 961px then. Can probably put this in an IE9 conditional statement. I hope there's a better way of solving this though.
I banged my head against the wall for several hours, at the end I solved it in a very strange way...
Change the width of .main to 961px, it seems that Microsoft does not know how to find the "middle" of an even range.

How do I set a border on a div that wraps a checkbox?

I am trying to create a div the dimensions of which are exactly the same as those of a checkbox. When I specify something like this
<html>
<head>
<style type="text/css">
.wrapper {
margin: 12px;
width: 13px;
height: 13px;
border: 1px solid green;
display: inline-block;
}
</style>
</head>
<body>
<div class="wrapper"><input type="checkbox" /></div>
</body>
</html>
I get a small top margin between the green border and the checkbox in both Chrome and Firefox. If I reduce the height of the div, it raises the lower boundary of the div without affecting the top. I've tried playing with the margin on the checkbox but that did not affect the gap.
Here's the jsfiddle that shows this behavior.
Thanks,
Gene
Set display: block on .wrapper input: http://jsfiddle.net/thirtydot/hpZqK/5/
Or, set line-height: 0 on .wrapper: http://jsfiddle.net/thirtydot/hpZqK/6/
You need to do this because input is treated as inline-block.
Hacky fix - give the checkbox -2px of top: http://jsfiddle.net/hpZqK/4/

Bottom text align or menu

I have a menu the have rectangular boxes 90x50. Some have single line text, other have multiline text
question : How to VERTICALLY align it to the bottom with pure css no hack please
Vertical aligninment in CSS isn't that easy as you'd intuitively expect. As far the straightforward property vertical-align: bottom works in table cells only. Here's an excellent resource which explains how (not) to vertical align in CSS: Understanding vertical-align, or "How (Not) To Vertically Center Content".
In a nut: the following works in real webbrowsers:
display: table-cell;
vertical-align: bottom;
But thus not in MSIE. You'd like to make the parent element relative and wrap the text in an absolutely positioned element and then put it to bottom. Here's a copy'n'paste'n'runnable example.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<style>
li {
position: relative;
width: 90px;
height: 50px;
border: 1px solid black;
}
li span {
position: absolute;
bottom: 0;
}
</style>
</head>
<body>
<ul>
<li><span>text</span></li>
<li><span>text<br>multiline</span></li>
<li><span>text</span></li>
</ul>
</body>
</html>
I think the vertical-align property does what you want. Otherwise, perhaps you can clarify your problem further?
Edit: You can force table-cell-like behaviour for any other element by using the display property with the value 'table-cell'. I am not perfectly sure if this works with well with the vertical-align property, but perhaps you can build on it. If I remember correctly, an additional intermediate element was required.

Resources