display:inline-block下,元素不能在同一水平线及元素间无margin间距的问题解决方法

本文探讨使用display: inline-block实现元素横排时遇到的间距与对齐问题,介绍如何通过设置父级font-size为0及垂直对齐属性解决这些问题。

在前端页面编辑中,常常用于块元素横排列时,我们会用到浮动或者dispaly:inline-block;

浮动虽然好用,效果明显,但是会存在潜在BUG,(暂且不论);那么display:inline-block也能使其横排,但是有两个很明显的问题:

1.横排元素之前没有设置,margin却多出了间距;

2.横排元素不能在同一水平线上;

看图:

处理方式:只需要在横排元素的父级设置font-size:0;即可

 

原因:

代码编译时,标签间有折行或空格,都相当于字符空格,这种字符,默认有font-size;所以除了设置font-size=0;还可以不进行折行或空格,但是这样不利于代码的阅读;

原图:

分析图:在父级元素首标签后按一个”delete“后面就会显示出空格字符

OK,接下来我们继续处理横排不在同一水平线的问题(顺带提一下,float无此BUG,都会以下边对齐);

原图:

处理:这是因为横排元素在父级元素盒子中,没有垂直方向的对齐方式;所以对横排元素垂直方向给与对齐设置即可;

 

详细的水平线问题,可以转移到这位老铁这里:

http://www.zhangxinxu.com/wordpress/2010/11/拜拜了浮动布局-基于displayinline-block的列表布局/

以及案例:

https://segmentfault.com/q/1010000007142118?_ea=1251429

