Aligning the h1 element in a heading section

I'm having trouble centering my h1 element in the header. It should be positioned in the middle of a 450px header, while keeping the nav bar at the top right. Additionally, I would like to add a logo to the top left. I've tried using align, position, and margin auto, but haven't had any luck. If someone could assist me and point out where I'm going wrong, I would greatly appreciate it. Thank you.

body {
  background-color: #999;
  font-family: times;
}
header {
  height: 450px;
  background: url(../Pictures/Flensburg.jpg) center center fixed;
  background-size: cover;
}
nav {
  float: right;
  30px 30px 0 0;
}
ul{
  display: inline;
  align: right;
  list-style-type: none;
  padding: 10px
}
li {
  display: inline-block;
  padding: 10px
}
a {
  background-color: #b3b3b3;
  color: white;
  text-transform: uppercase;
}
h1 {
  font-size: 72px
  width: 100%
}
<!DOCTYPE html>
<html>
  <head>
    <title>Keanu</title>
    <link href="CSS/Main.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <header>
    <nav>
      <ul>
    <li><a href="Index.html">Home</a></li>
    <li><a href="About Me.html">About Me</a></li>
    <li><a href="Portfolio.html">Portfolio</a></li>
    <li><a href="Testimonial.html">Testimonial</a></li>
    <li><a href="Contact.html">Contact</a></li>
  </ul>
  </nav>
  <h1>My Personal Website</h1>
</header>
<footer>

</footer>
  </body>
</html>

Answer №1

Ensure that your markup and CSS rules are valid before proceeding. Remember, CSS does not recognize a rule like align: right. Additionally, don't forget to include the semicolon in your h1 CSS rules.

To center your h1 element, simply apply text-align: center; to it while setting the nav element to display: block; to keep it on its own line.

body {
  background-color: #999;
  font-family: Times;
}

header {
  height: 450px;
  background: url(../Pictures/Flensburg.jpg) center center fixed;
  background-size: cover;
}

ul {
  display: block;
  text-align: right;
  list-style-type: none;
  padding: 10px;
}

li {
  display: inline-block;
  padding: 10px;
}

a {
  background-color: #b3b3b3;
  color: white;
  text-transform: uppercase;
}

h1 {
  font-size: 72px;
  width: 100%;
  text-align: center;
}
<header>
  <nav>
    <ul>
      <li><a href="Index.html">Home</a></li>
      <li><a href="About Me.html">About Me</a></li>
      <li><a href="Portfolio.html">Portfolio</a></li>
      <li><a href="Testimonial.html">Testimonial</a></li>
      <li><a href="Contact.html">Contact</a></li>
    </ul>
  </nav>
  <h1>My Personal Website</h1>
</header>

Answer №2

After making some CSS tweaks, I successfully resolved a few issues:

1) Set the width of the navigation element to 100%

2) Adjusted the width of the h1 element to 100%

3) Center-aligned the text within the h1 element

Answer №3

