简体   繁体   English

如何阻止子段落在 css 网格内拉伸父 div

[英]How to stop child paragraph from stretching parent div inside css grid

I have the following simple jsx code for a template page with a four column layout (2 columns for padding and two for a content-left and content-right section).我有以下简单的 jsx 代码,用于具有四列布局的模板页面(2 列用于填充,两列用于内容左和内容右部分)。 One column contains the content pulled from a CMS and the other contains images, also pulled from the same CMS.一列包含从 CMS 提取的内容,另一列包含同样从同一 CMS 提取的图像。 I am using the grid system for the layout.我正在使用网格系统进行布局。

return(
        <div className="container">

            <div className="header">
              <Header pageTitle="My Recent Work"/>
            </div>

              <div className="pad-l"></div>
              
                <div className="content-left">
                  <h1>{props.data.contentfulProject.projectTitle}</h1>
                  <h3>About the project</h3>
                  <div className="jsonData">
                  {documentToReactComponents(props.data.contentfulProject.postDescription.json,options)}
                  </div>
                </div>

                <div className="content-right">

                </div>

              <div className="pad-r"></div>
        </div>
    )
}

Which is styled using css grid as使用 css 网格作为样式

body{
    margin:0;
    padding:0;
    box-sizing: border-box;
}

.content-left{
    grid-area: 'content-left';
}

.content-right{
    grid-area: 'content-right';
}

.pad-l{
    grid-area: 'pad-l';
    content: '';
}

.pad-r{
    grid-area: 'pad-r';
    content: '';
 
}

.container{
    background: #fff;
    display: grid;
    grid-template-areas: "header header header header"
    "pad-l content-left content-right pad-r";
}

.header{
    grid-area: header;
}

.content-left h1{
    padding-top: 2rem;
    color:#5297e6;
}

The paragraph elements which are dynamically inserted into the div with class name "jsonData" appear to be stretching the parent div to the full width of the page as shown below:使用 class 名称“jsonData”动态插入到 div 中的段落元素似乎将父 div 拉伸到页面的整个宽度,如下所示:

段落拉伸父 div

What I'm trying to achieve is something like what is shown below where the paragraph element width will fit into the div content-left which is part of a four column grid spaced 1fr 1fr 1fr 1fr .我想要实现的是类似于下面显示的内容,其中段落元素宽度将适合 div content-left ,它是间隔1fr 1fr 1fr 1fr的四列网格的一部分。 I tried setting the parent element to display as inline-block and the child element to display as flex-grow as suggested here however this wasn't quite what I was looking for.我尝试按照此处的建议将父元素设置为显示为 inline-block 并将子元素设置为显示为 flex-grow 但这并不是我想要的。 How can I make the grid columns inflexible and the paragraphs stretch to the width of the grid columns rather?如何使网格列不灵活,而段落延伸到网格列的宽度?

目标样式

I was dealing with it right now.我现在正在处理它。 The only option that suited me was to set a fixed parent width适合我的唯一选择是设置固定的父宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM