Is there a way to design a CSS layout that automatically adjusts text wrapping based on the size of the page and columns?

I am attempting to design a CSS page that mimics the appearance of an aged newspaper.

The focus is on ensuring that the page is responsive, with a single column of text on smaller devices. However, the main issue I need help with lies elsewhere.

For larger screens, the layout should consist of either 2 or 3 columns, depending on the screen width.

While I have been successful in achieving this using CSS columns, I would like to add an additional layer of complexity by having the CSS columns wrap per page (set at 100vh).

My requirements are as follows:

  • The text content should be comprised of multiple variable paragraph tags
  • The text content must be split into three columns per page
  • Each page should be relative to the device screen (100vh)
  • The text must read left-to-right but flow top to bottom per column, similar to a newspaper's layout

Would it be possible to accomplish this layout using CSS Grid or any other method?

Below is an example of the HTML code:

<main class="page-content">
<h1 id="my-article-title">My article title</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
...
</main>

Thank you!

Update

Just to clarify further: Essentially, I am aiming for a design that replicates the appearance of a PDF document but in HTML format. I intend to create distinct pages of content, each determined by the viewport size. This would allow the user to read from top to bottom, left to right, across each page.

Answer №1

If you're looking for stability in your design, consider using column-fill: balance;. It works well with blocks of any height, even without 100% height. On the other hand, column-fill: auto requires a strict height to function properly. It may look good initially, but it can cause issues if the children's height exceeds that of the parent.

Check out this fullscreen demo to see the difference:

* {
  box-sizing: border-box;
}

html {
  height: 100%;
}

body {
  width: 100%;
  min-height: 100vh;
  min-height: calc(var(--vh, 1vh) * 100);
  margin: 0;
  padding: 0;
  position: relative;
}

.page-content {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  column-count: 3;
  column-fill: auto;
  padding: 10px;
}
<main class="page-content">
  <h1 id="my-article-title">My article title</h1>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
  ... (content continues)
</main>

For a more flexible approach, consider using flex. This demo showcases how it works:

* {
  box-sizing: border-box;
}

html {
  height: 100%;
}

body {
  width: 100%;
  min-height: 100vh;
  min-height: calc(var(--vh, 1vh) * 100);
  margin: 0;
  padding: 0;
  position: relative;
}

.page-content {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  padding: 0;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-content: flex-start;
  align-items: flex-start;
}

.page-content>* {
  flex: 0 1 auto;
  width: 33.33%;
  padding: 10px;
  margin: 0;
}
<main class="page-content">
  <h1 id="my-article-title">My article title</h1>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
  ... (content continues)
</main>

For the best results, you might want to consider a plain JavaScript solution. It adapts well to different viewport sizes. Don't forget to cover all direct children within tags for optimal performance.

setDimensions(); 
window.addEventListener('resize', () => {
  setDimensions(); 
});

function setDimensions() {
  let columns = 1;
  // Adjust columns based on window width
  // Check the full code snippet for detailed implementation
}
Full code snippet for plain text handling

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

When a link is clicked, it quickly blinks or flashes on the screen

https://i.sstatic.net/cCFmX.gif <div class="collapse navbar-toggleable-xs" id="exCollapsingNavbar2"> <%= link_to 'FARDIN KHANJANI', root_path, class: 'navbar-brand logo' %> </div> Upon viewing the gif I include ...

What is the most effective method to activate OnClientCommand from the server's perspective?

I've found myself in a bit of a dilemma - it seems that solving this issue will require some restructuring of my code. The situation involves a server-side timer running that needs to emulate clicking a tab on a RadTabStrip. On the client side, I hav ...

Build.js.erb requesting a partial script task

Struggling to make my create action function accurately in my rails app. The following two lines work as intended: $('#pit_form').remove(); //remove form $('#new_link').show(); //show new link again They successfully remove the form ...

Verify changes in the Cross Domain RSS feed by utilizing Ajax technology

I have a website where I am trying to automatically reload an RSS news feed from every 60 seconds if it has been updated. I attempted to use Ajax for this purpose, but I seem to be facing some issues: <script type="text/javascript" src="../js/jquery.a ...

