简体   繁体   English

如何强制HTML元素保持对齐且不自动换行

[英]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 仅适用于inlineinline-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.

相关问题 如何使用下拉元素进行左对齐导航 - how to make a left aligned navigation with dropdown elements HTML 当对齐不起作用时,如何从左到右更改对齐? - HTML How to change aligned from left to the right, when aligned not working? 如何强制两个内联元素包装在一起 - How to force two inline elements wrap together 如何强制HTML表单元格中的文本换行? - How to force text in an HTML table cell to wrap? 您如何强制html中的段落不换行? - How do you force a paragraph in html to not wrap? 如何使第一个元素与右侧对齐,其他元素在左侧对齐? - How to make the first element aligned to the right and the other elements in the left of that first? 如何居中<ul>尽管<li>元素文本左对齐?</li></ul> - How to center an <ul> while <li> elements are text-aligned to the left? 连续两个元素,一个左对齐,一个居中,如何? - Two elements in a row, one left aligned and one centered, how? 如何强制 html 表的“td”元素中的单独元素显示内联(从左到右)? - How can I force separate elements within a “td” element of an html table to display inline (left to right)? 如何在表格单元格(td)中定位元素,使得一个元素左对齐而另一个元素对齐? - How can I position elements within a table cell (td) such that one element is left aligned and the other right aligned?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM