将姓名和职位以两行显示在网页上的最佳方式是使用HTML和CSS。

huangapple go评论121阅读模式
英文:

What is the best way to put name and job title on two separate lines on a webpage using HTML, CSS?

问题

以下是已翻译的内容:

.landing {
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="landing">
   <div class="landing1">HasanMirza</div>
   <div class="landing2">Web De<span class="v">v</span>eloper</div>
</div>
英文:

What is the best way to put my name and job title centered vertically and horizontally on a webpage for my own website. I want to have my name with Web Developer underneath it.

I used this method:

.landing{

height: 100vh;
width: 100vw;
display: flex;
align-items: center;
justify-content: center;

}

This works but doesnt take into account a position:fixed border with height=60px into account (it doesn't come out totally vertically centered).

Is there another way to centred these items regardless of additional elements on the page? (margin: auto also creates the same problem as above).

In addition, how should the html layout look, currently:

&lt;div class=&quot;landing&quot;&gt;
   &lt;div class=&quot;landing1&quot;&gt;HasanMirza&lt;/div&gt;
   &lt;div class=&quot;landing2&quot;&gt;Web De&lt;span class=&quot;v&quot;&gt;v&lt;/span&gt;eloper&lt;/div&gt;
&lt;/div&gt;

(the 'v' is rotated to look like an angle bracket, just an effect I am trying to achieve).

答案1

得分: 2

如果您想在视口中无论其他元素如何都将某物定位在固定位置,请使用 position: fixed。Grid 允许您轻松地将项目放置在中心位置。请参阅下面的代码:

  • 有关定位的信息请参考 css tricks
  • 如何使用网格进行居中对齐请参考 W3docs.com
body {
  margin: 0; /* 如果不将其设置为零,则项目将具有 8px 的偏移 */
}

.landing {
  position: fixed;
  height: 100vh;
  width: 100vw;
  display: grid;
  place-content: center;
}
<div class="landing">
  <div class="landing1">HasanMirza</div>
  <div class="landing2">Web Developer</div>
</div>
英文:

If you want to position anything in your viewport in a fixed position irrespective of any other element then use position: fixed. Grid allows you to place items in the center really easily. See code below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  margin:0; /* the items will have an 8px offset if this isn&#39;t set to zero */
}

.landing {
  position: fixed;
  height: 100vh;
  width: 100vw;
  display: grid;
  place-content: center;
}

<!-- language: lang-html -->

&lt;div class=&quot;landing&quot;&gt;
   &lt;div class=&quot;landing1&quot;&gt;HasanMirza&lt;/div&gt;
   &lt;div class=&quot;landing2&quot;&gt;Web De&lt;span class=&quot;v&quot;&gt;v&lt;/span&gt;eloper&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 15:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定