0

I need to center a heading h1 inside of a div with .box class when the browser window width is less than 768 px.

.box {
  float: left;
  margin: 250px 0 0 0;
  height: 35%;
  width: 50%;
  background-image: url('img/header-bcg.png');
}
.box h1 {
  font-size: 40px;
  font-family: Bree serif;
  color: 33adae;
  margin-top: 30px;
  margin-right: 15px;
  line-height: 10px;
  float: right;
}
@media screen and (max-width: 768px) {
  .section1 {
    width: 100%;
  }
  .box {
    width: 100%;
  }
  .box h1 {
    text-align: center;
    margin: 0 auto;
  }
  footer,
  nav {
    width: 100%;
  }
}
<div class="box">
  <h1><p>Moderní webdesign</p> za nejnižší ceny!</h1>
</div>
j08691
  • 204,283
  • 31
  • 260
  • 272
nous3k
  • 43
  • 7

6 Answers6

1

Amongs other solutions, you can make your div to be rendered as a Table, and the h1 as a table-cell.

You also need to the body of the documento to be 100% of the window height, if you dont, it'll be the height of its content.

So, for the solution, it would be something like:

body,html{
    height:100%;
}
  .box {
    display:table;
        width:100%;
        height:100%;
        text-align: center;
  }
    .box h1 {
        display:table-cell;
        vertical-align: middle;
    }

Pen

Links

teefars
  • 612
  • 4
  • 13
0

You can use display:table-cell; for centering h1 vertically and horizontally.

Jsfiddle

html,
body {
  height: 100%;
}
.box {
  float: left;
  margin: 250px 0 0 0;
  height: 35%;
  width: 50%;
  background-image: url('img/header-bcg.png');
}
.box h1 {
  font-size: 40px;
  font-family: Bree serif;
  color: 33adae;
  margin-top: 30px;
  margin-right: 15px;
  line-height: 10px;
  float: right;
}
@media screen and (max-width: 768px) {
  .section1 {
    width: 100%;
  }
  .box {
    width: 100%;
    display: table;
    margin: 0;
    float: none;
    height: 100%;
  }
  .box h1 {
    text-align: center;
    margin: 0 auto;
    display: table-cell;
    float: none;
    vertical-align: middle;
  }
  footer,
  nav {
    width: 100%;
  }
}
<div class="box">
  <h1><p>Moderní webdesign</p> za nejnižší ceny!</h1>
</div>
Alex
  • 8,461
  • 6
  • 37
  • 49
0

H1 floated it's large as much as letters contained. Adding width 100% align can works.

Germano Plebani
  • 3,461
  • 3
  • 26
  • 38
0

You have a HTML error. <p> it´s not not allowed inside a heading.

Said that, you should use a <span> instead. Then you can center normally with text-align

CSS

 .box h1 {
    text-align: center;
    margin: 0 auto;
    line-height: 1em;
  }

DEMO HERE

Luís P. A.
  • 9,524
  • 2
  • 23
  • 36
0

You need to understand the difference between inline elements and block elements in HTML.

A block-level element always starts on a new line and takes up the full width available, when an inline element does not start on a new line and only takes up as much width as necessary.

Examples of inline elements in HTML:

<span></span>
<a href='#'></a>
<img src='#'></img>

Block elements:

<div>
<h1> - <h6>
<p>
<form>
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
0

You can easily centre the heading bootstrap 4.

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

<div class="container-fluid">
    <div class="row">
        <h2 class="mx-auto">Centered Heading</h2>
    </div>
</div>