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 ?
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”.
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.
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 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, " ", to separate those words instead of a normal space.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With