modifying the arrangement of span elements

Currently, I am assisting a colleague in crafting articles for scientific journals. Interestingly, various publishers have distinct requirements regarding the format of cited authors in the bibliography. Some demand "last name, first name," while others prefer "first name, last name" (with an ordered list by last name and potentially last names displayed in small caps).

Admittedly, utilizing a database would be more efficient; however, my colleague's expertise does not extend to that level.

If I were to create a basic HTML page with lines similar to this example:

<p><span class="lname">Doe</span>, <span class="fname">John</span></p>

Is there a CSS technique available that would display the line as "John DOE", or would I need to resort to using JavaScript?

Answer №1

If you want to achieve this layout using flexbox, you can make use of the order property (with some tweaking).

Here is the code snippet (https://jsfiddle.net/sebastianbrosch/7Lqr68ar/):

p {
  display:flex;
}
.fname {
  order:1;
}
.lname {
  order:2;
}
.fname:after {
  content:",\00a0";
}
<p>
  <span class="lname">Doe</span>
  <span class="fname">John</span>
</p>

You can also try a more dynamic solution like this:

#name {
  display: flex;
}
#name.fl .firstname:after {
  content: "\00a0";
}
#name.lf .lastname:after {
  content: ",\00a0";
}
#name.fl .firstname,
#name.lf .lastname {
  order: 1;
}
#name.fl .lastname,
#name.lf .firstname {
  order: 2;
}
<p class="fl" id="name">
  <span class="firstname">John</span><span class="lastname">Doe</span>
</p>
<p class="lf" id="name">
  <span class="firstname">John</span><span class="lastname">Doe</span>
</p>

Pro tip: While this solution works visually, it's recommended to prepare the name properly with PHP or directly from the database in the correct format. Using the order property only affects visual ordering and not the underlying code or accessibility for screen readers.

Answer №2

One way to fix this issue is by using the float property and hiding the comma in your HTML code.

.lname {float: left; padding-right: 5px;}
.comma {display: none;}
<p><span class="lname">Doe</span><span class="comma">,</span> <span class="fname">John</span></p>

Answer №3

Here's an innovative twist utilizing data-attributes along with the :before and :after pseudo-elements

If you have a container for your list of authors named .autherContainer that has either the extra class .lnLast or .lnFirst, and the author names are structured as shown, with the last name repeated twice in each version

HTML:

<div class="authorContainer lnFirst">
    <p data-ln-first="Doe, " data-ln-last=" DOE">John</p>
</div>

<div class="authorContainer lnLast">
    <p data-ln-first="Dear, " data-ln-last=" DEAR">Jane</p>
</div>

CSS

.authorContainer.lnFirst p:before {
    content: attr(data-ln-first);
}

.authorContainer.lnLast p:after {
    content: attr(data-ln-last);
}

I made some assumptions based on arranging the lists in a shared container, enabling them to be presented chronologically without adhering to a specific format or unnecessary code additions. However, individual preferences may vary.

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

The browser is opting to download the HTML file rather than displaying it

Recently, I set up a server using Node.JS express where I specified the location of an HTML file in the public folder. app.use(express.static(__dirname + '/public')); app.listen(8080); In previous projects, this setup worked seamlessly. However ...

Get rid of the .php extension in the URL completely

Lately, I've been experimenting a lot with the .php extension. I successfully used mod_rewrite (via .htaccess) to redirect from www.example.com/example.php to www.exmaple.com/example. Everything is running smoothly. However, I noticed that even though ...

Utilize a unique font exclusively for Internet Explorer

I am encountering a significant issue trying to utilize custom fonts in Internet Explorer. Currently, I have implemented a specific font using @font-face and it works perfectly in modern browsers but fails to render properly in IE. The method I am curren ...

The functionality of Small Caps is not supported by Twitter Bootstrap when using Chrome browser

I am working with a very basic markup <h1> <a href="#"> My Title </a> </h1> and I have this CSS applied h1 { font-variant: small-caps; } You can see the result on this jsfiddle link. The problem arises when u ...

Is there a way to keep this table fixed in place as the user scrolls?