body {
  background-color: #999;
  font-family: times;
}
header {
  height: 450px;
  background: url(../Pictures/Flensburg.jpg) center center fixed;
  background-size: cover;
}
nav {
  float: right;
  30px 30px 0 0;
}
ul{
  display: inline;
  align: right;
  list-style-type: none;
  padding: 10px
}
li {
  display: inline-block;
  padding: 10px
}
a {
  background-color: #b3b3b3;
  color: white;
  text-transform: uppercase;
}
h1 {
  font-size: 72px
  width: 100%;
text-align:center;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Keanu's Website</title>
    <link href="CSS/Main.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <header>
    <nav>
      <ul>
    <li><a href="Index.html">Home</a></li>
    <li><a href="About Me.html">About Me</a></li>
    <li><a href="Portfolio.html">Portfolio</a></li>
    <li><a href="Testimonial.html">Testimonial</a></li>
    <li><a href="Contact.html">Contact</a></li>
  </ul>
  </nav>
  <h1>Discover My World</h1>
</header>
<footer>

</footer>
  </body>
</html>

Answer №4

For optimal display, consider adding the following CSS properties to the h1 heading: display: inline-block;, text-align: center;, and width: 100%;.

h1 {
    font-size: 72px;
    width: 100%;
    display: inline-block;
    text-align: center;
}

Here is the fully updated code:

body {
  background-color: #999;
  font-family: times;
}
header {
  height: 450px;
  background: url(../Pictures/Flensburg.jpg) center center fixed;
  background-size: cover;
}
nav {
  float: right;
  30px 30px 0 0;
}
ul{
  display: inline;
  align: right;
  list-style-type: none;
  padding: 10px
}
li {
  display: inline-block;
  padding: 10px
}
a {
  background-color: #b3b3b3;
  color: white;
  text-transform: uppercase;
}
h1 {
  font-size: 72px;
  width: 100%;
  display: inline-block;
  text-align: center;
}
<header>
  <nav>
    <ul>
      <li><a href="Index.html">Home</a></li>
      <li><a href="About Me.html">About Me</a></li>
      <li><a href="Portfolio.html">Portfolio</a></li>
      <li><a href="Testimonial.html">Testimonial</a></li>
      <li><a href="Contact.html">Contact</a></li>
    </ul>
  </nav>
  <h1>My Personal Website</h1>
</header>
<footer>

</footer>

Answer №5

One issue at the root of your problem is the floated <nav>. Without clearing the float, the following element (the h1) is impacted by the floated nav.

To resolve this problem, add a div with clear:both after your </nav>. Nonetheless, it's important to note that there are several errors in your code, such as missing semicolons and an incomplete margin or padding line. Additionally, ensure you apply text-align:center to your h1 for centered text alignment. The corrected code with these adjustments is provided below:

body {
  background-color: #999;
  font-family: times;
}
header {
  height: 450px;
  background: url(../Pictures/Flensburg.jpg) center center fixed;
  background-size: cover;
}
nav {
  float: right;
  /* The previous line beneath was "30px 30px 0 0;" - I assumed you wanted a margin. Change if not */
  margin: 30px 30px 0 0;
}
ul{
  display: inline;
  align: right;
  list-style-type: none;
  padding: 10px;
}
li {
  display: inline-block;
  padding: 10px;
}
a {
  background-color: #b3b3b3;
  color: white;
  text-transform: uppercase;
}
h1 {
  font-size: 72px;
  width: 100%;
text-align:center;
}
.clear{
clear:both;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Keanu</title>
    <link href="CSS/Main.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <header>
    <nav>
      <ul>
    <li><a href="Index.html">Home</a></li>
    <li><a href="About Me.html">About Me</a></li>
    <li><a href="Portfolio.html">Portfolio</a></li>
    <li><a href="Testimonial.html">Testimonial</a></li>
    <li><a href="Contact.html">Contact</a></li>
  </ul>
  </nav>
<div class="clear"></div>
  <h1>My Personal Website</h1>
</header>
<footer>

</footer>
  </body>
</html>

Edit:
It's worth noting that without properly addressing the floating and rectifying css errors, even if the font size remains 72px, visual alignment may seem correct but the issue persists with the h1 being positioned behind the nav. To prevent this, make sure to clear the float and fix the CSS errors. Adjusting the font size may temporarily mask the problem, but it will resurface if left uncorrected.

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

Send properties to the makeStyles function and apply them in the CSS shorthand property of Material UI

When working with a button component, I pass props to customize its appearance: const StoreButton = ({ storeColor }) => { const borderBottom = `solid 3px ${storeColor}`; const classes = useStyles({ borderBottom }); return ( <Button varian ...

Attempting to place my icon font in a higher position than my link to achieve a similar effect to the one I am currently experiencing with my

I am currently working on a menu design that incorporates icons (images) above my menu links. However, I have decided to switch from using icon images to utilizing icon-fonts from the 'Font-Awesome' library. Despite this change, I am struggling ...

Is there a way to apply -webkit-line-clamp to this JavaScript content using CSS?

i have a random-posts script for my blogger website <div class="noop-random-posts"><script type="text/javascript"> var randarray = new Array(); var l=0; var flag; var numofpost=10; function nooprandomposts(json){ var total = ...

Adding content after text that has been wrapped is a simple process that involves

When the report generator creates orders in html, it uses absolute positioning to ensure the exact layout from the report designer is preserved. Each order row contains a product description and price. Following the order row, there is a horizontal line o ...

Issues are occurring with the @font-face css for the Futura Bk BT Bok.ttf font file

Is there a way to incorporate the 'Futura Bk BT Bok.ttf' font into my website using the CSS @font-face rule? Here is a snippet of my current CSS: @font-face { font-family: abcd; src:url('Futura Bk BT Bok.ttf') format('true ...

Trigger a series of functions upon clicking with ReactJS

Need some assistance with an alert message functionality. I have a button labeled Checkout, and when clicked, it should clear the cart and display an alert. At present, the individual functions work as expected - calling emptyCart() works fine, and calling ...

The options in the drop-down menu appear to be stuck and remain in place

Can someone with expertise lend me a hand? I've been racking my brain over this issue, and while I feel like I'm on the right track, I just can't seem to make my sub-sub menu items drop down to the right on hover. I suspect there might be c ...

How to Style Background with CSS on the Server-Side? (Including Links and Hover Effects)

I'm facing a dilemma in choosing between an ASP.NET control and an HTML element for creating a button with a CSS-defined background that changes on hover... 1. Initially, it seems the ASP.NET ImageButton control is not suitable for this purpose becau ...

The object holds a value

How can I successfully put an object into a value attribute, for example: <option value={valueSort}>// valueSort = {name: 'ASC'}, to sort by name in my project? Currently, the result shows as [object Object] and I'm unsure of what that ...

Mobile operating system "curtain" design on Android

I want to design a menu that functions similar to the Android notification drop-down section (where you pull down from the top). This menu should have its own scrollbar, incorporate images, and have smooth animations when scrolling down instead of jumping ...

Click here for more information in JavaScript Reading Link

I am trying to implement a feature where I only display half of the text from a Biography field on the first page, and have a continue reading link that reveals the rest of the text when clicked. I would like to accomplish this using HTML. Can anyone p ...

Is it possible to utilize bootstrap sizing by considering the size of the containing div?

Setting the Scene Imagine a scenario where two divs are placed side by side. div1: 500px wide div2: taking up the remaining width The goal is to make the items in div2 responsive using Bootstrap so that classes like mt-sm-3 can be utilized for differen ...

When using JavaScript to dynamically load canvases and create drawing contexts within a function, the context may suddenly disappear

Currently, I am modifying the canvases displayed through ajax calls and also updating what is drawn on each canvas. The primary issue I am facing is that my main drawing function fails on getContext and there are some unusual behaviors such as missing canv ...

List of dropdown options retrieved from SQLite database

I am attempting to retrieve data from an SQLite database table in order to populate a dropdown menu list. My thought process is outlined below. The main issue I am facing is how to integrate the JS function with the HTML section. HTML.html <label ...

Flex Row with Expansion Panel spacing concern

Experiencing issues with spacing of expansion panels within a flex row. Multiple Mat-Expansion-Panel are inside an ngFor loop, each containing varying amounts of items. When one panel is expanded, the adjacent panel also expands to the same height withou ...

"Enhance Your Website with Dynamic Text Effects using JavaScript

Is there a way to continuously animate text during the loading process of an AJAX request? I've tried implementing various methods, such as using a setTimeout function or an infinite loop, but nothing seems to work for repeating the animation once it& ...

Is there a way to show several elements simultaneously by hovering over another?

Can anyone help me with setting display:block; on all the p tags and the overlay div when I hover over my img tag? Is this achievable with just CSS or do I need to incorporate some JavaScript? Open to suggestions on the best approach. Any assistance woul ...

Interactive Features in Vue Component

In my Vue Component, I have a prop named src that is bound to a :style attribute like this: <template> <section :class="color" class="hero" :style="{ backgroundImage: src && 'url(' + src + ')' }"> <slot> ...

Is there a way to implement two distinct JavaScript functions - one for desktop and another for mobile - within the same onclick event attribute in an HTML document?

I'm currently working on a responsive HTML page that has versions for desktop and mobile. In order to properly execute specific functions based on the device's screen width, I have two separate functions - one for desktop and one for mobile - bot ...

inconsistencies observed in flex layout between webkit browsers (Chrome and Safari) compared to moz (Firefox)

I am experiencing unexpected behavior with flex on Mozilla (-moz-) and Chrome/Safari (-webkit-) Referenced the Mozilla tutorial to understand flex layout /** { border: solid; border-width: 0 1px; }*/ html, body { width: 100%; height: 1 ...