Why is my box changing sizes? - css

I have a div inside a div. The outer div's job is to position the box, while the inner div's job is to position the text. These divs are within a larger div, but I don't think that's the problem. When I try to put padding on the outer div, or in other words move the box, the padding is applied to the inner div and the box is thus getting bigger in that direction. The top-left hand corner is always stuck to the other div it is inside. How do I make it so that the padding is applied to the outside of the box instead of the inside?
Here is the formatting:
<div style="width:100px;
height:50px;
padding-left:10px;
padding-top:10px;
border: 3px solid #D8BFD8;
align:center;">
<div style="font-size:x-large;
padding-left:40px;
padding-top:0px;
font-family:'Arial';
color:black;">
Profile
</div>
</div>

Not too sure, but by moving the outer box are you sure you haven't mistaken padding with margin? Padding is applied to the inside of the div.
I just changed
padding-left:10px;
padding-top:10px;
to
margin-left:10px;
margin-top:10px;
and increased it to make it more obvious. Also moved the inline css to make it clearer.
http://jsfiddle.net/H334r/3/

1 - For readability, it's generally good practice to not mash a bunch of languages together, even though web dev requires it every now and then.
So separate the css into and throw it in the or use a css stylesheet.
2 - You'll want to have the outer div relative to the page. So in css, position: relative. And the inner div, you want to use an absolute position. So position: absolute.
I took the liberty to clean up code and threw it here in jsFiddle. http://jsfiddle.net/w7Ltp/1/
But if you want the throw it into a html page.
<style>
#outerbox{
width:100px;
height:50px;
padding-left:10px;
padding-top:10px;
border: 3px solid #D8BFD8;
align:center;
position: relative;
}
#innertext{
position: absolute;
font-size:x-large;
padding-left:10px;
padding-top:0px;
font-family:'Arial';
color:black;
}
</style>
<div id="outerbox">
<div id="innertext">
Profile
</div>
</div>

Padding is applied inside an element.
from W3Schools, The padding clears an area around the content (inside the border) of an element. The padding is affected by the background color of the element.
So if you are applying the padding to your outside div (div with 100px width), the elements inside it are the ones that get affected.
You might want to look at using margin instead. Or it would be better if you set the padding to the parent of the outer div; With that, all elements inside the parent of the outer div will be uniformly spaced.
I see that you have "align: center" for your outer div. Try using "margin: auto".

Related

CSS display but keep row height

I am using a table to display some data and for one row, I am displaying a 35-character preview of a (possibly) longer string. When the user does a mouse-over on one of the previews, it displays the full text next to it.
The problem is when the full text is displayed, the tr height is changed to fit the full text, but I want it to stay the same size.
<td><%=shortPreview%>
<br/><div class="details" style="display:none;" id="<%=ID%>"><%=rsTickets("details")%></div></td>
CSS
.details {
position:relative;
top:-15px;
left:260px;
background-color:#FFFFAA;
z-index:1;
padding-top:5px;
padding-bottom:5px;
padding-right:15px;
padding-left:5px;
border:1px;
border-style:solid;
border-color:#CCCC99;
-moz-border-radius: 1em 2em 2em 1em;
border-radius: 1em 2em 2em 1em;
}
Put a fixed size div inside your cell and set it to position: relative. Inside that div put your details one and set it to position: absolute;. This should work for 99%.
Thing is that when you set an object to be relative, then it is relative to the objects around it. This means that if you change the height of the div (or in this case, shows it), the surrounding objects will adapt to this change. So when you set the div to be visible the td, which encapsulates it, will change it's height as well.
Setting the div to an absolute position solves the problem, but then it might get difficult to position it right. Unless you put it in another div. If you put an absolute div within a relative div, then the absolute div will be absolute to the relavite div, thus solving the problem. Here's an example:
<table>
<tr><td><div style=position:relative>
This div will not affect the td more than will this text.
<div style=position:absolute;top10px;>
This div will not affect the td, since it's absolute, and will be
10px from the top of the outer div
</div>
</div></td></tr>
</table>
This should do the trick. Works for me at least.

CSS text stroke outside of letters

Okay, been struggling with this for a bit now and I have pretty much the appearance I want but am now struggling with positioning the items. Basically I want a stroked text with the stroke on the outside, meaning the webkit text stroke is useless.
So I figured I'll position two text elements on top of each other and do it that way. And that works great, except since I am using position:absolute the element essentially has no height.
The HTML looks like this:
<div class="hcontainer"
<h2>A Framework For Web Artisans</h2>
<span class="h2white">A Framework For Web Artisans</span>
</div>
The CSS like this:
h2{font-size:2em;
margin: 10px 0;
color:#234F70;
-webkit-text-stroke: 10px #531A16;
-webkit-text-fill-color:#FFF;
letter-spacing:-2px;
position:absolute;
top:10px;
left:0px;}
.h2white{font-family:dom_bold,arial black;
font-size:2em;
margin: 10px 0;
color:#FFF;
position:relative;
top:10px; left:0px;
letter-spacing:-2px;
position:absolute;}
.hcontainer{position:relative;clear:both;height:2em;}
So here's the issue. The hcontainer needs to have a set height because the element it contains is positioned absolutely therefore has no height and messes up the flow. The problem is making that height dynamic so I can space the elements properly.
I could make a separate container for each heading but that just seems a bit much. Can anyone think of a better way to do what I'm trying to do here? Or a way around the height issue?
http://jsfiddle.net/calder12/9M7YZ/
I don't really understand what it means that "The problem is making that height dynamic so I can space the elements properly." But if you want to not have to declare a height on .hcontainer, you can use a negative top margin on .h2white to place it on top of the red h2 instead of using absolute positioning. Like so:
http://jsfiddle.net/9M7YZ/10/
.h2white{
font-family:lemon;
font-weight:bold;
font-size:4em;
color:#FFF;
letter-spacing:-2px;
margin-top:-86px;
position:absolute;
}

