简体   繁体   English

如何使用CSS内联元素

[英]How to inline elements with CSS

I'm trying to make menubar at the top of my website. 我正试图在我的网站顶部制作菜单栏。 It should looke like this: 它应该像这样说: 在此输入图像描述

The red square is my button. 红色方块是我的按钮。

My problem is that my headline and my button are not in the same line. 我的问题是我的标题和我的按钮不在同一行。 So I tried to use a table but then there are both aligned to the left. 所以我尝试使用一个表,但是两个都对齐了左边。 After that I used float: right; 之后我使用了float: right; for my button. 为我的按钮。 It is now aligned right but in the next line. 它现在正确对齐,但在下一行。

How can I fix it so my button and my headline are in the same line and aligned like my picture. 我怎样才能修复它,这样我的按钮和标题就在同一条线上,并且像我的照片一样对齐。

HTML: HTML:

<div id="topbar">
   <h1>Fahrplan</h1>
   <button type="button" id="settings"></button>
</div>

CSS: CSS:

h1 {
  height: 44px;
  margin: 0;
  color:#FFFFFF;
  text-align: center;
  font-family: Helvetica, sans-serif;
  font-size: 16px; 
  line-height: 44px;
}
#topbar button {
  height: 20px;
  width: 20px;
  float: right;
}

For this kind of scenarios, you might consider using position s. 对于这种情况,您可以考虑使用position s。

 #topbar { position: relative; } h1 { margin: 0; color:#FFFFFF; text-align: center; font-family: Helvetica, sans-serif; font-size: 16px; line-height: 44px; background: #99f; } #topbar button { height: 20px; width: 20px; position: absolute; top: 50%; right: 5px; margin-top: -10px; } 
 <div id="topbar"> <h1>Fahrplan</h1> <button type="button" id="settings"></button> </div> 

Here I have given position to both #topbar and the button. 在这里,我已经给position#topbar和按钮。 The #topbar has a relative position and button has an absolute position: #topbar有一个relative位置,按钮有一个absolute位置:

#topbar {
  position: relative;
}
button {
  position: absolute;
  top: 50%;
  right: 5px;
  margin-top: -10px;
}

And I have also adjusted the button to be vertically centred by using the negative margin of half the height. 而且我还使用一半高度的负边距将按钮调整为垂直居中。 Hope this helps. 希望这可以帮助。

I would rather suggest you to use absolute along with translateY() to align your button vertically middle. 我宁愿建议你使用absolutetranslateY()来垂直对齐你的按钮。

Demo (Note: Am using SCSS on jsFiddle so don't get confused with the syntax) 演示 (注意:我在jsFiddle上使用SCSS,所以不要混淆语法)

header {
  height: 40px;
  background: tomato;
  position: relative;

  h4 {
    line-height: 40px;
    text-align: center;
  }

  button {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: 10px;
  }
}

Explanation: 说明:

I am using position: absolute; 我使用的position: absolute; to move your button to the right. 将按钮向右移动。 As far as vertical centering goes for your button, you can use top: 50% and transform to nudge your button exactly in the middle of your header vertically. 对于按钮的垂直居中,您可以使用top: 50%transform为在标题中间垂直轻推按钮。 It will always stay vertically centered without you declaring any static height . 它将始终保持垂直居中,而不会声明任何静态height

For your interest, here's how to do it with inline-blocks. 为了您的兴趣,这里是如何使用内联块。

 div > * { display:inline-block; vertical-align:middle; } h1 { width:100%; text-align:center; margin-right:-40px; background-color:#4F81BD; color:#FFF; font-family: Helvetica, sans-serif; font-size: 16px; height: 44px; } button { height: 30px; width: 30px; margin-right:-6px; border: 3px solid #8C3836; border-radius:5px; background-color:#C0504D; } 
 <div id="topbar"> <h1>Fahrplan</h1> <button type="button" id="settings"></button> </div> 

Here is a quick demo of how to do this using http://tachyons.io 以下是使用http://tachyons.io执行此操作的快速演示

<div class="bg-light-gray dt w-100">
  <div class="dtc v-mid w3"></div>
  <div class="dtc v-mid tc pv3">
    <h1 class="mv0 f5">Headline</h1>
  </div>
  <div class="dtc v-mid tr w3 pr2">
    <button class="bg-black br2 h2 w2"></button>
  </div>
</div>

CSS: .br2 { border-radius: .25rem; CSS:.br2 {border-radius:.25rem; } }

.dt {
  display: table;
}

.dtc {
  display: table-cell;
}

.h2 {
  height: 2rem;
}

.w2 {
  width: 2rem;
}

.w3 {
  width: 4rem;
}

.w-100 {
  width: 100%;
}

.bg-black {
  background-color: #111;
}

.bg-light-gray {
  background-color: #eee;
}

.pr2 {
  padding-right: .5rem;
}

.pv3 {
  padding-top: 1rem;
  padding-bottom: 1rem;
}

.mv0 {
  margin-top: 0;
  margin-bottom: 0rem;
}

.tr {
  text-align: right;
}

.tc {
  text-align: center;
}

.f5 {
  font-size: 1rem;
}

.v-mid {
  vertical-align: middle;
}

Demo: https://jsfiddle.net/r21mdrzs/ 演示: https//jsfiddle.net/r21mdrzs/

The downside is that you include an empty div. 缺点是你包括一个空div。 The plus side is that even if you zoom in or out, change font-size or size of button, everything will always be aligned to the middle, not matter what. 好的一面是,即使你放大或缩小,改变按钮的字体大小或大小,一切都将始终与中间对齐,无论如何。 This is in my experience is less brittle than using magic number values for positioning. 根据我的经验,这比使用幻数值进行定位要脆弱一些。

Give your headline float property; 给你的标题浮动财产;

.classNameGiveToHeading {
  float: left;
}
.buttonClassName {
  float: right;
}

OR give "float:right" to both of them as you like. 或者根据自己的喜好给他们两个“浮动:正确”。

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

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