Utilize flexbox to keep a paragraph element centered even when the text wraps, without directly centering the

Before this gets down-voted into oblivion, let me mention that I have thoroughly searched through numerous similar questions on SO. However, none of them provided a solution to my specific issue.

I am using flexbox to align <p> tags both horizontally and vertically within an element. My goal is to center the <p> elements themselves, not just the text inside them.

.app-outer {
  display: flex;
  flex-direction: column;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  justify-content: center;
  align-items: center;
}

.app p {
  display: inline-block;
}

.title-bar {
  background-color: #202225;
  color: #72767D;
  font-weight: bold;
  padding: 0 0 2px 8px;
  display: flex;
  flex-direction: row;
  width: 100%;
}
<div class="app-outer">
  <div class="title-bar">
    <span class="draggable">Skipwars</span>
    <span class="btns">
      <button id="btn-minimize" tabindex="-1">-</button><!--
      --><button id="btn-close" tabindex="-1">&times;</button>
    </span>
  </div>
  <div class="app">
    <p>Add a browser source pointed at <!--<a href="#">http://localhost:3333/</a>--></p>
    <p>
      Optional parameter <code style="display:inline">threshold=n</code>. ex: <a
        href="#">http://localhost:3333/?threshold=4</a> (default 8)
    </p>
  </div>
</div>

My app has a fixed width of 300px (I am using electron).

https://i.sstatic.net/56Lft.png

If the paragraph content does not span multiple lines, it works perfectly. However, when there is enough text to wrap to subsequent lines, the paragraph expands to the width of the .app container, leaving the text left-aligned.

This screenshot illustrates the desired outcome:

https://i.sstatic.net/a6vRH.png

I initially believed that setting the paragraphs' display property to inline-block would resolve the issue, but unfortunately, it did not.

Answer №1

Everything is functioning as intended, however, there is an issue with the alignment of the <p> tags when the viewport is too small. The long URL causes it to wrap onto the next line and create a gap on the first line, giving the impression that it is not centered. To highlight this, I have added some padding to the <p> tags:

.app-outer {
  display: flex;
  flex-direction: column;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  justify-content: center;
  align-items: center;
}

.app p {
  display: inline-block;
  padding: 0 20px;
}

.title-bar {
  background-color: #202225;
  color: #72767D;
  font-weight: bold;
  padding: 0 0 2px 8px;
  display: flex;
  flex-direction: row;
  width: 100%;
}
<div class="app-outer">
  <div class="title-bar">
    <span class="draggable">Skipwars</span>
    <span class="btns">
      <button id="btn-minimize" tabindex="-1">-</button><!--
      --><button id="btn-close" tabindex="-1">&times;</button>
    </span>
  </div>
  <div class="app">
    <p>Add a browser source pointed at
      <!--<a href="#">http://localhost:3333/</a>--></p>
    <p>
      Optional parameter <code style="display:inline">threshold=n</code>. ex: <a href="#">http://localhost:3333/?threshold=4</a> (default 8)
    </p>
  </div>
</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

What is the method for accessing font sizes through CSS code?

Why is the font size value in CSS returning incorrect values? Check out this jsFiddle example Here's the HTML code snippet: <div id="test">TEXT</div> <div id="test2">TEXT2</div> Now, take a look at the CSS: #test{ font ...

"Displaying mongoose date in the HTML5 input type date format (dd/mm/yyyy) is a simple process that can

