css div父边框相对定位

标签 css css-position

有一个父 div 和子 div。在子 div 元素中使用相对定位来放置其内容。父 div 元素边框未包围子 div 元素。尝试了溢出:父 div 元素中的隐藏和填充选项,但它不起作用。 http://jsfiddle.net/p5UMA/1/粘贴下面的代码

<html>
<head>
 <style>
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
        margin: 0;
        padding: 0;
        border: 0;
        font-size: 100%;
        font: inherit;
        vertical-align: baseline;
}

#parid{
color:#000000;
border: 3px solid #336c2b;
width: 960px;
margin: 0 auto;    
}
#childcon{
background-color: #000000;
color:#ffffff;
border: 5px solid #003748;
position:relative;
left:100px;
top:100px;
}

</style>
</head>

<body>
     <div id="parid">
        <div id="childcon">
                child contents
        </div>
     </div>



</body>
</html>

最佳答案

引用自dev.opera

The thing to remember about relative positioning is that it’s only the generated box that is shifted. The element still remains where it was in the static document flow. That’s where it “takes up space” as far as other elements are concerned. This means that the shifted box may end up overlapping other elements’ boxes, because they still act like the relatively positioned element has remained where it should be, before the positioning was applied.

使用 margin 代替

Demo

#childcon{
   background-color: #000000;
   color:#ffffff;
   border: 5px solid #003748;
   position:relative;
   margin-left:100px;
   margin-top:100px;
}

例如,增加子元素height会导致父元素height也增加,它只是移动了子元素,但文字元素 >位置保持在原来的位置...

Demo 2

关于css div父边框相对定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19970063/

相关文章:

html - 我怎样才能让下拉菜单向上展开而不是向下展开

html - 如何将我的图像保持在左侧,将我的内容保持在右侧?

asp.net - 页脚不会随着内容高度的增加而向下移动

html - Safari 和 Firefox 中的定位不同

html - CSS 定位问题 [img|span]

css - 如何收听 Bootstrap 动画的结尾

html - 从链接的一部分中删除下划线

html - 使用 CSS 将包含 div 的 div 向下推

javascript - 如何在屏幕中间有一个 float 菜单

css - 绝对定位忽略父级的填充