有时候,我们可能会产生多个div标签横向排列而不换行的需求,具体有以下几种实现方法:

1. 同级div设置display:inline-block,父级div强制不换行
例如:

<html>
<head></head>
<body>
<div id="container">
<div class="lable">测试测试</div>
<div class="lable">测试测试</div>
<div class="lable">测试测试</div>
<div class="lable">测试测试</div>
<div class="lable">测试测试</div>
<div>
</body>
<style>
#container {
width:400px;
height: 200px;
background-color: red;
overflow: auto;
white-space: nowrap;
}
.lable {
width: 100px;
background-color: blue;
display: inline-block;
}
</style>
</html>

2. 通过position绝对定位实现
例如:

<html>
<head></head>
<body>
<div id="container">
<div id="lable1">测试测试</div>
<div id="lable2">测试测试</div>
<div id="lable3">测试测试</div>
<div id="lable4">测试测试</div>
<div id="lable5">测试测试</div>
<div>
</body>
<style>
#container {
width:400px;
height: 200px;
background-color: red;
overflow: auto;
position: relative;
}
#lable1 {
width: 100px;
margin-left: 0;
background-color: blue;
position: absolute;
}
#lable2 {
width: 100px;
margin-left: 100px;
background-color: blue;
position: absolute;
}
#lable3 {
width: 100px;
margin-left: 200px;
background-color: blue;
position: absolute;
}
#lable4 {
width: 100px;
margin-left: 300px;
background-color: blue;
position: absolute;
}
#lable5 {
width: 100px;
margin-left: 400px;
background-color: blue;
position: absolute;
}
</style>
</html>

  

3. 通过flex方式实现
具体请参考:Flex 布局教程:语法篇
例如:

<html>
<head></head>
<body>
<div id="container">
<div class="lable">测试测试</div>
<div class="lable">测试测试</div>
<div class="lable">测试测试</div>
<div class="lable">测试测试</div>
<div class="lable">测试测试</div>
<div>
</body>
<style>
#container {
width:400px;
height: 200px;
background-color: red;
display: flex;
display: -webkit-flex;
/* 从左端开始沿水平轴防止flex item */
flex-direction: row;
/* 强制 flex item不换行,沿着同一行堆叠 */
flex-wrap: nowrap;
/* flex item在主轴上的对齐方式,这里定义左对齐 */
justify-content: flex-start;
/* 定义交叉轴对其方式 */
align-items: flex-start
}
.lable {
width: 100px;
margin-left: 5px;
background-color: blue;
}
</style>
</html>

  

  不过,这样以来,flex容器的overflow属性将不再起作用。在flex布局下,所有的flex item均分或根据开发者定义分配容器空间,而不会超过flex容器空间。

注意
  值得注意的时,如果仅仅设置父div容器的overflow属性,容器内的元素均为inline或者inline-block,也无法一直堆叠在同一行而不换行,他们无法撑开父容器。当一行无法放置下它们时间,他们会自动换行。

————————————————
版权声明:本文为CSDN博主「Kiloveyousmile」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Kiloveyousmile/article/details/80248083

多个div排列在同一行而不换行的更多相关文章

  1. CSS 让div,span等块级、非快级元素排列在同一行

    让div,span等块级.非快级元素排列在同一行 by:授客 QQ:1033553122 例子:让两个div排列在同一行 给div添加float样式 <!DOCTYPE html> < ...

  2. DIV+CSS 让同一行的图片和文字对齐【转藏】

    DIV+CSS 让同一行的图片和文字对齐 DIV+CSS 让同一行的图片和文字对齐 在div+css布局中,如果一行(或一个DIV)内容中有图片和文字的话,图片和文字往往会一个在上一个在下,这是一个新 ...

  3. div中让内容能不换行就尽量不换行.【纯原】

    div中让内容能不换行就尽量不换行,部分左对齐,部分右对齐. <html> <head> <title>九歌·少司命</title> <style ...

  4. DIV+CSS 让同一行的图片和文字对齐

    在div+css布局中,如果一行(或一个DIV)内容中有图片和文字的话,图片和文字往往会一个在上一个在下,这是一个新手都会遇到问题,我的解决方法有三: 1.添加CSS属性:vertical-align ...

  5. 前端页面中如何在窗口缩放时让两个div始终在同一行显示

    直接贴代码吧 先总结一下吧  有两种方法 一    最外层设置一个大div  给这个大div固定的宽度和高度 给里面两个小div 设置浮动  设置宽高 <!DOCTYPE html> &l ...

  6. if函数+isna函数+vlookup函数实现不同列相同单元格内容排列在同一行

    1,首先学习的网址:https://jingyan.baidu.com/album/22a299b5dd0f959e19376a22.html?picindex=1 2,excel 这也许是史上最好最 ...

  7. 使用inline-block,使前面img,后面空div居中显示在一行后,导致当div中有内容时,div下移问题

    .pro_li img,.pro_sm{display: inline-block; *display:inline;*zoom:1;vertical-align: middle ;} 解决方法:使用 ...

  8. 几个简单的css设置问题:div居中,ul li不换行 ,内容超出自动变省略号等

    1  div在页面居中的问题 1)position值为relative时(相对定位),css设置属性margin:0 auto;(0 auto,表示上下边界为0,左右则根据宽度自适应相同值,即居中)即 ...

  9. DIV内英文或者数字不换行的问题 解决办法

    word-wrap:break-word; word-break:break-all;

随机推荐

  1. 27.Python列表(list)、元组(tuple)、字典(dict)和集合(set)详解

    本章将会介绍 Python 内置的四种常用数据结构:列表(list).元组(tuple).字典(dict)以及集合(set). 这四种数据结构一但都可用于保存多个数据项,这对于编程而言是非常重要的,因 ...

  2. koa 基础(八)koa 中间件的执行顺序

    1.koa 中间件的执行顺序 app.js /** * koa 中间件的执行顺序 */ // 引入模块 const Koa = require('koa'); const router = requi ...

  3. jsp页面中使用javabean

    <%@ page language="java" import="java.util.*,com.loaderman.demo.b_cases.*" pa ...

  4. 在本地局域网 windows server 2008 下安装 Nginx 1.12.1

    简介: Nginx ("engine x") 是高性能 HTTP.反向代理服务器,也是 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗 ...

  5. springBoot整合spring、springMVC、mybatis

    前文 1.为什么使用springBoot 众所周知,spring是Java在搭建后台时非常实用的框架,其整合了市场上几乎所有的主流框架于一体,使后端编程更加高效.快速: 而SpringBoot更是把s ...

  6. HashMap 的实现原理解析(转载)

    HashMap 概述 HashMap 是基于哈希表的 Map 接口的非同步实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.此类不保证映射的顺序,特别是它不保证该顺序恒久不 ...

  7. Python sys.argv[] 使用

    sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径;比如在CMD命令行输入 “python  test.py -help”,那么sys.argv[0]就代表“tes ...

  8. Linux 服务器基本优化

    一:修改ulimit数 vi /etc/security/limits.conf 添加如下行: * soft noproc 65535 * hard noproc 65535 * soft nofil ...

  9. jdk 1.8中的list排序

    首先看看collections实现 public static <T> void sort(List<T> list, Comparator<? super T> ...

  10. 基于redis的高并发秒杀的JAVA-DEMO实现!

    在Redis的事务中,WATCH命令可用于提供CAS(check-and-set)功能.假设我们通过WATCH命令在事务执行之前监控了多个Keys,倘若在WATCH之后有任何Key的值发生了变化,EX ...