I have a <div> element and I have added 15px margins for the border.
Please have a look at the html & css codes.
HTML
<div id="test">
<h1>Lorem ipsum</h1>
<p>Lorem ipsum dolor sit amet, mazim nemore postulant qui ut,</p>
</div>
CSS
#test{
width:800px;
margin-top:15px;
margin-right:15px;
margin-bottom:15px;
margin-left:15px;**
border:2px solid black;
-moz-box-shadow:2px 2px 1px 1px black;
-webkit-box-shadow:2px 2px 1px 1px black;
}
So according to my css stylesheet margins are set to 15px. (I could have used shorthand notation margin:15px). When this is run in the browser only top and left margins are set to 15px and obviously there are more than 15px margins from bottom and right. Why does this happen even when we have defined margins for right and bottom?
Is not it necessary to define margins for all the sides?
By default, your div sits at the top left of the viewport—or 15px from it, because of the margin on the top and on the left.
If you had another div to the right of this div and one below it, you'd see that the margin was 15px on all sides. At the moment, there is open space to the right and underneath the div, so the margin means nothing. It's like putting a margin on the Earth: it won't push it any further away from outer space. :-)
I'm not too sure what you are trying to achieve. But, here are a few questions? If the div is displaying block why do you need to set the width and right margin at the same time? What are these ** for? Basically if you remove those ** and the width you should be fine. Here's the resulting css...
#test{
margin-top:15px;
margin-right:15px;
margin-bottom:15px;
margin-left:15px;
border:2px solid black;
-moz-box-shadow:2px 2px 1px 1px black;
-webkit-box-shadow:2px 2px 1px 1px black;
}
And here is a JS Fiddler which is hopefully what you are trying to achieve
Your element is exactly where it should be when all margins are set like that. You might just be mixing up the terminology a bit, let me clarify.
(I apologize for the casual English, but I find this helps explain complex concepts better.)
Margins do not resize the element to fit the area around them. Nor do they resize the window. Rather, they let elements keep their default height/width and place a buffer around them, telling other elements "you can come this close but no closer!" Even the window obeys... it puts 15px distance between the top-left walls and the element. The other two sides (the bottom and right side) are clearly further away though, so this margin does not affect them at all.
If your goal is to have the element stretch out to fill the window, with a 15px buffer on all sides, then this CSS uses the method you need:
#test{
position: absolute;
left: 15px;
top: 15px;
right: 15px;
bottom: 15px;
border:2px solid black;
-moz-box-shadow:2px 2px 1px 1px black;
-webkit-box-shadow:2px 2px 1px 1px black;
}
If you want the window itself to size down to the element's size (plus the 15px buffer), you will need some Javascript coding instead, as CSS does not currently have that capability. Numerous tutorials exist on the internet if that's what you need.
From what I can tell, your element is exactly where it should be with margin:15px; all around. The "extra" space you are seeing is the rest of the body filling the browser window. I threw together a JS Fiddle using a second copy of your div and colors so you can see exactly what is going on.
The first <div> and the second both have 15px margins all the way around. This results in a 30px gap between them. If we were to define them both as display:inline-block; you would see a similar 30px gap between them horizontally.
Remember, the CSS box model uses margins to define the area directly outside the border of your element.
Related
I'm trying to decrease the distance between the text in the h1 element and the border to the right so it looks like a small vertical line which separates it from the following text.
This is what my current css looks like:
.test{
border-right: 2px solid black;
padding-right: 0px;
}
The right border still appears very far on the right although I thought through setting the padding to 0px at the right it should appear directly next to the text.
I guess this is a pretty dumb question, I am still a beginner!
Thanks in advance
h1 elements are display: block by default (with width: 100%), which means they stretch to the full width of their container.
If you want to have the element only be as wide as it needs to be, make it display: inline-block instead (and then use padding, as you've identified, to determine the distance between the end of the text and the right border):
.test{
border-right: 2px solid black;
padding-right: 0px;
display: inline-block;
}
<h1 class="test">This is a test</h1>
h1 elements are display:block by default, 100% width by default. Try changing width:300px or display:inline-block.
I would like my wordpress site to show the different excerpts of text in separate containers that seem to float on top of a textured background, as in the following website: http://www.sopcial.com/
I would also like this effect in my posts, so that the post itself is in one container and the sidebar with related posts is in a different container. Is that possible?
I tried using the code provided by jmoore009 (except I changed the width to 1000px), but somehow the height of the container changes (or it looks like that to me). Besides, adding this code brings up a container in the header and another one in the footer... How can I erase them?
The link is http://www.planificamostuviaje.com
Thank you
you can use box-shadow to give it the appearance that the container is sitting on top of the background:
<div class="container content-box"></div>
.content-box{
background: #FFF;
width: 200px;
margin: auto;
padding: 20px;
box-shadow: 0px 0px 7px rgba(0,0,0,.4);
border-radius: 3px;
-moz-box-sizing:border-box; //add to fix padding
-webkit-box-sizing:border-box;
box-sizing:border-box;
}
FIDDLE
I'm seeing a rendering issue for a 2px dotted border similar to CSS dotted border issue in adjacent columns in a table rendered as dash in Chrome but on desktop Safari and Chrome. I tried several widths and it happens in all of them
This is a sample:
the vertical line ending has the same issue but it's out of the picture.
Sample:
http://jsfiddle.net/bcdQQ/
This issue happens if the width is not divisible by the border-width.
This works:
http://jsfiddle.net/bcdQQ/5/ (i made it a little bit bigger, for better sight)
#prodpre {
border-bottom: #555 5px dotted;
height: 20px;
margin: 0px 0px 2px 0px;
padding-bottom: 10px;
width: 505px;
}
So, the only possibility to catch this issue, would be a javascript solution, which corrects the width of the div, so it is divisible by the border-width (cause it is dynamically in your example).
could you put it in a smaller container div with overflow hidden?
I have text and want to give the text a left and right border which vertically extends over the entire webpage. This is my html
<div id="vLine">
<h1>text</h1>
</div>
and this is my css
#vLine {
height: 100%;
width: 50px;
border-left: 3px solid black;
border-right: 3px solid black;
Now, even though I set my height to 100%, it still like a tiny 2px gap between the top of the line and the top of the webpage, it stretch over the entire page and connect till the top of the page. Why is this gap there? I am using the chromium browser, and i'm guessing this gap will vary depending on browsers but how do I get rid of this gap completely regardless of the browser?
Hm okay so what I tried way,
#vLine {
margin: -10px;
}
and it worked, turns out that gap which looked like just 2px was 10px but yea, it fixed it.
Here is my site
Currently still fiddling around with design layout and there is one problem I can't quite solve.
On my header I have a red border-bottom to separate it from the main content. Likewise, on the footer I have a red top-border to do the same. Is there a way of extending the borders beyond the div (and consequently outside of the wrapper, with overflow visible) without it conflicting with the other elements? I want to configure the layout so it has more of a grid-like design.
#wrap {
width: 1000px;
background-color: #ffffff;
text-align:left;
margin: 0 auto 0 auto;
border-left: 1px solid #000000;
border-right: 1px solid #000000; }
#head{
background-color: #ffffff;
width: 1000px;
height: 159px;
border-bottom: 1px solid #ff0000; }
kind of like this?
Yes. You can set a negative margin, causing the div to extend outside of its parent, like this. (I only did the header, but you get the idea)
The solution in the fiddle uses the annoying !important to get the styling right - you might want to reorganize your CSS to get around this and make your code more maintainable, see this post.