Unveiling the secrets to isolating elements from a list based on distinct characteristics

Currently, I am attempting to scrape job skills from the Wuzzuf website using the following code snippet: result = requests.get("https://wuzzuf.net/search/jobs/?q=data+analysis&a=navbl") src = result.content soup = BeautifulSoup(src, "lx ...

Implementing onClick functionality in RecyclerView post JSON data extraction

I recently implemented a RecyclerView in a fragment and successfully parsed JSON data from a website to display it in the RecyclerView following a helpful tutorial found at: Now, my next challenge is adding an onClick listener to the items in the Recycler ...

Adjusting responsive behavior with Bootstrap 4 fluid container min-width feature

I am currently developing a Bootstrap 4 single-page application with a fluid-container that is specifically designed for desktop use only. My goal is to set a minimum body width of 1200px and enable horizontal scrolling if the browser width is reduced bel ...

"Turn off sweep gesture on Surface tablet when using Chrome after pinching to zoom

In the process of developing my app, I encountered the need to disable zooming and scrolling, which I addressed using the viewport meta tag: <meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-sc ...

Issue with calling function from props in React is not being resolved

There seems to be an issue with the function not being called when passed into a functional component. While the onSubmit function is triggered, the login(email, password) function inside the Login component is never executed. Despite placing console.log s ...

What is the best way to locate a table of a specific class using jQuery selectors?

Is there a way to specifically target a table with the class "d" using jQuery selectors? I'm having trouble making it work... var dTableTags = $(".d table"); For instance, an example table would look like this... <table id="thetable" class="d"&g ...

Transforming a namespaced function into an asynchronous operation by utilizing setTimeout

Looking for help with making a function that uses namespaces asynchronous. The function is currently being called on the click of a button. var ns = { somemfunc: function (data) { alert("hello"); } } Edit: ...

Looking to create a pop-up using javascript, css, or jQuery?

When visiting digg.com and clicking the login button, a sleek in-screen popup appears to input user data. I'm curious about the best way to achieve this on my own site. It is built with RoR and includes some Javascript elements. Searching for "javasc ...

Bug causing connection issues in Node.js when dealing with IP redirection

I recently encountered an issue with NodeJS while using a kafka node on a node-red instance installed on my RPI3. Let me paint you the scenario: I have a cluster set up with a functioning Kafka instance. The machine hosting the Kafka broker has a private ...

Don't use onchange() in place of keyup()

Issue: I am facing a problem where the keyup() function is calling ajax multiple times with each key press, and I have tried using onChange() but it did not work as expected. Here is the code to check if an email already exists in the database: $.noConf ...

Creating a responsive layout for displaying products using CSS and HTML

I have a project that requires me to showcase products in a specific manner while being responsive. Here's what I want to accomplish: Based on your opinion and experience, what method do you recommend using to achieve this? Should I use divs, tables, ...

The consistent index is shared among all dynamically generated elements

I'm currently working on a project where I am generating dropdowns within a table dynamically. I am attempting to determine the index of the dropdown that triggered the event in the following way: $(".template").on('change', '.dataType ...

Monitor the status of an AJAX request for fetching JSON data in a Rails application

My JS code fetches JSON data from a controller method in a Rails app: $.ajax({ xhr: function() { var xhr = $.ajaxSettings.xhr(); xhr.onprogress = function(e) { if (e.lengthComputable) { console.log(e.loaded ...

Unable to execute PHP alongside a JavaScript event listener

Using PHP, I am creating a canvas for writing and the text output will appear in a textarea (handled by other functions). There are additional input tags like a title to gather user input. The values from these input tags (title and textarea) will be submi ...

Tips for showing the React-Select array label in a text field

I'm a beginner in the world of React and I've encountered a puzzling issue. I'm attempting to showcase the selected value from a dropdown component in the adjacent text field in my React project. Can anyone guide me on how to achieve this? ...

The flat function for JavaScript Arrays is not defined in React Native

I am currently developing an application using react-native and it's common knowledge that we can utilize JavaScript code in this particular project as well as any other react projects. However, whenever I attempt to use the following code snippet, t ...