【问题标题】:how to move a section below another using margin property in css如何使用css中的边距属性将一个部分移动到另一个下方
【发布时间】:2020-11-29 05:59:13
【问题描述】:

我有一个 html 页面,主页中有很多部分,我有两个部分,如下所示:

<div class='first section'>
  <a href=''>
    <button style='background: linear-gradient(to bottom, #cc99ff 0%, #ff99cc 100%);'>

          <h3> HMT Rice</h3>
          </button>
  </a>
</div>

<div class='second section'>
  <a href=''>
    <button style='background: linear-gradient(to bottom, #cc99ff 0%, #ff99cc 100%);'>

          <h3> PKS Rice</h3>
          </button>
  </a>
</div>

我使用 margin-top 属性将第一部分降低了一点,但它只是在上面创建了一个空白区域。

我只想使用 css 使第一部分低于第二部分,如下图所示:

无论如何使用z-index或其他东西将第一部分直接带到下面。请帮忙。提前致谢

【问题讨论】:

  • 使用 padding-top 代替边距。我以前见过这种问题。
  • @MurtazaJAFARI 它还在上方创建空白区域,并将所有元素向下移动,就像顶部边距一样
  • 请添加minimal reproducible example 和预期输出

标签: javascript html css margin


【解决方案1】:

您可以尝试使用以下 HTML 和 CSS。

.section{
    position: relative;
}
.second_section{
    margin-top: 0px;
}
.first_section{
   position: absolute;
   margin-top: 70px; /* Adjust this margin top pixed according to the space you needed. */
}
<div class="section">
    <div class="first_section">
      <a href=''>
        <button style='background: linear-gradient(to bottom, #cc99ff 0%, #ff99cc 100%);'>

          <h3> HMT Rice</h3>
          </button>
      </a>
    </div>

    <div class="second_section">
      <a href=''>
        <button style='background: linear-gradient(to bottom, #cc99ff 0%, #ff99cc 100%);'>

          <h3> PKS Rice</h3>
          </button>
      </a>
    </div>
</div>

我已通过检查元素解决了您网站中的问题。请遵循以下代码。

您应该用 slide-button-wrapper div 包装按钮 div 和滑块 div 并添加 css。

<div class="slide-button-wrapper">            
    <div class="mainsme" id="grid">

      <h2 style="font-size:28px;">Shop by Category</h2>
      <div class="gridmine">
         <!---buttons will goes here-->
      </div>

    </div>
                     
    <div class="main-row slider-section" style="order: 1;">
        <banner codes are here>
    </div>
</div>


@media only screen and (max-width: 600px) {
        .slide-button-wrapper {
            display: flex;
            flex-direction: column;
        }
        
        .mainsme{
          order: 2;
        }
        .slider-section{
          order: 1
        }`
    }

【讨论】:

  • 这不起作用兄弟,这仍然在该部分中给出一个空白并删除所有部分
  • 你用
    包裹了 first_section 和 second_section div 吗?
  • 是的,兄弟,其实网站是用opencart建的,后台没办法编辑代码,我用编辑器放了一个HTML部分,我想重新排列一个部分下面的部分跨度>
  • 我有使用 opencart 的经验。可以分享一下你的网址吗?
  • 请给出两个按钮所在的页面网址
【解决方案2】:

您可以使用order property重新排列元素

注意父必须是display: flex;

.container {
  display: flex;
  flex-direction: column;
}

.container div {
  width: 100px;
  height: 50px;
  text-align: center;
}

.first {
  background-color: lightgreen;
  order: 2;
}

.second {
  order: 1;
  background-color: lightblue;
}
<div class="container">
  <div class="first">first</div>
  <div class="second">second</div>
</div>

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签