Adjusting the alignment of text using the "=" symbol

Can you help me align text with the "=" sign, just like in the image below?

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

I know I can achieve this using mathjax, but I'd prefer to do it with CSS. However, my current method isn't producing aligned equal signs. I would appreciate your assistance.

Here's what I've attempted:

Problem: 
Given f(x) = 3x 
g(x) = x + 2 
fog(x) = 18 
To find: x = ?
Steps:
fog(x) = 18
or, f(x + 2) = 18 
or, 3(x + 2) = 18
or, x + 2 = 6
Therefore, x = 4

Answer №1

To style your classes, you have the option to utilize display, text-align, and width properties:

.class-left{
  text-align: right;
  width: 50px;
  display: inline-block;
}

.class-middle{
  text-align: center;
  width: 10px;
  display: inline-block;
}

.class-right{
  text-align: left;
  width: 50px;
  display: inline-block;
}
<div>
  <div class="class-left">a + b</div>
  <div class="class-middle">=</div>
  <div class="class-right">c</div>
</div>
<div>
  <div class="class-left">2a + 2b</div>
  <div class="class-middle">=</div>
  <div class="class-right">3c</div>
</div>

If this doesn't meet your requirements, let me know.

Answer №2

If you're struggling with formatting text to be easily styled with CSS, the key is transforming it into a JSON array of strings.

[
  "Solution,",
  "Given, f(x) = 3x",
  "g(x) = x + 2",
  "fog(x) = 18",
  "To find: x = ?,",
  "Now,",
  "fog(x) = 18",
  "or, f(x + 2) = 18",
  "or, 3(x + 2) = 18",
  "or, x + 2 = 6",
  "∴ x = 4"
]

To achieve this transformation and make the text style-able, you can use a function like the one demonstrated below using plain Javascript and CSS grid:

[...document.querySelectorAll('math-element')].forEach(el => {
  // reset, in case you run this more than once...
  el.innerHTML = '';
  
  // we need JSON.parse as dataset.text is a string
  JSON.parse(el.dataset.text).forEach(text => {
    const div = document.createElement('div');
    // split each row by `=` sign, if it has any
    text.split('=').forEach(t => {
      const span = document.createElement('span');
      if (text.split('=').length < 2) {
        // adds `.single` to items which are single on their row
        span.classList.add('single');
      }
      span.innerHTML = t;
      el.appendChild(span);
    });
    // add a `<hr>` after each element
    const separator = document.createElement('hr')
    el.appendChild(separator);
  })
})
math-element {
  display: grid;
  grid-template-columns: auto 1fr;
}
math-element hr {
  display: none;
}
math-element span {
  grid-column-start: 1;
  text-align: right;
  padding: 0 2px 0 1rem;
}
math-element span:not(.single) {
  font-style: italic;
}
math-element span.single {
  text-align: left;
  padding-top: .5rem;
  font-style: normal;
}
math-element span + span {
  grid-column-start: 2;
  text-align: left;
  padding: 0 2px;
}
math-element span + span:before {
  content: '=';
}
<math-element data-text='["Solution,","Given, f(x) = 3x","g(x) = x + 2","fog(x) = 18","To find: x = ?,","Now,","fog(x) = 18","or, f(x + 2) = 18","or, 3(x + 2) = 18","or, x + 2 = 6","∴ x = 4"]'></math-element>

If coding the values directly as a string doesn't suit your needs, you can dynamically create the elements and iterate over the data without storing them as strings.

In case the CSS grid syntax seems complex, an alternative could be utilizing <table>, <tr>, and <td> tags for simpler selectors, although it's not generally recommended.

The usage of <hr> tags marks the end of each "row" due to CSS grid requirements. Alternatively, nesting the row contents within a single element like a <div> could be considered along with adjusting column widths.

You have the liberty to customize the CSS properties such as removing font-style, tweaking padding values, etc.

Lastly, if a "row" contains multiple = signs, additional CSS rules must be added to align these elements properly.

By modifying the number of columns in the rule and updating accordingly, you can personalize the layout as needed:

math-element {
  grid-template-columns: auto auto 1fr;
}

Feel free to adapt and enhance the CSS styles to match your design preferences.

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

Transform bootstrap CHECKBOX into the appearance of a bootstrap BADGE

Is there a way to create Bootstrap checkboxes that resemble bootstrap badges, while still functioning as checkboxes? I attempted to style the checkbox label as a badge, but it was unsuccessful. <head> <link rel='stylesheet' href= ...

Vue.js is displaying one less item

Recently I started working with Vuejs and encountered an unexpected issue in my application. The purpose of my app is to search for channels using the YouTube API and then display those channels in a list. However, when I try to render the list of subscri ...

