Tips for aligning text to the right of an <h> tag

Currently working on a website and facing a little issue. I have an <h4> tag with some text and I'm trying to place a <p> tag containing additional text next to it, but with some space in between. However, I can't seem to get the desired placement. It's either too close to the <h4> tag or ends up below it when using align:right. Below is the code snippet:

<div class="col-md-8">
    <div class="col-md-8">
        <h1 id="title">Education</h1>
        <h4 style="display: inline">University</h4>
        <p style="align=right"> 2009 - 2015<br></p>
        <p style="display: inline">B.Sc in Computer Science</p>
        <p style="display: inline"> Grades: 2:1</p>
        <span>
            <h4>Highschool</h4>
            <p>2006 - 2009</p>
        </span>   
    </div>
</div>

Bootstrap is being utilized for the grid layout. Not sure if this framework is causing the issue.

Answer №1

Keeping Code Structure Consistent

To maintain a consistent code structure, align the elements in a single line by using inline display and float the date range to the right.

.title-right {
    float: right;
}

.title-left {
    display: inline;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-md-8">
    <div class="col-md-8"
        <h1 id="title">Education</h1>
        <h4 class="title-left">University</h4>
        <span class="title-right"> 2009 - 2015</span>
        <span>B.Sc in Computer Science</span>
        <span> Grades: 2:1</span>
        <span>
            <h4>Highschool</h4>
            <p>2006 - 2009</p>
        </span>   
    </div>
</div>

This approach eliminates the need to include the date range in the header as suggested in Maddy's answer, which may not comply with proper HTML5 semantics.

Additional considerations:

  • Avoid using paragraphs for non-paragraph content. Utilize <span> for building lines from multiple elements without needing to specify display:inline repeatedly.
  • Refactor inline styles into classes to improve code readability and maintainability.

Enhanced Code Organization

Traditional Approach

If you prefer a two-column layout, create separate <div> sections for each line and set text alignment accordingly.

.float-right {
    float: right;
}

.inline {
    display: inline;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-md-8">
    <div class="col-md-8">
        <h1 id="title">Education</h1>
        <div>
            <h4 class="inline">University</h4>
            <span class="float-right"> 2009 - 2015</span>
        </div>
        <div>
            <span>B.Sc in Computer Science</span>
            <span class="float-right"> Grades: 2:1</span>
        </div>
        <div>
            <h4 class="inline">Highschool</h4>
            <p class="float-right">2006 - 2009</p>
        </div>
    </div>
</div>

Modern Approach

By applying flexbox styling within an existing DOM structure, achieve streamlined alignment of content within rows without altering the underlying markup.

.side-layout {
    display: flex;
    justify-content: space-between;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-md-8">
    <div class="col-md-8">
        <h1 id="title">Education</h1>
        <div class="side-layout">
            <h4>University</h4>
            <span> 2009 - 2015</span>
        </div>
        <div class="side-layout">
            <span>B.Sc in Computer Science</span>
            <span> Grades: 2:1</span>
        </div>
        <div class="side-layout">
            <h4>Highschool</h4>
            <p>2006 - 2009</p>
        </div>
    </div>
</div>

Answer №2

Insert a span element within the h4 tag to style content.

<div class="col-md-8">
  <h1 id="title">Education</h1>
  <h4 style="display: inline">University
    <span style="float:right"> 2009 - 2015</span>
  </h4>
  <p style="display: inline">B.Sc in Computer Science</p><p style="display: inline"> Grades: 2:1</p>
  <span>
    <h4>Highschool</h4>
    <p>2006 - 2009</p>
  </span>   
</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

I would like to display a preview image with the same ID across several divs

I am looking to add previews within multiple divs in the same container. Here are my code snippets: HTML Code: <div class="thumbnail"> <div id="resim_view"> <img class="image img-thumbnail" src="#" /> </div> & ...

Positioning a video element in the center using the margin property with a value of 0 and

I'm having trouble getting this video element centered on my webpage. Here's the code snippet I've been working with: HTML <div id="container">     <video id="vid" x-webkit-airplay="allow" controls width="640" height="360" src ...

The update query is failing to make changes, but it is not presenting any errors upon execution

I'm currently developing a PHP user administration system for my workplace. I have successfully implemented the functionality to create new users and update details such as first name, last name, email, and password for the logged-in user. Additionall ...

Can I insert JavaScript code in any location within an HTML file?

Typically, Javascript code is placed in the header section of HTML code. <head> <script type="text/javascript" language="javascript" src="core.js"></script> ... </head> However, I've tested putting Javascript code in the body ...

Display the HTML/CSS layout following the JavaScript window.open action

Encountering issues with printing output in an opened window using JavaScript. I'm creating a new document through window.open and including CDN links to Bootstrap files. While everything appears fine in the opened window, when attempting to print (XP ...

Page Time Tracker: Monitor Your Website's Performance

How can we determine the amount of time a user spends on a webpage using C# code? Are there any available open source projects in .NET that enable us to track the time spent? ...

The foundation grid system is not being implemented

How come my div is not affected by the foundation grid system: render: function(){ var {todos,showCompleted,searchText} = this.state; var filteredTodos = TodoAPI.filterTodos(todos,showCompleted,searchText); return ( <div> ...

What is the process for extracting query parameters from an HTTP client and transmitting them to a Node.js server?

Despite extensive searching, I have not been able to find a solution for sending query parameters in a get request. Below is the Node.js server-side code that I have been working with: let propertiesObject = {q: 'lord of the rings'}; let url = ht ...

What causes the small black line to appear on the right side of an image when it is turned into a link?

When I turn an image into a link, why does it always seem to display a tiny black line to the right of the image? ...

data-cy appears in the DOM structure, yet remains unseen

I'm seeking some clarity on how to effectively use Cypress. While writing end-to-end tests, I encountered a failed test due to the element not being visible. A helpful answer I found assisted me in resolving this issue by clicking an element that was ...

Responsive navigation menu featuring dynamic dropdown menus using HTML and CSS

Looking to design a responsive HTML and CSS navigation menu with multiple dropdown menus? You're in the right place. I've scoured countless YouTube videos, but haven't quite found the perfect template for a two-part navigation menu that tic ...

jQuery tooltips experiencing lag or duplication when closing on adjacent elements

My tool tips are not disappearing correctly, specifically in IE where they lag after you move to the next element. Using jQuery: $(document).ready(function() { loadMap(x, y,z); $(document).tooltip({ at: 'center top', my: ...

The mobile devices are not showing my HTML website

I have implemented the following CSS link code on my website: <link rel="stylesheet" href="index_files/front.css" media="all" type="text/css" > Additionally, I have included the following code <meta name="HandheldFriendly" content="True"> & ...

Modify the color of the background div beforehand

How can I change the background color of the div "#cont-comentarios" when a checkbox is clicked? The change should occur in a loop using 'this'. I have tried numerous solutions before reaching out for help. See the code snippet below for better u ...

Featuring a visual arrangement of categorized images with AngularJS for an interactive display

As I work on developing a web application with Angular, my goal is to create a grid layout that organizes items by category. Each row will represent a different category. Below is an example of the code structure I have in place: <ion-item ng-repeat="i ...

Ways to get rid of the white horizontal line generated by bootstrap framework?

I'm currently working on a responsive navbar design and I want it to appear transparent over a background image. However, my front-end knowledge is limited as I've only been learning for a week. Can anyone advise me on how to remove the white bar ...

Can someone help me figure out how to make my Dropdown stay open when I highlight input, drag, and release

While working with the react bootstrap Dropdown component, I've encountered a specific behavior that is causing some trouble. To better illustrate the issue, I've attached some images. In my dropdown, there is an input filter box along with a li ...

Can you explain the distinction between the onclick(function(){}) and on('click',function(){}) functions in jQuery?

My goal is to dynamically load pages into a specific div using ajax. Here's my HTML code: <ul id="nav" class="nav" style="font-size:12px;"> <li><a href="#" id="m_blink">Tab1</a></li> <li><a href="#" id= ...

Unlocking the potential of # to craft interactive web pages

Lately, I've been seeing more and more websites like Twitter that have integrated AJAX into their layouts. One thing that has really piqued my interest is the inclusion of #! in URLs. I'm curious about how I can implement this on my own site or w ...

Why isn't my navbar taking up the full width of the screen with CSS?

Hello everyone, I'm encountering a little issue with my responsive web app. When I resize my browser manually, everything seems to be working fine. However, when I test out the responsive options, I notice that my navbar is only taking up about 90% of ...