【问题标题】:Align div to left将 div 向左对齐
【发布时间】:2014-04-10 18:29:13
【问题描述】:

我正在尝试将 div 设置为左​​ 0,但我不能。这是我的代码

.bottom
{
width: 100%;
position: relative;
height: 100px;
background: #21241c;
float: left;
margin: 0;
}

我不想将我的 div 定位到绝对位置!

这是html

 <div class='top'></div>
 <div class='middle'></div>
 <div class='bottom'></div>

我希望底部 div 始终位于中间 div 之下,宽度为 100%。我尝试向左浮动和边距 0 但没有。我的底部 div 接缝位于屏幕的中心,在屏幕的右侧和左侧留下空白。 你能帮忙吗?

谢谢!

【问题讨论】:

  • 你试过float:left吗?
  • 剩下什么?需要查看您的 HTML
  • 相对于您想要定位 div 的位置?将 div 建立为这个元素的子元素(可能是body),你应该没问题。
  • 你试过了吗:clear: both
  • 我怀疑从父元素继承了 text-align:center; 属性。尝试在.bottom 上设置text-align:left; 或删除width:100%;

标签: css


【解决方案1】:

您只需使用以下代码:

.bottom
 {
  width: 100%;
  height: 100px;
  background: #21241c;
  float: left;
  margin: 0;
  }

【讨论】:

    【解决方案2】:

    尝试这样使用

    float:left; 
    margin:0;
    

    无需将位置设置为绝对位置。如果您使用margin:0 auto;,它会将您的 div 固定在中心,因此请使用边距为零。

    【讨论】:

      【解决方案3】:

      问题不是您的 .bottom div,而是父元素之一,大概是 body 元素。从body 中删除任何填充和/或边距:

      body {
          margin: 0;
          padding: 0;
      }
      

      【讨论】:

        【解决方案4】:

        您需要更正您的 Html。由于我们没有代码,我认为您的 div 只是在正文中?尝试:html,正文 { 宽度:100%; 高度:100%; 边距:0; }

        【讨论】:

          【解决方案5】:

          感谢您的宝贵时间,我找到了解决方案。只需在我的 css 中添加以下代码即可。

          html, body
          {
            margin: 0;
            padding: 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-05-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-22
            • 2017-07-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多