如何避免我的固定垂直导航栏(其宽度是动态的)与页面的主内容重叠?

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

How do I avoid overlap of my fixed vertical navbar (its width being dynamic) with the page's main content?

问题

You can achieve this by using the CSS calc() function without variables. Here's the modified code for your situation:

main {
  width: calc(100% - 20%);
  padding: 1rem;
}

This will make the main element's width adjust dynamically to occupy the remaining space after allocating 20% for the nav element. This should help you avoid overlap without the need for JavaScript.

英文:

I have a fixed vertical <nav> element that has a dynamic width. I want it not to overlap with the <main> element no matter what, even if the screen width is small

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

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;style&gt;
* {
  box-sizing: border-box;
  margin: 0;
  scroll-behavior: smooth;
}

body {
  display: flex;
}

nav {
  position: fixed;
  display: flex;flex-direction: column;
  flex-wrap: wrap;
  width: 20%;
  min-width: max-content;
  height: 100%;
  padding: 1rem 0.5rem;
  background-color: #5382a1;
  font-family: Tahoma, sans-serif;
}

nav header {
  font-weight: bold;
  color: white;
}

nav ul {
  padding: 0;
}

nav li {
  display: block;
  padding: 0.4rem;
  border: 1px solid white; 
  margin: 0.2rem 0;
  text-align: center;
  border-radius: 3px;
  background-color: white;
  color: hsl(34, 94%, 54%);
}

nav li:hover {
    color: white; background-color: hsl(34, 94%, 54%);
}

nav a {
text-decoration: none;
color: inherit;
}

nav a:hover {
  color: white;
}

main {
  width: 70%;
  min-width: 200px;
  margin: 1rem 1rem 2rem 25%;
}

p {
  margin: 1rem 0;
}
&lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;nav id=&quot;navbar&quot;&gt;
      &lt;header&gt;Navbar&lt;/header&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;a class=&quot;nav-link&quot; href=&quot;#section-one&quot;&gt;Section One&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a class=&quot;nav-link&quot; href=&quot;#section-two&quot;&gt;Section Two&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a class=&quot;nav-link&quot; href=&quot;#section-three&quot;&gt;Section Three&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/nav&gt;
    &lt;main&gt;
&lt;section id=&quot;section-one&quot; class=&quot;main-section&quot;&gt;
        &lt;header&gt;
          &lt;h2&gt;Section One&lt;/h2&gt;
        &lt;/header&gt;
&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
&lt;/section&gt;
&lt;section id=&quot;section-two&quot; class=&quot;main-section&quot;&gt;
        &lt;header&gt;
          &lt;h2&gt;Section Two&lt;/h2&gt;
        &lt;/header&gt;
&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
&lt;/section&gt;
&lt;section id=&quot;section-three&quot;&gt;
        &lt;header&gt;
          &lt;h2&gt;Section Three&lt;/h2&gt;
        &lt;/header&gt;
&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
&lt;/section&gt;
&lt;/main&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

You don't see any overlap in the code snippet above, but that's because the width is big enough. If you shrink it roughly to the size of a smartphone, you can start to see the issue

You may notice that &lt;body&gt; has a display: flex property, but it doesn't help since the &lt;nav&gt; is fixed (so any flex behavior doesn't apply to it). However, it may give you a better idea of what I try to achieve here

I could hypothetically do something like this

main {
  width: calc(100% - nav.width);
  padding: 1rem;
}

but the problem is you can't reference variables in the calc() function

I also tried this

nav {
  width: 20vw;
}

main {
  width: 80vw;
}

What would you recommend to avoid overlap of a fixed dynamically-sized vertical navbar with the main content without the use of JS?

I came across this similar question, but none of the answers proved helpful

答案1

得分: 1

请添加一个父级div,并给它添加position: fixeddisplay: flex,然后从nav中移除position: fixed

另外,别忘了给main添加height: 100%overflow-y: scroll

英文:

Add parent div with position: fixed and display: flex and remove postion:fixed from nav.

also, don't forget to add height: 100% and overflow-y: scroll to main

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

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

* {
  box-sizing: border-box;
  margin: 0;
  scroll-behavior: smooth;
}

.page {
    position: fixed;
    display: flex;
    height: 100%;
    width: 100%;
}
nav {
  /* position: fixed; */
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  width: 20%;
  min-width: max-content;
  height: 100%;
  padding: 1rem 0.5rem;
  background-color: #5382a1;
  font-family: Tahoma, sans-serif;

  flex: 0 0 auto;
}

nav header {
  font-weight: bold;
  color: white;
}

nav ul {
  padding: 0;
}

nav li {
  display: block;
  padding: 0.4rem;
  border: 1px solid white;
  margin: 0.2rem 0;
  text-align: center;
  border-radius: 3px;
  background-color: white;
  color: hsl(34, 94%, 54%);
}

nav li:hover {
  color: white;
  background-color: hsl(34, 94%, 54%);
}

