OddCore OddCore - 6 months ago 12
HTML Question

How to spread three columns in CSS across a page equally

So I have looked at other questions on SE and from Google results but either they are too duct tape-y (using magic numbers, i.e. X amount of pixels in margin or padding or whatever, which I don't like cause it's not scalable) or using methods that someone in some other search result says I shouldn't unless as a last resort (like float: left) or they are doing things I have done but which for whatever reason don't work.

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;
}


I almost have the desired result, but there is more space on the right of the right column than on the left of the left column as the columns aren't centered. I don't really care about the width or specific margin, padding or whatever of the columns, I just want them to be the same width and to be centered, if that makes sense.

Answer

you may use flex :

You can simply test adding these 2 rules to your style sheet: (snippet below to see by yourself :) )

.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>