Alignment in HTML / CSS / ReactJS: Leveraging the Text-align attribute

My attempt to align the text to the right within my div element has been unsuccessful. Despite using text-align: right and width: 100%, the issue persists. It seems that the problem lies within the left-side and right-side attributes, but I am unable to pinpoint the exact cause of the problem.
Below is my code snippet :

.left-block {
  width: 50%;
  height: 450px;
  display: inline-block;
  padding-left: 1%;
  padding-right: 1%;
}

.selected-file {
  color: #12073C;
  font-size: 25px;
  font-weight: 700;
  padding-right: 2%;
  padding-left: 3%;
}

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

.right-side {
  text-align: right;
  color: red;
}

.small-title {
  font-weight: bold;
  font-size: 20px;
  padding-left: 2%;
  margin-bottom: 1.5%;
}
<div className="left-block">
  <div className="file-informations">
    <div className="small-title">File Information</div>

    <div className="file-values">
      <span className="left-side">File Size</span>
      <span className="right-side"><b>{this.humanFileSize(this.state.fileSize)}</b></span><br/> File category <b>{this.state.type}</b><br/>
    </div>
  </div>

  <div className="merging-informations">
    <div className="small-title">
      Merging statistics
    </div>

    <div className="merging-values">
      <b>Primary Key</b> <br/> o/w New Lease <br/> o/w New Well <br/> o/w Existing <br/>
      <b>o/w Conflicted</b> <br/> o/w Distric Name <br/> o/w Field2 <br/> o/w Field3 <br/>
    </div>
  </div>

</div>

Answer №1

Span tags are considered inline elements, which means they cannot utilize the text-align property for aligning content left or right.

To achieve the desired layout, consider utilizing floats:

.file-values {
  overflow: hidden;
}

.left-side {
  float: left;
}

.right-side {
  float: right;
}

For more information on floats, check out this resource: https://developer.mozilla.org/en-US/docs/Web/CSS/float

Alternatively, you could explore using Flexbox by applying a simple rule like this:

.file-values {
  display: flex;
}

Implementing this approach should help you achieve the desired layout.

Answer №2

The alignment of your text works fine, but the issue lies in using a span as your container, which is an inline element. By default, it takes up the necessary width to fit its content. To fix this, consider setting a wider width and changing the display to block to see the text align properly to the left or right.

It might be more suitable in your case to use div elements, as they have a default display of block.

Instead of using <b> tags, you can achieve bold styling using CSS with font-weight: bold.

.left-block {
  width: 50%;
  height: 450px;
  display: inline-block;
  padding-left: 1%;
  padding-right: 1%;
}

.selected-file {
  color: #12073C;
  font-size: 25px;
  font-weight: 700;
  padding-right: 2%;
  padding-left: 3%;
}

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

.right-side {
  text-align: right;
  color: red;
}

.small-title {
  font-weight: bold;
  font-size: 20px;
  padding-left: 2%;
  margin-bottom: 1.5%;
}
<div class="left-block">
  <div class="file-informations">
    <div class="small-title">File Information</div>

    <div class="file-values">
      <div class="left-side">File Size</div>
      <div class="right-side"><b>{this.humanFileSize(this.state.fileSize)}</b></div><br/> File category <b>{this.state.type}</b><br/>
    </div>
  </div>

  <div class="merging-informations">
    <div class="small-title">
      Merging statistics
    </div>

    <div class="merging-values">
      <b>Primary Key</b> <br/> o/w New Lease <br/> o/w New Well <br/> o/w Existing <br/>
      <b>o/w Conflicted</b> <br/> o/w Distric Name <br/> o/w Field2 <br/> o/w Field3 <br/>
    </div>
  </div>

</div>

Answer №3

<span> element being used is an inline element, which means its width is determined by the size of its content. To adjust its width, we can apply the display: inline-block property.

The .left-block parent element has a fixed width of 50% of the body. By setting the width: 100%; for the <span> element, it will match the size of the .left-block.

Changing the display mode of the element allows us to align the text according to our preference.

Below is an example of the code:

.right-side {
  display: inline-block;
  width: 100%;
  text-align: right;
  color: red;
}

