Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable wrapping in HTML elements?

I'm developing an Ionic app, and having trouble with my header component. Its elements are wrapping with small screen sizes, and I would like them not to.

Here's the goal:

Good-form heading

Here's what's happening now:

The problematic situation

I know I could set a fixed width to the header, but I would like not to. I also wouldn't like to use JavaScript to calculate the width.

Here's the HTML/Angular/Ionic code for the title component:

<h1 *ngIf="backButton; else titleBackButton">{{ title }}</h1> <!-- show if backButton != true -->
<ng-template #titleBackButton> <!-- show if backButton == true -->
  <button ion-button round class="button-back">
    <ion-icon name="arrow-back"></ion-icon>
  </button>
  <h1 class="floated-title">{{ title }}</h1> <!-- this has been floated to the right -->
</ng-template>

Here are my CSS styles:

.button-back {
  margin: 17px 0 0 10px;
}

.floated-title {
  float: right;
}
like image 959
mkkekkonen Avatar asked Sep 07 '17 19:09

mkkekkonen


People also ask

How do I turn off Wrap in HTML?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).

How do you make an element not wrap?

You need the white-space property. As we know, this property helps us to handle the spaces within the element. So, if you want not to wrap the contents of the elements mentioned above, you need to use the “nowrap” value of the white-space property.

How do I turn off text wrapping?

Enable or disable text wrapping for a text box, rich text box, or expression box. Right-click the control for which you want to enable or disable text wrapping, and then click Control Properties on the shortcut menu. Click the Display tab. Select or clear the Wrap text check box.

How do I keep my DIV from wrapping?

Alternative way to do this is to use the display:inline-block . Where row element has white-space:nowrap. inline-block makes the element inline but preserves the settings such as width, height, top and bottom margins, paddings.


1 Answers

Any time you want to force elements to line-up in a row, and never wrap, give the parent container display: flex. This automatically applies flex-wrap: nowrap and flex-direction: row.

The above suggestion applies to plain CSS. Some frameworks may set different defaults.

For instance, in React, flex-direction defaults to column.

Alternatively, you can apply white-space: nowrap to the container, which suppresses all line breaks inside the container.

like image 71
Michael Benjamin Avatar answered Nov 10 '22 23:11

Michael Benjamin