Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep elements on same line with overflow

I am trying to make a page with some divs aligning horizontally and I want the width to resize based on content so I will get a horizontal scroll if the content is bigger than screen size. I have this:

    <div>
    <div style="width: 300px; height: 300px; background-color: black; margin-left: 10px; float: left;"></div>
    <div style="width: 300px; height: 300px; background-color: black; margin-left: 10px; float: left;"></div>
    <div style="width: 300px; height: 300px; background-color: black; margin-left: 10px; float: left;"></div>
    <div style="width: 300px; height: 300px; background-color: black; margin-left: 10px; float: left;"></div>
    <div style="width: 300px; height: 300px; background-color: black; margin-left: 10px; float: left;"></div>
</div>

the child divs are aligning side by side but when the screen size is reached it breaks into a new line. Any ideas ?

like image 313
ardb Avatar asked Mar 16 '14 13:03

ardb


People also ask

How do I force HTML elements to stay on the same line?

You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.

How do I make my text stay on one line?

If you want to limit the text length to one line, you can clip the line, display an ellipsis or a custom string. All these can be done with the CSS text-overflow property, which determines how the overflowed content must be signalled to the user.

How do you align items on the same line in CSS?

To align things in the inline direction, use the properties which begin with justify- . Use justify-content to distribute space between grid tracks, and justify-items or justify-self to align items inside their grid area in the inline direction.

How do I keep text on the same line in HTML?

How to Prevent Word Wrap on a Web Page: HTML Method. If you only have the one-off instance of two or more words that you want to force the browser to keep on a single line, the easiest way is to use the non-breaking space character, " &nbsp; ", to separate those words instead of a normal space.


1 Answers

I would use white-space: nowrap along with display: inline-block. Live demo (click).

By the way, try to use CSS instead of inline styles in your HTML. Inline styles should be avoided unless absolutely necessary.

<div class="row">
  <div></div>
  <div></div>
  <div></div>
  <div></div>

CSS:

.row {
  white-space: nowrap;
}

.row > div {
  width: 300px;
  display: inline-block;
}
like image 82
m59 Avatar answered Oct 09 '22 05:10

m59