Each characterHas its own line

Is there a way to display all text in a div vertically, with one character per line and centered, without using a fixed width?

I have achieved this layout by giving the div a fixed width of width: 7%; and using word-break: break-all;

Would it be possible to achieve this using CSS without setting a fixed width?

.flex-container {
    display: flex;
    border: 1px solid #000000;
}

.flex-container > div {
    width: 400px;
    height: 300px;
    text-align: center;
    line-height: 75px;
    font-size: 30px;
}

.flex-container > div:nth-child(odd) {
    background-color : #62d962;
}

.flex-container > div:last-child {
    display: flex;
    justify-content: flex-end;
}

.flex-container > div:last-child div {
    width: 7%;
}

.flex-container div:last-child div:nth-child(odd){
    background-color: #65e9e3;
    word-break: break-all;
    line-height: 1em;
    padding: 1em 0;
}
<div class="flex-container">
  <div></div>
  <div></div>
  <div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
  </div>
</div>

Answer №1

To achieve the desired output, you can utilize the writing-mode and text-orientation CSS properties.

.flex-container {
    display: flex;
    border: 1px solid #000000;
}

.flex-container > div {
    width: 400px;
    height: 300px;
    text-align: center;
    line-height: 75px;
    font-size: 30px;
}

.flex-container > div:nth-child(odd) {
    background-color : #62d962;
}

.flex-container > div:last-child {
    display: flex;
    justify-content: flex-end;
}

.flex-container > div:last-child div {
    writing-mode: vertical-rl;
    text-orientation: upright;
}

.flex-container div:last-child div:nth-child(odd){
    background-color: #65e9e3;
    line-height: 1em;
    padding: 1em 0;
}
<div class="flex-container">
  <div></div>
  <div></div>
  <div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
    <div></div>
    <div>Box</div>
  </div>
</div>

Answer №2

css

#onechar {
    width: min-content;
    word-break: break-all;
}

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div id="onechar">box</div>
</body>
</html>

Thanks for reading :)

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

Seeking to duplicate script for a new table without making any changes to the original script

I am working with two tables that require the capability to dynamically add and delete rows using separate scripts. How can I modify the second script so that it only affects the second table and not the first one? Table: <table id="myTable" class=" t ...

Hover over the div to center an SVG inside it

Simply put, I am working on a design where a gradient bar moves above a specific element when hovered, creating a visual effect of a triangle. I am now looking for a way to center an SVG inside the element on hover, to create a similar triangular gradient ...

What could be causing my UI Bootstrap datepicker-popup to suddenly stop functioning?

After updating to UI Bootstrap 0.11.0, I encountered an issue where my datepickers were no longer appearing correctly. To demonstrate this problem, I have created a plunker which can be viewed here. Essentially, the code snippet causing the problem is as f ...

iPhone 5 Internet Browser - charcoal matte .png with transparent background

When it comes to web design, I often rely on 24-bit transparent .png files. Recently, I was reviewing a website on an iPhone 5 and noticed that all my .png files had a black matte around them. However, when I checked the same website on an iPhone 4, everyt ...

Attempting to incorporate a vibrant hover effect into a sleek carousel design

I am struggling to implement a hover effect with color on an image within a slick carousel on my Wordpress website. I have been exploring various options but haven't found a solution yet. ...

How can the front design of Material-UI's Card header be customized?

Currently, I am facing an issue with the Material-UI card header as the background color is affecting the readability of the default font. My aim is to use the typography prop h4 for the header, but I am struggling to achieve this. https://i.stack.imgur.c ...

Numerous column pictures that occupy the entire browser screen

I am looking to create a grid of clickable images that expand to cover the width of the browser window without any space on either side. Ideally, I would like each image to be 180x180px in size, but if it's easier they can resize based on the browser ...

Perform CSS transitions simultaneously

Hey there! I'm currently working on a button that includes a Font Awesome icon at the end of it. To display the icon, I'm using the :after pseudo-element on the button, which is working fine so far. The issue I'm facing is with the CSS tran ...

What methods can I use to prevent a number from becoming negative?

I am currently developing a game where players collect resources, spend them to create more resources, and engage in various activities. However, I'm facing an issue where the resource count goes into negative numbers when too much of a certain item i ...

Guiding you to choose a specific child class by identifying its parent class in CSS and jQuery

Hey there! I'm currently working on a website that allows users to post content. I want to ensure that the site is mobile-friendly, so I need to make some adjustments with media queries. Unfortunately, I've run into an issue where media query CS ...

The issue with Grid component in Nested Single file Component

I'm currently working on creating a grid component in Vue to set up a sortable and searchable chart using Single File Component. I've also integrated vue-router into the project. Below are my two .vue files. At the moment, I can only see the sear ...

What are your preferred HTMLPurifier configurations for sanitizing CSS styles?

I'm currently in the process of developing an application that allows users to input their own custom CSS for their profiles, similar to platforms like MySpace, Friendster, and Blogger. However, I'm struggling to find an effective method to prot ...

JavaScript Evaluation Test Outcome

(Apologies, I am still in the process of learning JavaScript) My goal is to create a checklist quiz that provides different outcomes based on the number of checkboxes selected by the user. There are a total of 7 checkboxes, and if the user selects 1 or few ...

The output from the Python and SQLite combination is accurate, however the HTML rendering is not displaying the data correctly

Retrieving data from a database using different methods can yield varied results. When the method is set to 'get', it returns all information, but when it's 'post', it only returns the searched item. Although the post method return ...

What is the reason behind shadow dom concealing HTML elements when viewed in inspect mode?

https://i.stack.imgur.com/UZM7f.png Monday.com has implemented Shadow Dom to protect its source code. How can I work around this limitation? ...

What is the best way to line up my columns when they are split between groups of three and two?

I am having trouble aligning my columns in the way I want. The image shows that some tables have 3 columns while others have 2, and I would like to align the blue with the blue and the red with the red as shown in the picture: https://i.stack.imgur.com/1bL ...

Surprise scrolling on a mobile device's screen

I am trying to figure out why there is a horizontal scroll bar on the page. I would like the content to fill the entire screen. https://i.sstatic.net/Y0ToM.png <section class="who" data-aos="fade-in"> <img class=" ...

CSS/React JS: I wish for the input fields to maintain focus even after entering values

I'm working with a confirmation code that is made up of 6 digits. I've noticed that when I click on an input field, it becomes focused and underlined in blue. However, as soon as I start typing in the next input field, the blue underline disappe ...

Is there a way to prevent my Header links from wrapping during window size testing?

https://i.stack.imgur.com/YTc3F.png The image above showcases my standard header layout, while the one below illustrates what occurs as I resize the window. https://i.stack.imgur.com/re44T.png Is there a specific CSS solution to prevent the Text navLink ...

The functionality of the "this" keyword appears to be malfunctioning

I've been grappling with the concept of the this keyword in JavaScript and decided to create this script to test it out: function click(){ this.innerHTML="changed"; } However, when I tried to use it in this HTML: <button id="clicker" onclick ...