Proper formatting for setting text indentation in CSS

Issue:

I am struggling with indenting text properly on responsive devices. While the desktop version looks fine, the text goes out of control on mobile views. Here is how it currently appears:

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

Below is an example after applying a text indent using the following CSS for an iPhone 5:

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

Code:

  feature-bar {
    background-color: #f8f8f8;
    height: 200px;
    margin-top: 0px;
    text-decoration: solid;
    color: red;
    text-indent: 100px;
    padding-top: 20px;
}

Question:

How can I properly format the text indent to display correctly on mobile devices?

UPDATE

Based on the suggestions provided, I experimented with using <li> or vw. Specifically, I want the time range (13.00 - 13.20) to remain on one line and have the text start on the next line for mobile views. Would implementing a media query help resolve this issue?

SECOND UPDATE

Desired appearance on mobile devices: https://i.sstatic.net/PZywF.png

Answer №1

To add extra space around a list in HTML, one common approach is to use left padding. Here is an example of CSS code that achieves this:

ul {
  padding-left: 100px;
  padding-right: 40px;
  outline: 1px solid red; /* for demonstration purposes */
}
li {
  padding-bottom: 20px;
  list-style: none;
}
<p>Heading</p>
<ul>
  <li>First item, demonstrating how wrapping occurs when text goes beyond one line</li>
  <li>Second item</li>
</ul>

If you wish to increase the vertical space around the list, there are two methods. One way is to simply add more padding to the bottom of the ul:

ul {
    padding-bottom: 10px; /* in addition to the existing 20px from each li element */
}

Alternatively, you can set specific padding just for the last li element:

ul {
    padding-bottom: 30px;
}
li:last-child {
    padding-bottom: 0;
}

The latter option may be more foolproof and easier to maintain in complex scenarios.

Regardless of your choice, it's advisable to apply specific classes to differentiate styling for individual lists rather than affecting all lists site-wide with generic selectors like ul and li.

Lastly, using bottom padding instead of top padding is recommended for better organization and debugging in CSS layouts.


Bonus Tip

In response to your mobile display requirement, consider wrapping the time value in a block-level element on small screens. An example implementation could look like:

<li>
    <span>Time on new line for mobile</span> Additional content
</li>
@media (max-width: yyypx) {
    span {
        display: block;
    }
}

Adjust the breakpoint value in the media query (yyypx) as needed. Additionally, for semantic markup, utilize the time HTML element:

<li><time datetime="13:00/13:30">13:00-13:30<time> Additional content</li>

Further enhance this styling by targeting the time element in your CSS rules according to best practices.

Answer №2

To avoid using pixels, consider using viewport units, specifically utilize vw which stands for viewport width and will decrease as the screen width decreases.

For more information on CSS units, visit this link: CSS Units

Check out the snippet below:

Alternatively, you can see a demonstration here > jsfiddle (you can resize the window for better viewing)

.feature-bar {
    background-color: #f8f8f8;
    height: 200px;
    margin-top: 0px;
    text-decoration: solid;
    color: red;
    text-indent: 10vw;
    padding-top: 20px;
}
<div class="feature-bar">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet.
</div>
<div class="feature-bar">
Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia facilisis, magno lacus vehicula varius.
</div>

After editing your question, consider using lists:

Note that the ul tag has default padding, so adjust it to padding-left:5vw to make it smaller when resizing the window.

See the snippet below or access the fiddle here: jsfiddle with list

ul {padding-left:5vw}
br { display:none;} /* if you want all content of `li` to be on the same row, time, and text */

@media only screen and (max-width: 767px) { /* For mobile devices */
  
  br { display:block; } /* Time on one row, text on the next rows */
}
<h1>
Seminar Program
</h1>

<ul>
  <li>13.00 - 13.30 <br />
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras lorem arcu, consequat sed ex eu, bibendum fringilla est.
</li>
  <li >14.00 - 14.30<br />
Suspendisse fermentum nisl eget velit gravida tempus. Donec viverra velit vel aliquam ultrices. Interdum et malesuada fames ac ante ipsum primis in faucibus.
</li>
</ul>

Answer №3

The reason behind this issue is that the text-indent property only affects a new line of text. In the case of your mobile phone's display, the rule is interrupted which causes the text to go back to the beginning of the division.

To fix this issue, you can replace text-indent: 100px with padding-left: 100px

If you have other content that should not be affected by this alignment adjustment, consider wrapping that content in a span or div element and applying the styling specifically to that element.

Answer №4

This is tabular information that can be easily displayed using a table. Tables are semantic and require minimal maintenance or adjustments with media queries.

table {
  width: 80%;
  margin: auto;
  color: red;
}
table tr td {
  vertical-align: top;
}
table tr td:first-child {
  white-space: nowrap;
}
<table>
  <tr>
    <td>11:30 - 12:30</td>
    <td>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptatum, nihil nam veniam perferendis facilis error.</td>
  </tr>
  <tr>
    <td>12:30 - 13:30</td>
    <td>Lorem ipsum dolor sit amet.</td>
  </tr>
  <tr>
    <td>13:30 - 14:30</td>
    <td>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iste, nihil!</td>
  </tr>
</table>

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

Placing a pair of labels onto a bootstrap form while ensuring column alignment

