css - 没有额外容器的多个网格元素周围的边框?

标签 css border grid-layout css-grid

我试图了解在网格中执行此类操作的正确方法。因为我不确定我是否做对了。

正如问题所暗示的,主要是关于在没有额外容器的情况下围绕网格元素设置边界。但是,如果您在这里,在阅读了问题的标题后,非常感谢您做出贡献或改进 以任何可能的方式。

想法|问题

Straight forward example of how everything should look after problem is solved

我需要用元素制作单列网格布局:

  • 标题
  • 主要
  • 页脚

那应该以浏览器为中心。

然后所有这三个元素都应该有一个边框。

此外,随着更多元素被添加到 <main> 中,布局必须能够在 heigh 中扩展元素。

限制:

  1. 无需创建额外的容器。
  2. 不使用边框 Individual Sides 属性
    • 上边框
    • 右边界
    • 边框底部
    • 左边框

到目前为止我做了什么和尝试了什么:

  1. 设置 html, body {height: 100%;}
  2. 制作<html>元素网格。
  3. 制作<body>元素网格。
  4. 在 Grid 中定位 Grid 元素,它是单列单行
    • place-self: auto center; justify-self: center;
  5. <body>周围画了一个边框元素。

	html, body {height: 100%;}
	
	html {
		display: grid;
		grid-template-rows: 1fr;
		grid-template-columns: 1fr;
		
	}
	
	body {
		border: solid 5px black;
		width: 50%;
		
		display: grid;
		grid-template-rows: 50px auto 50px;
		grid-template-columns: 1fr;
		place-self: auto center;
		justify-self: center;
		
	}
	
	
	header { background-color: #0000b7;}
	main { background-color: blue;}
	footer { background-color:lightblue;}
	
	
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Home Page</title>
	<style>
  	
	
	</style>
</head>
<body>
	<header></header>
	<main>
	
	</main>
	<footer></footer>
</body>
</html>

最佳答案

您的 place-self:center 使正文顶部略高于 html 文档。使用 place-self:auto center; 应该可以解决这个问题。

html, body {height: 100%;}
	
	html {
		display: grid;
		grid-template-rows: 1fr;
		grid-template-columns: 1fr;
		
	}
	
	body {
		border: solid 5px black;
		width: 50%;
		margin-top:5px;
		display: grid;
		grid-template-rows: 50px auto 50px;
		grid-template-columns: 1fr;
		place-self: auto center;
		justify-self: center;
		
	}
	
	
	header { background-color: #0000b7;}
	main { background-color: blue;}
	footer { background-color:lightblue;}
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Home Page</title>
	<style>
  	
	
	</style>
</head>
<body>
	<header></header>
	<main>
	
	</main>
	<footer></footer>
</body>
</html>

关于css - 没有额外容器的多个网格元素周围的边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50399519/

相关文章:

css - 使边框环绕整个窗口会导致显示垂直滚动条吗?

WPF 更改网格和网格边框不透明度而不影响子项

qt - 如何从左上到右填充 QGridLayout?

css - onclick css - 边框半径不起作用

javascript - 如何将一组html元素包装在一个div中

php - 在 Yii 2.0 (Menu::Widget) 中使用跨度?

css - Angular ui bootstrap 轮播不工作 - 显示静态图像

java - JPanel 在绘制图像时显示奇怪的错误

java - 如何设置GridLayout第一列的边距?

jquery - 如何在 Foundation 中实现 mobile "top-bar"?