What is the best way to position this span on the right side of the div?

Here is some HTML code snippet:

<div class="title">
    <span>Cumulative performance</span>
    <span>20/02/2011</span>
</div>

This is the corresponding CSS:

.title
{
    display: block;
    border-top: 4px solid #a7a59b;
    background-color: #f6e9d9;
    height: 22px;
    line-height: 22px;
    padding: 4px 6px;
    font-size: 14px;
    color: #000;
    margin-bottom: 13px;
    clear:both;
}

In a jsFiddle linked here: open link to view

The issue I am facing is that the Title and Date are not aligned properly. Is there any solution to make the date align to the right? I attempted using float: right; on the second <span> but it affected the layout and pushed the date outside of the div container.

Answer №1

If you have the ability to make changes in the HTML, take a look at this example

<div class="header">
  <span class="title">Overall progress</span>
  <span class="date">15/07/2014</span>
</div>

.header .date { float:right }
.header .title { float:left }

Answer №2

Dealing with floats can get a little messy:

Many 'simple' layout tricks like this can actually be achieved using flexbox.

   div.container {
     display: flex;
     justify-content: space-between;
   }

In my opinion, in 2017 using flexbox is the preferred solution (compared to floats) if you don't need to support older browsers: https://caniuse.com/#feat=flexbox

Take a look at this fiddle to see how different float techniques stack up against flexbox ("may include some competing answers"): https://jsfiddle.net/b244s19k/25/. If you still need to use float, I recommend the third version.

Answer №3

Instead of relying on floats, another approach is to utilize absolute positioning:

.header {
  position: relative;
}

.header span:last-of-type {
  position: absolute;
  right: 8px;   /* should match parent's right padding */
}

For a live example, check out this fiddle.

Answer №4

Implementing flexbox layout without the use of justify-content: space-between.

<div class="title">
  <span>Cumulative performance</span>
  <span>20/02/2011</span>
</div>

.title {
  display: flex;
}

span:first-of-type {
  flex: 1;
}

By applying flex:1 to the first <span>, it fills up the remaining space and shifts the second <span> to the right. Check out the working example here: https://jsfiddle.net/2k1vryn7/

To compare two different ways of using flexbox, visit this link: https://jsfiddle.net/7wvx2uLp/3/. It demonstrates the difference between flexbox with justify-content: space-between and flexbox with flex:1 on the first <span>.

Answer №5

You have the ability to achieve this task without making any changes to the original html code. http://jsfiddle.net/9KdpX/2356/

<div class="heading">
<h2>Overall progress</h2>
<p>03/12/2013</p>
</div>

.heading h2:nth-of-type(1) { float:right }
.heading p:nth-of-type(2) { float:left }

Answer №6

ul, ol { /** styles for both ul and ol elements **/
    list-style: none; /** removes bullet points/numbering **/
    padding-left: 0px; /** removes actual indentation **/
}

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 there a way to trigger the opening of a new file or page when a CSS animation comes to an end?

Is there a way to delay the loading of a function or page until after an animation has finished running in JavaScript, HTML, and CSS only? For instance, I'd like to run an animation first and then have a different website or content load afterwards fo ...

List arranged in order with a hyperlink in the center and an image positioned on the right side

I am trying to create an ordered list with a link to an article in the middle and a small png image file positioned directly to the right of it. For reference, here is an image depicting the exact type of list that I am aiming to achieve: https://i.stack. ...

Top tips for optimizing HTML and utilizing div elements

Could you please provide some insights on how to effectively utilize HTML5 in conjunction with div tags? Below is the snippet of my HTML code. I am incorporating the article tag and have segmented sections, which seem to be in order. However, I am also ...

Avoiding the collapse of the Bootstrap 4 navbar

Having trouble with the navbar collapsing on itself? The issue seems to occur around 1280px window size, but works fine when the window is wider or narrower. https://www.bootply.com/AcsaaqReAL .navbar-custom { background-color: #333333; border- ...

Modifying the overflow behavior within a Vuetify dialog

Is there a way to have a floating action button positioned on the top right of a dialog, slightly overflowing so it's offset from the dialog itself? I'm struggling to override the 'overflow-y: hidden' property set by v-dialog. <v-dia ...

