Html CSS:在标题中左/中/右放置 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11570107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:44:46  来源:igfitidea点击:

CSS: Placing divs left/center/right inside header

csshtmlpositioncss-float

提问by L42

I've been trying to create a site with the following structure:
desired structure

我一直在尝试创建一个具有以下结构的站点:
所需结构

But I can't seem to get the header correct (e1 left, e2 centered, e3 right). I want the three elements e1, e2 and e3 to be left, middle and right positioned. This is what I'm trying:

但我似乎无法让标题正确(e1 左,e2 居中,e3 右)。我希望三个元素 e1、e2 和 e3 位于左侧、中间和右侧。这就是我正在尝试的:

<div id="wrapper">
<div id="header">
    <div id="header-e1">
        1
    </div>
    <div id="header-e2">
        2
    </div>
    <div id="header-e3">
        3
    </div>
</div>
<div id="nav">
    links
</div>
<div id="content">
    content
</div>
<div id="footer">
    footer
</div>
</div>

With this css:

有了这个CSS:

#wrapper
{
    width: 95%;
    margin: 20px auto;
    border: 1px solid black;
}

#header
{
    margin: 5px;
}
#header-e1
{
    float: left;
    border: 1px solid black;
}
#header-e2
{
    float: left;
    border: 1px solid black;
}
#header-e3
{

    border: 1px solid black;
}

#nav
{
    margin: 5px;
}
#content
{
    margin: 5px;
}
#footer
{
    margin: 5px;
}

Can someone give me tips to what I can do? The structure is going to be used on a mobile website.

有人可以给我提示我可以做什么吗?该结构将用于移动网站。

UPDATE

更新

The code I have above gives me this: current resultBut I want the 2 centered and the 3 on the right side. I don't want to set the width to a percent because the content in the elements may vary, meaning it may be 20/60/20 - 10/80/10 - 33/33/33 or something else.

我上面的代码给了我这个: 当前结果但我想要 2 居中,3 在右侧。我不想将宽度设置为百分比,因为元素中的内容可能会有所不同,这意味着它可能是 20/60/20 - 10/80/10 - 33/33/33 或其他东西。

回答by ScottS

Utilize the Magic of Overflow: Hidden

利用溢出的魔法:隐藏

If you can swap the html position of 2 & 3 like so:

如果您可以像这样交换 2 和 3 的 html 位置:

<div id="header-e1">
    1 is wider
</div>
<div id="header-e3">
    3 is also
</div>
<div id="header-e2">
    2 conforms
</div>

Then you can set this css which will cause 2 to "fill" the available spacebecause of the overlow: hiddenon it. So if 1 & 3 expand, 2 narrows(shrink window down to see what happens at really small size).

然后你可以设置这个css,这将导致2 “填充”可用空间,因为overlow: hidden它在上面。因此,如果 1 和 3 扩展,则 2 会变窄(缩小窗口以查看在非常小的尺寸下会发生什么)。

#header-e1 {float: left;}
#header-e2 {overflow: hidden;}
#header-e3 {float: right;}

Technically, you could keep your current html order and your float: lefton both 1 & 2 and make 3 the flex divwith overflow: hidden. You could do the same with 1by reversing the order of the html completely and setting 2 & 3 to float: rightwith 1 having overflow: hidden. To me it would seem best to have the middle flex, but you know your application better than I.

从技术上讲,您可以保留当前的 ​​html 顺序以及float: left1 和 2 上的顺序,并使 3 成为带有overflow: hidden. 您可以通过完全反转 html 的顺序并将 2 和 3 设置为float: right1来对 1 执行相同的操作overflow: hidden。对我来说最好有中间的 flex,但你比我更了解你的应用程序。

回答by RevConcept

If you are trying to make the site with a responsive width, you can try the following (33% is roughly one-third):

如果您正在尝试使网站具有响应式宽度,您可以尝试以下操作(33% 大约为三分之一):

#header-e1 {
    float: left;
    width:33%;
    border: 1px solid black;
}

