【发布时间】:2017-09-09 15:08:02
【问题描述】:
我现在正在学习 Web 开发几个月,我通常在前端和 UI 布局方面遇到一些问题。我经常很难将元素放置在我想要的位置。在这种情况下,要么我使用相对值并破坏网站的响应能力,要么我编写一些在我看来像是 hack 的规则。
例如,让我们考虑这张图片:
如您所见,有一个 Bootstrap 容器,全角背景色,容器内部有两个经典元素,外部有一个图像。
对于这种布局,我会执行以下操作:
<!-- /* MAIN WRAPPER -->
<div class="pull-right">
<img src="/img/topright_image.PNG" alt="shape">
</div>
<div class="bg-red"> <!-- Red background color. -->
<div class="container">
<header class="row">
<div class="hidden-sm hidden-xs col-sm-2" id="logo"> <!-- I'm using Bootstrap 3, IIRC there's a better way to do that in Bootstrap 4. -->
<img src="/img/logo.PNG" alt="logo">
</div>
<div class="col-sm-6 col-sm-push-3" id="title"> <!-- First difficulty, how to make sure the title will always be centered without being relative to the logo and no matter its content? -->
<h1>Centered title</h1>
</div>
</header>
</div>
</div>
<div class="bg-green"> <!-- Multiple containers, just to have colored backgrounds at 100% width of the page. -->
<div class="container">
<section></section>
</div>
</div>
<!-- MAIN WRAPPER */ -->
这是一个快速的草稿,但你明白了。然后 CSS 将为标题和部分(300 像素和 400 像素)实现任意高度,然后是容器的最大宽度。
如何正确地做到这一点?
(如果我想让徽标略高于标题;在两行之间怎么办?)
【问题讨论】:
-
根据您需要支持的内容以及支持的严格程度,Grid Layout 是一个不错的选择。
标签: html css twitter-bootstrap user-interface layout