html - 并排div w/负相对位置

标签 html css css-float

我正在尝试在包装器中创建两个并排的 div。第一个 div,#content,是一个 position:relative div。

#wrapper {
background-image: url(assets/images/BG2.gif);
margin-right: auto;
margin-left: auto;
width: 996px;
overflow: auto;
}

#content {
position: relative;
top: 10px;
left: 10px;
width: 745px;
background-color: red;
}

#important {
float: right;
position: relative;
top: -1120px;
width: 231px;
margin-right: 10px;
background-color: green;
}

问题是第二个 div #important 显示在第一个 div 的下面。 div 和填充/边距都有足够的空间。我可以通过将它 float 到右侧并使用负顶部位置来使其工作并且它显示正常,但我觉得好像有更好/正确的方法来做到这一点。

HTML:

<div id="wrapper">
  <div id="content">
    <img src="assets/photos/bid day 046.jpg" alt="Bid Day" width="745" height="311" />
      <div id="fraternity">
      <p align="center"><span class="style5"><strong>TITLE</strong><br />
      Subtitle<br />
      Sub-subtitle</span></p>
      <p align="justify">depry derp</p>
      <p align="justify">derp derp derp</p>
      <p class="style5" align="center"><br/><strong>Title<br/>
      Twitter Feed</strong></p>   
      <div id="twitter">
        <script src="http://widgets.twimg.com/j/2/widget.js"></script>
        <script>...</script>
      </div>
    </div>
    <div class="fb-like-box" data-href="something" data-width="292" data-colorscheme="dark" data-show-faces="false" data-stream="true" data-header="false">
    </div> 
  </div>
  <div id="important">
    <p align="center"><strong>IMPORTANT INFORMATION</strong><br />Derp!</p>
    <img class="divider" src="assets/images/hr.gif" alt="HR" width="183" height="15" />
    <p align="center"><strong>Achievements<br /></p>
    <img class="divider" src="assets/images/hr.gif" alt="HR" width="183" height="15" />
    <p align="center" class="style11"><strong>UPCOMING  EVENTS<br /></strong>stuff<br /></p>
    <p align="center"><br /> <strong>more stuff</strong><br /></p>
  </div>
</div>

最佳答案

使#wrapper的位置为相对绝对

然后将内部 div 位置设置为 absolute

jsfiddle 示例:http://jsfiddle.net/kRHTj/

关于html - 并排div w/负相对位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11441943/

相关文章:

javascript - What happens to an HTML5 web worker thread when the tab is closed while it's running?

html - 将锚定图像放置在电子邮件模板的表格背景之上

php - 如何从函数内部回显有关每个项目的数据?

css - 以大众单位定义的字体大小在桌面上显得太大

html - 将输入文本的光标向右移动

html - 将 mvc 存储库获取的数据定位到 View 中(MVC4 Web)

css - 如何用骨架制作 100% 高度的柱子?

html - CSS 顶栏 : vertical separator wraps the content of the bar

javascript - 如何垂直排列 float 元素

css - 如何将控件 float 在图像顶部(使用图像背景)