如何使用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 class="esqcima">
  <p>Esquerda em Cima</p>
</div>
<div class="esqbaixo">
  <p>Esquerda em Baixo</p>
</div>
<div class="dircima">
  <p>Direita em Cima</p>
</div>
<div class="dirbaixo">
  <p>Direita em Baixo</p>
</div>

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

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

1

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

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

我的 HTML 代码

    <div class="parentDiv">
    <div class="esqcima">
        <p>Esquerda em Cima</p>
    </div>
      
    <div class="esqbaixo">
        <p>Esquerda em Baixo</p>
    </div>
      
    <div class="dircima">
        <p>Direita em Cima</p>
    </div>
      
    <div class="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 class="esqcima">
  <p>Esquerda em Cima</p>
</div>
<div class="esqbaixo">
  <p>Esquerda em Baixo</p>
</div>
<div class="dircima">
  <p>Direita em Cima</p>
</div>
<div class="dirbaixo">
  <p>Direita em Baixo</p>
</div>

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

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

相关推荐

  • css画一个圆:圆形的美丽

    使用css画一个圆,可以使用`border-radius`属性:上面的代码将会创建一个宽度和高度都是200px的圆形div。…

    2023-06-03 12:24:22
    0 14 63
  • css字间距怎么设置:标题

    示例示例CSS中字间距是指文本中每个字符之间的距离。可以使用CSS中的`letter-spacing`属性来设置字间距。代码示例:…

    2023-10-06 11:53:52
    0 26 80
  • css详细教程:CSS Tutorial

    CSS(Cascading Style Sheets)是用来控制网页的样式表。它可以让你改变文字的大小,颜色,字体,对齐方式,背景图片,布局等等。…

    2023-07-03 01:58:25
    0 79 86
  • html5和css3的新特性:Welcome to the Future of Web Design!

    示例示例HTML5新特性(新的语义元素…

    2023-05-20 12:24:46
    0 23 97
  • css 背景色 透明:Let Your True Colors Shine Through!

    示例示例CSS 背景色透明可以使用 rgba 函数来实现,其中 a 代表 alpha 通道,取值范围为 0-1,0 代表完全透明,代表完全不透明。示例代码:…

    2023-06-22 06:58:38
    0 75 58
  • html css引用:这是一个标题

    示例示例HTML CSS引用是指在HTML文件中通过标签引用CSS文件,使HTML文件能够使用CSS文件中的样式设置来控制其外观。代码示例:…

    2023-06-04 14:35:55
    0 46 82
  • css底部对齐:Welcome to Our Website

    示例示例css底部对齐指的是将多个元素的底部对齐,使用CSS实现底部对齐可以使用flex布局。代码示例:…

    2023-05-01 13:46:07
    0 31 23
  • css 第三方字体:Welcome to my Website!

    示例示例CSS 第三方字体是指使用外部字体文件的 Web 字体,它们可以在网站上使用而不需要安装到本地计算机上。示例代码:…

    2023-11-10 12:47:36
    0 73 80

发表评论

登录 后才能评论

评论列表(39条)