这个`Div`底部为什么有额外的空间?如何使其高度适应子元素?[手机]

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

Why that Div has additional space at bottom? How to make it height fit to children?[Mobile]

问题

我想让该div在底部没有额外的空间,我在Codepen上添加了链接。我只谈论移动分辨率,媒体查询存在问题。

.main-sidebar {
    display: flex;
    flex-direction: column;
    background-color: var(--VeryDarkBlue);
    padding: 2.1875rem 1.5625rem;
    min-height: 100%;
    overflow: hidden;
}
.main-sidebar h2 {
    color: hsl(35, 77%, 62%);
    font-size: 2.8125rem;
    font-weight: 700;
}
.main-sidebar-container {
    display: flex;
    flex-direction: column;
    padding: 1.875rem 0;
    border-bottom: solid 1px var(--DarkGrayishBlue);
}
.main-sidebar-container:last-child {
    border-bottom: none;
}
.main-sidebar-container-header {
    color: var(--Offwhite);
    font-weight: 700;
    font-size: 1.25rem;
    margin-bottom: .9375rem;
}
.main-sidebar-container-description {
    color: var(--Grayishblue);
    line-height: 1.5625rem;
}

我尝试了很多方法,但都没有奏效。我是初学者,感谢帮助 这个`Div`底部为什么有额外的空间?如何使其高度适应子元素?[手机]

英文:

i want to make that div full height without additional space at bottom. I am adding a link to codepen. I am talking about mobile resolution only, problem with media queries.
Image
Codepen: Link

.main-sidebar {
      display: flex;
      flex-direction: column;
      background-color: var(--VeryDarkBlue);
      padding: 2.1875rem 1.5625rem;
      min-height: 100%;
      overflow: hidden;
    }
    .main-sidebar h2 {
      color: hsl(35, 77%, 62%);
      font-size: 2.8125rem;
      font-weight: 700;
    }
    .main-sidebar-container {
      display: flex;
      flex-direction: column;
      padding: 1.875rem 0;
      border-bottom: solid 1px var(--DarkGrayishBlue);
    }
    .main-sidebar-container:last-child {
      border-bottom: none;
    }
    .main-sidebar-container-header {
      color: var(--Offwhite);
      font-weight: 700;
      font-size: 1.25rem;
      margin-bottom: .9375rem;
    }
    .main-sidebar-container-description {
      color: var(--Grayishblue);
      line-height: 1.5625rem;
    }

I tried a lot of things, but none of them worked. I am begginer, thanks for help 这个`Div`底部为什么有额外的空间?如何使其高度适应子元素?[手机]

答案1

得分: 0

min-height: 100%.main-sidebar中引起了问题,您可以改为使用height:100%overflow: unset

英文:

I suppose the issue that you have is for max-width:900px media?

min-height: 100% in .main-sidebar is causing the problem, you can give height:100% and overflow: unset instead.

答案2

得分: 0

问题出在移动分辨率上。对于移动设备,请使用 @media (max-width:900px) {}

问题出在 .main-sidebar 类中,您设置了 min-height: 100%; 属性。

您可以使用 height 属性。作为临时解决方案,您还可以使用 min-height: 60%;

此外,每当编写 CSS 代码时,应按正确结构编写。我们应该为块级元素定义高度和宽度,并应按层次结构编写块级元素的 CSS 代码,以便更容易进行调试。

英文:

I got the problem is in mobile resolution. For Mobile @media (max-width:900px) {}

The issue is in class .main-sidebar where you set a property of min-height: 100%;

You may use height property. For temporary solution you can also use min-height: 60%;

Moreover, whenever we write CSS code it should be in proper structure. we should define height and width for block element and the block elements CSS code should be written in hierarchy so that debugging got easy.

答案3

得分: 0

我帮你翻译如下代码部分:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- 根据用户设备正确显示网站 -->
  <meta http-equiv="X-UA-Compatible" content="IE edge">
  <link rel="icon" type="image/png" sizes="32x32" href="./assets/images/favicon-32x32.png">
  <link rel="stylesheet" href="test.css" type="text/css">

  <title>Frontend Mentor | News homepage</title>

  <!-- 随意删除这些样式或在自己的样式表中进行自定义 -->
  <style>
    .attribution {
      font-size: 11px;
      text-align: center;
    }

    .attribution a {
      color: hsl(228, 45%, 44%);
    }
  </style>
</head>

<body>
  <nav class="navbar">
    <img src="//unsplash.it/75/75" alt="logo" id="logo">
    <ul class="navbar-list">
      <li><a href="#" class="navbar-link">Home</a></li>
      <li><a href="#" class="navbar-link">New</a></li>
      <li><a href="#" class="navbar-link">Popular</a></li>
      <li><a href="#" class="navbar-link">Trending</a></li>
      <li><a href="#" class="navbar-link">Categories</a></li>
    </ul>
  </nav>

  <main class="main-wrapper">
    <div class="main-image"></div>
    <div class="main-bottom">
      <div class="main-bottom-first-container">
        <h1>The Bright Future of Web 3.0?</h1>
      </div>
      <div class="main-bottom-second-container">
        <p>We dive into the next evolution of the web that claims to put the power of the platforms back into the
          hands of the people. But is it really fulfilling its promise?</p>
        <button>Read more</button>
      </div>
    </div>
    <aside class="main-sidebar">
      <h2>New</h2>
      <div class="main-sidebar-container">
        <h3 class="main-sidebar-container-header">Hydrogen VS Electric Cars</h3>
        <p class="main-sidebar-container-description">Will hydrogen-fueled cars ever catch up to EVs?</p>
      </div>
      <div class="main-sidebar-container">
        <h3 class="main-sidebar-container-header">The Downsides of AI Artistry</h3>
        <p class="main-sidebar-container-description">What are the possible adverse effects of on-demand AI image
          generation?</p>
      </div>
      <div class "main-sidebar-container">
        <h3 class="main-sidebar-container-header">Is VC Funding Drying Up?</h3>
        <p class="main-sidebar-container-description">Private funding by VC firms is down 50% YOY. We take a look at
          what that means.</p>
      </div>
    </aside>
    <div class="left_box_container">
      <div class="__box">
        <div class="box_image" id="box_first_image"></div>
        <div class="box_container">
          <div class="box_number">01</div>
          <div class="box_header">Reviving Retro PCs</div>
          <div class="box_description">What happens when old PCs are given modern upgrades?</div>
        </div>
      </div>
      <div class="__box">
        <div class="box_image" id="box_second_image"></div>
        <div class="box_container">
          <div class="box_number">02</div>
          <div class="box_header">Top 10 Laptops of 2022</div>
          <div class="box_description">Our best picks for various needs and budgets.</div>
        </div>
      </div>
      <div class="__box">
        <div class="box_image" id="box_third_image"></div>
        <div class="box_container">
          <div class="box_number">03</div>
          <div class="box_header">The Growth of Gaming</div>
          <div class="box_description">How the pandemic has sparked fresh opportunities.</div>
        </div>
      </div>
    </div>
  </main>
  <footer class="attribution">
    Challenge by <a href="https://www.frontendmentor.io?ref=challenge" target="_blank">Frontend Mentor</a>.
    Coded by <a href="#">Your Name Here</a>.
  </footer>
</body>

</html>

希望这对你有所帮助。如果有其他问题,请随时告诉我。

英文:

I took the liberty of correcting your code by removing all redundant parts and adding another breakpoint. I hope this solves your problem. <br> Sorry I had to use some random images since I had no access to your assets folder.

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

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

@import url(&quot;https://fonts.googleapis.com/css2?family=Inter:wght@400;700;800&amp;display=swap&quot;);
/* http://meyerweb.com/eric/tools/css/reset/ 
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
padding: 20px 5rem;
font-family: &quot;Inter&quot;, sans-serif;
}
ol, ul {
list-style: none;
}
blockquote,
q {
quotes: none;
}
blockquote:before, blockquote:after, q:before, q:after {
content: &quot;&quot;;
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
:root {
--Offwhite: hsl(36, 100%, 99%);
--Grayishblue: hsl(233, 8%, 79%);
--DarkGrayishBlue: hsl(236, 13%, 42%);
--VeryDarkBlue: hsl(240, 100%, 5%);
--SoftOrange: hsl(35, 77%, 62%);
--SoftRed: hsl(5, 85%, 63%);
--gap: 1.875rem;
--pad: 2rem;
}
.navbar {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
margin-bottom: var(--gap);
}
.navbar-list {
display: flex;
justify-content: space-between;
gap: 2.5rem;
}
.navbar-link {
text-decoration: none;
color: var(--DarkGrayishBlue);
}
.main-wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: var(--gap);
grid-template-areas: 
&quot;main-image main-image main-sidebar&quot;
&quot;main-bottom main-bottom main-sidebar&quot;
&quot;box-container box-container box-container&quot;
;
}
.main-image {
grid-area: main-image;
background-image: url(//unsplash.it/800/800);
background-size: cover;
background-position: center;
height: 18.75rem;
}
.main-bottom {
grid-area: main-bottom;
display: grid; 
gap: var(--gap); 
grid-template-columns: 1fr 1fr;
}
.main-bottom-first-container h1 {
font-size: 3.4375rem;
font-weight: 800;
color: var(--VeryDarkBlue);
}
.main-bottom-second-container p {
color: var(--DarkGrayishBlue);
font-size: 1rem;
line-height: 1.5625rem;
margin-bottom: var(--gap);
}
.main-bottom-second-container button {
text-transform: uppercase;
letter-spacing: 0.3125rem;
font-weight: 700;
color: var(--Offwhite);
background-color: hsl(5, 85%, 63%);
padding: 0.9375rem var(--gap);
border: none;
}
.main-sidebar {
grid-area: main-sidebar;
display: flex;
flex-direction: column;
gap: var(--gap);
background-color: var(--VeryDarkBlue);
padding: var(--pad);
}
.main-sidebar h2 {
color: hsl(35, 77%, 62%);
font-size: 2.8125rem;
font-weight: 700;
}
.main-sidebar-container:not(:last-child) {
padding-bottom: var(--gap);
border-bottom: solid 1px var(--DarkGrayishBlue);
}
.main-sidebar-container-header {
color: var(--Offwhite);
font-weight: 700;
font-size: 1.25rem;
}
.main-sidebar-container-description {
color: var(--Grayishblue);
line-height: 1.5625rem;
}
.left_box_container {
grid-area: box-container;
display: grid; 
grid-template-columns: 1fr 1fr 1fr;
gap: var(--gap);
}
.__box {
display: flex;
flex-direction: column;
gap: 0rem;
}
.box_image {
height: 6.25rem; 
max-width: 100%;
}
.box_container {
display: flex;
flex-direction: column;
gap: .5rem;
padding: var(--pad);
}
.box_number {
font-size: 1.625rem;
font-weight: 700;
color: var(--Grayishblue);
}
.box_header {
font-size: 1.125rem;
font-weight: 800;
color: var(--VeryDarkBlue);
}
.box_description {
color: var(--DarkGrayishBlue);
font-size: 0.9375rem;
line-height: 1.5625rem;
}
#box_first_image {
background-image: url(//unsplash.it/400/399);
background-size: cover;
background-position: center;
}
#box_second_image {
background-image: url(//unsplash.it/400/400);
background-size: cover;
background-position: center;
}
#box_third_image {
background-image: url(//unsplash.it/400/401);
background-size: cover;
background-position: center;
}
.attribution {
display: none;
margin: var(--gap) 0;
}
/* Actives */
.navbar-link:hover {
color: var(--SoftRed);
}
.main-sidebar-container-header:hover {
cursor: pointer;
color: var(--SoftOrange);
}
.main-bottom-second-container &gt; button:hover {
cursor: pointer;
color: white;
background-color: black;
}
.box_header:hover {
cursor: pointer;
color: var(--SoftRed);
}
/* Tablet */
@media (max-width: 1045px) {
body {
padding: 20px;
}
#logo {
transform: scale(0.9);
}
.main-wrapper {
grid-template-columns: 1fr 1fr;
grid-template-areas: 
&quot;main-image main-image&quot;
&quot;main-bottom main-bottom&quot;
&quot;main-sidebar box-container&quot;
&quot;main-sidebar box-container&quot;;
}
.main-sidebar {
align-self: start;;
}
.left_box_container {
grid-template-columns: 1fr;
}
}
/* Phone */
@media (max-width: 700px) {
.navbar {
flex-direction: column;
gap: var(--gap);
}
.navbar-list {
gap: 1rem;
}
.main-wrapper {
grid-template-columns: 1fr;
grid-template-areas: 
&quot;main-image&quot;
&quot;main-bottom&quot;
&quot;main-sidebar&quot;
&quot;box-container&quot;;
}
.main-bottom {
grid-template-columns: 1fr;
}
}

