【问题标题】:Resizing text inside gatsby-background-image inside CSS-Grid resizes the whole image在 CSS-Grid 中调整 gatsby-background-image 中的文本大小调整整个图像的大小
【发布时间】:2020-09-04 23:42:45
【问题描述】:

我的背景图片上覆盖了一些文字。

我正在尝试使文本更小,但是当我使用 CSS 调整文本大小时

.titleSize{
    font-size: 0.8em;
}

它最终会调整整个背景图像的大小


这是Github 上的最小可重现示例

您必须使用yarn installnpm install 安装节点模块,然后运行gatsby develop 并在浏览器中查看localhost:8000 的网页

注意:如果将 textClass='small' 更改为 textClass='big',请对 textClass='small' 的所有 3 个实例执行此操作以查看大小更改


这是我正在使用的代码

import React from "react"
import { Link, graphql, useStaticQuery } from "gatsby"
import BackgroundImage from 'gatsby-background-image'
import '../style.css'

const BgImage = ({className, imgProps, textClass}) => (
  <Link className={className} to='#'>
    <BackgroundImage
      Tag="section"
      role="img"
      className='bgImage'
      fluid={imgProps}
      backgroundColor={`#040e18`}
    >
      <div className='outer'>
        <h2 className={textClass}>How was the math test?</h2>
      </div>
    </BackgroundImage>
  </Link>
)

const IndexPage = () => {

  const data = useStaticQuery( graphql`
      query MyQuery {
        allImageSharp {
          edges {
            node {
              fluid {
                sizes
                src
                srcSet
                srcSetWebp
              }
            }
          }
        }
      }
    `)

    const imgProps = data.allImageSharp.edges[2].node.fluid

    return (
      <div className='container'>
        <div className='my-grid'>
          <BgImage className='un' textClass='small' imgProps={imgProps} />
          <BgImage className='du' textClass='small' imgProps={imgProps} />
          <BgImage className='twa' textClass='small' imgProps={imgProps} />
        </div>
        <div className='my-grid'>
          <BgImage className='un' textClass='big' imgProps={imgProps} />
          <BgImage className='du' textClass='big' imgProps={imgProps} />
          <BgImage className='twa' textClass='big' imgProps={imgProps} />
        </div>
      </div>
    )
}

export default IndexPage
.container{
    width: 100%;
    display: flex;
}

.beside{
    width: 50%;
}

.my-grid{
    height: 400px;
    display: grid;
    grid-gap: 10px;
    grid-template: repeat(3, 1fr) / repeat(2, 1fr);
    grid-template-areas:
        "u u"
        "u u"
        "d t";            
    width: auto;
    padding: 20px;
}

.un{
    grid-area: u;
}

.du{
    grid-area: d;
}

.twa{
    grid-area: t;
}

.bgImage{
    width: 100%;
    height: 100%;
}

.small{
    margin-top: auto;
    font-size: 0.8em;
}

.big{
    margin-top: auto;
}

.outer{
    display: flex;
    height: 100%;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-end;
    text-align: right;
}

【问题讨论】:

标签: css reactjs css-grid gatsby gatsby-image


【解决方案1】:

网格的宽度会根据字体大小而变化。尝试将width: 50% 添加到您的班级.my-grid

【讨论】:

  • 有没有办法避免添加 width: 50% ? width: auto 无论如何都会这样吗?
  • 不,width: auto 不会成功。您的网格元素位于带有display: flex 的容器内。如果您希望其子元素具有相同的宽度,则必须为两个元素指定相同的宽度。由于您的 div 带有文本“屏幕的 50%”,网格 div 可能应该自动填充为其他 50% 的大小,但它仅在 textsize 不是 0.8em 时才具有width: 50%,它的兄弟必须有width: 50%以及获得相等的宽度。
猜你喜欢
  • 2011-03-18
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
  • 2021-10-02
  • 2019-06-17
  • 2023-03-06
  • 2013-03-26
  • 1970-01-01
相关资源
最近更新 更多