Struggling to align the h1 tag with a p tag on the same line

How can I align the balance to the upper right-hand corner of my website? I'm new to HTML and CSS and struggling to get the p tag next to the h1 tag. I've tried using display inline for the h1 tag and float right for the p tag, as well as setting the p tag to inline block based on some suggestions I found online.

body {
    text-align: center;
    font-family: sans-serif;

}

.cards {
    height: 175px;
    width: 125px;
    margin: 1px;
}

.hitButton {
    width: 100px;
    height: 50px;
    font-size: 20px;
}
.resetButton {
    width: 125px;
    height: 50px;
    font-size: 20px;
}
.standButton {
    width: 100px;
    height: 50px;
    font-size: 20px;
}

.slidecontainer{
    text-align: center; 
}      
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>first js website</title>
  <link rel = "stylesheet" href="style.css">
  <script src="app.js"></script>
</head>
<body>
  <div>
    <h1 display="inline"> Dealer:<span id="dealer-sum"></span> </h1>
    <p display="inline-block">Balance: <span id="balanceAmt">50</span>$ </p>
  </div>
  <div id="your-cards">  </div>
    <h1 class="center">Player:<span id="player-sum"></span></h1>
    <div class="slidecontainer">
      <p>You will bet: <span id="betAmt">0</span>$. Next round</p>
      <input type="range" min="0" max="100" value="0" class="slider" id="betSlider" oninput="updateSlider()">
    </div>
  <div>

    <br>
      <button class="hitButton" id="hitId" onclick="hit()">Hit</button>
      <button class="standButton" id="stanId" onclick="stand()">Stand</button>
      <button class="resetButton" id="resetId" onclick="resetGame()">Deal Cards</button>
  </div>
  <h1 id="result"></h1>
</body>
</html>

Answer №1

To align two block elements using flexbox, create a class like .line and assign it to the div wrapping the h1 and p tags.

.line {
  display: flex;
  align-items: center;
  justify-content: center;
  gap:20px;
}

Add the class in your HTML:

<body>
  <div class="line"> <!-- Add this class -->
    <h1> Dealer:<span id="dealer-sum"></span> </h1>
    <p>Balance: <span id="balanceAmt">50</span>$ </p>
  </div>

Note: When adding inline styling, use style="display:inline;" instead of display="inline".

body {
    text-align: center;
    font-family: sans-serif;

}

.cards {
    height: 175px;
    width: 125px;
    margin: 1px;
}

.hitButton {
    width: 100px;
    height: 50px;
    font-size: 20px;
}
.resetButton {
    width: 125px;
    height: 50px;
    font-size: 20px;
}
.standButton {
    width: 100px;
    height: 50px;
    font-size: 20px;
}

.slidecontainer{
    text-align: center; 
}

