Align headings h2 and h4 on the left and right sides of the same line, ensuring they are at an

I am attempting to create a header with h2 text on the left and h4 on the right, positioned on the same line with equal height and some padding. Here is the code snippet I have written:

.alignleft {
  float: left;
}

.alignright {
  float: right;
}

.nav {
  clear: both;
}

.nav {
  background-color: grey;
}
<div class="nav">
  <h2 class="alignleft">Left side</h2>
  <h4 class="alignright">RIGHT SIDE</h4>
</div>

If you'd like to see the code in action, check out this fiddle. Any assistance would be greatly appreciated.

Answer №1

When specifying a class name in the HTML class attribute, do not include the dot before the class name. Your code should look like this:

<div class="nav">
    <h2 class="alignleft">Left side</h2>
    <h4 class="alignright">RIGHT SIDE</h4>
</div>

Also, remember to add a line-height property in your CSS rules:

.alignleft {
    float: left;
    line-height: 25px;
}

.alignright {
    float: right;
    line-height: 25px; /*or any other desired pixel value*/
}

.nav {
    clear: both;
    background-color: grey;
}

Answer №2

After advice from @MrGeek, it is recommended to eliminate the prefix . from the class name. Ensure that both the h2 and h4 elements have the same line height.

.alignleft {
  float: left;
}

.alignright {
  float: right;
}

.nav {
  clear: both;
}

.nav {
  background-color: grey;
}

h2, h4 {
  line-height: 24px;
}
<div class="nav">
  <h2 class="alignleft">Left side</h2>
  <h4 class="alignright">RIGHT SIDE</h4>
</div>

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

Creating a PDF document using html2pdf: A step-by-step guide

Currently, I am immersed in a project using React. The main goal right now is to dynamically generate a PDF file (invoice) and then securely upload it to Google Drive. In the snippet of code provided below, you can see how I attempted to create the PDF f ...

Algorithm for detecting collisions in Javascript

I am looking to implement a collision detection function in JavaScript for canvas. Specifically, I have coin and piggy bank objects and want to create a function that triggers the disappearance of a coin object when it comes into contact with a piggy bank. ...

How can I keep a button element in place when resizing the window using CSS?

I'm currently experiencing an issue with my navbar where a button escapes from it when I resize the screen. Which CSS property should I modify to ensure that the button remains inside the navbar and doesn't escape? Here is a JFiddle link demonst ...

Switch the ng-bind-html option

Dealing with a string in my scope, I must determine whether I want the HTML escaped or not. A boolean value helps to decide if the HTML should be escaped or left as is. Snippet Check out some of my code examples below: $scope.result = "<b>foo</ ...

Resizing the background while scrolling on a mobile web browser

My website background looks great on desktop, but on mobile (using Chrome) the background starts to resize when I scroll, mainly due to the browser search bar hiding and reappearing upon scroll. Is there a way to prevent my background from resizing? Check ...

Obtaining rotate3d values using JavaScript / jQuery

When dealing with an element that has a transformation like the following: style="transform: rotate3d(1, 0, 0, -50deg);" I am looking to extract the value -50 using Javascript or jQuery. It's essential for me to get the absolute value, so negative d ...

Discrepancy in CSS rendering in Chrome and Firefox

When viewing in Chrome, the stats display properly within the gray box at the bottom left corner. However, when using Firefox, the stats appear positioned much lower below the box, with the bottom half of them hidden from view. Here is my CSS code being ...

What's the connection between CSS and frameset?

Currently, I have an .html document with XHTML 1.0 Frameset doctype and the following code: <frameset rows="20%, 80%" border="1"> ... </frameset> Upon validating this code on W3C Validator, it gives me the error message: there is no a ...

Latest FF 35 showing alert for blank field in HTML5 email input box

My form includes an email input field with a default value. When the user focuses on the field, it clears out if the value matches the default one. Upon blurring the element, the default value is restored if the field remains empty. In Firefox 35, clickin ...

Update the content of a div using jQuery and AJAX by targeting a specific button ID

I am in the process of developing a website that functions as an interactive "choose your own adventure" game. The page will present users with a question along with three different choices represented as buttons. Upon selecting one of the options, jQuery ...

When trying to open an HTML table that was exported to .xls file using FireFox for downloading, it fails

I have been utilizing a function that was sourced from stackoverflow. It functions correctly on Internet Explorer and Google Chrome, however, on FireFox, the downloaded file cannot be opened in Excel. When attempting to open the file in Excel, it shows up ...

Each Browser Approaches Simple Search Fields in its Own Unique Way

In the process of creating a search field with an input field and an svg icon, I encountered different browser behaviors in Chrome, Firefox, and IE11. Chrome – the preferred version https://i.sstatic.net/ikBgD.png Firefox https://i.sstatic.net/9lcqQ.pn ...

Transferring JSON data from Django for visualization on a D3 graph

I'm currently working on customizing a D3 bar graph found at http://bl.ocks.org/3885304 to accommodate JSON data from Django. In Django, I have a variable that holds JSON formatted data like this: [{"score": 1, "color": "blue"}, {"score": 5, "color": ...

Once you utilize the register function with react-hook-form, the input text becomes uneditable

Struggling to configure react-fook-form for form validation, I encountered an issue where the text input (specifically the username field) becomes uneditable after using the register function. Despite my efforts, I can't type anything into it. const { ...

Methods to prevent images from spilling over into divs?

I am looking to implement a unique functionality that involves three images sliding out of the page to the right, fading in the process, and not overflowing. Simultaneously, three other images slide in to take their place. If you want to see the Framework ...

What steps should I take to ensure that clicking this button triggers the API request and returns the data in JSON format?

I'm attempting to have the button with id 'testp' return an api request in json format, however it seems to not be functioning properly. You can find the HTML code link here: https://github.com/atullu1234/REST-API-Developer-1/blob/main/js-bu ...

Saving user input from a PHP form into a text file

Below is a simple PHP form that performs a calculation when submitted. Now, I want to save the inputs (height, weight) and calculation result to a text file without displaying the path of the file. Ideally, I would like each submission to add a new line in ...

The issue lies in the alignment of the navbar at the top

After retrieving this script: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <nav class="navbar bg-light navbar-expand-sm justify-content-center"> <a class="navbar-brand" ...

Align the text vertically in your HTML email

Attempting to vertically center text in a TD, I've experimented with valign: middle inline styling and vertical-align: middle within the style tags in the header. I'm puzzled as to why it's not functioning, especially since it works fine on ...

Hover over the text to reveal its content

I am currently in the process of developing a website that features interactive images with text appearing when hovered over. This concept is inspired by the functionality on the nowthisnews.com site. Despite my efforts, I have been unable to get this fea ...