不应该自动边距将元素推至可用空白填充的最远位置吗?

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

Shouldn't a margin of auto push an element to the farthest it can go by filling in available whitespace?

问题

以下是您要翻译的内容:

"I'm learning flexbox, and I am trying to create a common layout that is found with dashboard kind of templates. These kinds of templates contain a main navigation menu on the left (the sidebar), a top navigation menu for things like user notifications or site themes, and a main content area below the top navigation menu.

The problem that I am facing is that one of my nav list items 'B' (it has a white border around it for visibility) is for some unknown reason not being pushed to the bottom of the 'purple' div.

I set the height to both the nav and the ul elements to have a height of 100% so that they take up as much vertical space as the body does. So from my understanding adding a margin-top: auto should in fact push B all the way to the bottom.

Is there a fundamental issue with my understanding? And am I on the right track to implementing this kind of theme correctly?

Thank you for your time and for sharing your knowledge!"

请注意,这段文本中包含一些英文专业术语和代码,我将其翻译为中文以方便您的理解。

英文:

Image of my current layout

I'm learning flexbox, and I am trying to create a common layout that is found with dashboard kind of templates. These kinds of templates contain a main navigation menu on the left (the sidebar), a top navigation menu for things like user notifications or site themes, and a main content area below the top navigation menu.

The problem that I am facing is that one of my nav list items "B" (it has a white border around it for visibility) is for some unknown reason not being pushed to the bottom of the "purple" div.

I set the height to both the nav and the ul elements to have a height of 100% so that they take up as much vertical space as the body does. So from my understanding adding a margin-top: auto should in fact push B all the way to the bottom.

Is there a fundamental issue with my understanding? And am I on the right track to implementing this kind of theme correctly?

Thank you for your time and for sharing your knowledge!

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

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

* {
  margin: 0;
  padding: 0;
  /* box-sizing: border-box; */
  /* border: 1px solid black; */
}

body {
  display: flex;
  height: 100vh;
}

#left {
  background-color: aqua;
  width: 5rem;
}

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

.top-left,
.top-right {
  padding: 1rem;
  background-color: aliceblue;
}

nav {
  background-color: red;
  height: 100%;
}

ul {
  background-color: blueviolet;
  height: 100%;
}

nav ul li:last-child {
  margin-top: auto;
  border: 1px solid white;
}

#right {
  background-color: orange;
  flex-grow: 1;
}

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;link href=&quot;./project.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;div id=&quot;left&quot; class=&quot;column&quot;&gt;
    &lt;div class=&quot;top-left&quot;&gt;
      Logo
    &lt;/div&gt;
    &lt;nav&gt;
      &lt;ul&gt;
        &lt;li&gt;A&lt;/li&gt;
        &lt;li&gt;B&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/nav&gt;
  &lt;/div&gt;

  &lt;div id=&quot;right&quot; class=&quot;column&quot;&gt;
    &lt;div class=&quot;top-right&quot;&gt;
      Top right
    &lt;/div&gt;
    &lt;div&gt;
      Content
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

如果您想使用margin-top:auto,请尝试以下方式:

ul {
    background-color: blueviolet;
    height: 100%;
    display: flex;
    flex-direction: column;
}

您还可以尝试其他方式,如:

ul {
    display: flex;
}

nav ul li:last-child {
    align-self: flex-end;
}

以及使用grid

ul {
    display: grid;
}

nav ul li:last-child {
    align-self: flex-end;
}
英文:

If you want use margin-top:auto try the following:

ul {
    background-color: blueviolet;
    height: 100%;
    display: flex;
    flex-direction: column;
  }

You can also try in other ways like :

ul {
  display: flex;
}

nav ul li:last-child {
  align-self: flex-end;
}

And by grid :

ul {
  display: grid;
}

nav ul li:last-child {
  align-self: flex-end;
}

答案2

得分: 0

我通过将以下CSS属性添加到ul元素中,成功将B推到底部:

display: flex;
flex-direction: column;

我仍然不确定为什么需要这样做。

英文:

I managed to get B pushed to the bottom by adding the following CSS properties to the ul element:

display: flex;
flex-direction: column;

I am still not sure why this was needed.

答案3

得分: 0

你的CSS代码是正确的,但你将其应用到了错误的元素上。ul 应该获得弹性盒子属性,以便所有 li 项目显示为列。将弹性属性应用到 ul

ul {
  display: flex;
  flex-direction: column;
  background-color: blueviolet;
  height: 100%;
}
英文:

Your CSS code is correct but you are applying it to wrong element. The ul should get the flex box property so that all li items are displayed as columns. Apply flex property to ul.

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

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

* {
  margin: 0;
  padding: 0;
  /* box-sizing: border-box; */
  /* border: 1px solid black; */
}

body {
  display: flex;
  height: 100vh;
}

#left {
  background-color: aqua;
  width: 5rem;
}

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

.top-left,
.top-right {
  padding: 1rem;
  background-color: aliceblue;
}

nav {
  background-color: red;
  height: 100%;
}

ul {
  display: flex;
  flex-direction: column;
  background-color: blueviolet;
  height: 100%;
}

nav ul li:last-child {
  margin-top: auto;
  border: 1px solid white;
}

#right {
  background-color: orange;
  flex-grow: 1;
}

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;link href=&quot;./project.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;div id=&quot;left&quot; class=&quot;column&quot;&gt;
    &lt;div class=&quot;top-left&quot;&gt;
      Logo
    &lt;/div&gt;
    &lt;nav&gt;
      &lt;ul&gt;
        &lt;li&gt;A&lt;/li&gt;
        &lt;li&gt;B&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/nav&gt;
  &lt;/div&gt;

  &lt;div id=&quot;right&quot; class=&quot;column&quot;&gt;
    &lt;div class=&quot;top-right&quot;&gt;
      Top right
    &lt;/div&gt;
    &lt;div&gt;
      Content
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月8日 12:45:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381465.html
匿名

发表评论

匿名网友

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

确定