热门标签 | 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;

推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 自己用过的一些比较有用的css3新属性【HTML】
    web前端|html教程自己用过的一些比较用的css3新属性web前端-html教程css3刚推出不久,虽然大多数的css3属性在很多流行的浏览器中不支持,但我个人觉得还是要尽量开 ... [详细]
  • 本文详细介绍了 org.apache.commons.io.IOCase 类中的 checkCompareTo() 方法,通过多个代码示例展示其在不同场景下的使用方法。 ... [详细]
  • 本文探讨了如何在Classic ASP中实现与PHP的hash_hmac('SHA256', $message, pack('H*', $secret))函数等效的哈希生成方法。通过分析不同实现方式及其产生的差异,提供了一种使用Microsoft .NET Framework的解决方案。 ... [详细]
  • 使用JS、HTML5和C3创建自定义弹出窗口
    本文介绍如何结合JavaScript、HTML5和C3.js来实现一个功能丰富的自定义弹出窗口。通过具体的代码示例,详细讲解了实现过程中的关键步骤和技术要点。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文详细介绍了 GWT 中 PopupPanel 类的 onKeyDownPreview 方法,提供了多个代码示例及应用场景,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
  • 深入解析ESFramework中的AgileTcp组件
    本文详细介绍了ESFramework框架中AgileTcp组件的设计与实现。AgileTcp是ESFramework提供的ITcp接口的高效实现,旨在优化TCP通信的性能和结构清晰度。 ... [详细]
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社区 版权所有