英文:
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:
<div class="landing">
<div class="landing1">HasanMirza</div>
<div class="landing2">Web De<span class="v">v</span>eloper</div>
</div>
(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:
- Info on positioning on css tricks
- How to center using grid W3docs.com
<!-- 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't set to zero */
}
.landing {
position: fixed;
height: 100vh;
width: 100vw;
display: grid;
place-content: center;
}
<!-- language: lang-html -->
<div class="landing">
<div class="landing1">HasanMirza</div>
<div class="landing2">Web De<span class="v">v</span>eloper</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论