【问题标题】:Tailwind: How can i overlap the header with hero section?Tailwind:如何将标题与英雄部分重叠?
【发布时间】:2021-09-05 14:29:06
【问题描述】:

我想将我的图像与透明标题重叠?如果没有 margin-top: -36px in section,我怎么能做到这一点?谢谢

<header class="bg-transparent  shadow-md sticky top-0 z-50">
   <div class="flex items-center justify-between">     
        <a href="#" class="border-2 border-red-500"><img class="h-8" src="/images/eco2.png" alt=""></a>
        <div class="border-2 border-red-500">
            <a class ="" href="#">Home</a>
            <a class ="" href="#">About Us</a>
            <a class ="" href="#">Contact Us</a>
            <a class ="" href="#">Projects</a>
        </div>
    </div>
    <div class="flex flex-col hidden">
        <a href="#" class="border-2 border-red-500">Home</a>
        <a href="#" class="border-2 border-red-500">About Us</a>
        <a href="#" class="border-2 border-red-500">Contact Us</a>
        <a href="#" class="border-2 border-red-500">Projects</a>
    </div>
</header>

<section class="h-screen  w-full bg-hero-pic" >
</section>

【问题讨论】:

  • 上图就是展示的那一张。我想要的是让英雄部分占据空白。我不知道为什么它有空格,即使标题的 bg 是透明的。希望你们能帮助我

标签: tailwind-css


【解决方案1】:

您可以使用位置fixed 代替sticky,fixed 将从流中删除 div 项,这将使下一个 div(您的图片)取而代之。

您可以添加w-full 以及您需要的所有其他样式。

&lt;header class="bg-transparent fixed shadow-md w-full z-50"&gt;

如果您不希望它具有粘性的行为,只需使用绝对值,absolute 也会将其从流程中删除,但主要区别 absolute 根据其父级更改位置并保留在那里,@987654328 @ 根据屏幕而不是父屏幕保持其位置。

【讨论】:

    【解决方案2】:

    试试这个,Tailwind Playground Link

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
            <meta
          name="viewport"
          content="minimum-scale=1, initial-scale=1, width=device-width"
        />
        <title>Tailwind Header by JsWizard</title>
        <!---
        You may need jQuery
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        --->
        <style>
              /*
          You may need this for responsive background
          header {
            background: url('bg-425.jpg');
          }
    
          @media only screen and (min-width:640px) {
            header {
                background: url('bg-640.jpg');
            }
          }
    
          @media only screen and (min-width:768px) {
            header {
                background: url('bg-768.jpg');
            }
          }
    
          @media only screen and (min-width:1024px) {
            header {
                background: url('bg-1024.jpg');
            }
          }
    
          @media only screen and (min-width:1025px) {
            header {
                background: url('bg-max.jpg');
            }
          } */
    
              header {
                background:url('https://images.unsplash.com/photo-1580587771525-78b9dba3b914?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1567&q=80');}
        </style>
      </head>
      <body>
        <nav id="nav" class="fixed inset-x-0 top-0 flex flex-row justify-between z-10 text-white bg-transparent">
          <div class="p-4">
            <div class="font-extrabold tracking-widest text-xl"><a href="#" class="transition duration-500 hover:text-indigo-500">Your Logo</a></div>
          </div>
    
          <!-- Nav Items Working on Tablet & Bigger Sceen -->
          <div class="p-4 hidden md:flex flex-row justify-between font-bold">
            <a id="hide-after-click" href="#home" class="mx-4 text-lg border-b-2 border-transparent hover:border-b-2 hover:border-indigo-300 transition duration-500">Home</a>
            <a href="#about" class="mx-4 text-lg border-b-2 border-transparent hover:border-b-2 hover:border-indigo-300 transition duration-500">About</a>
            <a href="#contactus" class="mx-4 text-lg border-b-2 border-transparent hover:border-b-2 hover:border-indigo-300 transition duration-500">Contact Us</a>
            <a href="#products" class="mx-4 text-lg border-b-2 border-transparent hover:border-b-2 hover:border-indigo-300 transition duration-500">Products</a>
          </div>
    
          <!-- Burger Nav Button on Mobile -->
          <div id="nav-open" class="p-4 md:hidden">
            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-menu">
              <line x1="3" y1="12" x2="21" y2="12"></line>
              <line x1="3" y1="6" x2="21" y2="6"></line>
              <line x1="3" y1="18" x2="21" y2="18"></line>
            </svg>
          </div>
        </nav>
    
        <!-- Opened Nav in Mobile, you can use javascript/jQuery -->
        <div id="nav-opened" class="fixed left-0 right-0 hidden bg-white mx-2 mt-16 rounded-br rounded-bl shadow z-10">
          <div class="p-2 divide-y divide-gray-600 flex flex-col">
            <a href="#home" class="p-2 font-semibold hover:text-indigo-700">Home</a>
            <a href="#about" class="p-2 font-semibold hover:text-indigo-700">About</a>
            <a href="#contactus" class="p-2 font-semibold hover:text-indigo-700">Contact Us</a>
            <a href="#products" class="p-2 font-semibold hover:text-indigo-700">Products</a>
          </div>
        </div>
    
        <header id="up" class="bg-fixed bg-no-repeat bg-center bg-cover h-screen relative">
          <!-- Overlay Background + Center Control -->
          <div class="h-screen bg-opacity-50 bg-black flex items-center justify-center" style="background:rgba(0,0,0,0.5);">
            <div class="mx-2 text-center">
              <h1 class="text-gray-100 font-extrabold text-4xl xs:text-5xl md:text-6xl">This is sample Header design</h1>
              <h2 class="text-gray-200 font-extrabold text-3xl xs:text-4xl md:text-5xl leading-tight">by JsWizard</h2>
              <div class="inline-flex">
                <button class="p-2 my-5 mx-2 bg-indigo-700 hover:bg-indigo-800 font-bold text-white rounded border-2 border-transparent hover:border-indigo-800 shadow-md transition duration-500 md:text-xl">Learn More</button>
              </div>
            </div>
          </div>
        </header>
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2021-07-15
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      相关资源
      最近更新 更多