.line {
  display: flex;
  align-items: center;
  justify-content: center;
  gap:20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>first js website</title>
  <link rel = "stylesheet" href="style.css"> 
  <script src="app.js"></script>
</head>
<body>
  <div class="line">
    <h1> Dealer:<span id="dealer-sum"></span> </h1>
    <p>Balance: <span id="balanceAmt">50</span>$ </p>
  </div>
  <div id="your-cards">  </div>
    <h1 class="center">Player:<span id="player-sum"></span></h1>
    <div class="slidecontainer">
      <p>You will bet: <span id="betAmt">0</span>$. Next round</p>
      <input type="range" min="0" max="100" value="0" class="slider" id="betSlider" oninput="updateSlider()">
    </div>
  <div>

    <br>
      <button class="hitButton" id="hitId" onclick="hit()">Hit</button>
      <button class="standButton" id="stanId" onclick="stand()">Stand</button>
      <button class="resetButton" id="resetId" onclick="resetGame()">Deal Cards</button>
  </div>
  <h1 id="result"></h1>
</body>
</html>

Answer №2

<h1 display="inline">
does not have the desired effect in HTML because there is no display attribute in HTML. Instead, you can utilize CSS within a stylesheet or use the style attribute like so:
<h1 style="display:inline;">

...and the same applies for other inline styling needs.

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

Is the external panel in jQuery Mobile always visible or can it be positioned?

I am looking to use the same panel across multiple pages in a jQuery multipage HTML file. My goal is to have consistent code throughout all pages. However, I am encountering an issue with the positioning of external panels. Currently, it seems that extern ...

What could be causing my class to not be affecting a specific element in Sass?

Using React and sass, I attempted to utilize the activeClassName property on a NavLink component but found that my active class was not working as expected due to its order in relation to the default class. How can I resolve this issue? Here is the code: ...

Troubleshooting iFrame Loading Issues with HTML5 PostMessage

Our code is utilizing the newest postMessage feature in HTML 5 to address cross-domain communication challenges. The issue I am facing is figuring out how to determine if the messages posted to an iFrame have been successfully loaded. If the frame fails to ...

Revise Bootstrap accordion setup

I currently have a Bootstrap accordion set up on my website using Bootstrap 4.1.0 <div id="accordion" class="mt-3"> <div class="card"> <div class="card-header bg-dark text-white" id="headingOne"> <h5 class="mb-0 f ...

Encountering issues with uploading to AWS S3 from Mocha test suite

Attempting to execute an s3 upload from a mocha test: 'use strict'; describe('S3 testing scenario', function() { it.only('Executes S3 test case 1', function*() { var AWS = require('aws-sdk'); // ...

How to find the position of the cursor on an ASP.NET page

While debugging my code, I successfully changed the cursor position with a timer. However, when I published and hosted it, the code stopped working. I implemented the following code and imported Windows.Forms for this child lock application. The purpose i ...

When refreshing a JavaScript array of objects, it creates duplicate entries within the array

Currently, I am developing a chat application within a Vue project and experimenting with different features. A fully-featured chat app must have the ability to live-update "seen" states. This means that when one user views the last sent message, the othe ...

Is it possible to send an entire HTML table to the server and then update the database table with it?

Recently, I encountered an issue that has me stumped. Suppose I have a database table A with multiple columns and the server (PHP script) renders this data into an HTML table for the web client. Now, the challenge lies in allowing users to add/delete rows ...

Set the alignment of the div to the right side (but not all the way to

I'm currently learning and attempting to create a layout with 4 divs that fits my desired page size, rather than adjusting based on the user's window size. My main struggle is getting the right div to align with the left div, without necessarily ...

Why does the text in a div display in Safari 8.2 and Chrome 39, but not in Firefox 34?

In my HTML document, I have a div tag located within the body like so: <div id="content_text"></div>​ Using javascript, I later set the contents of the div like this: var content_text = document.getElementById("content_text") c ...

Is there a way to arrange the outcomes in a single line?

I need help displaying my results in three rows side by side in React/JavaScript using flexbox. Since I only have one CardItem, I can't just copy and paste it three times as it would show the same result in each row. Is there a way to achieve this wit ...

Optimal method for showcasing a multitude of columns on an HTML page

Seeking to showcase a vast array of data in HTML (over 10,000 columns with approximately 200-300 rows) to present a schedule. The information will be structured as movable blocks containing text and background colors. Is it feasible to exhibit such a mas ...

What is the best way to set up a loop for displaying Chat Bubbles?

Is there a way to create a loop for chat bubbles? I want them to appear on the right when I send a message and on the left when the chat partner replies. How can I implement this loop? This is the code I currently have: <div class="Webview"> &l ...

Ways to integrate a component within a custom component in the React framework

I am looking to dynamically change the body content, how can I achieve this? import React from 'react' // import { Link } from 'react-router-dom' export default function ProjectTemplate(props) { const Css= { "--backgr ...

Guide to outputting a JSON array in Struts 2

Below is the code for my Struts action: @Action("/trylogin") @ParentPackage("json-default") @Result(type = "json", params = { "includeProperties", "msg, productsList" }) public class Login extends ActionSupport { private static final long serialVersio ...

The pointer cursor seems to be missing in action

Currently using ReactJS, I have implemented a custom upload image button on my webpage. .upload-btn-wrapper { position: relative; overflow: hidden; display: inline-block; cursor: pointer; } .upload-btn-wrapper input[type=file] { font-size: 10 ...

AHK is unable to fully load all HTML content

I have successfully implemented code to change a frame within the webpage which is working effectively. fr1 :=IE.document.frames[13] fr1.navigate("http blablabla ") However, I am encountering an issue where I need to parse one of the tables on th ...

The initial render of children elements in React Google Maps Api may not display properly

I am struggling to incorporate the Google Maps API into my app. Everything works smoothly until I try to display a Marker at the initial rendering of the map. The Marker does not show up, but if I add another Marker after the rendering is complete, then it ...

I'm curious about the process by which custom hooks retrieve data and the detailed pathway that custom hooks follow

//using Input HOOK I am curious to understand how this custom hook operates. import { useState } from "react"; export default initialValue => { const [value, setValue] = useState(initialValue); return { value, onChange: event =&g ...

Is it possible to perform direct URL searches using react-router-dom?

Encountering an issue when attempting to directly copy a route, resulting in the following error: Error: Cannot Access / home Despite utilizing various methods such as browserHistory, I am unable to successfully render views when navigating with menu i ...