I'm trying to format a form according to my UI/UX specifications, where there is a parent label and two sub-labels. Can this be achieved using Bootstrap? https://i.stack.imgur.com/J8wJZ.png However, I'm struggling to align the columns and rows ...

Tips for ensuring only one submenu is open at a time

I have successfully created a responsive menu that is working well. However, I am facing an issue. I want only one menu to be open at a time. When a submenu is opened, the other menus should be hidden. How can I achieve this? Below is my Script code $( ...

The Transform feature doesn't seem to be functioning as expected within the Bootstrap

I was experimenting with creating a simple transform transition animation using css and js, so I wrote the following code: (test page) <!DOCTYPE html> <html> <head> <style type="text/css"> *{ ...

Tips for deleting the drop-down arrow from Mozilla Firefox

Is there a way to eliminate the dropdown arrow that FireFox usually displays? I have included an image below showing what I am referring to: This is the CSS code I'm using: @-moz-document url-prefix(){ .class select { width: 110%; } } .class > ...

Basic JavaScript framework centered around the Document Object Model (DOM) with a module structure similar to jQuery. Currently facing challenges with

In order to create a simple framework with jQuery style, I have written the following code: (function(window) { window.smp=function smpSelector(selector) { return new smpObj(selector); } function smpObj(selector) { this.length = 0; if (!s ...

Is there a way to implement a flex display in my react slider component?

Welcome to my custom slider design! <Slider {...customSettings}> <div style={{ 'display': 'flex !important' }}> <CustomToolComponent key={uniqueName} color={customColor} /> <UniqueTool key={uniqueName} co ...

The CSS selector for attribute ending with a specific value is not functioning as anticipated

I am facing an issue with selecting elements based on classes ending with either x-control-ui or y-control-ui: <div class="x-control-ui">x-control: <output id="x-control-output"></output></div> <input type=&q ...

Stop the bubbling effect of :hover

How can the hover effect be prevented for the parent element when hovering over its children? Please take a look at the following code snippet: const parent = document.getElementById('parent') parent.onmouseover = function testAlert(e) { /* ...

Steps for turning off image maps on a slider for mobile users

Hello, I'm currently working on a website that has an image slider with image maps embedded in each image. Everything works perfectly on desktop, but when it comes to mobile versions, things start to get messy. I was wondering if there is any way to d ...

Is there a way to eliminate the additional gap found at the bottom of a div element?

I've been encountering some difficulty when trying to insert an image into a div without any padding. The issue I'm facing is that the sizing doesn't seem to be correct; there's unnecessary extra space at the bottom. Despite my efforts ...

JavaScript function that adjusts the top position when toggling

When the user clicks on a div, it expands or closes. I also want to adjust the top position of another fixed div based on the state of the original div. For instance: If the original div is expanded, I want the second div to have top:10px If the original ...

Tips for changing the appearance of an entire table by overriding the table tr:nth-child styling

In the CSS code below, I currently alternate background colors for table lines by default and can specify individual rows to not alternate. Is there a way to implement this at the table level instead of per row? Essentially, I want to define something at ...

WebDriver does not support compound class names and will throw an error

I have a function that can calculate the total number of elements within divs and return this count. public int getNumberOfOpenBets() { openBetsSlip = driver.findElement(By.id("form_open_bets")); openBets = openBetsSlip.findElements(By.classNa ...

Enabling the `dbc.Select()` function to require a dropdown selection in DASH does not automatically highlight an empty dropdown

While working on my plotly-Dash form, I utilized bootstraps dbc.Select() to generate a dropdown. I hoped to have it outlined in red, similar to my dbc.Input() boxes, when no option is selected. However, the red border is not showing up. I set required = ...

Header and footer that stick in place while content scrolls automatically

To create a layout where the header and footer remain sticky while the content has a minimum height and shows a scrollbar when the viewport is too small, I found success with this codepen: http://codepen.io/DlafCreative/pen/wzdOEN inspired by another sourc ...

Refresh is required for resizing

My goal is to have 3 div elements scroll over each other, with their positions fixed when they reach the top. Everything works fine until the window is resized, requiring a page refresh. Is there a way to achieve this without refreshing the page? Using th ...

Using Jquery and CSS to display or conceal table elements depending on viewport width

I am seeking advice for my issue. Here is the code snippet on JSFiddle. I have a list of models and corresponding values that vary across different categories. I have created a simple table to display this data. However, I need to alter the table structur ...

Tips for disabling scrolling on a <div> using another <div> as a lock

I am facing an issue with a page where a div is appended to the body of an HTML. The problem is that there are two scrolls appearing - one on the new overlaying div and another behind it. Here is an approximate structure of the page, how can I ensure that ...

Issue with CSS styles not linking correctly to ejs templates

I'm having trouble linking my CSS stylesheet in my project. I have the CSS file stored in a public folder with a css subfolder within it. Despite trying various methods, I can't seem to get the stylesheet to connect properly. Below is a snippet f ...

My CSS code is not successfully positioning the items to the right, despite my best efforts

I'm currently working on a website project and facing an issue with aligning the text 'Axxon' to the left of the div-header-gradient class, and the a href positioned to the right of the header. However, my code doesn't seem to be workin ...