Check out a live example of this code in action (I've converted it to HTML/CSS for easier observation).

Answer №4

For a solution, consider using display:flex in this manner:

*{
  margin:0;
}
.left-block {
  width: 50%;
  height: 450px;
  display: inline-block;
  padding-left: 1%;
  padding-right: 1%;
}

.selected-file {
  color: #12073C;
  font-size: 25px;
  font-weight: 700;
  padding-right: 2%;
  padding-left: 3%;
}
.file-values{
  display:flex;
  flex-wrap:wrap;
}
.right-side{
  margin-left:auto;
}

.small-title {
  font-weight: bold;
  font-size: 20px;
  padding-left: 2%;
  margin-bottom: 1.5%;
}
<div class="left-block">
  <div class="file-informations">
    <div class="small-title">File Information</div>

    <div class="file-values">
      <span class="left-side">File Size</span>
      <span class="right-side"></span>File category
    </div>
  </div>

  <div class="merging-informations">
    <div class="small-title">
      Merging statistics
    </div>

    <div class="merging-values">
      <b>Primary Key</b> <br/> o/w New Lease <br/> o/w New Well <br/> o/w Existing <br/>
      <b>o/w Conflicted</b> <br/> o/w Distric Name <br/> o/w Field2 <br/> o/w Field3 <br/>
    </div>
  </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

Connecting CSS Style Sheet to JSP Page

I'm facing an issue with linking a CSS file located in my WEB-INF folder to a JSP page. In the JSP page, I have specified the location of the CSS file as follows: <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/WEB- ...

Ways to obtain jquery object for replaced DOM element

Is there a way to select the new content after using the replaceWith function in my plugin? I want to chain my plugin for the new content. $.fn.customPlugin = function() { this.replaceWith('<div>New Content</div>'); }; So, ideal ...

In search of bundle.js, the React app is scouring through the component folder, rather than

Upon refreshing on a nested route (register/email-confirmation), the console displays errors that do not occur on non-nested routes. The issue seems to be related to the search for bundle.js and the image within the nested route path, rather than the root ...

Adding a task to my database successfully through Postman integration

I'm having trouble implementing the code for my todo app using React. I am encountering an issue with adding a new todo to the database using the handleSubmit function. Oddly enough, it works fine when testing with Postman, but not when trying to inpu ...

The CSS sibling combinator is not functioning as expected

Imagine having this HTML structure, <div id="banner-message"> <p>Hello World</p> <button>Hover to change color</button> </div> My goal is to target the p element from the button. Why is the following CSS not workin ...

The React module encountered a parsing error due to an unexpected character '@' in the code

I encountered an issue while attempting to import the following in my react component: import FontIconPicker from '@fonticonpicker/react-fonticonpicker'; import '@fonticonpicker/react-fonticonpicker/dist/fonticonpicker.base-theme.react.css& ...

.Net RadioButton object

Having trouble styling my radio button. When I compile and check the code with Firebug, something unusual happens. Code snippet: <asp:RadioButton ID="selType1" GroupName="Type" runat="server" CssClass="radioB" Checked="true" /> Firebug result: & ...

There seems to be a glitch with the Flask flash messaging feature, as

My Flask application includes login and register routes where I aim to display flash messages when username passwords are not matched. Specifically, this issue arises in my index and login routes: @app.route('/') @login_required def index(): ...

Styling Fonts in Safari with CSS

Because FixedSys doesn't appear correctly in Chrome or Safari, I switch it to Lucida Console. While this solution works for Chrome, I encounter a problem with Safari. Unless Lucida Console stands alone, the font will not display as desired. If there a ...

Attempting to generate a dynamic list of input fields with React

I recently created this website at , and now I am looking to migrate the frontend to React in order to enhance my React skills. When using vanilla JavaScript, dynamically creating elements was a breeze. By simply using createElement, I could easily loop t ...

Identify moving objects with react-beautiful-dnd

I've successfully implemented a draggable list using react-beautiful-dnd by following the tutorial. The result looked like this: https://i.sstatic.net/loXA2.png You can find the code for this implementation here: https://github.com/DDavis1025/react- ...

How to create a scrolling fixed div that moves horizontally and repositions to the center when the page is maximized

I've been searching for a solution to my problem and have managed to figure out the first part, but there's still one issue that I could use some help with. While I have some knowledge of html, css, and basic php, I'm completely new to javas ...

The library 'react-bootstrap' does not have a 'Grid' export available

I attempted to run npm start, but encountered the following error. ./src/App.js 104:16-24 'react-bootstrap' does not contain an export named 'Grid'. How can I resolve this issue? I already tried running npm install. Is there a substitu ...

Can React be hosted on a Node server?

As a beginner in back-end coding, I am struggling to grasp the concept of a "fullstack" app. Can someone explain what it actually looks like? Is it: An application with a Node-Express server serving index.html, which includes <script src="main ...

Continue to run upon clicking the button in the Document Object Model

I want the code to constantly change instead of executing only once. By default, the button has a dark mode for text and the background color is pink. When you click the button, the background color changes to black and the text in the button turns into li ...

Press the Instagram Follow Buttons by utilizing Selenium with Java

click here for image description Hello, I am currently working on automating the process of clicking follow buttons using Java. However, I'm encountering difficulties when trying to use JavascriptExecutor within a for loop. Below is the code snippet ...

Unable to import necessary modules within my React TypeScript project

I am currently building a React/Express application with TypeScript. While I'm not very familiar with it, I've decided to use it to expand my knowledge. However, I've encountered an issue when trying to import one component into another comp ...

CSS - Curved background image in the top left corner

Currently in the process of creating a website here. There is a postcode search div on the right side, and I am looking to round the top corner of it. I have decided to use images to achieve the rounded corners effect. My requirement is that any method us ...

Tips on expanding the content of a page all the way to the bottom of

Currently, I am facing an issue while working on a Joomla site where I am struggling to extend a page to the bottom of the screen. Attached is an image of the page: https://i.sstatic.net/uHZ9Q.png and below is the HTML code for the page: <div id="free_ ...

Are Half of the <a> Links Invisible due to a Possible Issue with Position:Fixed?

There seems to be an issue with some of the links in my navigation bar not working properly. It appears that they are being covered by an iframe element, preventing them from changing color when clicked. Although the iframe element is visibly below the na ...