英文:
Div not taking height mentioned in the css
问题
我对HTML和CSS很新,并且我在练习flexbox布局以将多个div居中。但我觉得这里有些问题。
这里contains
类中的第二个div没有占用任何高度或宽度,也没有背景颜色。但当我通过检查工具应用相同的属性时,它按预期工作。
我期望这两个div都应该有50px的高度和宽度,并且背景颜色为红色。
.contains {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.div1 {
height: 50px;
width: 50px;
background-color: red;
}
<div class="contains">
<div class="div1">1</div>
<div clsss="div1">2</div>
</div>
英文:
I am new to HTML and CSS and I was practicing flexbox layout to center multiple divs. but I think there's something wrong here.
Here the second div inside the contains
class is not taking any height or width and background color. But when I apply these same properties via inspect tool. It works as expected.
I was expecting that both the divs should have height and width of 50px and background color red.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.contains {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.div1 {
height: 50px;
width: 50px;
background-color: red;
}
<!-- language: lang-html -->
<div class="contains">
<div class="div1">1</div>
<div clsss="div1">2</div>
</div>
<!-- end snippet -->
答案1
得分: 2
你的HTML中有个拼写错误:你写成了 clsss
而不是第二个 div
元素的 class
。
修复这个拼写错误会得到预期的结果:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.contains {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.div1 {
height: 50px;
width: 50px;
background-color: red;
}
<!-- language: lang-html -->
<div class="contains">
<div class="div1">1</div>
<div class="div1">2</div>
</div>
<!-- end snippet -->
请注意,我只翻译了你提供的内容,不包括代码部分。
英文:
There is typo in your html: you wrote clsss
instead of class
for the second div.
Fixing this typo gives the intended result:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.contains{
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.div1{
height: 50px;
width: 50px;
background-color: red;
}
<!-- language: lang-html -->
<div class="contains">
<div class="div1">1</div>
<div class="div1">2</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论