Create a full bottom shadow for a circular shape using CSS 3

Hey there, I'm having an issue with creating a separation effect for a circle. I've been using the box-shadow property like this: box-shadow: 0 1px 0 rgba(0,0,0,.2);. However, as you can see in the image, the shadow on the left and right sides is ...

Develop a dynamic webpage using ASP.NET for enhanced interactivity

Currently, I am working with asp.net 2.0 and C#. I have a unique teacher-student dynamic in my platform, where I envision students receiving personalized pop-ups created by their respective teachers upon login. My challenge lies in providing teachers wit ...

Is it possible to insert clickable links within the content of a Twilio text message?

Currently, I am utilizing Twilio and Express to send programmable SMSs to the users of my web application. I'm curious if it's possible to include hyperlinks within the body of these text messages. Is there a method to achieve this? I have attem ...

Customizing Mouse Pointers in CSS

My goal is to customize the click pointer on a website using CSS. I have successfully changed the default cursor, but now I am seeking help in changing different 'versions' of the cursor. Here's my current attempt at customizing the pointe ...

Ways to prioritize the parent element's style over the child element's style attribute

I am looking to customize the appearance of an unfamiliar HTML code by changing its text color to cyan, regardless of its current color. I attempted the following: <p style="color:cyan !important;"> <span style="color:red;">Hello World</ ...

Creating a sleek navigation bar and sliding feature within the header of your Ionic 2

I am looking to create a sliding header for my website where the gallery hides and the navbar moves to the top as I scroll down, similar to the gif provided. Any help or ideas on how to achieve this would be greatly appreciated. Thank you. https://i.sstat ...

error displays stating that the module '../util/http_util' cannot be located

I'm currently using node and Notepad++ to develop a website that allows users to log in, save their details, and access the site. I've encountered an issue that I can't quite figure out even though everything seems fine in the code. Below yo ...

Is it possible that smarty assigns but doesn't execute the code?

In the process of developing a PHP-Script, I am tasked with assigning HTML code to various templates. PHP-File: $smarty->assign("PLACEHOLDER", getCode()); $smarty->display('index.html'); function getCode(){ return "{literal}some co ...

What steps can I take to ensure that the text content stays within the borders and does not overlap with the photo?

click here for the image description Html <p><img src="index-foto1.jpg" align="left" width=65% style="border-radius: 10px;" margin-right: 2px> <div class="tant"><h3>More detailed inform ...

Ways to verify the application of CSS hyphens using Chrome's developer tools

I cannot seem to get automatic hyphens to work via CSS. I have applied the following CSS: body { -moz-hyphens: auto; -ms-hyphens: auto; -o-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; } Despite adding this CSS, my text is not being hyph ...

What are the possibilities for modifying a MySQL database using HTML?

Currently, I have a MySQL database managed through phpmyadmin that I would like to present as an HTML table for easy editing of records and columns directly. I am unsure about the terminology to use when researching possible solutions. What options are av ...

Issue with calling a function to change the CSS color class of a button in Angular

Within my Angular code, I am attempting to set a CSS color for my button by calling a TypeScript function that will return the appropriate CSS class name. This is the code I have tried: <button style="height: 10%" class="getColor(days.date)">{{days ...

Creating rounded buttons with Font Awesome icons and hover effects

I am currently working on creating buttons similar to the design shown in the image below. https://i.sstatic.net/hMqHs.png I have utilized stacked Font Awesome icons to place the icons atop a circle, resulting in this look: https://i.sstatic.net/rtEkP.p ...

Is there a way to streamline the process of uploading an image and storing its details in a single HTML form with just one submit button?

Here is the code snippet I have: <form id="registration_form" action="AddProductServlet" method="post"> <div> <label> <input placeholder="Product ID" name="pid" type="text"> ...

Utilizing Bootstrap 4 columns to control the height of a separate column

Is there a way in CSS to make one column in a bootstrap grid determine the height of another column within the same row? For example, consider the following scenario: <div class="row"> <div class="col col-sm-6" id="column1"> <!-- an e ...