【问题标题】:Divs have different heights, where are they inheriting their heights from?Div 有不同的高度,它们从哪里继承高度?
【发布时间】:2021-03-26 00:04:38
【问题描述】:

我正在尝试使用卡片显示虚构的商店。我正在设置最大宽度,以便图像保持其纵横比,预计卡片尺寸不等。我将这些卡片分组在一个弹性容器中,这样我就不必过多担心它们在页面中的位置。但是,当我这样做时,我会得到相同大小的卡片。图像大小不同,并保持它们的宽高比很好,但卡片的主体会更改为使卡片大小相等的任何内容。

我试图了解为什么会发生这种情况。是什么让 flex 容器内部的元素大小相等?如果这是一个愚蠢的问题,我很抱歉,但我无法弄清楚。请注意,这仅在水平查看页面时可见。卡片的大小实际上是我在垂直查看时想要的大小。

/* Set HTML and Body width and height properties to 100% so other elements can use the % property */
html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    background-color: #557d79ff;
    /* f37970 */
}

/* Define the main Navigation block */
.myNav {
    display: flex;
    height: 3.5rem;
    width: 100%;
    line-height: 1.6;
    /* background-color: #e6a69c; */
    background-color: #FC766AFF;
    padding: 0;
    margin: 0;
}
/* Remove bullets, margin and padding */
.myNav ul {
    display: flex;
    list-style: none;
    padding: 0;
    margin: 0;
    font-family: 'Sansita Swashed', cursive;
}
.myNav li {
    float: left;
    /* Or you can use display: inline; */
}
/* Define the block styling for the links */
.myNav li a {
    display: inline-block;
    text-align: center;
    padding: 14px 16px;
    color: white;
}
/* This is optional, however if you want to display the active link differently apply a background to it */
.myNav li a.active {
    background-color: #557d79ff;
}

/**********************************  CARD SECTION START  **********************************/

/* Cards container */
.cards {
    display: flex;
    flex-flow: row wrap;
    background-color: white;
}

/* Card Body */
.card {
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
    transition: 0.3s;
    border-radius: 5rem 5rem 5rem 5rem; /* 5px rounded corners */
    width: 20rem;
    background-color: #c9d4ceFF;
    margin-top: 2rem;
    margin-left: 2rem;
    /* height: 100%; */
}