I have a structured data in MongoDB as shown below: var Person = new Schema({ "Name": { type: String, required: true }, "DOB": { type: Date, "default": Date.now } }); An instance of this schema is created using NodeJs with mongoose ODM: { "N ...

Tips for customizing image flipper AJAX code

I am working on a script that swaps the current image with a selected one, but I need to add an attribute so it specifically targets image tags with a certain class SCRIPT---- <script> $(function(){ Test = { UpdatePreview: func ...

Is there a way to display only the specific child div within the parent div using JavaScript without affecting the others?

   **** Sorry for the lengthy title **** Today I encountered a problem that I would like to discuss with all of you. When I click on a "COMMENT" button, instead of triggering JavaScript code to display a CHILD.div inside the corresponding ...

Customizing Ngx-bootstrap Carousel Indicator, Previous, and Next Button Styles

<carousel > <a href=""> <slide *ngFor="let slide of slides"> <img src="{{slide.imgUrl}}" alt="" style="display: block; width: 100%;"> </slide> 1. Is there a way to substitute the indicators with images ...

Is there a way to pass locale data using props in VueJS Router?

To access hotel data, the URL path should be localhost:8080/hotel/:id (where id is equal to json.hoteID). For example, localhost:8080/hotel/101 This path should display the specific data for that hotel. In order to achieve this, we will utilize VueJS vu ...

Clicking the delete button in Firebase to remove an item

I am in the process of developing a simple CRUD application and have opted for Firebase as my backend solution. While I have successfully implemented the create and read functionalities, I've hit a roadblock with the delete operation. When attempti ...

I'm looking for recommendations on the best technologies to implement in a location-based mobile app. Any suggestions

Currently working on a server-side website tailored for mobile devices. My main objective is to create a member system where users can log in and share their geolocation data. Once logged in, the user's geolocation information will be stored in my dat ...

Adding DIV classes and IDs to my stylesheet

This is the code that I added to my Stylesheet: .friend { border:4px dashed 008000; } .enemy { border:2px dashed 008000; } .family { border:2px dashed 008000; } #archnemesis { border:4px solid 008000; } * { border:4px ...

Issue with CSS causing elements to display improperly within nested grids

I am facing an issue where I want to nest a grid within another grid, but the elements in the nested grid are not appearing in the order I desire. Specifically, I would like the "#classes" section to be at the bottom of the nested grid, but it remains stuc ...

Stop the content inside a div box from wrapping around the edges when using Bootstrap

Utilizing bootstrap to structure a webpage, I have organized three divs next to each other within a row. The widths of the divs are set as follows: 2 columns, 6 columns, and 4 columns in a 12 column layout. The middle div has a minimum width defined. As a ...

Footer remains fixed and scrolls into the body when the window is resized

Having trouble with the footer's responsiveness when resizing the desktop window. It looks fine in developer tools, but in different browsers, the footer placement gets messed up and scrolls over the rest of the content. Any insights on what might be ...

Is there a way I can finish animating these divs in a diagonal motion?

I successfully made the blue and red divs move diagonally and switch positions. Now I am looking to achieve the same effect with the green and pink divs using a similar function. I am unsure about setting the position of both divs to 0 and 350 for those p ...

Display or conceal a <div> segment based on the drop down selection made

A dropdown menu controls the visibility of certain div elements based on the selection made. While this functionality is working for one dropdown, it's not working for another even though the code is very similar. I've tried various solutions but ...

Error message indicating that the function 'slice' is not defined for the data variable

I've already searched for solutions to a similar issue, but none of them worked for me. I'm dealing with an object that fetches a list of cities including their names, populations, and states. Here is my HTTP service request: cidadesUrl = 'h ...

Filtering a section of the HTML using Ajax and jQuery

Dealing with an Ajax call in jQuery that's delivering a lot of unexpected HTML content. I'm attempting to extract the specific part I need, but the console is showing an empty string. Can anyone provide guidance on resolving this issue? $.ajax({ ...

How can I conceal child <div> elements when the parent div is resized?

Struggling to implement a zoom in/out feature on my web app using the jqueryUI slider. Having trouble handling cases where the parent div shrinks too much, causing issues with the child containers. <div class="puck originator inline-block" style="w ...

Navigation menu elements out of place

I am facing some challenges while trying to create a horizontal navigation: The first element appears to be displaced Is there a better way to position the text centered below the icon? Currently, I have used a tag for this purpose. Also, how can I us ...

Creating an If statement tailored for a particular image source: a step-by-step guide

In the program I am running in Dreamweaver, there is a specific line of code that looks like this: function go() { if((document.test.test1.src == document.test.test2.src && document.test.test2.src == document.test.test3.src )) ...... ...... ..... ...

What is the smoothest way to animate price changes as you scroll?

After searching online, I could only find a version for swift. The only keyword that resulted in a search was ScrollCounter. Is it possible to create this type of animation using CSS and HTML? If anyone knows of a helpful resource or example, please share ...