html - 如何相对于其容器调整项目的大小和位置
问题描述
我对 HTML 完全陌生,我想相对于其容器定位和调整项目的大小。我复制了这段代码(https://codepen.io/Tectonics0x3F/pen/EfAgr),如果我将顶部和左侧的百分比更改为 25% 和 25%,例如,盒子在容器顶部下方 25% 处,并且如果我改变高度和宽度,在正确的方向上增加 25%,与盒子的大小相同,盒子的高度为容器高度的 25%,宽度为容器长度的 25%。但是,如果我在我的代码中这样做,那么 Boxes 和 Container 就会随处可见。我认为问题在于盒子并不真正在容器中,但我不知道该怎么做。如果你需要知道这一点,我想知道,但我想做的是一个带有容器中这些框的菜单。提前感谢您的帮助,并为我的英语不好感到抱歉。
我的代码
body{
background-image:url('../images/bg24.jpg');
color: #fff;
font-family: Arial, Helvetica, sans-serif;
font-size:16px;
line-height: 1.6em;
margin:0;
background-repeat:repeat;
margin: auto;
}
#container {
height:70%;
width:90%;
margin:auto;
top:25%;
left:5%;
border: 5px solid white;
position:relative;
}
.button {
background-color: #474B4F;
border: 2px solid Black;
color: white;
padding: 5% 5%;
text-align: center;
text-decoration: none;
display: inline;
font-size: 20px;
cursor: pointer;
border-radius: 5px;
box-sizing: border-box;
}
#button1{
top: 0;
left: 0;
position:absolute;
}
#button2{
margin-top:0;
margin-left: 25%;
position: absolute;
}
<body>
<div id="container">
<div id="button1">
<a href="Somepage.html" class="button">clickme1</a>
<div id="button2">
<a href="Somepage2.html" class="button">clickme2</a>
</div>
</div>
</body>
解决方案
这是实现菜单的最简单方法,如果您想要更详细的演练,请回复。
.button {
background-color: #474B4F;
border: 2px solid Black;
color: white;
padding: 5% 5%;
text-align: center;
text-decoration: none;
font-size: 20px;
cursor: pointer;
border-radius: 5px;
box-sizing: border-box;
}
#button1,#button2 {
margin:12px;/* If you want spacing between them */
display:inline-block;
}
<body>
<div id="container">
<div id="button1">
<a href="Somepage.html" class="button">clickme1</a>
</div>
<div id="button2">
<a href="Somepage2.html" class="button">clickme2</a>
</div>
</div>
</body>
推荐阅读
- javascript - 解析 package.json 数据失败,package.json 必须是实际的 JSON,而不仅仅是 JavaScript
- context-free-grammar - 从语言生成上下文无关语法
- json - 附加到 JSON 数组的 Powershell 脚本
- apache-pig - 分组时如何获取包中的第一个元组
- r - 合并时间和日期列时不需要的 NA
- javascript - 当注册(通过散列)和登录在 2 个不同的 javascript 函数中时,如何将密码与数据库中的散列密码进行比较?
- python - 输出英国地址的不同变体的 Python 或 R 函数
- javascript - 如何编写通过 websocket 发送的文件?
- ruby-on-rails - 如何使用 Nokogiri 解析带有非对标签的 XML
- javascript - 从 phpadmin 中删除记录