简体   繁体   English

如何在同一背景div的左侧放置图片和菜单? (含图片)

[英]How do you position a picture to the left and a menu in the middle of the same background div? (Picture included)

So I have 3 elements: the Nav bar which is the container and stretches across the whole screen, the logo which should be contained and positioned to the left (although the height is bigger than the container so I don't know if this causes a problem) and the navigation menu which should be exactly in the middle of the page. 因此,我有3个元素:导航栏,它是容器并延伸到整个屏幕,徽标应包含在左侧并定位在左侧(尽管高度大于容器,所以我不知道这是否会导致问题)和导航菜单,它们应该恰好位于页面中间。 Sorry for the bad paint picture but I wanted to be able to show what I mean: 对不起,油漆不好,但我想表明我的意思: 在此处输入图片说明

I've tried multiple ways to do this, made the Nav bar a block and the nav menu and logo inline-blocks and tried to text-align: center the nav menu but it stays to the left. 我尝试了多种方法来做到这一点,将导航栏设置为一个块,将导航菜单和徽标内联块设置为文本,并尝试进行文本对齐:将导航菜单居中,但它保持在左侧。 I could use a percentage margin here to try get it to the middle but it seems off center if I try it on different screens. 我可以在此处使用百分比裕度来尝试将其居中,但是如果在其他屏幕上尝试,它似乎偏离了中心。 I tried to just use margin: auto 0; 我试图只使用margin:auto 0; on the nav menu and it centers it but then appears below the menu not inside the nav bar. 在导航菜单上,它居中,然后显示在菜单下方,而不在导航栏中。 The closest I've come is having the logo outside the nav bar div then the nav bar floated next to it, but then margin: auto 0; 我最接近的是在导航栏div外面有徽标,然后导航栏浮动在它旁边,但是接着是margin:auto 0; would only put it in the center of the shortened nav bar and not the center of the page. 只会将其放在缩短的导航栏的中心,而不是页面的中心。

Thanks in advance. 提前致谢。

Edit: 编辑:

It works by floating the logo left within the navBar div like so: 它可以通过如下方式将徽标浮动在navBar div中:

    #navBar { 
background-image: url('transp50.png'); 
min-width: 100%; 
height: 50px; 
} 
    #logo { 
float: left; 
width: 200px; 
} 
    #navmenu { 
margin: 0 auto; 
width: 350px; 
}

The only thing that doesn't work with this, is that when I resize the window to be small the menu elements get knocked onto another line. 唯一与此不起作用的是,当我将窗口调整为较小的尺寸时,菜单元素会敲到另一行。 Anyway to prevent this? 无论如何要防止这种情况?

Have the logo float to the left side, and then it will not interfere with the width of the Nav bar. 将徽标浮动到左侧,这样就不会干扰导航栏的宽度。 Your LOGO image should be inside the NAV bar element and float:left, set the width, but ensure that the min-width of the nav bar is equal (or greater) to the width of the image and the nav menu. 您的LOGO图像应位于导航栏元素内,并浮动:左,设置宽度,但要确保导航栏的最小宽度等于(或大于)图像和导航菜单的宽度。

Your picture is awesome. 你的照片很棒。

Pseudo code / HTML5: 伪代码/ HTML5:

<nav>

<a>menu element</a>
...
<a>menu-element</a>
</nav>
<img id="logo" src="..." width="120">

CSS: CSS:

nav {
width:100%;
text-align:center;
padding-left:120px; // the same width as the logo to prevent overlap 
}
nav > a {
display:inline-block;
}
#logo {
float:left;
width:120px;
}
#logo::after {
clear:both; //clears the floating element space.
}

There are various guides about working this better to ensure that the nav elements Never move within the width of the logo, as in, the nav elements always stay 120px away from the left hand side of the screen, but this is a starting point for you 有多种方法可以更好地确保导航元素永远不会在徽标的宽度内移动,例如,导航元素始终与屏幕左侧保持120px的距离,但这是您的起点

ANSWER PART 2 答案第二部分

The only thing that doesn't work with this, is that when I resize the window to be small the menu elements get knocked onto another line. 唯一与此不起作用的是,当我将窗口调整为较小的尺寸时,菜单元素会敲到另一行。 Anyway to prevent this? 无论如何要防止这种情况?

Without seeing your HTML for this section, I am guessing you need to use min-width to stop the #navmenu section shrinking. 在没有看到此部分的HTML的情况下,我猜测您需要使用min-width来停止#navmenu部分的缩小。

so judging that the 350px is your minimum working width for that #navmenu add: 因此,请判断350px是该#navmenu添加的最小工作宽度:

#navBar {
min-width:550px; //200+350
}

You can do this: 你可以这样做:

<div id="navbar">
<img id="mylogo" />
<div id="nav"></div>
</div>

some css that may help: 一些CSS可能会有所帮助:

#navbar {
  // should not require anything in particular
}

#nav {
  width: 200px;
  margin-left: auto;
  margin-right: auto;
}

#mylogo {
  float: left; // won't increase the bar size will stay on the left
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM