OddCore
OddCore

Reputation: 1594

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.

Upvotes: 0

Views: 1437

Answers (6)

mooiamaduck
mooiamaduck

Reputation: 2156

You have extra space to the right because each of the columns has a set width of 522px, but the container has a width of 1700px. The columns align to the left, leaving 134px of empty space.

A nice trick is to use display: table on the container of the columns and display: table-cell on the columns themselves. Then, set a width of 33.3333...% on each of the columns. This way, the width of the columns will be determined automatically and does not need to be hard-coded.

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: 15px;
    width: 1700px;
    margin: auto;
    display: table;
}

.Column
{
    padding: 10px;
    display: table-cell;
    vertical-align: top;
    width: 33.333333%;
}

.TextContainer
{
    display: block;
    border: 1px solid #000;
    padding: 0 15px;
}

.TextContainer h1 {
    text-align: center;
}
<!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>

Upvotes: 1

Marc Audet
Marc Audet

Reputation: 46825

Your CSS is very close to what you need to achieve your layout.

There are two minor but important changes.

First, to .MainContent add the rule text-align: center and this will center your the inline-block .columns.

Second, to get your text to flow normally, add text-align: left to .column (text-align is inherited).

Note: You can use flex but I (personally) always hesitate because of current browser support (though this is always getting better).

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;
  text-align: center;
}
.Column {
  text-align: left;
  border: 1px solid #000;
  padding: 10px;
  margin: 0 10px;
  display: inline-block;
  width: 500px;
  vertical-align: top;
}
.TextContainer {
 }
<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>

Upvotes: 1

Priti
Priti

Reputation: 1

it will be easy if you go with bootstrap, just add bootstrap css files into your code and code like this:-

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

Upvotes: 0

Dhaval Chheda
Dhaval Chheda

Reputation: 5187

just add display:flex; in the parent container ( if not there then create 1) and in the child elements put flex:1; and they will occupy the full width of the parent..

Upvotes: 1

G-Cyrillus
G-Cyrillus

Reputation: 106078

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>

Upvotes: 2

Noam Gur
Noam Gur

Reputation: 65

Have you tried using bootstrap?

<div class="row">
   <div class="Column col-md-4"></div>
   <div class="Column col-md-4"></div>
   <div class="Column col-md-4"></div>
</div>

Upvotes: 0

Related Questions