Background属性即使使用Tailwind和普通CSS也未被应用。

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

Background property is not being applied even using tailwind and normal css

问题

import React from "react";
import Navbar from "./Components/Navbar"

function App() {

  return (
    <div className="w-full h-full bg-amber-800"  >
      <Navbar/>
    </div>
  );
}

export default App;

This App.jsx


import react from "react";
import logo from "../assets/tesla.svg"


function Navbar(){

    return(
        <nav className="w-full tex-black-100 bg-transparent h-6 flex flex-row absolute z-1 top-4  ">
        
        <ul className="flex flex-row w-full" >

        <span className="w-1/4 relative left-10 top-2">
         <img  src={logo} className="App-logo w-1/3  " alt="logo" />
        </span>

        <div className="flex flex-row w-1/2 justify-evenly" >

        
        <li><a href="">Model S</a></li>

        <li><a href="">Model 3</a></li>

        <li><a href="">Model X</a></li>

        <li><a href="">Model Y</a></li>
    
        <li><a href="">Solar Roof</a></li>
    
        <li><a href="">Solar Panel</a></li>
        </div>
    
        <div className="flex flex-row w-1/4 justify-evenly" >
        <li><a href="">Shop</a></li>
    
        <li><a href="">Account</a></li>
    
        <li><a href="">Menu</a></li>
        </div>
    
        </ul>
        </nav>
    )

}

export default Navbar;

This Navbar.jsx

Each and every tailwind property is working leaving bg-amber-800 in App.jsx

This is what it looks like

[![enter image description here](https://i.stack.imgur.com/ZQf3g.png)](https://i.stack.imgur.com/ZQf3g.png)

Leaving bg-amber-800 background property all of the others are working.

What am I doing wrong?

https://codedamn.com/project/tesla-clone/code/PssQI88lIveSFpfO-oIMy

You can update here if you want

Make sure you open this in new tab it is important

[![enter image description here](https://i.stack.imgur.com/ajx2v.png)](https://i.stack.imgur.com/ajx2v.png)
英文:
import React from &quot;react&quot;;
import Navbar from &quot;./Components/Navbar&quot;
function App() {
return (
&lt;div className=&quot;w-full h-full bg-amber-800&quot;  &gt;
&lt;Navbar/&gt;
&lt;/div&gt;
);
}
export default App;
This App.jsx
import react from &quot;react&quot;;
import logo from &quot;../assets/tesla.svg&quot;
function Navbar(){
return(
&lt;nav className=&quot;w-full tex-black-100 bg-transparent h-6 flex flex-row absolute z-1 top-4  &quot; &gt;
&lt;ul className=&quot;flex flex-row w-full&quot; &gt;
&lt;span className=&quot;w-1/4 relative left-10 top-2&quot;&gt;
&lt;img  src={logo} className=&quot;App-logo w-1/3  &quot; alt=&quot;logo&quot; /&gt;
&lt;/span&gt;
&lt;div className=&quot;flex flex-row w-1/2 justify-evenly&quot; &gt;
&lt;li&gt;&lt;a href=&quot;&quot;&gt;Model S&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;&quot;&gt;Model 3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;&quot;&gt;Model X&lt;/a&gt;&lt;/li&gt;
    \&lt;li\&gt;\&lt;a href=&quot;&quot;\&gt;Model Y\&lt;/a\&gt;\&lt;/li\&gt;
\&lt;li\&gt;\&lt;a href=&quot;&quot;\&gt;Solar Roof\&lt;/a\&gt;\&lt;/li\&gt;
\&lt;li\&gt;\&lt;a href=&quot;&quot;\&gt;Solar Panel\&lt;/a\&gt;\&lt;/li\&gt;
\&lt;/div\&gt;
\&lt;div className=&quot;flex flex-row w-1/4 justify-evenly&quot; \&gt;
\&lt;li\&gt;\&lt;a href=&quot;&quot;\&gt;Shop\&lt;/a\&gt;\&lt;/li\&gt;
\&lt;li\&gt;\&lt;a href=&quot;&quot;\&gt;Account\&lt;/a\&gt;\&lt;/li\&gt;
\&lt;li\&gt;\&lt;a href=&quot;&quot;\&gt;Menu\&lt;/a\&gt;\&lt;/li\&gt;
\&lt;/div\&gt;
\&lt;/ul\&gt;
\&lt;/nav\&gt;
)

}

export default Navbar;

This Navbar.jsx

Each and every tailwind property is working leaving bg-amber-800 in App.jsx

This is what it looks like

Background属性即使使用Tailwind和普通CSS也未被应用。

Leaving bg-amber-800 background property all of the others are working.

What am I doing wrong?

https://codedamn.com/project/tesla-clone/code/PssQI88lIveSFpfO-oIMy

You can update here if you want

Make sure you open this in new tab it is important

Background属性即使使用Tailwind和普通CSS也未被应用。

答案1

得分: 1

使用 h-screen 而不是 h-full,以匹配视口的高度。

英文:

Use h-screen instead of h-full for the height to match the viewport's height.

答案2

得分: 1

这实际上是有效的。
根据我的理解,您希望App组件占据整个高度并应用bg-amber样式。问题在于您使用h-full(height: 100%)来设置高度,而应该使用h-screen(height: 100vh)。
当您在元素上设置height: 100%时,该元素将采用其直接父元素的高度,而该父元素必须具有明确定义的高度(例如height: 500px),以便该元素继承该高度,而APP组件的直接父组件是body。
100vh(视口高度)是浏览器中可见部分的高度的100%(在浏览器中可见的屏幕大小)。无论元素在DOM层次结构中的位置如何,100vh都将占据浏览器窗口的整个可见高度。

英文:

It is actually working.
From what I understand, you want the App component to occupy the entire height and apply bg-amber to it. The problem is that you set the height with h-full(height: 100%) while you should have set it with h-screen(height: 100vh).
When you set height: 100% on an element, that element takes the height of its direct parent element that has a defined height. This means that the element must have a parent with an explicitly defined height (eg height: 500px) for it to inherit that height, in which case the direct parent component of the APP component is the body.
100vh (viewport height) is 100% of the viewport height (the size of the screen visible in the browser). No matter where the element is in the DOM hierarchy, 100vh will take up the entire visible height of the browser window.

huangapple
  • 本文由 发表于 2023年7月20日 21:52:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730606.html
匿名

发表评论

匿名网友

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

确定