html - 有没有人有更好的方法来做到这一点?我只是在学习如何使用 CSS

标签 html css

图像是目标。我需要在方框内正确定位每种颜色。它还必须位于浏览器的中心。我得到的结果正是图像只是文字。我真的在寻找一种更好的方法来做到这一点,因为我不得不猜测左边和底部的像素。我也知道它可以做得更好,并且真的很想学习如何做。

enter image description here

.border{
  border: 2px solid black;
  width: 500px;
  height: 700px;
  margin:25px 450px;
  padding: 1px;
}

#one {
  background: red;
  height:100px;
}

#two{
 background: yellow;
 height:600px;
 width:100px;
}

#three {
  background: blue;
  height:550px;
  width:300px;
  position: relative;
  left: 100px;
  bottom: 600px;
}

#four{
  background: yellow;
  height: 600px;
  width:100px;
  position: relative;
  left:400px;
  bottom: 1150px;
}

#five{
  background: green;
  height:50px;
  width:300px;
  position: relative;
  left: 100px;
  bottom: 1200px;
 }
   <div class="border">
    <div id="one">One</div>
    <div id="two">Two</div>
    <div id="three">Three</div>
    <div id="four">Four</div>
    <div id="five">Five</div>
   </div>

最佳答案

我个人会选择使用 percentage-based measurements ,这样你只需要更新 width height .border 本身。对于 .border,我选择了 vw and vh units 分别用于 widthheight,以便表格的大小根据视口(viewport)的大小进行调整。

请注意,您可能还希望使用类而不是 ID,这样您就可以拥有多个表。我在示例中使用了类,还替换了名称以使位置标识符更加明显。

我还选择为 #middle#bottom 元素创建一个容器,将它们视为单个“列”。这样,您就可以利用 float 来对齐 #left#middle_container#right 列无需担心 #bottomwidth

您可以通过添加 margin: 0 auto 使整个内容居中.border,这是声明不应该有任何垂直边距的简写,并且应该自动计算水平边距(它使相关元素水平居中)。

这可以从以下方面看出:

.border {
  border: 2px solid black;
  width: 50vw;
  height: 100vh;
  padding: 1px;
  margin: 0 auto;
}

.top {
  background: red;
  width: 100%;
  height: 20%;
}

.left,
.middle_container,
.right {
  float: left;
}

.left {
  background: yellow;
  width: 20%;
  height: 80%;
}

.middle_container {
  width: 60%;
  height: 80%;
}

.middle {
  background: blue;
  height: 80%;
}

.bottom {
  background: green;
  height: 20%;
  bottom: 0;
}

.right {
  background: yellow;
  width: 20%;
  height: 80%;
}
<body>
  <div class="border">
    <div class="top">Top</div>
    <div class="left">Left</div>
    <div class="middle_container">
      <div class="middle">Middle</div>
      <div class="bottom">Bottom</div>
    </div>
    <div class="right">Right</div>
  </div>
</body>

希望对您有所帮助! :)

关于html - 有没有人有更好的方法来做到这一点?我只是在学习如何使用 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47686170/

相关文章:

html - 为什么在邮件中发送时文本不居中?

javascript - 获取Json数据并在分割窗口onclick html上显示

html - 如何在 Bootstrap 中获取跨越父级整个宽度的按钮组?

html - 当 HTML 作为文本时,outlook 2013 中 2 个图像之间的垂直间隙

html - 如果 XHTML 不允许嵌套表单,那么如何/为什么验证这一点?这个代码可以使用吗?

css - 垂直居中跨度( Bootstrap 标签)与其他元素一起

jquery - 选择下拉菜单周围的蓝色边框?

javascript - jQuery - .val 到不保留换行符的文本区域

jquery - Div 在网站上的顺序和外观

HTML 按钮 "Push"效果