user8473431
user8473431

Reputation: 119

How do I center h1 in div?

I set both "top" and "left" to "50%" and "position" to "absolute" - the .supporting h1 is centered in div ONLY when the window is minimised. I thought setting position to absolute would not do that. When the window is enlarged, the .supporting h1 moves towards bottom of div.

How can I fix this? Still new to html/css and learning as I go!

not centered

html,
body {
  font-family: ;
  margin: 0 auto;
  text-transform: lowercase;
}

.container {
  margin: 0 auto;
  padding: 0 !important;
}

.nav {
  float: right;
  display: inline;
  margin: 0 auto;
  list-style: none;
}

.nav li {
  list-style: none;
  display: inline;
  padding-top: 0;
  padding-left: 10px;
}

.nav>li>a {
  padding: 0px !important;
  color: black;
}

a:hover {
  background-color: yellow;
}

.header {
  width: 100%;
  display: inline-block;
  padding: 10px 0 0 0;
  margin: 0 auto;
}

.header h1 {
  font-size: 20px;
  margin: 0 auto;
  display: inline-block;
}

.header .nav {
  padding-left: 10px;
}

.supporting h1 {
  border-left: 0px solid black;
  padding: 10px;
  margin: 0 auto;
  display: inline-block;
  top: 50%;
  left: 50%;
  position: absolute;
}

.supporting .container-fluid {
  background: rgba(225, 225, 225, .8);
  height: 500px;
}

.work .container-fluid {
  height: 500px;
  margin: 0 auto;
  background: white);
  width: 100%;
  padding: 0;
}

div.container-fluid {
  padding: 0;
  width: 100%;
  height: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
  <div class="container">
    <div class="header">
      <h1 class="logo">header</h1>
      <ul class="nav nav-pills">
        <li><a href="#">Projects</a></li>
        <li><a href="#">Photography</a></li>
        <li><a href="#">blog</a></li>
      </ul>
    </div>
  </div>
  <div class="supporting">
    <div class="container-fluid">
      <div class="container">
        <h1>hi.</h1>
      </div>
    </div>
  </div>
  <div class="work">
    <div class="container-fluid">
      <div class="container">
        <h1>work</h1>
      </div>
    </div>
  </div>
</div> //added this div

Upvotes: 1

Views: 2138

Answers (2)

kukkuz
kukkuz

Reputation: 42370

If you want to center using absolute positioning add position: relative to the supporting div and add transform: translate(-50%, -50%) to the h1 tag.

See demo below:

html,
body {
  font-family: ;
  margin: 0 auto;
  text-transform: lowercase;
}

.container {
  margin: 0 auto;
  padding: 0 !important;
}

.nav {
  float: right;
  display: inline;
  margin: 0 auto;
  list-style: none;
}

.nav li {
  list-style: none;
  display: inline;
  padding-top: 0;
  padding-left: 10px;
}

.nav>li>a {
  padding: 0px !important;
  color: black;
}

a:hover {
  background-color: yellow;
}

.header {
  width: 100%;
  display: inline-block;
  padding: 10px 0 0 0;
  margin: 0 auto;
}

.header h1 {
  font-size: 20px;
  margin: 0 auto;
  display: inline-block;
}

.header .nav {
  padding-left: 10px;
}
.supporting {
  position: relative;
}
.supporting h1 {
  border-left: 0px solid black;
  padding: 10px;
  margin: 0 auto;
  display: inline-block;
  top: 50%;
  left: 50%;
  position: absolute;
  transform: translate(-50%, -50%);
}

.supporting .container-fluid {
  background: rgba(225, 225, 225, .8);
  height: 500px;
}

.work .container-fluid {
  height: 500px;
  margin: 0 auto;
  background: white);
  width: 100%;
  padding: 0;
}

div.container-fluid {
  padding: 0;
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="container-fluid">
  <div class="container">
    <div class="header">
      <h1 class="logo">header</h1>
      <ul class="nav nav-pills">
        <li>
          <a href="#">Projects</a>
        </li>
        <li>
          <a href="#">Photography</a>
        </li>
        <li>
          <a href="#">blog</a>
        </li>
      </ul>
    </div>
  </div>

  <div class="supporting">
    <div class="container-fluid">
      <div class="container">
        <h1>hi.</h1>
      </div>
    </div>
  </div>

  <div class="work">
    <div class="container-fluid">
      <div class="container">
        <h1>work</h1>
      </div>
    </div>
  </div>

Upvotes: 3

Laeeq Khan Niazi
Laeeq Khan Niazi

Reputation: 648

you can not use container inside the container .. above you used container-fluid and inside that div you used container . this is wrong. just see documentation . you are not allowed to use container inside container

Upvotes: 1

Related Questions