热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将两个内嵌块左右对齐放在同一行上-Aligntwoinline-blocksonsameline,onetotheleftandonetotheright

HowcanIaligntwoinline-blockssothatoneisleftandtheotherisrightonthesameline?Whyi

How can I align two inline-blocks so that one is left and the other is right on the same line? Why is this so hard? Is there something like LaTeX's \hfill that can consume the space between them to achieve this?

如何对齐两个内线块,使其中一个是左的,另一个是右的?为什么这么难?是否有像乳胶的\hfill这样的东西可以消耗它们之间的空间来达到这个目的?

I don't want to use floats because with inline-blocks I can line up the baselines. And when the window is too small for both of them, with inline-blocks I can just change the text-align to center and they will be centered one atop another. Relative(parent) + Absolute(element) positioning has the same problems as floats do.

我不想使用浮点数,因为有了内联块,我可以将基线对齐。当窗口对这两个窗口来说都太小时,使用内联块,我就可以将文本对齐到中心,它们将会彼此重叠。相对(父)+绝对(元素)定位与浮动有相同的问题。

The HTML5:

HTML5:


    

Title

The css:

css:

header {
    //text-align: center; // will set in js when the nav overflows (i think)
}

h1 {
    display: inline-block;
    margin-top: 0.321em;
}

nav {
    display: inline-block;
    vertical-align: baseline;
}

Thery're right next to each other, but I want the nav on the right.

它们相邻,但是我想要右边的nav。

a diagram

7 个解决方案

#1


105  

Edit: 3 years has passed since I answered this question and I guess a more modern solution is needed, although the current one does the thing :)

编辑:我回答这个问题已经三年了,我想需要一个更现代的解决方案,尽管目前的方案是这样的:)

1.Flexbox

1. flexbox

It's by far the shortest and most flexible. Apply display: flex; to the parent container and adjust the placement of its children by justify-content: space-between; like this:

它是迄今为止最短、最灵活的。应用显示:flex;对父容器进行调整,并根据其正当性内容:空格;是这样的:

.header {
    display: flex;
    justify-content: space-between;
}

Can be seen online here - http://jsfiddle.net/skip405/NfeVh/1073/

在这里可以看到http://jsfiddle.net/skip405/NfeVh/1073/

Note however that flexbox support is IE10 and newer. If you need to support IE 9 or older, use the following solution:

不过请注意,flexbox支持的是IE10和更新版本。如果您需要支持IE 9或以上版本,请使用以下解决方案:

2.You can use the text-align: justify technique here.

2。您可以在这里使用“文本对齐”技术。

.header {
    background: #ccc; 
    text-align: justify; 

    /* ie 7*/  
    *width: 100%;  
    *-ms-text-justify: distribute-all-lines;
    *text-justify: distribute-all-lines;
}
 .header:after{
    content: '';
    display: inline-block;
    width: 100%;
    height: 0;
    font-size:0;
    line-height:0;
}

h1 {
    display: inline-block;
    margin-top: 0.321em; 

    /* ie 7*/ 
    *display: inline;
    *zoom: 1;
    *text-align: left; 
}

.nav {
    display: inline-block;
    vertical-align: baseline; 

    /* ie 7*/
    *display: inline;
    *zoom:1;
    *text-align: right;
}

The working example can be seen here: http://jsfiddle.net/skip405/NfeVh/4/. This code works from IE7 and above

可以在这里看到工作示例:http://jsfiddle.net/skip405/NfeVh/4/。此代码适用于IE7及以上版本

If inline-block elements in HTML are not separated with space, this solution won't work - see example http://jsfiddle.net/NfeVh/1408/ . This might be a case when you insert content with Javascript.

如果HTML中的inline-block元素没有与空格分开,那么这个解决方案将不起作用——请参见示例http://jsfiddle.net/NfeVh/1408/。使用Javascript插入内容时可能会出现这种情况。

If we don't care about IE7 simply omit the star-hack properties. The working example using your markup is here - http://jsfiddle.net/skip405/NfeVh/5/. I just added the header:after part and justified the content.

如果我们不关心IE7,那就忽略star-hack属性。使用您的标记的工作示例在这里—http://jsfiddle.net/skip405/NfeVh/5/。我只是添加了标题:后部分,并证明内容。

In order to solve the issue of the extra space that is inserted with the after pseudo-element one can do a trick of setting the font-size to 0 for the parent element and resetting it back to say 14px for the child elements. The working example of this trick can be seen here: http://jsfiddle.net/skip405/NfeVh/326/

为了解决在after伪元素中插入额外空间的问题,我们可以使用这样的技巧:将父元素的字体大小设置为0,将子元素的字体大小设置为14px。这个技巧的工作示例可以在这里看到:http://jsfiddle.net/skip405/NfeVh/326/

#2


3  

If you don't want to use floats, you're going to have to wrap your nav:

如果你不想使用浮动,你需要将你的nav包裹起来:


Title

