Bootstrap宽度类/属性不起作用

huangapple go评论49阅读模式
英文:

Bootstrap width class/attribute not working

问题

在下面的代码片段中,通过使用Chrome开发工具检查,可以看到divWithNoWidthSet元素没有设置其width属性,尽管它具有w-80和/或mw-80类。我可以在生成的源代码中看到class属性,但在检查面板中没有width属性存在。

我在Stack Overflow 上看到过一些帖子,类似的行为原因是div元素包含一个具有img-fluid类的图像,但在这里不是这种情况。任何帮助都将不胜感激 - 谢谢!

<div className="m-auto w-30 rounded d-block" style={{ height: '300px', backgroundColor: 'whitesmoke' }}>
  <div style={{ height: '150px' }}>
    <a href={...} target="_blank" rel="noopener"><img src={...`} className="img-fluid h-150 hm-150 mw-50 d-block m-auto" alt={...} /></a>
  </div>
  <a href={...} target="_blank" rel="noopener">
    <div id="divWithNoWidthSet" className="w-80 mw-80 text-center m-auto p-3" style={{border: '1px solid red'}}>
      <h5>{...}</h5>
      <p>{...}</p>
    </div>
  </a>
</div>

Bootstrap宽度类/属性不起作用
Bootstrap宽度类/属性不起作用

英文:

Using bootstrap 4.5 (with NextJS 11.1 on ReactJS 18.2)

In the below snippet, per inspection with Chrome dev tools, the divWithNoWidthSet div is not getting its width attribute set despite having w-80 and/or mw-80 class. I can see the class attribute in generated source, but no width attribute exists in the inspection panel.

I saw posts on SO where the cause of similar behavior was the div containing an image with img-fluid class, but such is not the case here. Any help appreciated--thanks!

&lt;div className=&quot;m-auto w-30 rounded d-block&quot; style={{ height: &#39;300px&#39;, backgroundColor: &#39;whitesmoke&#39;}}  &gt;
  &lt;div style={{ height: &#39;150px&#39;}} &gt;
	&lt;a href={...} target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src={...`}  className=&quot;img-fluid h-150 hm-150 mw-50 d-block m-auto&quot; alt={...} /&gt; &lt;/a&gt;
  &lt;/div&gt;
  &lt;a href={...} target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;
	&lt;div id=&quot;divWithNoWidthSet&quot; className=&quot;w-80 mw-80 text-center m-auto p-3&quot; style={{border: &#39;1px solid red&#39;}}&gt;
	  &lt;h5&gt;{...}&lt;/h5&gt;
	  &lt;p&gt;{...}&lt;/p&gt;
	&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt; 

Bootstrap宽度类/属性不起作用
Bootstrap宽度类/属性不起作用

答案1

得分: 1

根据Bootstrap官方网站上的说明:

宽度和高度工具是从_variables.scss中的$sizes Sass映射生成的。默认情况下,支持25%50%75%100%auto。根据需要修改这些值以生成不同的工具。

如果您想使用w-80类,则需要将其添加到_variables.scss中。默认情况下,没有w-80类。

英文:

As stated on official Bootstrap website:

> Width and height utilities are generated from the $sizes Sass map in
> _variables.scss. Includes support for 25%, 50%, 75%, 100%, and auto by default. Modify those values as you need to generate different
> utilities here.

If you want to use the class w-80 then you need to add it in _variables.scss. By default, there's no w-80 class.

huangapple
  • 本文由 发表于 2023年2月6日 03:12:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定