<!-- 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 name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;!-- displays site properly based on user&#39;s device --&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;link rel=&quot;icon&quot; type=&quot;image/png&quot; sizes=&quot;32x32&quot; href=&quot;./assets/images/favicon-32x32.png&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;test.css&quot; type=&quot;text/css&quot;&gt;
&lt;title&gt;Frontend Mentor | News homepage&lt;/title&gt;
&lt;!-- Feel free to remove these styles or customise in your own stylesheet &#128077; --&gt;
&lt;style&gt;
.attribution {
font-size: 11px;
text-align: center;
}
.attribution a {
color: hsl(228, 45%, 44%);
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;nav class=&quot;navbar&quot;&gt;
&lt;img src=&quot;//unsplash.it/75/75&quot; alt=&quot;logo&quot; id=&quot;logo&quot;&gt;
&lt;ul class=&quot;navbar-list&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#&quot; class=&quot;navbar-link&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot; class=&quot;navbar-link&quot;&gt;New&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot; class=&quot;navbar-link&quot;&gt;Popular&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot; class=&quot;navbar-link&quot;&gt;Trending&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot; class=&quot;navbar-link&quot;&gt;Categories&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;main class=&quot;main-wrapper&quot;&gt;
&lt;div class=&quot;main-image&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;main-bottom&quot;&gt;
&lt;div class=&quot;main-bottom-first-container&quot;&gt;
&lt;h1&gt;The Bright Future of Web 3.0?&lt;/h1&gt;
&lt;/div&gt;
&lt;div class=&quot;main-bottom-second-container&quot;&gt;
&lt;p&gt;We dive into the next evolution of the web that claims to put the power of the platforms back into the
hands of the people. But is it really fulfilling its promise?&lt;/p&gt;
&lt;button&gt;Read more&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;aside class=&quot;main-sidebar&quot;&gt;
&lt;h2&gt;New&lt;/h2&gt;
&lt;div class=&quot;main-sidebar-container&quot;&gt;
&lt;h3 class=&quot;main-sidebar-container-header&quot;&gt;Hydrogen VS Electric Cars&lt;/h3&gt;
&lt;p class=&quot;main-sidebar-container-description&quot;&gt;Will hydrogen-fueled cars ever catch up to EVs?&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;main-sidebar-container&quot;&gt;
&lt;h3 class=&quot;main-sidebar-container-header&quot;&gt;The Downsides of AI Artistry&lt;/h3&gt;
&lt;p class=&quot;main-sidebar-container-description&quot;&gt;What are the possible adverse effects of on-demand AI image
generation?&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;main-sidebar-container&quot;&gt;
&lt;h3 class=&quot;main-sidebar-container-header&quot;&gt;Is VC Funding Drying Up?&lt;/h3&gt;
&lt;p class=&quot;main-sidebar-container-description&quot;&gt;Private funding by VC firms is down 50% YOY. We take a look at
what that means.&lt;/p&gt;
&lt;/div&gt;
&lt;/aside&gt;
&lt;div class=&quot;left_box_container&quot;&gt;
&lt;div class=&quot;__box&quot;&gt;
&lt;div class=&quot;box_image&quot; id=&quot;box_first_image&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;box_container&quot;&gt;
&lt;div class=&quot;box_number&quot;&gt;01&lt;/div&gt;
&lt;div class=&quot;box_header&quot;&gt;Reviving Retro PCs&lt;/div&gt;
&lt;div class=&quot;box_description&quot;&gt;What happens when old PCs are given modern upgrades?&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;__box&quot;&gt;
&lt;div class=&quot;box_image&quot; id=&quot;box_second_image&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;box_container&quot;&gt;
&lt;div class=&quot;box_number&quot;&gt;02&lt;/div&gt;
&lt;div class=&quot;box_header&quot;&gt;Top 10 Laptops of 2022&lt;/div&gt;
&lt;div class=&quot;box_description&quot;&gt;Our best picks for various needs and budgets.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;__box&quot;&gt;
&lt;div class=&quot;box_image&quot; id=&quot;box_third_image&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;box_container&quot;&gt;
&lt;div class=&quot;box_number&quot;&gt;03&lt;/div&gt;
&lt;div class=&quot;box_header&quot;&gt;The Growth of Gaming&lt;/div&gt;
&lt;div class=&quot;box_description&quot;&gt;How the pandemic has sparked fresh opportunities.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/main&gt;
&lt;footer class=&quot;attribution&quot;&gt;
Challenge by &lt;a href=&quot;https://www.frontendmentor.io?ref=challenge&quot; target=&quot;_blank&quot;&gt;Frontend Mentor&lt;/a&gt;.
Coded by &lt;a href=&quot;#&quot;&gt;Your Name Here&lt;/a&gt;.
&lt;/footer&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 15:41:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553779.html
匿名

发表评论

匿名网友

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

确定