/* On mouse-over, add a deeper shadow */
.card:hover {
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
  
/* Add some padding inside the card container */
.container {
    padding: 2px 16px;
    border-radius: 0rem 0rem 5rem 5rem;
}
 
/* Add rounded corners to the top left and the top right corner of the image */
img {
    /* border-radius: 1rem 1rem 1rem 1rem; */
    margin-top: 0rem;
    border-radius: 5rem 5rem 0rem 0rem;
    max-width: 20rem;
}
<!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">
    <!-- Google Fonts! -->
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Sansita+Swashed:wght@300;400;500;600&display=swap"
        rel="stylesheet">
    <!-- Font Awesome! -->
    <script defer src="all.js"></script>
    <title>Cards & Navbar</title>
    <!-- Custom CSS! -->
    <link rel="stylesheet" href="home.css">
</head>

<body>

    <!-- NAVBAR START -->
    <nav class="myNav">
        <ul>
            <li><a href="index.html"><i class="fas fa-mug-hot"></i> Find Me Food Now!</a></li>
            <li><a href="about.html">About</a></li>
            <li><a href="contact.html">Contact</a></li>
        </ul>
    </nav>
    <!-- END NAVBAR -->

    <!-- CARDS START -->

    <!-- FLEX CARDS -->
    <div class="cards">
        <div class="card">
            <div class="image">
                <a href="https://www.youtube.com/">
                    <img src="https://source.unsplash.com/ItaV89TNkks" alt="Avatar">
                </a>
            </div>
            <div class="container">
                <h3><b>Best Cafe</b></h3>
                <h4>Jonathan</h4>
                <p>Cozy cafe with great food and drinks!</p>
            </div>
        </div>
        <div class="card">
            <div class="image">
                <a href="https://www.youtube.com/">
                    <img src="https://source.unsplash.com/d5SZqLkpIrY" alt="Avatar">
                </a>
            </div>
            <div class="container">
                <h3><b>Best Cafe</b></h3>
                <h4>Jonathan</h4>
                <p>Cozy cafe with great food and drinks!</p>
            </div>
        </div>
    </div>
    
</body>

</html>

【问题讨论】:

    标签: javascript html css inheritance flexbox


    【解决方案1】:

    div 的高度是拉伸的,不是继承的。

    'display:flex' 默认的 'align-items' 是 'stretch',所以它会拉伸到最大轴的高度。

    要获得您想要的外观,您需要将“align-items”更改为以下之一:“baseline, center, flex-start, flex-end”。

    /* Set HTML and Body width and height properties to 100% so other elements can use the % property */
    html, body {
        margin: 0;
        padding: 0;
        height: 100%;
        width: 100%;
        background-color: #557d79ff;
        /* f37970 */
    }
    
    /* Define the main Navigation block */
    .myNav {
        display: flex;
        height: 3.5rem;
        width: 100%;
        line-height: 1.6;
        /* background-color: #e6a69c; */
        background-color: #FC766AFF;
        padding: 0;
        margin: 0;
    }
    /* Remove bullets, margin and padding */
    .myNav ul {
        display: flex;
        list-style: none;
        padding: 0;
        margin: 0;
        font-family: 'Sansita Swashed', cursive;
    }
    .myNav li {
        float: left;
        /* Or you can use display: inline; */
    }
    /* Define the block styling for the links */
    .myNav li a {
        display: inline-block;
        text-align: center;
        padding: 14px 16px;
        color: white;
    }
    /* This is optional, however if you want to display the active link differently apply a background to it */
    .myNav li a.active {
        background-color: #557d79ff;
    }
    
    /**********************************  CARD SECTION START  **********************************/
    
    /* Cards container */
    .cards {
        display: flex;
        flex-flow: row wrap;
        background-color: white;
        align-items: baseline;
    }
    
    /* Card Body */
    .card {
        box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
        transition: 0.3s;
        border-radius: 5rem 5rem 5rem 5rem; /* 5px rounded corners */
        width: 20rem;
        background-color: #c9d4ceFF;
        margin-top: 2rem;
        margin-left: 2rem;
        /* height: 100%; */
    }
    
    /* On mouse-over, add a deeper shadow */
    .card:hover {
        box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
    }
      
    /* Add some padding inside the card container */
    .container {
        padding: 2px 16px;
        border-radius: 0rem 0rem 5rem 5rem;
    }
     
    /* Add rounded corners to the top left and the top right corner of the image */
    img {
        /* border-radius: 1rem 1rem 1rem 1rem; */
        margin-top: 0rem;
        border-radius: 5rem 5rem 0rem 0rem;
        max-width: 20rem;
    }
    <!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">
        <!-- Google Fonts! -->
        <link rel="preconnect" href="https://fonts.gstatic.com">
        <link href="https://fonts.googleapis.com/css2?family=Sansita+Swashed:wght@300;400;500;600&display=swap"
            rel="stylesheet">
        <!-- Font Awesome! -->
        <script defer src="all.js"></script>
        <title>Cards & Navbar</title>
        <!-- Custom CSS! -->
        <link rel="stylesheet" href="home.css">
    </head>
    
    <body>
    
        <!-- NAVBAR START -->
        <nav class="myNav">
            <ul>
                <li><a href="index.html"><i class="fas fa-mug-hot"></i> Find Me Food Now!</a></li>
                <li><a href="about.html">About</a></li>
                <li><a href="contact.html">Contact</a></li>
            </ul>
        </nav>
        <!-- END NAVBAR -->
    
        <!-- CARDS START -->
    
        <!-- FLEX CARDS -->
        <div class="cards">
            <div class="card">
                <div class="image">
                    <a href="https://www.youtube.com/">
                        <img src="https://source.unsplash.com/ItaV89TNkks" alt="Avatar">
                    </a>
                </div>
                <div class="container">
                    <h3><b>Best Cafe</b></h3>
                    <h4>Jonathan</h4>
                    <p>Cozy cafe with great food and drinks!</p>
                </div>
            </div>
            <div class="card">
                <div class="image">
                    <a href="https://www.youtube.com/">
                        <img src="https://source.unsplash.com/d5SZqLkpIrY" alt="Avatar">
                    </a>
                </div>
                <div class="container">
                    <h3><b>Best Cafe</b></h3>
                    <h4>Jonathan</h4>
                    <p>Cozy cafe with great food and drinks!</p>
                </div>
            </div>
        </div>
        
    </body>
    
    </html>

    【讨论】:

    • 好的,这很有意义,非常感谢!这比我想象的要简单得多哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    相关资源
    最近更新 更多