Element won't move

I have an element in a div that I'm having some problems giving a margin-top.
Instead of moving the element in the div, I can only get it to move the entire div.
It's the purple circle that I want to give a margin-top.
http://jsfiddle.net/9J8R5/
#step1 {
width:100px;
height:100px;
border-radius:50px;
background-color:#5020B8;
left:0;
right:0;
margin-left:auto;
margin-right:auto;
top:1em;
font-size:60px;
color:#ffffff;
font-family:Cusmyrb;
line-height:105px;
text-align:center;
padding:0;
}
The margin-top doesn't appear on the child element the way you expect because of margin collapse.
If there is no border, padding, inline content, or clearance to separate the margin-top of a block with the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block with the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.
Source: https://developer.mozilla.org/en-US/docs/CSS/margin_collapsing
If you really want the margin-top to exist on the child element, either of these additions will do it for you:
#competeinfo {
border: 1px solid transparent;
}
http://jsfiddle.net/9J8R5/5/
Or
#competeinfo {
overflow: hidden;
}
http://jsfiddle.net/9J8R5/6/
Otherwise, padding on the parent element is probably the best choice.
For how the CSS box model works, check the W3C
To fix your problem you have two options:
use margin, as you mentioned, but add some content before the circle. E.g. jsFiddle
use padding-top on #completeinfo, instead of margin-top. E.g. jsFiddle

margin-top:100%; goes far outside of containing element

Please check this fiddle http://jsfiddle.net/JH4Ew/1/. I want align <p class="email"> to the bottom. I thought if I put
margin-top:100%;
it means 100% from height of the parent element. On the fiddle seems more then 100% of height. How to do this in right way?
PS I have updated the fiddle to right content, previous wasn't updated
Add the following CSS to align the paragraph to the bottom of the div:
#opening4 {
position:relative;
border: 1px solid #999;
}
.email{
position:absolute;
bottom:0;
}​
jsFiddle example
By positioning the div relatively it allows you to set the position on the email paragraph absolutely, and by setting the bottom to zero, it will remain at the bottom of the parent.
Remove position: relative from the parent if you want it to be at the bottom of the window:
http://jsfiddle.net/2VyCj/1/
Margin is distance from sibling elements, not the distance from the inside of the parent.

Bizzare behavior with CSS margins

This page I have is super simple, this should be a breeze but I'm stumped.
I have two DIVs, one inside the other. In the first DIV, I have the margins set so that it lays at the top of the page, centered. The second DIV should lay inside the first, centered, but with a 50px margin at top. However, the 50px margin is being applied to the parent DIV and not the child. If I add a border to the parent DIV, it behaves like I expect it to, but not without.
Can anyone offer me any insight to this? Thanks in advance.
<div id="pageWrapper">
<div id="mainWrapper">
<p>foo</p>
</div>
</div>
*{
margin:0px;
padding:0px;
}
body{
background-color:#034375;
}
#pageWrapper{
width:960px;
margin:0px auto 0px auto;
background:url('i/blue-gradient.jpg') top left no-repeat;
}
#mainWrapper{
width:500px;
margin:50px auto 0 auto;
border:1px solid #000000;
background-color:#eeeeee;
}
This issue has to do with the CSS spec on rendering adjacent margins. Essentially, because there's nothing "in between" the margins of the containing div and the margins on the inner div, the larger value is used for both.
You'll see this mainly in Firefox, and although the behavior seems to follow the letter of the law, I'm not sure this particular case behaves as intended by the spec writers.
Fortunately, it's easy to fix -- put something "between" the margins. You've already noticed that putting a border on the parent div works. You can make this border transparent, and reduce the inner margin by 1px, and it will appear functionally the same as your above case. Another option is to apply one pixel of padding-top to the parent div. A third option is to use padding-top: 50px on the parent div instead of applying a top margin to the child div.
More information on collapsing margins.
You don't say which browser you're seeing this in. For me it works as expected in Firefox. However, I suspect you're seeing the issue in Internet Explorer. This is probably because the inner div doesn't have hasLayout applied - this is usually the cause of IE styling bugs. Try adding zoom:1 to the mainWrapper CSS declaration and see if that works.
You probably want to set the padding of mainWrapper instead of margin.
padding:50px 0 0 0;
Check out this description of the box model to see how margins and padding differ.

Resources