如何去除我的弹性布局中的少量内边距

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

how can i remove little padding in my flex

问题

我一直在YouTube上关注一个关于只使用HTML、CSS和JavaScript制作简单网站的教程。我正在做一些修改,但我一直试图去除flex下面的一点内边距,有一点内边距我不知道如何移除。

这是图片
[![图片下面有一点内边距][1]][1]

[1]: https://i.stack.imgur.com/YfDZ6.png

已经回顾了我观看的视频,仍然找不到解决方案,你们能帮帮我吗?请用最简单的方式解释一下...

以下是我的HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- 省略部分 -->
  </head>

  <body>
    <!-- 省略部分 -->
  </body>
</html>
我的CSS
```css
/* 省略部分 */
和我的JavaScript
```javascript
// 省略部分

<details>
<summary>英文:</summary>

i have been following someone on youtube about making simple website with only using html,css and javascript. i&#39;m doing some modification myself, and i&#39;ve been trying to get rid of padding on my flex. there&#39;s little bit padding below that i don&#39;t know how to remove.

here&#39;s the pict
[![little bit padding below the pict][1]][1]


  [1]: https://i.stack.imgur.com/YfDZ6.png

already review the video i watch and still can&#39;t find the solution, can you guys help me? please explain it in most newbie ways...

below is my HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Proj Hotel</title>

&lt;!-- Fonts --&gt;

&lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot; /&gt;
&lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin /&gt;
&lt;link
  href=&quot;https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,300;0,400;0,700;1,200&amp;display=swap&quot;
  rel=&quot;stylesheet&quot;
/&gt;

&lt;!-- Fonts end --&gt;

&lt;!-- Feather icons --&gt;
&lt;script src=&quot;https://unpkg.com/feather-icons&quot;&gt;&lt;/script&gt;
&lt;!-- Icons end --&gt;

&lt;link rel=&quot;stylesheet&quot; href=&quot;css/Style.css&quot; /&gt;

</head>

<body>
<!-- Navbar -->
<nav class="navbar">
<a href="#" class="navbar-logo"> Hotel <span>Proj</span></a>

  &lt;div class=&quot;navbar-nav&quot;&gt;
    &lt;a href=&quot;#home&quot;&gt;home&lt;/a&gt;
    &lt;a href=&quot;#about&quot;&gt; About us&lt;/a&gt;
    &lt;a href=&quot;#rooms&quot;&gt; Rooms &lt;/a&gt;
    &lt;a href=&quot;#contact&quot;&gt;Contact &lt;/a&gt;
  &lt;/div&gt;

  &lt;div class=&quot;navbar-extra&quot;&gt;
    &lt;a href=&quot;#&quot; id=&quot;search&quot;&gt; &lt;i data-feather=&quot;search&quot;&gt;&lt;/i&gt; &lt;/a&gt;
    &lt;a href=&quot;#&quot; id=&quot;shopping-cart&quot;&gt;
      &lt;i data-feather=&quot;shopping-cart&quot;&gt;&lt;/i&gt;
    &lt;/a&gt;
    &lt;a href=&quot;#&quot; id=&quot;hamburger-menu&quot;&gt; &lt;i data-feather=&quot;menu&quot;&gt;&lt;/i&gt;&lt;/a&gt;
  &lt;/div&gt;
&lt;/nav&gt;

&lt;!-- Navbar end --&gt;

&lt;!--Hero section  --&gt;
&lt;section class=&quot;hero&quot; id=&quot;home&quot;&gt;
  &lt;main class=&quot;content&quot;&gt;
    &lt;h1&gt;Get Some Nice &lt;span&gt;Sleep!&lt;/span&gt;&lt;/h1&gt;

    &lt;div class=&quot;inpad&quot;&gt;
      &lt;p&gt;
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id, fuga.
      &lt;/p&gt;

      &lt;a href=&quot;#&quot; class=&quot;cta&quot;&gt;book now!&lt;/a&gt;
    &lt;/div&gt;
  &lt;/main&gt;
&lt;/section&gt;

&lt;!-- Hero section end --&gt;

&lt;!-- about sextion --&gt;