#header-e2 {
    float: left;
    width:33%;
    border: 1px solid black;
}

#header-e3 {
    float: left;
    width:33%;
    border: 1px solid black;
}

You could also used fixed widths for the divs. If you want the further from each other you can play with their left/right margins etc. Hope that helps!

您还可以为 div 使用固定宽度。如果你想彼此更远,你可以玩他们的左/右边缘等。希望有帮助!

Here is an edit for no widths:

这是无宽度的编辑:

#wrapper {
    position:relative; (add to wrapper)
}

#header-e1 {
    position:absolute;
    left:0;
    border:1px solid black;
}

#header-e2 {
    position:absolute;
    left:50%;
    border:1px solid black;
}

#header-e3 {
    position:absolute;
    right:0;
    border: 1px solid black;
}

回答by Andy

You need to give the divs in your header a width, and float header-e3 left.

您需要为标题中的 div 指定一个宽度,并将 header-e3 向左浮动。

Note: They all have the same CSS properties, so just give them the same class like .headerDivs and then you don't have repeating code

注意:它们都具有相同的 CSS 属性,所以只需给它们相同的类,如 .headerDivs,然后你就没有重复的代码

Edit: here is a working jsfiddle: http://jsfiddle.net/eNDPG/

编辑:这是一个有效的 jsfiddle:http: //jsfiddle.net/eNDPG/

回答by Zhihao

I'm using a similar idea to what RevCocnept suggested with the width: 33%, except using display: inline-blockinstead of float: left. This is to avoid removing the divelements inside #headerfrom the flow of the page and causing the height of #headerto become zero.

我使用了类似的想法是什么RevCocnept与建议width: 33%,除了使用display: inline-block的替代float: left。这是为了避免从页面流中移除div内部元素#header并导致的高度#header变为零。

#header > div {
    display: inline-block;
    width: 31%;
    margin: 5px 1%;
}

Demo

演示

回答by Leniel Maccaferri

You can do something like this:

你可以这样做:

HTML

HTML

<div>
    <div id="left">Left</div>
    <div id="right">Right</div>
    <div id="center">Center</div>
</div>

CSS

CSS

#left {
  float: left;
  border: 1px solid red;
}

#right {
  float: right;
  border: 1px solid blue;
}

#center {
  margin-left: 50px;
  margin-right: 50px;
  border: 1px solid green;
  text-align: center;
}

The centered <div>must come as the last one in the HTMLcode.

居中的<div>必须作为HTML代码中的最后一个。

Here's a JS Bin to test: http://jsbin.com/evagat/2/edit

这是要测试的 JS Bin:http: //jsbin.com/evagat/2/edit

回答by P P

                        <style type="text/css">
                        body {
                        margin:0;
                        }
                        #header {
                        width:100%;
                        **strong text**margin:auto;
                        height:10%;
                        background-color:red;
                        }
                        #left {
                        width:20%;
                        float:left;
                        #margin:auto auto auto auto;
                        height:100%;
                        background-color:blue;
                        }
                        #right {
                        float:right;
                        width:20%;
                        #margin:auto auto auto auto;
                        height:100%;
                        background-color:green;
                        }
                        #middle {
                        position:relative;
                        left:0;
                        right:0;
                        margin:auto;
                        height:80%;
                        background-color:yellow;
                        width:100%;
                        }
                        #middle1 {
                        width: 80%;
                        margin:auto;
                        height:45%;
                        background-color:black;
                        }
                        #middle2 {
                        width: 80%;
                        margin:auto;
                        height:40%;
                        background-color:brown;
                        }
                        #middle3 {
                        width: 80%;
                        margin:auto;
                        height:15%;
                        background-color:orange;
                        }
                        #midmain {
                        width: auto;
                        margin:auto;
                        height:100%;
                        background-color:white;
                        }
                        #footer {
                        width:100%;
                        margin:auto;
                        height:10%;
                        background-color:red;
                        }
                        </style>

now check comment for html design.