Is there an alternative way to fix the content on the header so that it remains visible on the screen until the next table, even when scrolling? I've tried using position: sticky and top: 0, but the header still scrolls past. I have checked for any ov ...

Django: Troubleshooting problem with offcanvas PDF preview iterations

Hey everyone! I'm in the process of creating a webpage that features a table with metadata regarding PDF files. To enhance user experience, I want to provide users with a preview of stored files in an off-canvas format. This is my HTML file setup: & ...

Changing the color of an image with a click event in HTML

My coding dilemma involves two HTML codes: one displaying a table with grey square buttons, and the other featuring the same table but with orange buttons instead. I am looking for a way to change the color of the button from grey to orange when clicked, ...

Persisting Big IndexedDB in the Browser

As we embark on the development of a Line of Business (LOB) HTML5 web application, our key objective is to ensure that the application has offline capabilities. Our plan involves retrieving a substantial amount of SQL data from the server and storing it in ...

What is the best way to line up two forms side by side?

Looking to create a login view for a webpage with two forms (one for registration and one for login) aligned side by side. Initially attempted using a table, then tried using bootstrap form groups (rows and cols) without success. Finally, aligned them in t ...

What is the best way to connect a fresh post to a different URL in ReactJS and Redux?

Currently, I am working with Redux and ReactJS to develop a discussion platform. One of the key features I am trying to implement is where users can input the title and content of their post, which will then be submitted to create a new post. After submit ...

Utilize jQuery to extract various input/select box values and compile them into an array for submission using .ajax()

I am currently facing an issue with dynamically generated forms using PHP and updated with jQuery's .appendTo() function as visitors interact with it. My main goal is to collect all input text and select box values from the current form and submit the ...

When viewing a webpage on a small browser, the content will automatically expand to fill the

Attempting to utilize responsive CSS media queries to hide the sidebar unless the screen is large or a tablet big enough in landscape mode. It appears to be functioning properly while resizing the browser, but at a certain size, it occupies the entire scre ...

Show only the selected option with jQuery's on change event and disable or remove the other options

My goal is to make it so that when a user selects an option from a dropdown menu, the other options are disabled or hidden. For example, if option "1" is selected, options "2", "3", and "4" will be removed: <div class="abc"> <div class="xyz"> ...

Could one harness the power of SO's script for adding color to code within questions?

Similar Question: Syntax highlighting code with Javascript I've observed that Stack Overflow utilizes a script to apply color coding to any code shared in questions and answers, making it resemble how it would appear in an IDE. Is this script pub ...

Kohana ajax causing removal of JQuery Data attributes

Currently, I am developing an application where jquery data is used to pass variables to HTML elements. It has been successful in one part of the site when the data attributes are added to a tr tag. The following code works: <tr class="js-instructions ...

npm installation for phonegap failed due to shasum check discrepancy

I've attempted numerous times, but I keep encountering this error (shasum check failed) 4784 error Error: shasum check failed for C:\Users\FENGXI~1\AppData\Local\Temp\npm-7004-QbpFFte5\1387269030233-0.28223602287471 ...

AngularJS: ng-show causing flickering issue upon page refresh

Recently, I encountered an issue with my code snippet: <body> <ng-view></ng-view> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script> <script src="http://ajax.googleapis.com/ajax/ ...

A technique for adding a border to a Mat Card that mimics the outline of a Mat Form Field within a custom

I am faced with a unique design challenge where I am utilizing the MatCardComponent and each card contains a table. I would like to incorporate a floating label within the border gap similar to what is seen in MatFormField. https://i.stack.imgur.com/51LBj ...

Enable scrolling exclusively for the content within a tab, without affecting the tab label in Clarity Tabs

I have a tab with lengthy content in my project (check out the StackBlitz reference here). This causes the appearance of a scroll. The corresponding code snippet is provided below: <div class="content-container"> <div class=&qu ...

Running PHP code within an HTML file served by PHP

A unique and innovative approach was taken by incorporating a DIV element in the HTML code, which dynamically populates with content from the main_login.php file. The main_login.php file plays a crucial role in loading the homepage with member-specific con ...