&lt;section id=&quot;about&quot; class=&quot;about&quot;&gt;
  &lt;h2&gt;&lt;span&gt;About&lt;/span&gt; us&lt;/h2&gt;

  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;about-img&quot;&gt;
      &lt;img src=&quot;img/aboutfix.jpg&quot; alt=&quot;About us&quot; /&gt;
    &lt;/div&gt;

    &lt;div class=&quot;content&quot;&gt;
      &lt;h3&gt;Why Choose us?&lt;/h3&gt;
      &lt;p&gt;
        Lorem ipsum dolor sit amet consectetur, adipisicing elit. Velit sit,
        in voluptatem adipisci minima eligendi dolor unde quis aliquid
        itaque ut quas pariatur iste neque? Aut labore voluptatibus
        laboriosam sapiente.
      &lt;/p&gt;

      &lt;p&gt;
        Lorem, ipsum dolor sit amet consectetur adipisicing elit. Similique
        in esse recusandae quis architecto ad deleniti voluptatum pariatur
        facere aliquam impedit nesciunt fugiat animi dolorum officiis rerum,
        praesentium molestiae eum!
      &lt;/p&gt;

      &lt;p&gt;
        Lorem ipsum dolor sit, amet consectetur adipisicing elit.
        Recusandae, vel? Quasi error quas officiis esse mollitia doloremque
        saepe dolorem magnam ad excepturi temporibus, illo possimus quis,
      &lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

&lt;!-- about section end --&gt;

&lt;!-- feather icons --&gt;

&lt;script&gt;
  feather.replace();
&lt;/script&gt;

&lt;!-- javascript my --&gt;
&lt;script src=&quot;js/script.js&quot;&gt;&lt;/script&gt;

&lt;!-- Feather icons --&gt;

</body>
</html>

my CSS

:root {
--bg: #fffbeb;

--Primary: #745b36;
}

  • {
    font-family: "Poppins", sans-serif;

    margin: 0;

    padding: 0;

    box-sizing: border-box;

    outline: none;

    border: none;

    text-decoration: none;
    }

html {
scroll-behavior: smooth;
}

body {
font-family: "Poppins", sans-serif;

background-color: var(--bg);

color: #fff;

min-height: 5000px;
}

/* Navbar */

.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1.2rem 7%;
background-color: #f2deba;
border-bottom: 0.5px solid #f8cba6;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 999;
}

.navbar .navbar-logo {
font-size: 2rem;
font-weight: 700;
color: #bb8d64;
font-style: italic;
}

.navbar .navbar-logo span {
color: var(--Primary);
}

.navbar .navbar-nav a {
display: inline-block;
font-size: 1.3rem;
margin: 0 1rem;
color: var(--Primary);
}

.navbar .navbar-nav a:hover {
color: #0c0c0c;
}

.navbar .navbar-nav a::after {
content: "";
display: block;
padding-bottom: 0.5rem;
border-bottom: 0.1rem solid #0a0a0a;
transform: scaleX(0);
transition: 0.2s linear;
}

.navbar .navbar-nav a:hover::after {
transform: scaleX(0.5);
}

.navbar .navbar-extra a {
color: var(--Primary);
margin: 0 0.5rem;
}

.navbar .navbar-extra a:hover {
color: #fff;
}

#hamburger-menu {
display: none;
}

/* Navbar end */

/* hero section style start */
.hero {
min-height: 100vh;
display: flex;
align-items: center;
background-image: url("../img/1.jpg");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
position: relative;
}

/* below is gradient section /
.hero::after {
content: "";
display: block;
position: absolute;
width: 100%;
height: 30%;
bottom: 0;
background: linear-gradient(
0deg,
rgba(255, 251, 235, 1) 8%,
rgba(255, 255, 255, 0) 50%
);
}
/
gradient section end */

.hero .content {
padding: 1.4rem 7%;
max-width: 70rem;
}

.hero .content h1 {
font-size: 5rem;
color: #fff;
text-shadow: 1px 1px 3px rgba(1, 1, 3, 0.5);
line-height: 1;
}

.hero .content p {
font-size: 1.6rem;
margin-top: 1rem;
line-height: 1.4;
color: #fff;
font-weight: 100;
text-shadow: 1px 1px 3px rgba(1, 1, 3, 0.5);
}

.hero .content span {
color: rgba(241, 213, 162, 2);
}

.hero .content .cta {
display: inline-block;
margin-top: 1rem;
padding: 0.5rem 2rem;
font-size: 1.4rem;
color: #fff;
background-color: #f1d5a2a9;
border-radius: 0.5rem;
box-shadow: 1px 1px 3px rgba(1, 1, 3, 0.3);
margin-bottom: 0.5rem;
}

