html - 最好的布置方式是什么?水平规则?

标签 html css layout rule

我正在尝试将水平线与菜单中的白线对齐。我希望在不同屏幕上查看时保持对齐。我这样做的最佳选择是什么?它需要看起来像的图片:

Screenshot

* {
  margin: 0;
}
@font-face {
  font-family: jaapokkisubtract;
  src: url('jaapokkisubtract.ttf');
}
body {
  background-color: #ca3600;
}
#head {
  height: 65px;
  border-bottom: 3px solid white;
  float: right;
  width: 51%;
}
h1 {
  color: white;
  margin: 10px 0 0 10px;
  font-family: jaapokkisubtract;
  font-size: 50px;
  float: left;
}
#work_btn {
  display: block;
  width: 96px;
  height: 68px;
  background: url(http://i.imgur.com/7m1Eh9j.gif) no-repeat 0 0;
  overflow: hidden;
  text-indent: -9999px;
  float: right;
}
#work_btn:hover {
  background-position: 0 -68px;
}
#resume_btn {
  display: block;
  width: 125px;
  height: 68px;
  background: url(http://i.imgur.com/x2eaW4T.gif) no-repeat 0 0;
  overflow: hidden;
  text-indent: -9999px;
  float: right
}
#resume_btn:hover {
  background-position: 0 -68px;
}
 <h1>Alexander</h1>
<div id="menu">
  <a id="resume_btn" href="resume.html" title="Resume">Resume</a>
  <a id="work_btn" href="index.html" title="Work">Work</a>
  <div id="head"></div>
</div>

最佳答案

您可以通过稍微修改 CSS 和 HTML 代码并使用转换将菜单项移动到屏幕中央来实现这一点。

为此你需要:

  • 用底部边框包裹 div 中的所有内容(例如:#head)
  • 将页面标题 (h1) float 到左侧(尽管将其位置更改为 absolute 可能会更好,否则它可能会影响菜单链接)<
  • 将所有导航元素包裹在一个 div 中(例如:#menu),绝对位置位于 #head 的中心(left:50% )
  • 转换 #menu div 以将其宽度的 50% 向左移动。这可以通过将其添加到其样式来实现:

    transform:translate(-50%, 0%)
    

您可以在此处查看演示:http://jsfiddle.net/o4ff4thc/或以下:

* {
    margin: 0;
}
@font-face {
    font-family: jaapokkisubtract;
    src: url('jaapokkisubtract.ttf');
}
body {
    background-color: #ca3600;
}
#head {
    height: 65px;
    border-bottom: 3px solid white;
}
h1 {
    color: white;
    margin: 10px 0 0 10px;
    font-family: jaapokkisubtract;
    font-size: 50px;
    float: left;
}
#work_btn {
    display: inline-block;
    width: 96px;
    height: 68px;
    background: url(http://i.imgur.com/7m1Eh9j.gif) no-repeat 0 0;
    overflow: hidden;
    text-indent: -9999px;
}
#work_btn:hover {
    background-position: 0 -68px;
}
#resume_btn {
    display:inline-block;
    width: 125px;
    height: 68px;
    background: url(http://i.imgur.com/x2eaW4T.gif) no-repeat 0 0;
    overflow: hidden;
    text-indent: -9999px;
}
#resume_btn:hover {
    background-position: 0 -68px;
}

#menu {
    position:absolute;
    left:50%;
    transform:translate(-50%,0%);
    height:20px;
    width:245px;
}
<div id="head">
     <h1>Alexander</h1>

    <div id="menu"> 
        <a id="resume_btn" href="resume.html" title="Resume">Resume</a>
        <a id="work_btn" href="index.html" title="Work">Work</a>
    </div>
</div>

关于html - 最好的布置方式是什么?水平规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29503879/

相关文章:

css - <HTML> 缩略图翻转图像仅出现在屏幕左侧

html - 绘制路径点 html canvas

android - 使用两个 TextView 进行布局

layout - Flutter 中的基本对齐文本和虚线

html - 使用左右 CSS 属性绝对定位 INPUT 时的奇怪行为

javascript - 所有人评级的可访问属性

html - 悬停元素改变其他元素

html - 自动换行时无法强制与 float 元素右对齐

html - 如果屏幕小于 768px,则将文本居中对齐

PHP 用户样式不符合现有样式