nav a {
  text-decoration: none;
  color: inherit;
}

nav a:hover {
  color: white;
}

main {
  /* position: fixed; */
  overflow-y: scroll;
  height: 100%;
  width: 70%;
  min-width: 200px;
  margin: 1rem 1rem 2rem 5%;
}

p {
  margin: 1rem 0;
}

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;

  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;div class=&quot;page&quot;&gt;
      &lt;nav id=&quot;navbar&quot;&gt;
        &lt;header&gt;Navbar&lt;/header&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;a class=&quot;nav-link&quot; href=&quot;#section-one&quot;&gt;Section One&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a class=&quot;nav-link&quot; href=&quot;#section-two&quot;&gt;Section Two&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a class=&quot;nav-link&quot; href=&quot;#section-three&quot;&gt;Section Three&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/nav&gt;
      &lt;main&gt;
        &lt;section id=&quot;section-one&quot; class=&quot;main-section&quot;&gt;
          &lt;header&gt;
            &lt;h2&gt;Section One&lt;/h2&gt;
          &lt;/header&gt;
          &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
        &lt;/section&gt;
        &lt;section id=&quot;section-two&quot; class=&quot;main-section&quot;&gt;
          &lt;header&gt;
            &lt;h2&gt;Section Two&lt;/h2&gt;
          &lt;/header&gt;
          &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
        &lt;/section&gt;
        &lt;section id=&quot;section-three&quot;&gt;
          &lt;header&gt;
            &lt;h2&gt;Section Three&lt;/h2&gt;
          &lt;/header&gt;
          &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
        &lt;/section&gt;
      &lt;/main&gt;
    &lt;/div&gt;
  &lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

Here is the jsfiddle link.

答案2

得分: -1

这种方法可以工作,但对于我来说有点太硬编码了。请注意这些更改:

nav {
  /* ... */
  width: 20vw;
  min-width: 110px; /* 这是硬编码的 */
  /* ... */
}
main {
  width: 80vw;
  min-width: 200px;
  padding: 0 1rem;
  margin: 1rem 1rem 2rem max(20vw, 110px); /* 这也是硬编码的 */
}
英文:

This kind of works, but it's a bit too hard-coded for my liking. Notice these changes

nav {
  /* ... */
  width: 20vw;
  min-width: 110px; /* this is hard-coded */
  /* ... */
}
main {
  width: 80vw;
  min-width: 200px;
  padding: 0 1rem;
  margin: 1rem 1rem 2rem max(20vw, 110px);
}

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

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;style&gt;
* {
  box-sizing: border-box;
  margin: 0;
  scroll-behavior: smooth;
}

body {
  display: flex;
}

nav {
  position: fixed;
  display: flex;flex-direction: column;
  flex-wrap: wrap;
  width: 20vw;
  min-width: 110px;
  height: 100%;
  padding: 1rem 0.5rem;
  background-color: #5382a1;
  font-family: Tahoma, sans-serif;
}

nav header {
  font-weight: bold;
  color: white;
}

nav ul {
  padding: 0;
}

nav li {
  display: block;
  padding: 0.4rem;
  border: 1px solid white; 
  margin: 0.2rem 0;
  text-align: center;
  border-radius: 3px;
  background-color: white;
  color: hsl(34, 94%, 54%);
}

nav li:hover {
    color: white; background-color: hsl(34, 94%, 54%);
}

nav a {
text-decoration: none;
color: inherit;
}

nav a:hover {
  color: white;
}

main {
  width: 80vw;
  min-width: 200px;
  padding: 0 1rem;
  margin: 1rem 1rem 2rem max(20vw, 110px);
}

p {
  margin: 1rem 0;
}
&lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;nav id=&quot;navbar&quot;&gt;
      &lt;header&gt;Navbar&lt;/header&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;a class=&quot;nav-link&quot; href=&quot;#section-one&quot;&gt;Section One&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a class=&quot;nav-link&quot; href=&quot;#section-two&quot;&gt;Section Two&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a class=&quot;nav-link&quot; href=&quot;#section-three&quot;&gt;Section Three&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/nav&gt;
    &lt;main&gt;
&lt;section id=&quot;section-one&quot; class=&quot;main-section&quot;&gt;
        &lt;header&gt;
          &lt;h2&gt;Section One&lt;/h2&gt;
        &lt;/header&gt;
&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
&lt;/section&gt;
&lt;section id=&quot;section-two&quot; class=&quot;main-section&quot;&gt;
        &lt;header&gt;
          &lt;h2&gt;Section Two&lt;/h2&gt;
        &lt;/header&gt;
&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
&lt;/section&gt;
&lt;section id=&quot;section-three&quot;&gt;
        &lt;header&gt;
          &lt;h2&gt;Section Three&lt;/h2&gt;
        &lt;/header&gt;
&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;
&lt;/section&gt;
&lt;/main&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月7日 18:32:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193382.html
匿名

发表评论

匿名网友

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

确定