How can I position an elements header in the top right corner of the div at any media size?

296 Views Asked by At

I need help getting the header title (h2,h3,h4) region always appear in the top right corner of their respective div region, no matter what view is being displayed.

Also, the header content keeps overflowing as I shrink the web browser, how can I stop that from running over?

I tried moving the margins and the padding of the header, and the header won't move to the right past 350px.

I tried to adjust the size of the div to allow the header to move top right.

Should I have used the "section element" instead of div?

I haven't tried using absolute or relative positioning.

/********** Base styles **********/

* {
  font-family: arial, sans-serif, helvetica;
  font-style: normal;
  font-size: 16px;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  color: rebeccapurple;
}

h1 {
  text-align: center;
  font-size: 175%;
  margin-bottom: 40px;
  color: black;
}

h2,
h3,
h4 {
  font-size: 125%;
  text-align: center;
  font-weight: bold;
  border: 1px solid black;
  max-width: 30%;
  margin-bottom: 5px;
  position: relative;
  bottom: 11px;
  left: 231px;
}

h2 {
  background-color: Pink;
  color: black;
}

h3 {
  background-color: red;
  color: white;
}

h4 {
  background-color: greenyellow;
  color: black;
}

div {
  background-color: lightgray;
  padding: 10px 0px 10px 10px;
  border: 1px solid black;
  margin: 0 auto;
  margin-bottom: 10px;
  max-width: 400px;
  position: relative;
}


/*Large devices*/

@media (min-width: 992px) {
  .box1 {
    float: left;
    margin-left: 20px;
    margin-right: 60px;
    max-width: 30%
  }
  .box2 {
    float: left;
    max-width: 30%;
  }
  .box3 {
    float: right;
    max-width: 30%;
    margin-right: 20px;
  }
  h2,
  h3,
  h4 {
    position: relative;
    left: 70.2%;
  }
}


/*Medium devices*/

@media (min-width: 768px) and (max-width:991px) {
  .box1 {
    float: left;
    max-width: 43%;
    margin-bottom: 30px;
    margin-left: 40px;
    margin-right: 0px;
  }
  .box2 {
    float: right;
    max-width: 43%;
    margin-bottom: 30px;
    margin-right: 40px;
  }
  .box3 {
    margin-top: 40px;
    clear: both;
    max-width: 100%;
    margin-left: 40px;
    margin-right: 40px;
  }
  h2,
  h3,
  h4 {
    position: relative;
    left: 70.2%;
  }
}


}

/*small devices*/
@media (max-width:767px) {
  div {
    margin-top: 30px;
  }
  
  h2,
  h3,
  h4 {
    position: relative;
    left: 100%;
  }
}
<!DOCTYPE html>
<html lang="en-CA">

<head>
  <meta charset="utf-8">
  <title> Our Menu </title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="css/STYLE.css" rel="stylesheet" type="text/css">
</head>

<body>
  <h1>Our Menu </h1>

  <div class="box1">
    <h2>Meat</h2>
    <p id="p1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit error quis ab perspiciatis eos inventore recusandae iste itaque numquam facilis, tenetur. Doloribus officiis quae facilis, nisi, ex similique. Animi, perferendis</p>
  </div>

  <div class="box2">
    <h3>Pizza</h3>
    <p id="p2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit error quis ab perspiciatis eos inventore recusandae iste itaque numquam facilis, tenetur. Doloribus officiis quae facilis, nisi, ex similique. Animi, perferendis</p>
  </div>

  <div class="box3">
    <h4>Salads</h4>
    <p id="p3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit error quis ab perspiciatis eos inventore recusandae iste itaque numquam facilis, tenetur. Doloribus officiis quae facilis, nisi, ex similique. Animi, perferendis</p>
  </div>


</body>

</html>

/********** Base styles **********/

* {
  font-family: arial, sans-serif, helvetica;
  font-style: normal;
  font-size: 16px;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  color: rebeccapurple;
}

h1 {
  text-align: center;
  font-size: 175%;
  margin-bottom: 40px;
  color: black;
}

h2,
h3,
h4 {
  font-size: 125%;
  text-align: center;
  font-weight: bold;
  border: 1px solid black;
  max-width: 30%;
  margin-left: 310px;
  margin-bottom: 5px;
  margin-top: 0px;
  padding: 0px;
}

h2 {
  background-color: Pink;
  color: black;
}

h3 {
  background-color: red;
  color: white;
}

h4 {
  background-color: greenyellow;
  color: black;
}

div {
  background-color: lightgray;
  padding: 10px 0px 10px 10px;
  border: 1px solid black;
  margin: 0 auto;
  margin-bottom: 10px;
  max-width: 400px;
}


/*Large devices*/

@media (min-width: 992px) {
  .box1 {
    float: left;
    margin-left: 20px;
    margin-right: 60px;
    max-width: 30%
  }
  .box2 {
    float: left;
    max-width: 30%;
  }
  .box3 {
    float: right;
    max-width: 30%;
    margin-right: 20px;
  }
}


/*Medium devices*/

@media (min-width: 768px) and (max-width:991px) {
  .box1 {
    float: left;
    max-width: 43%;
    margin-bottom: 30px;
    margin-left: 40px;
    margin-right: 0px;
  }
  .box2 {
    float: right;
    max-width: 43%;
    margin-bottom: 30px;
    margin-right: 40px;
  }
  .box3 {
    margin-top: 40px;
    clear: both;
    max-width: 100%;
    margin-left: 40px;
    margin-right: 40px;
  }
}


/*small devices*/

@media (max-width:767px) {
  div {
    margin-top: 30px;
  }
}
<!DOCTYPE html>
<html lang="en-CA">

<head>
  <meta charset="utf-8">
  <title> Our Menu </title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="css/STYLE.css" rel="stylesheet" type="text/css">
</head>

<body>
  <h1>Our Menu </h1>

  <div class="box1">
    <h2>Meat</h2>
    <p id="p1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit error quis ab perspiciatis eos inventore recusandae iste itaque numquam facilis, tenetur. Doloribus officiis quae facilis, nisi, ex similique. Animi, perferendis</p>
  </div>

  <div class="box2">
    <h3>Pizza</h3>
    <p id="p2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit error quis ab perspiciatis eos inventore recusandae iste itaque numquam facilis, tenetur. Doloribus officiis quae facilis, nisi, ex similique. Animi, perferendis</p>
  </div>

  <div class="box3">
    <h4>Salads</h4>
    <p id="p3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit error quis ab perspiciatis eos inventore recusandae iste itaque numquam facilis, tenetur. Doloribus officiis quae facilis, nisi, ex similique. Animi, perferendis</p>
  </div>


</body>

</html>

0

There are 0 best solutions below