.hero .content .inpad a:hover {
background-color: #d3ba8d65;
}

.hero .content .inpad {
display: inline-block;
margin-top: 1rem;
padding: 0.5rem 1rem;
font-size: 1rem;
color: #fff;
background-color: rgba(116, 91, 54, 0.7);
border-radius: 0.5rem;
}
/* Hero section styles ends */

/* about start */

.about {
padding: 8rem 20%;
}

.about h2 {
text-align: center;
font-size: 2.4rem;
margin-bottom: 3rem;
color: rgba(116, 91, 54, 0.7);
}

.about .row .content p {
color: rgba(116, 91, 54, 0.5);
margin-bottom: 0.8rem;
font-size: 1rem;
font-weight: 700;
line-height: 1.7rem;
margin-left: 1rem;
}

.about .row {
display: flex;
background-color: rgba(116, 91, 54, 0.2);
}

.about .row .about-img {
flex: 1 1 25rem;
}

.about .row .about-img img {
width: 100%;
}

.about .row .content {
flex: 1 1 20rem;
}

.about .row .content h3 {
font-size: 1.5rem;
color: rgba(116, 91, 54, 0.5);
padding-left: 1rem;
}

/* about end */

/* media queries */

/* laptop */
@media (max-width: 1366px) {
html {
font-size: 75%;
}
}

/* end */

/* Tablet */
@media (max-width: 768px) {
html {
font-size: 62.5%;
}

#hamburger-menu {
display: inline-block;
}

.navbar .navbar-nav {
position: absolute;
top: 100%;
right: -100%;
background-color: #fff;
width: 25rem;
height: 100vh;
transition: 0.5s;
}

.navbar .navbar-nav.active {
right: 0;
}

.navbar .navbar-nav a {
color: var(--Primary);
display: block;
margin: 1.5rem;
padding: 0.5rem;
font-size: 2rem;
}

.navbar .navbar-nav a::after {
transform-origin: 0 0;
}

.navbar .navbar-nav a:hover::after {
transform: scaleX(0.3);
}

.about .row {
flex-wrap: wrap;
}

.about .row .about-img img {
height: 25rem;
object-fit: cover;
object-position: center;
}

.about .row .content {
padding: 0;
}

.about .row .content h3 {
margin-top: 1rem;
font-size: 2rem;
}

.about .row .content p {
font-size: 1.6rem;
}
}

/* end */

/* mobile */
@media (max-width: 450px) {
html {
font-size: 55%;
}
}

/* end */

/* Media queries end */


and my Javascript

// toggle class active
const navbarNav = document.querySelector(".navbar-nav");
//ketika hamburger menu di klik
document.querySelector("#hamburger-menu").onclick = () => {
navbarNav.classList.toggle("active");
};

// outside click to close sidebar

const hamburger = document.querySelector("#hamburger-menu");

document.addEventListener("click", function (e) {
if (!hamburger.contains(e.target) && !navbarNav.contains(e.target)) {
navbarNav.classList.remove("active");
}
});

i&#39;m sorry about the messy codes i made, thanks in advance.

</details>


# 答案1
**得分**: 1

你是在指这个空间吗?

这不是填充,而是由左侧图像的高度引起的。这是因为图像设置为与右侧文本相同的宽度(由于 `flex: 1`),因此图像的高度导致右侧内容的高度也增加,这就是为什么你会看到底部有这个空白空间。

一个简单的选择是将 `align-items: center` 应用于 flex 容器,这将在顶部或底部提供相等的空间。但这不会完全消除间距。

<details>
<summary>英文:</summary>

Are you referring to this space?

[![enter image description here][1]][1]

This is not padding, but rather this is caused by the height of the image on the left. This is because the image is set to take up the same width as the text on the right (due to `flex: 1`), and so the height of the image causes the height of the content on the right to also increase, which is why you see this blank space at the bottom.

A simple option could be to apply `align-items: center` to the flex-container which will give equal space on the top or bottom. But this will not remove the spacing altogether.



  [1]: https://i.stack.imgur.com/Z83ee.png

</details>



# 答案2
**得分**: 0

从css中的`body`标签中删除`min-height: 5000px`。

<details>
<summary>英文:</summary>

Remove `min-height: 5000px` from inside the `body` tag in css

</details>



huangapple
  • 本文由 发表于 2023年2月19日 22:25:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500811.html
匿名

发表评论

匿名网友

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

确定