What is the best way to center the header on the page?

Struggling to align the header and images centrally on the page, I've been experimenting with the box model but haven't had much success. Being new to this area, any guidance or tips would be greatly appreciated.

https://i.sstatic.net/ejR8L.png

body {
  background: radial-gradient(gold, goldenrod);
  font-family: monospace;
  text-align: center;
  font-size: 1.5rem;
  position: relative;
}

img {
  padding: 10px;
}

img:hover {
  transform: scale(1.4);
  transition:all 1s;
}

.container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  min-height: 400px;
}
<html>
  <head>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
  <h1>Choose Your Weapon</h1>
    <div class="container">
      <img src="images/rock.svg" alt="rock">
      <img src="images/paper.svg" alt="paper">
      <img src="images/scissors.svg" alt="scissors">
      <p></p>

    </div>
    <script src="app.js"></script>
  </body>
</html>

Answer №1

If you want to create a layout like this, consider using flexbox.

body {
  height: 100vh;
  background: radial-gradient(gold, goldenrod);
  font-family: monospace;
  font-size: 1.5rem;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

img {
  padding: 10px;
}

img:hover {
  transform: scale(1.4);
  transition: all 1s;
}
<h1>Choose Your Weapon</h1>
<div class="container">
  <img src="images/rock.svg" alt="rock">
  <img src="images/paper.svg" alt="paper">
  <img src="images/scissors.svg" alt="scissors">
  <p></p>

</div>

Answer №2

Here is an example that you can try:

body {
  background: radial-gradient(gold, goldenrod);
  font-family: monospace;
  text-align: center;
  font-size: 1.5rem;
  position: relative;
}

img {
  padding: 10px;
}

img:hover {
  transform: scale(1.4);
  transition: all 1s;
}

.container {
  width: auto;
  margin: auto;
}
<html>

<head>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <h1>Pick Your Element</h1>
  <div class="container">
    <img src="images/fire.svg" alt="fire">
    <img src="images/water.svg" alt="water">
    <img src="images/earth.svg" alt="earth">
    <p></p>

  </div>
  <script src="app.js"></script>
</body>

</html>

Answer №3

The problem lies in the positioning and use of the left, top properties, which end up hiding the top text when the screen size is reduced. A better approach would be to change its position to relative and then use margin to center it on the page.

body {
  background: radial-gradient(gold, goldenrod);
  font-family: monospace;
  text-align: center;
  font-size: 1.5rem;
  position: relative;
}

img {
  padding: 10px;
}

img:hover {
  transform: scale(1.4);
  transition: all 1s;
}

.container {
  position: relative;
  min-height: 400px;
  background: red;
  margin: auto;
  display: inline-block;
}
<h1>Choose Your Weapon</h1>
<div class="container">
  <img src="http://via.placeholder.com/100x100" alt="rock">
  <img src="http://via.placeholder.com/100x100" alt="paper">
  <img src="http://via.placeholder.com/100x100" alt="scissors">
  <p></p>
</div>

Answer №4

Alright, I have a little trick up my sleeve that I learned when I first started out. But before I share that with you, let me suggest some improvements to your code:

A) There's no need to set position: relative; for the body element because all elements automatically move in relation to the body of your page. Setting the position to relative confines the positioning of everything inside the body as relative, which might interfere with the absolute positioning of the .container class.

B) Secondly, if you want to keep the header and images together, consider putting them under a single class. This will make it easier to move them around together instead of shifting them individually.

Now for the trick, start by setting the min-height and min-width of the container class in pixels. Once that's done, you can center the class using the following CSS:

top: 50%;
left:50%;
margin-top: -200px; /*half of the height of the container*/
margin-left: -200px; /*half of the width of the container*/

So, your updated code should look something like this:

body {
  background: radial-gradient(gold, goldenrod);
  font-family: monospace;
  text-align: center;
  font-size: 1.5rem;
}

img {
  padding: 10px;
}

img:hover {
  transform: scale(1.4);
  transition:all 1s;
}

.container {
  position: absolute;
  min-width: 400px;
  min-height: 200px;
  top:50%;
  left: 50%;
  margin-top: -100px;
  margin-left: -200px;
}
  <body>
  <div class="container">
    <h1>Choose Your Weapon</h1>
    
      <img src="images/rock.svg" alt="rock">
      <img src="images/paper.svg" alt="paper">
      <img src="images/scissors.svg" alt="scissors">
      <p></p>

    </div>
    <script>
    
    </script>
  </body>

And the best part is, it's fully responsive! :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How is it possible for the zero value to still be submitted even after I have included display error messages?

I have written some code to display error messages in PHP, but it seems that the error message is not displayed when all fields are empty. This results in submitting empty values to the database. <!DOCTYPE HTML> <html> <head> <style ...

Tips for resizing a larger image to display only a specific portion in CSS (and incorporating JS if needed)

I need to resize an image that measures 1024x1024 and is segmented into 4 quadrants: My goal is to reduce the size of this image so that quadrant 2 is 256x256 while masking out or hiding the remaining 3 quadrants, displaying only the desired section on th ...

