css - 如何在原生样式中计算()高度

标签 css react-native styles stylesheet calc

有一个灰色方块放置 4 个小方块
有一个黑色方块,在黑色方块的每边中心有4个小方块。
对于普通网页,我可以通过计算宽度来完成
但是我应该如何在 react native 中做到这一点,其中 calc(100% - 20) 在那里不起作用

请检查codepen中的代码:Codepen

索引.html

<!DOCTYPE html>

<head>
    <title>squares in square</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class='container'>
        <div class='invisiblesquare'>
            <div class='bigsquare'></div>
            <div class='col1'>
                <div class="smallsquare"></div>
            </div>
            <div class='col2'>
                <div class="smallsquare"></div>
                <div class="smallsquare"></div>
            </div>
            <div class='col3'>
                <div class="smallsquare"></div>
            </div>
        </div>
    </div>
    </div>


</body>

</html>

样式文件
.container{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    width: 100vw;

}
.invisiblesquare{
    position: relative;
    height: 20%;
    width:20%;
    border: 1px solid grey;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.bigsquare{
    position: absolute;
    width: calc(100% - 22px);
    height: calc(100% - 22px);
    border: 1px solid black;
}
.col1{
    height: 20px
}
.col2{
    height: calc(100% - 22px);
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
}
.col3{
    height: 20px
}

.smallsquare{
    height: 20px;
    width: 20px;
    border: 1px solid black
}

最佳答案

您可以使用以下方法获取屏幕尺寸,

import {Dimensions} from 'react-native';

const screenWidth = Dimensions.get('window').width;
const screenHeight = Dimensions.get('window').height;

然后你可以做你的计算如下,
width: screenWidth - 20

希望这对你有帮助。

关于css - 如何在原生样式中计算()高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60733361/

相关文章:

javascript - Codeigniter - 打印网页时遇到问题

ios - 将 native rootView 颜色更改为黑色?

android - 如何在一次交易中在 Firebase 上创建用户和上传数据

javascript - 如何设置 Google Chrome 语音输入控件的麦克风样式

java - 如何从我的代码在 textView 和 editText 中添加样式?

c# - WPF - 从触发器访问子项

css - 如何创建类似于图像填充效果的 CSS3 动画?

javascript - 当我将鼠标焦点放在元素上时,mouseenter 和 mouseleave 会继续触发

html - 不同视口(viewport)中的段落不在一起

reactjs - 如何正确为React Native Web Pressable组件添加onHoverIn TS类型?