css 如何使两列(40% 60%)网格元素具有相同的高度?

9udxz4iz  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(78)

使用CSS Grid系统,我有两列;第一列是40%,第二列是60%,使用grid-template-column。第一列是文本,第二列是img;如何使两列的高度相同?

.container
{
    max-width: 1130px;
    margin: 0 auto;
}

section
{
    padding: 30px;
}

#sec-1
{
    padding: 0;
}

.sec-1 img
{
    width: 100%;
    display: block;
}

#sec-1 .wrapper
{
    padding: 50px;
    background-image: linear-gradient(to bottom right, #a454fd, #2b05df);
    color: white;
}

section[id^="sec"] h1
{
    font-size: 34px;
    font-weight: 900;
}

#sec-1 a 
{
    text-decoration: none;
    color: white;
    border: 1px solid white;
    padding: 15px 30px;
    border-radius: 25px;
    margin: 50px auto;
    display: block;
    width: 115px;
}

#sec-1 a:hover
{
    color: #0000cc;
    cursor: pointer;
}

/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {
}

/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {
}

/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {
    #sec-1
    {
        display: grid;
        grid-template-columns: 40% 60%;
    }
}

个字符

1tuwyuhd

1tuwyuhd1#

考虑将height: 100%应用于<img>元素:

.container
{
    max-width: 1130px;
    margin: 0 auto;
}

section
{
    padding: 30px;
}

#sec-1
{
    padding: 0;
}

.sec-1 img
{
    width: 100%;
    display: block;
}

#sec-1 .wrapper
{
    padding: 50px;
    background-image: linear-gradient(to bottom right, #a454fd, #2b05df);
    color: white;

}

section[id^="sec"] h1
{
    font-size: 34px;
    font-weight: 900;
}

#sec-1 a 
{
    text-decoration: none;
    color: white;
    border: 1px solid white;
    padding: 15px 30px;
    border-radius: 25px;
    margin: 50px auto;
    display: block;
    width: 115px;
}

#sec-1 a:hover
{
    color: #0000cc;
    cursor: pointer;
}

/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {


}

/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {



}

/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {

    #sec-1
    {
        display: grid;
        grid-template-columns: 40% 60%;
    }

    img
    {
        height: 100%;
    }

}

个字符

o4hqfura

o4hqfura2#

根据提供的代码,为了实现所需的使网格页面中的内容列和图像列的高度相同的功能,可以使用以下CSS代码:

@media only screen and (min-width: 992px) {
    #sec-1 img
    {
        width: 100%;
        display: block;
        height: 100%;
    }
}

字符串

相关问题