简体   繁体   English

div 或 p 标记在 flex box 包装内

[英]div or p tag inside of flex box wrapping

Is there a way to have the logo text div tag called Title take up its content space inside the parent flexbox instead of wrapping.有没有办法让名为 Title 的 logo 文本 div 标签占据父 flexbox 内的内容空间,而不是包装。

I don't want to set the Title which is just a div to 100% or use white-space:nowrap.我不想将只是一个 div 的 Title 设置为 100% 或使用 white-space:nowrap。 I just want it to act like a div where it fills its content space and only wraps if it needs too.我只是希望它像一个 div 一样填充其内容空间,并且仅在需要时才进行包装。

Also why does this happen when another element is set to 100%.还有为什么当另一个元素设置为 100% 时会发生这种情况。 Like the code snippet below.就像下面的代码片段。

 const styled = window.styled; const Header = styled.div ` width: 100%; display: flex; border: 1px solid gray; align-items: center; width: 100%; min-height: 50px; padding: 10px 10px 10px 30px; background-color: #147189; color: white; `; const Title = styled.div ` `; const TestDiv = styled.div ` width: 100%; border: 1px solid red; `; const App = () => { return ( <div> <Header > <Title > Logo Text </Title> <TestDiv > hello </TestDiv> </Header> </div> ); } ReactDOM.render(<App/>, document.getElementById("react") );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <script src="//unpkg.com/styled-components@4.0.1/dist/styled-components.min.js"></script> <div id="react"></div>

Remove width: 100% from the TestDiv definition and add margin-left: 2rem (or whatever value you wish) to separate it from the other flex item (ie the Title component):TestDiv定义中删除width: 100%并添加margin-left: 2rem (或您希望的任何值)以将其与其他 flex 项(即Title组件)分开:

const TestDiv = styled.div `
    margin-left: 2rem; 
    border: 1px solid red; 
`;

Alternatively, you could set the widths for the flex items as percentages (totaling 100%) of their parent's ( Header's) width, like below:或者,您可以将弹性项目的宽度设置为其父项( Header's)宽度的百分比(总计 100%),如下所示:

const Title = styled.div `
    width: 10%;
`;

const TestDiv = styled.div `
    width: 90%;
    border: 1px solid red;
`;

Since you did not want to use whitespace: nowrap , I guess a workaround would be to parameterise the width of the Title div according to the title text.由于您不想使用whitespace: nowrap ,我想一种解决方法是根据标题文本参数化Title div 的宽度。 I set width of Title to be equal to width + 3 ch (3 extra just for padding and make sure it does not wrap).我将Titlewidth设置为等于width + 3 ch (另外 3 个仅用于填充并确保它不会换行)。 I guess if this might get too long, you could set max-width: 25ch or something like that.我想如果这可能会变得太长,你可以设置max-width: 25ch或类似的东西。

Hope this helps.希望这可以帮助。

 const styled = window.styled; const Header = styled.div ` width: 100%; display: flex; border: 1px solid gray; align-items: center; width: 100%; min-height: 50px; padding: 10px 10px 10px 30px; background-color: #147189; color: white; `; const Title = styled.div ` `; const TestDiv = styled.div ` width: 100%; border: 1px solid red; `; const style = () => ({}); const setWidth = (width) => ({ width: `${width + 3}ch`, }); const App = () => { return ( <div> <Header > <Title style={ setWidth('Logo Text Whatever'.length) } > Logo Text Whatever </Title> <TestDiv > hello </TestDiv> </Header> </div> ); } ReactDOM.render(<App/>, document.getElementById("react") );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <script src="//unpkg.com/styled-components@4.0.1/dist/styled-components.min.js"></script> <div id="react"></div>

如何访问`<div> `标签内的`<p> `标签? </p></div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $("p div").click(function() { $(this).css({ "color": "#F00", "font": "bold 20px verdana", "background-color": "#0FF" }); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;p&gt;Hello&lt;div&gt; World!&lt;/div&gt;&lt;/p&gt;</pre></div></div><p></p><p> 当我单击“世界”文本时,什么也没有发生,当我检查元素时,它说:</p><pre class="lang-html prettyprint-override"> &lt;p&gt;Hello&lt;/p&gt; &lt;div&gt; World&lt;/div&gt; &lt;p&gt;&lt;/p&gt;</pre><p> 注意那些&lt;p&gt;标签。 怎么会这样?</p></div> - How to access a `<div>` tag inside a `<p>` tag?

暂无
暂无

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

相关问题 将DIV包装在Anchor标签内或其他位置 - Wrapping DIV inside Anchor tag or otherwise 如何访问`<div> `标签内的`<p> `标签? </p></div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $("p div").click(function() { $(this).css({ "color": "#F00", "font": "bold 20px verdana", "background-color": "#0FF" }); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;p&gt;Hello&lt;div&gt; World!&lt;/div&gt;&lt;/p&gt;</pre></div></div><p></p><p> 当我单击“世界”文本时,什么也没有发生,当我检查元素时,它说:</p><pre class="lang-html prettyprint-override"> &lt;p&gt;Hello&lt;/p&gt; &lt;div&gt; World&lt;/div&gt; &lt;p&gt;&lt;/p&gt;</pre><p> 注意那些&lt;p&gt;标签。 怎么会这样?</p></div> - How to access a `<div>` tag inside a `<p>` tag? 在 <div> 并将文字放在标签内 <p></ p> - Search a word within a <div> and put text inside a tag <p> </ p> 将.addEventListener包装在div标签中 - Wrapping a .addEventListener in a div tag Flex 盒子里面有 2 个 div 一个里面有 canvas - Flex box having 2 div inside it one having canvas inside it jQuery:使用insertAfter方法在div标签内创建p标签 - jQuery: Use insertAfter method to create p tag inside div tag 中的图片 <p> div内的代码变得大于div的大小 - image in a <p> tag that is inside a div is coming larger than the div size 在TinyMCE中将标签包装在标签内 - Wrapping a tag inside a tag in TinyMCE 如何使用 vue 在 div 中获取 img 和 p 标签? - How to get img and p tag inside div using vue? 动态地将对象值添加到div里面 <p> 标签 - Dynamically add object value to div inside <p> tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM