[英]How to force HTML elements to be left aligned and not wrap
My question is very similar to Force float left with no line break no matter what but I'm not using tables 我的问题与强制浮动非常相似,无论如何都没有换行符,但是我没有使用表格
https://jsfiddle.net/hkzvo8h7/ https://jsfiddle.net/hkzvo8h7/
I have a the following layout (shortened) 我有以下布局(缩短)
<div>
<span>
item1
</span>
<span>
item1
</span>
<span>
item1
</span>
</div>
Nothing else. 没有其他的。 The CSS floats each
span
element and I'm hoping to have a long, single row of items floating horizontally. CSS会浮动每个
span
元素,我希望水平放置一排长长的单行项目。
The issue I have is, as soon as the element reaches the end of the page, it wraps. 我遇到的问题是,一旦元素到达页面末尾,它就会自动换行。 I would rather the page's width extends.
我希望页面的宽度可以扩展。
To simulate this, I've wrapped my span
in a div
: 为了模拟这一点,我将
span
包装在div
:
div {
max-width:350px;
white-space:nowrap;
overflow:scroll;
}
span{
float:left;
width:200px;
margin:10px;
background-color:#fe3e4a;
}
As per other posts, I've also included the white-space:nowrap
attribute. 与其他帖子一样,我还包括了
white-space:nowrap
属性。
What I'm seeing is: 我看到的是:
item1
item1
item1
item1
etc
or (depending on the width of the parent div or width of each span) 或(取决于父div的宽度或每个跨度的宽度)
item1 item1 item1
item1 etc
where as what I'm trying to achieve is (with no wrap but with scroll bar) 我要达到的目标是什么(没有换行但带有滚动条)
item1 item1 item1 item1 item1 item1 item1 item1 item1 item1 item1 item1 etc
What have I done wrong? 我做错了什么?
white-space: nowrap;
only works with inline
and inline-block
elements 仅适用于
inline
和inline-block
元素
if you remove the floats from the spans and display inline-block it works as intended! 如果您从跨度中删除浮子并显示内联块,则按预期工作!
span{ float: none; // don't really need this line, just remove the float! display: inline-block; }
An updated version of your fiddle https://jsfiddle.net/w3uk99gz/ 您的提琴的更新版本https://jsfiddle.net/w3uk99gz/
Update: Yes you can use div
s instead of span
s. 更新: 是的,您可以使用
div
而不是span
。
This is an example using different types of elements: https://jsfiddle.net/w3uk99gz/2/ all together 这是一个使用不同类型元素的示例: https : //jsfiddle.net/w3uk99gz/2/
You don't need to float the span
(floating containers wraps ) - remove it and set to and inline display
. 您不需要浮动
span
(浮动容器包装 )-删除它并设置为and inline display
。 See demo below where I have set span
to inline-block
: 请参阅下面的演示,其中我将
span
设置为inline-block
:
div { max-width: 350px; white-space: nowrap; overflow-y: scroll; } span { /*float: left;*/ display: inline-block; /* ADDED */ width: 200px; margin: 10px; background-color: #fe3e4a; }
<div> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> </div>
I would use a flex solution - we shouldn't need to float anything in this day and age of css: 我将使用flex解决方案-在CSS的这一时代,我们不需要浮动任何东西:
div { display: flex; flex-direction: row; flex-wrap: nowrap; }
<div> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> <span>item1</span> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.