【发布时间】:2018-11-20 14:05:23
【问题描述】:
如何将我的 css 代码应用为 app.js 中的样式组件? 如何将此“.container > div”转换为样式组件并在我的 app.js 中使用它。通过 npm install 安装 styled-component 然后导入它。我被困在这里。我无法在样式组件中应用某些 CSS 样式。
App.js
import React, { Component } from 'react';
import './App.css';
class App extends Component{
render(){
return (
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
);
}
}
export default App;
App.css
.container
{
display: grid;
grid-template-columns: auto auto auto;
grid-template-rows: 50px 50px;
}
.container > div
{
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
color: #ffeead;
}
html, body
{
background-color: #ffeead;
margin: 10px;
}
.container > div:nth-child(1n)
{
background-color: #96ceb4;
}
.container > div:nth-child(3n)
{
background-color: #88d8b0;
}
.container > div:nth-child(2n)
{
background-color: #ff6f69;
}
.container > div:nth-child(4n)
{
background-color: #ffcc5c;
}
【问题讨论】:
标签: javascript css reactjs styled-components