浮动面板左右CSS

3
我需要在地图上放置两个浮动面板,但是 float: right 不起作用,请帮忙。
我尝试了所有的方法,但似乎 position: absolute 禁用了 float: right 或其他什么东西。
有没有办法将第二个面板(#floating-panel2)对齐到右侧,而不改变第一个面板(#floating-panel1)的对齐方式?
我需要的是这样的布局: enter image description here 这是我的HTML和CSS:

    #wrapper { position: relative; }

    #floating-panel1 {
      position: absolute;
      width: 265px;
      top: 55px;
      left: 5px;
      z-index: 5000;
      background-color: rgb(66, 72, 79);
      padding: 5px;
      border: 1px solid rgb(66, 72, 79);
      border-radius: 1px;
    }
    
    #floating-panel2 {
      position: absolute;
      width: 265px;
      top: 55px;
      left: 0px;
      z-index: 5000;
      background-color: rgb(66, 72, 79);
      padding: 5px;
      border: 1px solid rgb(66, 72, 79);
      border-radius: 1px;
      float:right;
    }
<div id="wrapper" style="height: 100vh">
      <div id="floating-panel1">
        <h1>PANEL 1</h1>
      </div>
      <div id="floating-panel2">
        <h1>PANEL 2</h1>
      </div>
    </div>

请帮忙


你可能需要在绝对定位和浮动之间做出选择。在 #floating-panel 中,尝试去掉 float: right 和 left: 0px,并添加 right: 5px(使用绝对定位将其放置到右侧)。 - chaslewis
2个回答

5

这是您在寻找的内容吗?如果是,您只需在第二个面板中将left: 5px更改为right: 5px即可。

#wrapper { position: relative; }

    #floating-panel1 {
      position: absolute;
      width: 265px;
      top: 55px;
      left: 5px;
      z-index: 5000;
      background-color: rgb(66, 72, 79);
      padding: 5px;
      border: 1px solid rgb(66, 72, 79);
      border-radius: 1px;
    }
    
    #floating-panel2 {
      position: absolute;
      width: 265px;
      top: 55px;
      right: 5px;
      z-index: 5000;
      background-color: rgb(66, 72, 79);
      padding: 5px;
      border: 1px solid rgb(66, 72, 79);
      border-radius: 1px;
    }
<div id="wrapper" style="height: 100vh">
      <div id="floating-panel1">
        <h1>PANEL 1</h1>
      </div>
      <div id="floating-panel2">
        <h1>PANEL 2</h1>
      </div>
    </div>


0

使用float:left和float:right显示div,并将div1的位置设置为position:absolute,div2的位置设置为position:relative。

   #wrapper { position: relative; }

#floating-panel1 { 
  width: 265px;
  top: 55px; 
  float:left;
  position:absolute;
  z-index: 5000;
  background-color: rgb(66, 72, 79);
  padding: 5px;
  border: 1px solid rgb(66, 72, 79);
  border-radius: 1px;
}

#floating-panel2 { 
  width: 265px;
  top: 55px; 
  float:right;
  position:relative;
  z-index: 5000;
  background-color: rgb(66, 72, 79);
  padding: 5px;
  border: 1px solid rgb(66, 72, 79);
  border-radius: 1px;
  float:right;
}
<div id="wrapper" style="height: 100vh">
      <div id="floating-panel1">
        <h1>PANEL 1</h1>
      </div>
      <div id="floating-panel2">
        <h1>PANEL 2</h1>
      </div>
    </div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接