...and add some more specific css:

…并添加一些更具体的css:

header {
//text-align: center; // will set in js when the nav overflows (i think)
width:960px;/*Change as needed*/
height:75px;/*Change as needed*/
}

h1 {
display: inline-block;
margin-top: 0.321em;
}

#navWrap{
position:absolute;
top:50px; /*Change as needed*/
right:0;
}

nav {
display: inline-block;
vertical-align: baseline;
}

You may need to do a little more, but that's a start.

你可能需要做更多,但这只是一个开始。

#3


3  

Taking advantage of @skip405's answer, I've made a Sass mixin for it:

利用@skip405的答案,我做了一个Sass mixin:

@mixin inline-block-lr($container,$left,$right){
    #{$container}{        
        text-align: justify; 

        &:after{
            content: '';
            display: inline-block;
            width: 100%;
            height: 0;
            font-size:0;
            line-height:0;
        }
    }

    #{$left} {
        display: inline-block;
        vertical-align: middle; 
    }

    #{$right} {
        display: inline-block;
        vertical-align: middle; 
    }
}

It accepts 3 parameters. The container, the left and the right element. For example, to fit the question, you could use it like this:

它接受三个参数。容器,左边和右边的元素。例如,为了符合问题,你可以这样使用:

@include inline-block-lr('header', 'h1', 'nav');

#4


1  

If you're already using Javascript to center stuff when the screen is too small (as per your comment for your header), why not just undo floats/margins with Javascript while you're at it and use floats and margins normally.

如果在屏幕太小时,你已经在用Javascript处理中心问题(根据你的标题的注释),为什么不使用Javascript来撤销浮动/页边距,并且通常使用浮动和页边距。

You could even use CSS media queries to reduce the amount Javascript you're using.

您甚至可以使用CSS媒体查询来减少Javascript的使用量。

#5


0  

give it float: right and the h1 float:left and put an element with clear:both after them.

赋予它float: right和h1 float:left,并在它们后面加上一个clear:both。

#6


0  

I think one possible solution to this is to use display: table:

我认为一个可能的解决方法是使用display: table:

.header {
  display: table;
  width: 100%;
  box-sizing: border-box;
}

.header > * {
  display: table-cell;
}

.header > *:last-child {
  text-align: right;  
}

h1 {
  font-size: 32px;
}

nav {
  vertical-align: baseline;
}

JSFiddle: http://jsfiddle.net/yxxrnn7j/1/

JSFiddle:http://jsfiddle.net/yxxrnn7j/1/

#7


0  

For both elements use

两个元素使用

display: inline-block;

the for the 'nav' element use

使用“nav”元素

float: right;

推荐阅读
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 本文介绍了在HTML中实现表格的页眉页脚布局的解决方案。通过基本的HTML/CSS技术,避免使用内联样式和固定定位,实现了一个标准的页眉页脚布局。提供了一个替代的解决方案,为读者提供了参考。 ... [详细]
  • 本文整理了Java中java.lang.NoSuchMethodError.getMessage()方法的一些代码示例,展示了NoSuchMethodErr ... [详细]
  • 本文整理了常用的CSS属性及用法,包括背景属性、边框属性、尺寸属性、可伸缩框属性、字体属性和文本属性等,方便开发者查阅和使用。 ... [详细]
  • ZABBIX 3.0 配置监控NGINX性能【OK】
    1.在agent端查看配置:nginx-V查看编辑时是否加入状态监控模块:--with-http_stub_status_module--with-http_gzip_stat ... [详细]
  • css div中文字位置_超赞的 CSS 阴影技巧与细节
    本文的题目是CSS阴影技巧与细节。CSS阴影,却不一定是box-shadow与filter:drop-shadow,为啥?因为使用其他属性 ... [详细]
  • Iamtryingtoachievethearrowpointingupwards..iamtryingtoachieveitinmycssiamnotabl ... [详细]
  • 在本文中,我将向你介绍如何使用HTML5自定义数据属性。我还将向你介绍一些开发人员在工作中经常使用的优秀实例。为什么需要自定义数据属性?很多时候我们需要存储一些与不同DOM元素相关联的 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • CSS3 animation动画属性详解及用法
    本文详细介绍了CSS3 animation动画的各种属性及用法,包括关键帧动画、动画名称、动画时间、动画曲线、动画延迟、动画播放次数、动画状态和动画前后的状态等。通过本文的学习,读者可以深入了解CSS3 animation动画的使用方法。 ... [详细]
  • SmartRefreshLayout自定义头部刷新和底部加载
    1.添加依赖implementation‘com.scwang.smartrefresh:SmartRefreshLayout:1.0.3’implementation‘com.s ... [详细]
  • js中运行机制的详细分析(示例解析)
    web前端|js教程node.js,html5,html,css,javascriptweb前端-js教程本篇文章给大家带来的内容是关于js中运行机制的详细分析(示例解析),有一定 ... [详细]
author-avatar
a52713849_937
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有