<style> :root { --primary: #309bd1; --secondary: #19A0F5; --dark: #333; --light: #f5f7fa; --gray: #666; --light-gray: #eee; } body { margin: 0; padding: 0; font-family: "Microsoft YaHei", sans-serif; color: var(--dark); background-color: #fff; scroll-behavior: smooth; overflow-x: hidden; } .mar_1200 { width: 100%; max-width: 1200px; margin: 0 auto; padding: 0 20px; box-sizing: border-box; } .header { background-color: #fff; box-shadow: 0 2px 10px rgba(0,0,0,0.1); position: fixed; top: 0; left: 0; right: 0; z-index: 1000; transition: all 0.3s ease; } .header.scrolled { background-color: rgba(255, 255, 255, 0.95); box-shadow: 0 4px 15px rgba(0,0,0,0.1); } .logo { float: left; padding: 10px 0; } .logo img { height: 55px; object-fit: contain; transition: transform 0.3s ease; } .header.scrolled .logo img { height: 50px; } .nav_box { float: right; } .nav_box ul { list-style: none; margin: 0; padding: 0; } .nav_box li { float: left; position: relative; } .nav_box a { display: block; padding: 25px 20px; color: var(--dark); text-decoration: none; font-size: 16px; transition: all 0.3s; position: relative; overflow: hidden; } .nav_box a::before { content: ''; position: absolute; top: 0; left: -100%; width: 100%; height: 100%; background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent); transition: left 0.5s; } .nav_box a:hover::before { left: 100%; } .nav_box a:hover, .nav_box a.active { color: #fff; background-color: var(--primary); } .header.scrolled .nav_box a { padding: 22px 20px; } .banner { min-height: 500px; padding: 150px 20px 120px; background: linear-gradient(135deg, #309bd1 0%, #19A0F5 100%); color: #fff; text-align: center; position: relative; overflow: hidden; box-sizing: border-box; } .banner::before { content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.1'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E"); animation: move 20s linear infinite; opacity: 0.3; } @keyframes move { 0% { background-position: 0 0; } 100% { background-position: 600px 600px; } } .banner h1 { font-size: clamp(2.5rem, 5vw, 3rem); margin-bottom: 20px; opacity: 0; transform: translateY(30px); animation: fadeUp 1s forwards 0.3s; } .banner p { font-size: clamp(1rem, 2vw, 1.25rem); max-width: 700px; margin: 0 auto 30px; line-height: 1.6; opacity: 0; transform: translateY(30px); animation: fadeUp 1s forwards 0.6s; } .btn { display: inline-block; padding: 12px 30px; background-color: #fff; color: var(--primary); text-decoration: none; border-radius: 4px; font-weight: bold; transition: all 0.3s; position: relative; overflow: hidden; z-index: 1; } .btn::after { content: ''; position: absolute; top: 0; left: -100%; width: 100%; height: 100%; background-color: rgba(0,0,0,0.05); transition: all 0.3s ease; z-index: -1; } .btn:hover { background-color: var(--light); transform: translateY(-2px); box-shadow: 0 5px 15px rgba(0,0,0,0.1); } .btn:hover::after { left: 100%; } .banner .btn { opacity: 0; transform: translateY(30px); animation: fadeUp 1s forwards 0.9s; } @keyframes fadeUp { to { opacity: 1; transform: translateY(0); } } .module-section { padding: 80px 0; background-color: var(--light); } .section-title { text-align: center; margin-bottom: 60px; } .section-title h2 { font-size: clamp(1.5rem, 3vw, 2rem); margin-bottom: 15px; position: relative; display: inline-block; } .section-title h2::after { content: ''; position: absolute; bottom: -10px; left: 50%; transform: translateX(-50%); width: 60px; height: 3px; background-color: var(--primary); transition: width 0.3s ease; } .section-title:hover h2::after { width: 100px; } .section-title p { color: var(--gray); max-width: 700px; margin: 0 auto; } .module-cards { display: flex; flex-wrap: wrap; gap: 2%; } .module-card { width: calc(25% - 1.5%); background-color: #fff; border-radius: 8px; box-shadow: 0 5px 15px rgba(0,0,0,0.05); overflow: hidden; transition: all 0.3s; margin-bottom: 30px; position: relative; opacity: 1; } .module-card::before { content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: linear-gradient(135deg, rgba(48,155,209,0.1) 0%, rgba(25,160,245,0.1) 100%); opacity: 0; transition: opacity 0.3s ease; z-index: 1; } .module-card:hover::before { opacity: 1; } .module-card.animate { animation: fadeInUp 0.6s ease forwards; opacity: 0; } @keyframes fadeInUp { from { opacity: 0; transform: translateY(30px); } to { opacity: 1; transform: translateY(0); } } @media (max-width: 992px) { .module-card { width: calc(50% - 1%); } } @media (max-width: 576px) { .module-card { width: 100%; } } .module-card:hover { transform: translateY(-10px); box-shadow: 0 15px 30px rgba(0,0,0,0.1); } .module-card-img { position: relative; width: 100%; height: 0; padding-bottom: 75%; overflow: hidden; } .module-card-img img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; transition: all 0.5s; opacity: 0; } .module-card-img img.loaded { opacity: 1; transition: opacity 0.5s ease; } .module-card:hover .module-card-img img { transform: scale(1.1); } .module-card-content { padding: 25px; display: flex; flex-direction: column; flex-grow: 1; position: relative; z-index: 2; } .module-card-content .btn { margin-top: auto; align-self: center; } .module-card { display: flex; flex-direction: column; } .module-card h3 { color: var(--primary); margin: 0 0 15px 0; } .module-card ul { padding-left: 20px; margin-bottom: 20px; } .module-card li { margin-bottom: 8px; color: var(--gray); } .feature-section { padding: 80px 0; } .feature-grid { display: flex; flex-wrap: wrap; gap: 4%; } .feature-item { width: calc(50% - 2%); margin-bottom: 40px; display: flex; align-items: flex-start; opacity: 0; transform: translateY(30px); transition: all 0.6s ease; } .feature-item.visible { opacity: 1; transform: translateY(0); } @media (max-width: 768px) { .feature-item { width: 100%; } } .feature-icon { width: 80px; height: 80px; background-color: var(--light); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin-right: 20px; flex-shrink: 0; transition: all 0.3s ease; position: relative; overflow: hidden; } .feature-icon::after { content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: linear-gradient(135deg, rgba(48,155,209,0.2) 0%, rgba(25,160,245,0.2) 100%); opacity: 0; transition: opacity 0.3s ease; } .feature-item:hover .feature-icon::after { opacity: 1; } .feature-item:hover .feature-icon { background-color: var(--primary); transform: scale(1.1) rotate(5deg); } .icon-img-container { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; overflow: hidden; } .feature-icon img { width: 100%; height: 100%; object-fit: cover; object-position: center; min-width: 40px; min-height: 40px; transition: all 0.3s ease; } .feature-item:hover .feature-icon img { filter: brightness(100); } .feature-content h3 { margin: 0 0 10px 0; transition: color 0.3s ease; } .feature-item:hover .feature-content h3 { color: var(--primary); } .feature-content p { color: var(--gray); line-height: 1.6; } .cta-section { background-color: var(--primary); color: #fff; padding: 80px 20px; text-align: center; position: relative; overflow: hidden; } .cta-section::before { content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: radial-gradient(circle at 20% 20%, rgba(255,255,255,0.1) 0%, transparent 40%), radial-gradient(circle at 80% 80%, rgba(255,255,255,0.1) 0%, transparent 40%); animation: pulse 15s infinite; } @keyframes pulse { 0% { transform: scale(1); } 50% { transform: scale(1.1); } 100% { transform: scale(1); } } .cta-section h2 { font-size: clamp(1.5rem, 3vw, 2rem); margin-bottom: 20px; position: relative; } .cta-section p { font-size: clamp(1rem, 2vw, 1.125rem); max-width: 700px; margin: 0 auto 30px; position: relative; } .cta-section .btn { background-color: #fff; color: var(--primary); position: relative; } .footer { background-color: #333; color: #ccc; padding: 60px 0 30px; } .footer-content { display: flex; flex-wrap: wrap; gap: 20px; margin-bottom: 40px; } .footer-col { width: calc(25% - 15px); transition: transform 0.3s ease; } .footer-col:hover { transform: translateY(-5px); } @media (max-width: 768px) { .footer-col { width: calc(50% - 10px); } } @media (max-width: 576px) { .footer-col { width: 100%; } } .footer-col h4 { color: #fff; margin: 0 0 20px 0; font-size: 18px; position: relative; display: inline-block; } .footer-col h4::after { content: ''; position: absolute; bottom: -5px; left: 0; width: 0; height: 2px; background-color: var(--primary); transition: width 0.3s ease; } .footer-col:hover h4::after { width: 100%; } .footer-col ul { list-style: none; padding: 0; margin: 0; } .footer-col li { margin-bottom: 10px; } .footer-col a { color: #ccc; text-decoration: none; position: relative; padding-left: 0; transition: padding-left 0.3s ease, color 0.3s ease; } .footer-col a::before { content: '→'; position: absolute; left: -15px; opacity: 0; transition: opacity 0.3s ease, left 0.3s ease; } .footer-col a:hover { color: var(--primary); padding-left: 5px; } .footer-col a:hover::before { opacity: 1; left: -10px; } .copyright { text-align: center; padding-top: 30px; border-top: 1px solid #444; transition: color 0.3s ease; } .copyright:hover { color: #fff; } .clearfix::after { content: ""; display: table; clear: both; } @media (max-width: 992px) { .nav_box a { padding: 25px 12px; font-size: 14px; } .header.scrolled .nav_box a { padding: 22px 12px; } } @media (max-width: 768px) { .header .mar_1200 { display: flex; flex-direction: column; align-items: center; } .logo, .nav_box { float: none; } .nav_box li { float: none; display: inline-block; } } .menu-toggle { display: none; position: absolute; top: 20px; right: 20px; width: 40px; height: 40px; cursor: pointer; z-index: 1001; } .menu-toggle span { display: block; width: 30px; height: 3px; background-color: var(--dark); margin: 6px auto; transition: all 0.3s ease; } @media (max-width: 768px) { .menu-toggle { display: block; } .nav_box { display: none; width: 100%; text-align: center; } .nav_box.active { display: block; } .nav_box ul { margin-top: 10px; } .nav_box li { display: block; float: none; } .nav_box a { padding: 15px 20px; } .header.scrolled .nav_box a { padding: 15px 20px; } } .cursor-follower { position: fixed; width: 20px; height: 20px; background: radial-gradient(circle, rgba(48,155,209,0.5) 0%, rgba(25,160,245,0.3) 50%, transparent 70%); border-radius: 50%; pointer-events: none; z-index: 9999; transform: translate(-50%, -50%); transition: width 0.2s, height 0.2s; } .page-loader { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: linear-gradient(135deg, #309bd1 0%, #19A0F5 100%); display: flex; justify-content: center; align-items: center; z-index: 9999; transition: opacity 0.5s, visibility 0.5s; } .page-loader.fade-out { opacity: 0; visibility: hidden; } /* 核心修改:调整加载内容容器为Flex垂直布局,确保SVG在文字上方正中 */ .loader-content { text-align: center; color: white; display: flex; flex-direction: column; align-items: center; /* 水平居中 */ justify-content: center; /* 垂直居中 */ } /* 优化SVG图标样式,移除固定宽高,通过viewBox自适应,保持100px尺寸 */ .loader-logo { width: 100px; height: 100px; margin-bottom: 20px; /* 图标与文字间距 */ animation: pulse 2s infinite; } .loader-text { font-size: 1.2rem; margin-bottom: 20px; /* 确保文字不换行,保持居中 */ white-space: nowrap; } .loader-bar { width: 200px; height: 4px; background-color: rgba(255,255,255,0.3); border-radius: 2px; margin: 0 auto; overflow: hidden; } .loader-progress { height: 100%; width: 0; background-color: white; border-radius: 2px; transition: width 0.3s; } .typewriter { overflow: hidden; border-right: 2px solid white; white-space: nowrap; margin: 0 auto; animation: typing 3.5s steps(40, end), blink-caret 0.75s step-end infinite; } @keyframes typing { from { width: 0 } to { width: 100% } } @keyframes blink-caret { from, to { border-color: transparent } 50% { border-color: white } } .particles-container { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; overflow: hidden; } .scroll-progress { position: fixed; top: 0; left: 0; width: 0; height: 3px; background: linear-gradient(90deg, #309bd1, #19A0F5); z-index: 1001; transition: width 0.1s; } </style>,注释
10-08
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值