Style the CSS exclusively to the expanded menu section

I need help with applying CSS rules to my navbar menu without affecting the collapsed menu. I came across a solution here and tried using this code: @media (min-width: 980px) { /* your conditional CSS*/ } However, the issue arises when my browser win ...

Employing absolute picture placement

I have a collection of clipped images, each with an absolute position: <img style='position:absolute;clip(xpx xpx xpx xpx);'/> <img style='position:absolute;clip(xpx xpx xpx xpx);'/> <img style='position:absolute;cl ...

JavaScript clear when dynamically loading

I am encountering an issue with a javascript plugin I have for a specialized image scroller. This scroller includes various timeout methods and numerous variables with values set based on those timeouts. While everything is working flawlessly, the site I ...

The dropdown submenu in IE10 disappears prematurely, making it difficult to hover over the submenu

One of my clients is experiencing an issue with the navigation menu drop-down on their site at . While it works fine on all browsers, there seems to be a problem with hovering over submenus in IE 10 - they disappear too quickly. The client is using the SR ...

Struggling to resolve problem with MoveTargetOutOfBoundsException or encountering difficulty scrolling into view in Firefox

Despite trying numerous options, I've hit a roadblock in Firefox where my code can't seem to locate an element that is clearly visible on the screen. Below is a snippet of my code including variable declarations: ''' *** Variables ...

How can I adjust the border opacity in a React application?

Can we adjust the border color opacity in React when using a theme color that is imported? For example, if our CSS includes: borderBottomColor: theme.palette.primary.main and we are importing the theme with Material UI using makeStyles, is there a way to ...

Having trouble passing parameters from an HTML/JS form to a URL

As someone new to the realm of front-end development, I find myself tackling what appears to be a straightforward task. The challenge at hand involves taking a value entered by a user in an HTML/JS form and then adding that value to the end of a public se ...

I am experiencing difficulty with the display of the elements within my <li></li> tags on both the browser and in the inspect elements section. Strangely, only my <ol> tags are showing up properly

I'm having trouble with the display of elements in my list items () on the browser. When I inspect the page, only the paragraph tags show up and the total value is also not being displayed. However, I can see the items in the birdInCart array when I c ...

Show a collection of files in a table format

Here is the current code I have: index.html <!DOCTYPE html> <html> ... </form> </div> </body> </html> And here is my code.gs function doGet() { return HtmlService.createHtmlOutputFromFile('index'); } ...

Ways to add extra dots to your listing style

Is there a CSS property or list style that can display an expanding list? For example: Order now for a discount! <ol style="list-style: none"> <li>* Available only for the first 12 months</li> <li>** Only for new customers ...

What is the most effective way to show an error message within the login form itself?

I'm currently dealing with a dilemma involving my login.php and login_process.php files. The login form is in the former, while the validation process occurs in the latter. I'm struggling to figure out how to display error messages on the login f ...

Using jQuery to dynamically select all rows (`tr`) that contain table data cells (`td`) matching

Although my title may seem confusing, it's the best I could come up with. I'm looking to identify all tr elements that contain td elements matching specific filter criteria. Here is an example: <tr class="row" id="1"> <td class="ph ...

What causes the fading effect on my navigation bar when the slideshow is in use?

I recently created a navigation bar at the top of my webpage, placed above a picture slideshow. However, I noticed that as the slideshow transitions between images, the background color of the navigation bar fades to transparent. How can I prevent this and ...

Is there a way to create submit buttons that trigger printing in HTML? Additionally, how can I design a text input that spans 100% width on the same line as a <span>, <p>, or <div>?

These are the files I have: .dir { color: white; font-family: "Lucida Console", Monaco, monospace; font-size: 16px; } .cmdline { font-family: "Lucida Console", Monaco, monospace; background-color: black; border: 1px solid black; color: whi ...

How do I center align 2 td's in HTML?

I have a code snippet below for Submit and Cancel buttons. When I had 3 buttons, I aligned the middle one in the center which looked good. But now with only 2 buttons, they appear slightly to the left. How can I align them in the center of tr tag? Any su ...

Modify/Change the Class Name of a Link using jQuery within a Navigation List Item Header 4 in a Navigation Menu

In a row, I have 5 link items enclosed within an h4, which is then nested within an li element. The li element is finally nested within a ul, located inside a nav element. Currently, when one of the links is clicked (thanks to a helpful example found here ...

Customize Your Contact Form in Wordpress with a Stylish 2-Column Layout

Hey there, I've encountered a little problem with my website. My website is built on Wordpress and uses Contact Form 7, which has been heavily customized with CSS. While it looks great on desktop, the email field appears too small on mobile devices. I ...

Design an interactive table featuring rowspan functionality

Working on a project with React and attempting to build a dynamic table. Here is an example of how the table should look: https://i.sstatic.net/oEd7q.png The object being used: [ {category: "Type One", subCategory: "One", val: 1, rowSpan: 4}, {category: ...