Stack two paragraphs vertically, one centered and one to the right on the same line

According to the experts at W3Schools

The align attribute is no longer supported in HTML5. Using CSS is the recommended approach.

I am puzzled by how I can align two paragraphs on the same line.

I am open to using flexbox for this task, but I want to avoid adding unnecessary dummy elements or complicating it with rows and columns.

This is my current attempt, where the text-align property doesn't seem to have an effect:

p {
  display: inline-block;
}

.center {
  text-align: center;
}

.right {
  text-align: right;
}
<div class="container">
  <p class="center">Center</p>
  <p class="right">Right</p>
</div>

Answer №1

To achieve this layout, you can utilize the power of Flexbox along with the position property:

.centered {
  position: relative; /* parent container needs a position */
  display: flex; /* arranges child elements in a row */
  justify-content: center; /* centers items horizontally */
  align-items: center; /* centers items vertically */
}

.centered > .right {
  position: absolute; /* child element is positioned absolutely */
  right: 0;
}

@media (max-width: 480px) { /* responsive design adjustment */
  .centered {
    flex-direction: column; /* stacks child elements vertically on smaller screens */
  }
  .centered > .right {
    position: static; /* revert to default positioning */
    align-self: flex-end; /* aligns child element to the horizontal end */
  }
}
<div class="centered">
  <p class="center">Center</p>
  <p class="right">Right</p>
</div>

Answer №2

h1 {
   display: block;
}

.middle {
  margin-left:50%;

}

.left-align {
  float: left;
}

Answer №3

If you're looking to achieve a specific layout, consider incorporating percentage width for your paragraphs and applying float:right; to both of them. Remember the order in which you place the .right paragraph above the .center paragraph in your HTML code.

Check out the snippet below for guidance.

p {
  display: inline-block;
}

.center {
  text-align: center;
  width: 33.3333%;
  float: right;
}

.right {
  text-align: right;
  width: 33.33333%;
  float: right;
}
<div class="container">
  <p class="right">Right</p>
  <p class="center">Center</p>

</div>

I hope this provides some assistance.

Answer №4

Given the brief description provided, there isn't much to respond with.

Here's how I would approach it:

If I were to use flex:

.container {
display:flex;
}
.center {
  flex:1;
  text-align: center;
}

.right {
  text-align: right;
}
<div class="container">
  <p class="center">Center</p>
  <p class="right">Right</p>
</div>

If I were to utilize table-layout (compatible with IE8 and above):

.container {
  display: table;
  width: 100%;
}

p {
  display: table-cell;
}

.center {
  text-align: center;/* will use whole space available*/
}

.right {
  text-align: right;
  width: 5em;/* a small value to shrink it based on its content */
}
<div class="container">
  <p class="center">Center</p>
  <p class="right">Right</p>
</div>

If I decided to go with grid:

.container {
  display:grid;
  grid-template-columns:1fr auto;
}


.center {
  text-align: center;
}

.right {
  text-align: right;
}
<div class="container">
  <p class="center">Center</p>
  <p class="right">Right</p>
</div

I wouldn't resort to using float or inline-block where elements may wrap onto the next line unexpectedly (unless max-width is specified for both elements) ;)

Answer №5

Here is a straightforward solution:

.container {
  position: relative;
  text-align: center;
}

.center {
  display: inline-block;
}

.right {
  display: inline-block;
  position: absolute;
  top: 0;
  right: 0;
}

By using position: absolute on the .right class, you have the ability to reposition the paragraph within its nearest parent element that has position: relative set (if no parent elements have this property, it will relate to the window).

The reason why text-align does not work in this case is due to the display: inline-block setting.

Here are some fundamental concepts to consider:

  • A paragraph element acts as a container, with the text inside considered another element itself.

  • text-align aligns the content within an element, so the text aligns itself to the borders of the paragraph element.

  • The default behavior for a paragraph element is display: block, which means it takes up the full width of its parent element (or the window if not nested). As a result, text-align has a clear impact.

  • When display: inline-block is used, the element's borders shrink to fit the content size. Since text-align affects the content and not the element directly, the paragraph element remains stationary.

Answer №6

.box {
  display:flex;
}

span {
  display: block;
}

.centered {
  text-align: center;
}

.right-aligned {
  text-align: right;
}

.left-aligned {
  text-align: left;
}

<div class="box">
  <span class="centered">Centered</span>
  <br>
  <span class="right-aligned">Right Aligned</span>
  <br>
  <span class="left-aligned">Left Aligned</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

Struggling to display the sorted array items on the screen?

