导航栏的一部分在屏幕变小时为什么会消失

huangapple go评论79阅读模式
英文:

Why does a part of the navigation bar disappear when the screen gets smaller

问题

I created a nav bar in React and CSS with the following code:

<header className="home-header">
  <div className="auth-buttons">
    <input type="button" className="auth-button sign-up-button" value="Sign Up" />
    <input type="button" className="auth-button login-button" value="Login" />
  </div>
  <div className="menus-container">
    My Site
    <span></span>
    <span></span>
    <span></span>
  </div>
</header>

and

.home-header {
  position: sticky;
  padding: 1.5rem;
  margin-top: 0;
  min-width: 100vh;
  background-color: #1441aa;
  border-bottom-right-radius: 30px;
  box-shadow: 0 3px 10px rgb(0 0 0 / 0.2);
  display: flex;
  flex-direction: row;
  justify-content: space between;
}

.home-header .auth-buttons {
  display: flex;
  flex-direction: row;
  gap: 3px;
}

.home-header .auth-buttons .auth-button {
  padding: 0.5rem 1.5rem;
  border: none;
}

.home-header .auth-buttons .auth-button:hover {
  cursor: pointer;
}

.home-header .auth-buttons .sign-up-button {
  border-bottom-left-radius: 1rem;
}

.home-header .menus-container {
  color: #fff;
  font-size: 1.5rem;
  font-weight: 400;
}

.home-header .auth-buttons .login-button {
  border-top-right-radius: 1rem;
}

But when I make the screen smaller, the right part of the navbar disappears. I want the nav bar to be responsive and get smaller as the screen gets smaller, not disappear. Could you help me?

英文:

I created a nav bar in React and CSS with the following code:

&lt;header className=&quot;home-header&quot;&gt;
      &lt;div className=&quot;auth-buttons&quot;&gt;
         &lt;input type=&quot;button&quot; className=&quot;auth-button sign-up-button&quot; value=&quot;Sign Up&quot; /&gt;
         &lt;input type=&quot;button&quot; className=&quot;auth-button login-button&quot; value=&quot;Login&quot; /&gt;
     &lt;/div&gt;
     &lt;div className=&quot;menus-container&quot;&gt;
         My Site
         &lt;span&gt;&lt;/span&gt;
         &lt;span&gt;&lt;/span&gt;
         &lt;span&gt;&lt;/span&gt;
    &lt;/div&gt;
&lt;/header&gt;

and

.home-header {
    position: sticky;
    padding: 1.5rem;
    margin-top: 0;
    min-width: 100vh;
  
    background-color: #1441aa;
    border-bottom-right-radius: 30px;
    box-shadow: 0 3px 10px rgb(0 0 0 / 0.2);
  
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }

  .home-header .auth-buttons {
    display: flex;
    flex-direction: row;
    gap: 3px;
    /* margin-left: 10%; */
  }

  .home-header .auth-buttons .auth-button {
    padding: 0.5rem 1.5rem;
    border: none;
  }

  .home-header .auth-buttons .auth-button:hover {
    cursor: pointer;
  }

  .home-header .auth-buttons .sign-up-button {
     border-bottom-left-radius: 1rem;
  }

  .home-header .menus-container {
    /* margin-left: 60%; */
    color: #fff;
    font-size: 1.5rem;
    font-weight: 400;
  }

  .home-header .auth-buttons .login-button {
    border-top-right-radius: 1rem;
  }

But when I make the screen smaller, the right part of the navbar disappears, as shown below:

Full Screen:

导航栏的一部分在屏幕变小时为什么会消失

Smaller Screen:

导航栏的一部分在屏幕变小时为什么会消失

I want the nav bar to be responsive and get smaller as the screen gets smaller, not disappear. Could you help me?

答案1

得分: 1

以下是翻译好的内容:

因为您设置了min-width: 100vh;,它指的是窗口的高度。您需要将其更改为vw(视口宽度)或使用百分比(%)。

.home-header {
    position: sticky;
    padding: 1.5rem;
    margin-top: 0;
    min-width: 100%; // 不同的是这个值
    
    background-color: #1441aa;
    border-bottom-right-radius: 30px;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
    
    display: flex;
    flex-direction: row;
    justify-content: space-between;
}
<header class="home-header">
    <div class="auth-buttons">
        <input type="button" class="auth-button sign-up-button" value="Sign Up" />
        <input type="button" class="auth-button login-button" value="Login" />
    </div>
    <div class="menus-container">
        My Site
        <span></span>
        <span></span>
        <span></span>
    </div>
</header>
英文:

It's because u set min-width: 100vh; which referts to the window height. You need to change that to vw (viewport width) or use %.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.home-header {
    position: sticky;
    padding: 1.5rem;
    margin-top: 0;
    min-width: 100%; // the difference is this value
  
    background-color: #1441aa;
    border-bottom-right-radius: 30px;
    box-shadow: 0 3px 10px rgb(0 0 0 / 0.2);
  
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }

  .home-header .auth-buttons {
    display: flex;
    flex-direction: row;
    gap: 3px;
    /* margin-left: 10%; */
  }

  .home-header .auth-buttons .auth-button {
    padding: 0.5rem 1.5rem;
    border: none;
  }

  .home-header .auth-buttons .auth-button:hover {
    cursor: pointer;
  }

  .home-header .auth-buttons .sign-up-button {
     border-bottom-left-radius: 1rem;
  }

  .home-header .menus-container {
    /* margin-left: 60%; */
    color: #fff;
    font-size: 1.5rem;
    font-weight: 400;
  }

  .home-header .auth-buttons .login-button {
    border-top-right-radius: 1rem;
  }

<!-- language: lang-html -->

&lt;header class=&quot;home-header&quot;&gt;
      &lt;div class=&quot;auth-buttons&quot;&gt;
         &lt;input type=&quot;button&quot; class=&quot;auth-button sign-up-button&quot; value=&quot;Sign Up&quot; /&gt;
         &lt;input type=&quot;button&quot; class=&quot;auth-button login-button&quot; value=&quot;Login&quot; /&gt;
     &lt;/div&gt;
     &lt;div class=&quot;menus-container&quot;&gt;
         My Site
         &lt;span&gt;&lt;/span&gt;
         &lt;span&gt;&lt;/span&gt;
         &lt;span&gt;&lt;/span&gt;
    &lt;/div&gt;
&lt;/header&gt;

<!-- end snippet -->

答案2

得分: 1

.home-header的CSS中,min-width是使用vh单位而不是vw单位进行大小调整,这意味着它将根据屏幕的高度而不是宽度进行缩放。

min-width: 100vh;更改为min-width: 100vw;

此外,由于内边距的原因,您的页眉可能会在屏幕右侧被裁切。添加规则 box-sizing: border-box; 以确保它被正确调整大小。

英文:

In the CSS for .home-header, min-width is sized with the vh unit rather than vw, meaning it will scale according to the height of the screen rather than the width.

Change min-width: 100vh; to min-width: 100vw;.

Additionally, your header may get cut off the right side of screen due to its padding. Add the rule box-sizing: border-box; so that it will be sized correctly.

huangapple
  • 本文由 发表于 2023年4月6日 19:21:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75948930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定