【问题标题】:Replicate html positioning without using CSS float不使用 CSS 浮动复制 html 定位
【发布时间】:2020-03-18 01:40:34
【问题描述】:

我正在尝试复制使用float 属性来对齐其内容的Float ExampleGrid and Flex Solution。我想在任何地方替换 float 属性,并且除了包含 <aside><div> 框的 <section> 之外,我已经能够正确对齐所有内容。这是它应该看起来的样子:

* {
  box-sizing: border-box;
  text-align: center;
}

section {
  padding: 20px;
  margin-bottom: 20px;
  background: #FFA500;
}

#full {
  width: 100%;
  overflow: auto;
  clear: both;
  color: #fff;
  background: #394549;
}

#division {
  padding: 20px;
  margin-top: 20px;
  background: #214fb3;
}

aside {
  float: left;
  width: 33%;
  height: 200px;
  padding: 20px;
  margin-left: 30px;
  background: #a6b747;
  border: 1px solid #eee;
}
 <section id="full">
    <aside>
      &lt;aside&gt; #a6b747
    </aside>
    &lt;section&gt; #394549
    <div id="division">
      &lt;div&gt; #6ea3da
    </div>
  </section>

这就是我目前在&lt;aside&gt; 框上使用的position: absolute;

* {
  text-align: center;
  font-size: 24px;
  line-height: 32px;
}

#full {
  grid-area: full;
  background: #394549;
  color: #fff;
  height: 200px;
  padding: 20px;  
}

aside {
  position: absolute;
  height: 160px;
  width: 33%;
  padding: 20px;
  margin-left: 30px;  
  background: #a6b747;
  border: 1px solid #eee;
}

#division {
  width: 100%;
  padding: 20px;
  background: #214fb3;
}
<section id="full">
  <aside>
    &lt;aside&gt; #a6b747
  </aside>
  &lt;section&gt; #394549
  <div id="division">
    &lt;div&gt; #6ea3da
  </div>
</section>

我不得不弄乱像素以获得正确的高度,但我不确定如何修复 &lt;div&gt;&lt;aside&gt; 框的宽度。即使在调整窗口大小时,它们也应该与第一个示例保持一致,而且我也不确定如何对齐 &lt;section&gt;&lt;div&gt; 文本。

【问题讨论】:

  • 请提供您的 HTML + CSS 代码,谢谢
  • 我已将 html 和 css 的 sn-ps 添加到问题中。整个代码都在我一开始提到的代码笔中。不知道为什么我被否决了。
  • 不确定,这通常是因为他们没有足够的信息来解决您的问题。注意:我没有对你投反对票。感谢您提供代码。

标签: html css flexbox grid css-float


【解决方案1】:

因此,通过在侧面使用绝对定位,您需要在左侧为其余内容添加合适的填充,使其位于右侧空间的中心。

但是,由于您将 div 放在一边,因此您不能直接将那个大的填充放在该部分上,否则它会将里面的所有内容推到右侧。这意味着内容&lt;section&gt; #394549 必须放在自己的容器中。我在新容器中添加了 margin-bottom 以将蓝色框向下推一点。

我添加到左侧的内边距使用calc(33% + 64px); 来说明左侧的边距并在右侧空间中很好地居中。

我还在所有内容中添加了box-sizing: border-box;,因为它可以更轻松地计算宽度并导致基于百分比的大小调整行为更合理。您可以改用content-box(默认值),但您必须根据需要调整尺寸。您的原始尺寸使用了border-box 推理,所以我认为这是使您的尺寸表现适当的明智选择。

/* added box-sizing: border-box */
* {
  text-align: center;
  font-size: 24px;
  line-height: 32px;
  box-sizing: border-box;
}

#full {
  grid-area: full;
  background: #394549;
  color: #fff;
  height: 200px;
  padding: 20px;  
}

aside {
  position: absolute;
  height: 160px;
  width: 33%;
  padding: 20px;
  margin-left: 30px;  
  background: #a6b747;
  border: 1px solid #eee;
}

/* Added this container so we can put padding
   on content without effecting the blue box.
   also added margin-bottom to put a little
   space between this and the blue box */
#section-content {
  padding: 0 20px 0 calc(33% + 64px);
  margin-bottom: 10px;
}

/* added padding on the left */
#division {
  width: 100%;
  padding: 20px 20px 20px calc(33% + 64px);
  background: #214fb3;
}
<section id="full">
  <aside>
    &lt;aside&gt; #a6b747
  </aside>
  <!--added additional container #section-content-->
  <div id="section-content">
    &lt;section&gt; #394549
  </div>
  <div id="division">
    &lt;div&gt; #6ea3da
  </div>
</section>

【讨论】:

    猜你喜欢
    • 2011-03-14
    • 1970-01-01
    • 2010-12-08
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多