Using HTML and CSS to evenly align text in individual sections

Is it possible to recreate the exact look and feel of a justified page from a book or article online using HTML/CSS? This would include replicating the text justification with precise line breaks and setting the outer wrapper width to match the min/max-width of the text itself.

An example HTML structure could be:


...
<div class="page-wrapper">
<span class="line">The Republic of Plato is the longest of his works with the exception of the Laws,</span>
<span class="line">and is certainly the greatest of them. There are nearer approaches to modern</span>
<span class="line">metaphysics in the Philebus and in the Sophist; the Politicus or Statesman is</span>
<span class="line">more ideal; the form and institutions of the State are more clearly drawn out</span>
</div>
...

Note 1:

While this CSS-trick may work for specific scenarios, it has limitations as it relies on the rendering engine. The fixed width (e.g., 500px) can cause issues such as premature breaklines or excessive word-spacing. It may not be scalable across different devices.

.page-wrapper {
    text-align: justify;
    width: 500px;
}

.line:after {
    content: "";
    display: inline-block;
    width: 100%;
}

Note 2: Ideally, I'm looking for a pure HTML/CSS solution. If that's not achievable, we might need to consider JavaScript...

Answer №1

You have the option to utilize a <p> paragraph along with <br> break tags whenever needed.

Styling with CSS should not pose a challenge either

p {
   text-align: justify;
}

However, I am unsure about

  1. The specific details you are seeking

  2. Why an exact replication of the original is necessary, considering the dynamic nature of the web.

C) A case-by-case evaluation may not be effective either, as it might be perfect on desktop but lacking on mobile or vice versa.

If you are experiencing issues with varying screen sizes, @media queries could offer assistance.

@media only screen and (max-width: 600px) {
    // ADD STYLES IF SCREEN WIDTH IS LESS THAN 600px
}

You can also consider using two different divs, hiding one when needed and displaying the other.

div.mobile {
    display: block;
}
div.desktop {
    display: none;
}
@media only screen and (max-width: 600px) {
    div.desktop {
        display: block;
    }
    div.mobile{
        display: none;
    }
}

Answer №2

A quick and efficient solution:

To determine the maximum character length, use a monospaced font and set the wrapper width using the ch unit with your max character count, for example 82ch. Then, create a container with a maximum width of 100vw and include overflow:auto to make the content scrollable.

.page-container {
  max-width:100vw;
  overflow:auto;
}
.page-wrapper {
    text-align: justify;
    width: 82ch;
    background-color:#EEE;
    font-family:courier;
    font-size:15px;    
}

.line:after {
    content: "";
    display: inline-block;
    width: 100%;
}
<div class="page-container">
<p class="page-wrapper">
<span class="line">The Republic of Plato is the longest of his works with the exception of the Laws,</span>
<span class="line">and is certainly the greatest of them. There are nearer approaches to modern</span>
<span class="line">metaphysics in the Philebus and in the Sophist; the Politicus or Statesman is</span>
<span class="line">more ideal; the form and institutions of the State are more clearly drawn out</span>
</p>
</div>

A more stylish approach:

Build upon the previous method by adding media breakpoints to adjust the font size. Determine suitable font sizes and breakpoints accordingly.

.page-container {
  max-width: 100vw;
  overflow: auto;
}

.page-wrapper {
  text-align: justify;
  width: 82ch;
  background-color: #EEE;
  font-family: courier;
  font-size: 15px;
}

.line:after {
  content: "";
  display: inline-block;
  width: 100%;
}

@media screen and (max-width:768px) {
  .page-wrapper {
    background-color: #EFE;
    font-size: 12px;
  }

}

@media screen and (max-width:575px) {
  .page-wrapper {
    background-color: #FEE;
    font-size: 11px;
  }
}
<div class="page-container">
  <p class="page-wrapper">
    <span class="line">The Republic of Plato is the longest of his works with the exception of the Laws,</span>
    <span class="line">and is certainly the greatest of them. There are nearer approaches to modern</span>
    <span class="line">metaphysics in the Philebus and in the Sophist; the Politicus or Statesman is</span>
    <span class="line">more ideal; the form and institutions of the State are more clearly drawn out</span>
  </p>
</div>

Enhancing with javascript:

Utilize javascript to calculate the maximum number of characters per line and dynamically adjust the width using the ch unit based on the calculation.

If you're wondering about calculating font size based on width, it's worth noting that font size is typically determined by character height, so basing it on width can present challenges.

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

Is the form being submitted with the href attribute?

Does this code ensure security? <form id="form-id"> <input type='text' name='name'> <input type='submit' value='Go' name='Go'/></form> <a href="#" onclick="docume ...

When attempting to update a database, the Jquery ajax response is not being reflected

Currently, I am utilizing a dialog box for updating my database and removing services. However, I have encountered an issue where it only functions properly on the initial "remove" click. When I open the dialog box, I am presented with a list of 5 servic ...

Ensure that all content in the table rows remains visible, even in a table with unusually lengthy cells

My HTML table is structured like this: +-------------+-------------+----------+ | Single line | Single line | Very, | | | | very | | | | long | | | | text, | | ...

Automatically close an element when you click on a different element

Hello everyone! I need some help again. I'm working on a script that displays an overlay when a menu item is clicked. Within this menu, there is a modal contact form with a close icon or anchor that should appear when clicked. My goal is to have the o ...

The animation isn't loading

I discovered a handy script that displays a waiting message when my form is submitted. I implemented jquery-ui to showcase this message in a dialog box, and it was successful. However, upon customizing the text and adding an animated gif background using C ...

Adding descriptive text before a website link is a helpful way to provide context for the reader. For example

My goal is not just to have JavaScript change the URL after my page has loaded. I want to be able to enter something like 'blog.mywebsite.com' into the omnibar and have it locate my website similar to how Steam does with 'store.steampowered. ...

Setting the height of a div to 100% is not effective

I've designed a two-column layout. You can view the code on jsfiddle.net. My problem is that I want the center line with a width of 10px to have a height of 100%. I have a container div with the ID #obal (set to height: auto). When I set the height of ...

Inserting lines into the text, creating a visually appealing layout

Can a notepad design be created using CSS only? Is it feasible to include lines between text lines without using underscores? I want it to look like this example but with minimal spacing between lines and no manual insertion of span tags since the text wi ...

What is the best way to wrap text and move the lines up without affecting the bottom margin?

When the text length increases in the first image, it shifts upwards similar to what you see in this image. How can I achieve this effect? The use of "word-wrap" causes the line to break and move to the next line, which is not what I want. In the code sn ...

A more efficient approach to creating a personalized design

After realizing the original question was unclear and wouldn't solve my problem, I have decided to edit it and create a new one. For my project, I require a customized layout where users can move, resize, add, or remove each box according to their pr ...

Moving a popup div along with a marker in Leaflet can be achieved by using the set

After creating a map using leaflet and adding markers to different locations, I implemented code that displays a popup div with a message when a marker is clicked. Here is the code snippet: markers.on("click", function(d) { div.html("This is Some info ...

Fixing the error message "page attempting to load scripts from an unauthenticated source"

Can you help me troubleshoot an issue on my PHP page with ad scripts? I recently switched my site from HTTP to HTTPS and now the scripts are not appearing. Here is the error I found in the dev console: Failed to load resource: the server responded with ...

Get a polished look using HTML and CSS exclusively

Allow me to illustrate what I am intending to accomplish with an example. div{ width:100px;height:100px; border:3px solid #900; border-radius:0px 0px 140px 0px; } <div></div> I am seeking a method to ...

Submitting numerous data fields using a post AJAX call

I have roughly 143 form fields including text, textarea, and select options that I need to send via an AJAX post request. Is there a way to achieve this efficiently without manually adding each field to the query? Here is how I've set things up: Usi ...

Transitioning between pages causes a delay in loading the background

Today as I was working on my CSS, I encountered some issues. I utilized Bootstrap and Darkstrap for designing. In Darkstrap, the style for the body is body { color: #c6c6c6; background-color: #2f2f2f; } Meanwhile, in my own CSS: body { ...

Is there a way to disregard inherited styles without relying on the use of "!important"?

Is there a way to create a new class in CSS that ignores all inherited styles without needing to use !important for each one individually? This is my HTML code: <div class="text"> <h2>Title</h2> <span>Date</span> &l ...

What is the best way to include default text in my HTML input text field?

Is it possible to include uneditable default text within an HTML input field? https://i.stack.imgur.com/eklro.png Below is the current snippet of my HTML code: <input type="text" class="form-control input-sm" name="guardian_officeno" placeholder="Off ...

Employing a lexicon of hexadecimal color code values to harmonize CSS styles

I have a unique requirement where I need to utilize a dictionary of HTML color codes and then apply those colors as styles. It's an interesting challenge! Here is an example of how my color dictionary looks like: const colorCodes = { red: ...

Ways to reload an independent page in order to access PHP session variables:

Starting off, I want to mention that my approach may not be the most efficient. As a novice in JavaScript and PHP, I am aware that there are better and simpler ways to achieve what I'm attempting. The issue seems to be related to session variables an ...

Bootstrap 5 does not support tab navigation functionality

I created a new HTML page, and I encountered an issue while trying to enable tabs using JavaScript. <html> <head> <title>Excel Viewer</title> <link hr ...