Within the realm of my jsfiddle experiment, my aim is to organize items based on price from highest to lowest by utilizing the following function: function mySortingFunction() { var elements = [].slice.call(document.getElementsByClassName("price")); e ...

Enhancing JQuery UI Accordion with simple ICONS

Is it necessary to use the Jquery CSS file for adding icons, or is there an alternative method? I'm hoping I can avoid using it as I am working within Wordpress and enqueuing the Jquery CSS seems like a complex task for me. Apologies for the basic q ...

Is there a way to ensure a grid container occupies the entire page?

Having trouble getting my grid display to cover the full size of the page. I have a grid with three columns controlled by two elements but it is not taking up the entire screen as expected. How can I make the grid fullscreen? .wrapper { display: grid; ...

PHP Arrays and HTML Form Select Elements

I am facing a challenge with a form that contains multiple rows and numerous "select" drop-down menus in each row. While I'm presenting a simplified 2x2 example here, the actual form could extend up to 5x5. Here is an outline of my HTML form: <fo ...

Enhancing User Experience with Interactive React Hover Effects

Hello, I have tried various combinations but still cannot get the desired background color to display when hovering over my <p> element. I am looking for a CSS-only solution and not interested in using JavaScript hover events. I would appreciate it ...

Is there a way for me to direct to a different HTML page once I click on a certain data point?

At the moment, I have been utilizing this complete set of calendar codes. <!DOCTYPE html> <html> <head> <meta charset='utf-8' /> <link href='fullcalendar.css' rel='stylesheet' /> <link href=&a ...

What could be causing my website icon to not appear?

I'm having trouble displaying my website icon after uploading it to Favic-o Matic's Website and implementing the HTML code I received. Despite trying various troubleshooting steps, such as clearing the cache and saving the site as a bookmark, the ...

Transforming HTML output into JSON format on Firefox

Currently, I have a JSON file named persons.json that contains information about various individuals. [ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":" ...

Exporting Datatables to excel does not remove HTML tags as expected

I recently implemented a data tables export button on my data table. Some of the columns in this table contain HTML tags, such as: <button data-toggle="dropdown" class="btn btn-primary dropdown-toggle btn-xs">BUTTON NAME</butto ...

Creating a Custom Checkbox with HTML and Label

How can I position the label on top of a checkbox? HTML: <input type='checkbox' name='thing' value='valuable' id="thing"/> <label for="thing">Description</label> CSS: input[type=checkbox] { display:n ...

Design a floating text box similar to Gmail's style by incorporating Google's Material Design Lite

I have been utilizing the Material design lite library in an attempt to replicate the Floating text box feature seen in Gmail by Google. Despite not finding any official documentation on this particular component, I decided to experiment with the following ...

Safari is struggling with the backface visibility feature not functioning as expected

I have implemented a cssflip animation in my code where the element rotates on hover. I included transition and backface-visibilty properties. While it works well on Chrome, I faced issues with Safari. I even added the webkit prefix for Safari browser. `. ...

Select a specific division element using a pseudo element

My goal is to specifically target the third div in my HTML code with a class called .counter-wrap. When viewed on a mobile device, this particular div has a margin-bottom of 40px. I am looking to eliminate the margin-bottom for only the third stacked div n ...

Display the URL created in the DIV

I have a JavaScript function that constructs a URL using form inputs and allows users to preview it in a DIV named "preview" before opening the URL in a new window via a submit button. While everything is working as expected, I am looking for a way to mod ...

The appearance of random instances of the letter "L" within text on Internet Explorer 8

Help needed! I'm encountering an issue where mysterious "L" characters are appearing in IE 8. The problem is specifically occurring in the Healthcare Professionals section and the bottom two blocks of the page. Has anyone else experienced this or have ...

Is there a way to ensure that my JavaScript code remains permanent?

Is there a way to have the website remember the user's theme preference between visits? For example, if they choose the dark theme, can it automatically be loaded the next time they access the site? This is my current javascript code for handling the ...

Tips for incorporating fading text and a CTA into your content block

I am looking to protect the full content of my blog posts and allow access only to subscribed members. I want readers to see just a glimpse of the article before it disappears, prompting them to take action with a Call to Action (CTA) like in the fading te ...

Is there a method to transform the lower portion of a background image into a uniform white color?

Hey everyone! I'm a new user trying to work on my personal project. I was wondering if there's a way to use CSS to turn the bottom half of this image (link: ) completely white. Any tips or suggestions would be greatly appreciated! Thank you! ...

Inserting information into a SQL database using PHP

Looking for some assistance here. I've double-checked the SQL table names multiple times, but whenever I try to post, I keep getting an error. I'm fairly new to this, so any help would be greatly appreciated. Thanks in advance. require_once(&ap ...

Customizing event colors in Full Calendar

My interactive calendar is created using : $('#calendar').fullCalendar({ height: 300, //............. events: jsonData, month: firstMonth }) I am looking to dynamically change the color of an event based on certain conditions ...