gpt4 book ai didi

html - 如何将 CSS 中的三列平均分布在一个页面上

转载 作者:太空宇宙 更新时间:2023-11-03 20:33:29 24 4
gpt4 key购买 nike

所以我查看了关于 SE 和 Google 结果的其他问题,但要么它们太过胶带式(使用魔术数字,即边距或填充中的 X 像素数量或其他,我不喜欢,因为它是不可扩展)或使用某些其他搜索结果中的某人说我不应该使用的方法,除非作为最后的手段(如 float: left),或者他们正在做我做过的事情但无论出于何种原因都不起作用。

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GDb</title>
<link rel="stylesheet" type="text/css" href="style.css" media="screen">
</head>
<body>
<header>
<nav>
<ul class="NavBanner">
<li><a class="Activatable" href="Home.html">Home</a></li>
<li><a class="Activatable" href="#">Search</a></li>
<li><a class="Active" href="#">Browse</a></li>
</ul>
</nav>
</header>

<div class="MainContent">
<div class="Column">
<div class="TextContainer">
<h1>By Genre</h1>
<h2>Action</h2>
<div>
<ul>
<li>Rainbow Six: Siege</li>
</ul>
</div>
<h2>Adventure</h2>
<div>
<ul>
<li>Sunless Sea</li>
<li>Alien: Isolation</li>
</ul>
</div>
<h2>Strategy</h2>
<div>
<ul>
<li>
Stellaris
</li>
</ul>
</div>
<h2>RPG</h2>
<div>
<ul>
<li>
The Witcher 3: Wild Hunt
</li>
</ul>
</div>
</div>
</div>

<div class="Column">
<div class="TextContainer">
<h1>By Developer</h1>
<h2>CD Projekt RED</h2>
<div>
<ul>
<li>
The Witcher 3: Wild Hunt
</li>
</ul>
</div>
<h2>Creative Assembly</h2>
<div>
<ul>
<li>Alien: Isolation</li>
</ul>
</div>
<h2>Failbetter Games</h2>
<div>
<ul>
<li>Sunless Sea</li>
</ul>
</div>
<h2>Paradox Interactive</h2>
<div>
<ul>
<li>
Stellaris
</li>
</ul>
</div>
<h2>Ubisoft</h2>
<div>
<ul>
<li>Rainbow Six: Siege</li>
</ul>
</div>
</div>
</div>

<div class="Column">
<div class="TextContainer">
<h1>By Year</h1>
<h2>2016</h2>
<div>
<ul>
<li>Stellaris</li>
</ul>
</div>
<h2>2015</h2>
<div>
<ul>
<li>Rainbow Six: Siege</li>
<li>Sunless Sea</li>
<li>The Witcher 3: Wild Hunt</li>
</ul>
</div>
<h2>2014</h2>
<div>
<ul>
<li>Alien: Isolation</li>
</ul>
</div>
</div>
</div>
</div>
</body>
</html>

CSS:

body
{
background: -webkit-linear-gradient(top, #b3b3b0, #e3e2dd);
background-color: #b3b3b0;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
margin: 0;
padding: 0;
}

.NavBanner
{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}

.Activatable
{
color: white;
text-decoration: none;
padding: 14px 16px;
float: left;
text-align: center;
}

.Activatable:hover:not(.active)
{
background-color: #111;
}

.Active
{
background-color: #4CAF50;
color: white;
cursor: default;
text-decoration: none;
padding: 14px 16px;
float: left;
text-align: center;
}

.MainContent
{
background-color: whitesmoke;
box-shadow: 0px 0px 8px rgba(0,0,0,0.7);
padding: 56px 14px 15px;
width: 1700px;
margin: auto;
position: relative;

}

.Column
{
text-align: center;
border: 1px solid #000;
padding: 10px;
margin: 0 10px;
display: inline-block;
width: 500px;
vertical-align: top;
}

.TextContainer
{
display: inline-block;
text-align: left;
}

我几乎得到了想要的结果,但右列右侧的空间比左列左侧多,因为列没有居中。我并不真正关心宽度或特定边距、填充或任何列,我只希望它们具有相同的宽度并居中,如果这有意义的话。

最佳答案

你可以使用 flex :

您可以简单地测试将这 2 条规则添加到您的样式表中:(下面的代码片段您可以自行查看:))

.MainContent {
display: flex;
}

.Column {
flex: 1;
}

body {
background: -webkit-linear-gradient(top, #b3b3b0, #e3e2dd);
background-color: #b3b3b0;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
margin: 0;
padding: 0;
}

.NavBanner {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}

.Activatable {
color: white;
text-decoration: none;
padding: 14px 16px;
float: left;
text-align: center;
}

.Activatable:hover:not(.active) {
background-color: #111;
}

.Active {
background-color: #4CAF50;
color: white;
cursor: default;
text-decoration: none;
padding: 14px 16px;
float: left;
text-align: center;
}

.MainContent {
background-color: whitesmoke;
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.7);
padding: 56px 14px 15px;
width: 1700px;
margin: auto;
position: relative;
display: flex;
}

.Column {
text-align: center;
border: 1px solid #000;
padding: 10px;
margin: 0 10px;
flex: 1;
vertical-align: top;
}

.TextContainer {
display: inline-block;
text-align: left;
}
    <nav>
<ul class="NavBanner">
<li><a class="Activatable" href="Home.html">Home</a></li>
<li><a class="Activatable" href="#">Search</a></li>
<li><a class="Active" href="#">Browse</a></li>
</ul>
</nav>
</header>

<div class="MainContent">
<div class="Column">
<div class="TextContainer">
<h1>By Genre</h1>
<h2>Action</h2>
<div>
<ul>
<li>Rainbow Six: Siege</li>
</ul>
</div>
<h2>Adventure</h2>
<div>
<ul>
<li>Sunless Sea</li>
<li>Alien: Isolation</li>
</ul>
</div>
<h2>Strategy</h2>
<div>
<ul>
<li>
Stellaris
</li>
</ul>
</div>
<h2>RPG</h2>
<div>
<ul>
<li>
The Witcher 3: Wild Hunt
</li>
</ul>
</div>
</div>
</div>

<div class="Column">
<div class="TextContainer">
<h1>By Developer</h1>
<h2>CD Projekt RED</h2>
<div>
<ul>
<li>
The Witcher 3: Wild Hunt
</li>
</ul>
</div>
<h2>Creative Assembly</h2>
<div>
<ul>
<li>Alien: Isolation</li>
</ul>
</div>
<h2>Failbetter Games</h2>
<div>
<ul>
<li>Sunless Sea</li>
</ul>
</div>
<h2>Paradox Interactive</h2>
<div>
<ul>
<li>
Stellaris
</li>
</ul>
</div>
<h2>Ubisoft</h2>
<div>
<ul>
<li>Rainbow Six: Siege</li>
</ul>
</div>
</div>
</div>

<div class="Column">
<div class="TextContainer">
<h1>By Year</h1>
<h2>2016</h2>
<div>
<ul>
<li>Stellaris</li>
</ul>
</div>
<h2>2015</h2>
<div>
<ul>
<li>Rainbow Six: Siege</li>
<li>Sunless Sea</li>
<li>The Witcher 3: Wild Hunt</li>
</ul>
</div>
<h2>2014</h2>
<div>
<ul>
<li>Alien: Isolation</li>
</ul>
</div>
</div>
</div>
</div>

关于html - 如何将 CSS 中的三列平均分布在一个页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37296783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com