After sending a post request, the form in HTML with Node.js is returning an undefined response

Upon creating an HTML <form></form> object to capture a username, email, and password, I encountered an issue where the value returned is always undefined when accessing req.body.name or any other field. It seems like the form is not functionin ...

Text remains constant until the mouse hovers over the image

Check out this Example Here is the HTML code: <ul class="img-list"> <li> <img src="http://s11.postimg.org/ynw9rnexf/Cetina_river.jpg" width="360px" height="280px" /> <span class="text-content"><span> Text To Dis ...

Twitter Bootstrap 3.3.5 navigation menu with a drop-down feature aligned to the right

I've been attempting to position the drop-down menu all the way to the right, following the search input field and submit button. I've tried using pull-right and navbar-right classes without success. Is there a method to accomplish this? <na ...

Open the window by using the `Window.open` method, then find and

Is there a way to open a document, not in a new window but on the same page using JavaScript.Window.Open()? Could it be possible to display the document within a specific div element that is obtained with getElementById? For example: document.getElementB ...

The method item.appendChild does not exist as a function

Despite being a common error, I've researched extensively and still can't figure out why it's happening. It seems like it should be an easy fix, but I'm struggling to find the solution on my own. var item = document.createElement("div" ...

Creating a Functional Dropdown Menu with CSS and HTML

I recently created a test site without a drop-down menu, and here is how it looks: Check out my site here: Now, I want to add a simple drop-down menu for "serwery". Can anyone help me figure out what's wrong with my code? Here's the HTML: < ...

Is there a way to ensure my chat view functions properly without relying on partial views?

I am currently working on integrating a private chat feature using SignalR in my project, and I have encountered an issue while creating a View with accessible model values to pass to the server. Specifically, I have a list of doctors, and when a user clic ...

The back button fails to refresh the page on a website utilizing Ajax technology

Recently, I implemented AJAX on a client's website to introduce some smooth animations for page transitions. When navigating from the homepage of firedogcreative.com to the edit page, and then to one of the work pages, we see a history like this: fir ...

Sending both the minimum and maximum slider values to PHP using POST can be achieved by formatting the data correctly

I am attempting to transmit the minimum and maximum values to PHP using submit through a dual slider bar The static page makes a request to the server-side PHP code <?php include('server/server.php') ?> Below is the JavaScript code ...

Updating the div#content dynamically with Jquery without the need to refresh the page

After spending countless hours on this forum, I have yet to find a solution that perfectly fits my needs, so I will pose my question. Here is the gist of what I am attempting to accomplish: When the page loads, the default page fades in and displays. Wh ...

Tips for aligning a flex element child vertically

I am trying to achieve a layout where there are 3 divs vertically centered in one flex container with flex-direction:column. However, I need the 3rd div to be positioned at the bottom of the container. To better illustrate what I am looking for, you can v ...

The ineffectiveness of setting width to 0

When I set @media screen and (max-width: 700px), aside {width: 0}, it doesn't disappear. Even after resizing the window, it remains as null space with width: 52px. What could be causing this issue and how can I resolve it? * { margin:0; padding ...

What could be the reason for the malfunctioning of the "subscribe" button?

Whenever the subscribe button is clicked, it should send an email to the "subscriptions" section of the database. Unfortunately, when I click the button, nothing seems to happen. My understanding of this is very limited and I can't seem to troubleshoo ...

`What is the method for accepting parameters on an aspx page?`

I am currently facing an issue with my webpage. I have an .html page that contains a link to a .aspx page. When sending parameters from the html page to the aspx page, I do it using the following code: <a href="javascript:openContent('page1.aspx? ...

Using fixed positioning in CSS caused a malfunction in the layout of Materialize columns

Looking to develop a unique sidenav menu design for Large and Medium screens using Materialize. After referring to the example provided in the Materialize documentation, I successfully implemented it and found it cool! However, my goal is to have the Sid ...

What is the best way to position the hamburger menu icon on the right side of the header?

I need some assistance with adjusting the position of my mobile hamburger menu icon. It's currently on the left side of the screen, but I want to move it to the right. I've tried making the changes myself using CSS and HTML, but I'm not an e ...

Implementing jQuery selector for CSS and properly handling special characters in the code

I've been attempting to use jQuery to change the background color of a radio button. I provided the CSS code below, but even after escaping special characters in jQuery as shown below, the solution still isn't working. #opt5 > [type="radio"]: ...

Is there a way to conceal/remove the text displayed on the submit button?

I am facing an issue with a submit button in my PHP code. The button displays an integer value that is crucial for running a script, but I want to hide the value as it interferes with the design using CSS. I have attempted various solutions like removing t ...