如何使用CSS在HTML中定位元素

好的,这是一个非常初学者的问题。但我是初学者,所以很好

好的,这是一个非常初学者的问题。但我是初学者,所以很好

我最近开始使用 CSS,为了将我的确认保存在我的大脑中,我搜索了一些练习。

我看到这个基本的 HTML:

和以下挑战:

Create a CSS that allow doing this:

所以,我不浪费时间,让我自己的版本,有去马 code:

.esqcima {
  background-color: red;
  height: 100px;
  width: 100px;
}
.esqbaixo {
  background-color: green;
  height: 100px;
  width: 100px;
  position: relative;
  top: 230px
}
.dircima {
  background-color: yellow;
  height: 100px;
  width: 100px;
  position: relative;
  bottom: 232px;
  left: 500px
}
.dirbaixo {
  background-color: blue;
  height: 100px;
  width: 100px;
  position: relative;
  left: 500px
}
<div cl="esqcima">
  <p>Esquerda em Cima</p>
</div>
<div cl="esqbaixo">
  <p>Esquerda em Baixo</p>
</div>
<div cl="dircima">
  <p>Direita em Cima</p>
</div>
<div cl="dirbaixo">
  <p>Direita em Baixo</p>
</div>

它的工作原理,但似乎有点怪我

你们有一个更简单的方法来做到这一点?Thx 的关注:)

1

所以我也不专业,但我会与你分享我的解决方案。

所以首先你最好把所有的正方形放在一个父 div 中,有很多原因,例如你更好地控制它们,我稍后会解释

我的 HTML 代码

    <div cl="parentDiv">
    <div cl="esqcima">
        <p>Esquerda em Cima</p>
    </div>
      
    <div cl="esqbaixo">
        <p>Esquerda em Baixo</p>
    </div>
      
    <div cl="dircima">
        <p>Direita em Cima</p>
    </div>
      
    <div cl="dirbaixo">
        <p>Direita em Baixo</p>
    </div>
</div>

这里是我的 CSS 代码

*{
   margin: 0%;
   padding: 0%;
   box-sizing: border-box;
}
.parentDiv{
   width: 100%;
   height: 60vh;
   background-color: rgb(240, 240, 240);
   position: relative; 
}
.esqcima {
   background-color: red;
   height: 100px;
   width: 100px;
 } 
.esqbaixo {
  background-color: green;
  height: 100px;
  width: 100px;
   position: absolute;
   right: 0%;
   top: 0%;
 }
.dircima {
   background-color: yellow;
   height: 100px;
   width: 100px;
   position: absolute;
   bottom: 0%;
}
.dirbaixo {
  background-color: blue;
  height: 100px;
  width: 100px;
  position: absolute;
  bottom: 0%;
  right: 0%;
}

所以当你把它们放在一个父 div 上,这个父 div 有一个相对的位置,你可以把绝对的位置放在子 div 上(我很抱歉我的英语水平),然后你可以使用百分比来控制它们,因为你看到绿色正方形例如在 cssposition: absolute;right: 0%中,这意味着正方形将在右边,就像其他正方形一样

祝你好运

0

CSS 中的定位实际上非常简单,有多种方法,例如 position:static;对于将始终保持在一个位置并且不受任何位置影响的元素:relative;对于将处于正常位置直到更改位置的元素:fixed;对于即使在滚动位置后仍将保持不变的元素:absolute;对于将相对于最近定位的元素文本对齐的元素:(中心,左侧或右侧);对于文本

注意:所有可以修改左,右,顶部和底部,除了静态

0

你可以使用网格做类似的事情。

body {
  display: grid;
  height: 800px;
  width: 100%;
  grid-template-areas: "red  yellow" "green blue";
}
div {
  height: 100px;
  width: 100px;
}
.esqcima {
  background-color: red;
  grid-area: red;
}
.esqbaixo {
  background-color: green;
  grid-area: green;
  justify-self: start;
  align-self: end;
}
.dircima {
  background-color: yellow;
  grid-area: yellow;
  justify-self: end;
}
.dirbaixo {
  background-color: blue;
  grid-area: blue;
  justify-self: end;
  align-self: end;
}
<div cl="esqcima">
  <p>Esquerda em Cima</p>
</div>
<div cl="esqbaixo">
  <p>Esquerda em Baixo</p>
</div>
<div cl="dircima">
  <p>Direita em Cima</p>
</div>
<div cl="dirbaixo">
  <p>Direita em Baixo</p>
</div>

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(58)
角4:如何从Excel中读取数据
上一篇
使用标准对 actix_webapi进行基准测试时出现问题
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(58条)