英文:
How can I put my picture and my header side by side on the top of the page, but without a huge gap between the two using HTML, CSS, and bootstrap?
问题
我正在制作一个单页的个人作品集,我想把图片和h1标签放在页面顶部中央的同一水平线上,使得图片稍微下移15像素,h1标签则在图片顶部中间位置。目前我已经接近实现了,但是图片和h1文本之间有一个很大的间隙。以下是我的HTML代码和相应的CSS样式,以及带有图片的输出结果。
HTML代码:
<!-- Header image and information -->
<div class="container">
<div class="row">
<div class="col-md-4 text-center">
<img id="myPic" src="images/Me.jpg" alt="Me" class="img-fluid">
</div>
<div class="col-md-8 text-center">
<h1>Somebody Jone| Does some crazy stuff!</h1>
</div>
</div>
</div>
CSS样式:
h1 {
color: darkslateblue;
margin: 75px 0 0 0;
padding: 0;
}
#myPic {
border: 4px inset darkslategray;
border-radius: 25px;
height: auto;
margin: 25px 0 0 0;
padding: 0;
width: 150px;
}
英文:
I am making a single page portfolio and I want to put the picture and the h1 tag on the same level at the top center of the page so that the image is maybe 15px down and the h1 is beside it about midway down from the top of the picture. What I have thus far is close but there is a huge gap between the image and the h1 text. Below is my html, and the css for it with an image.
HTML:
<!-- Header image and information -->
<div class="container">
<div class="row">
<div class="col-md-4 text-center">
<img id="myPic" src="images/Me.jpg" alt="Me" class="img-fluid">
</div>
<div class="col-md-8 text-center">
<h1>Somebody Jone| Does some crazy stuff!</h1>
</div>
</div>
</div>
CSS:
h1 {
color: darkslateblue;
margin:75px 0 0 0;
padding: 0;
}
#myPic {
border: 4px inset darkslategray;
border-radius: 25px;
height: auto;
margin: 25px 0 0 0;
padding: 0;
width: 150px;
}
Output Image:
[![Output image][1]][1]
[1]: https://i.stack.imgur.com/szQx2.png
答案1
得分: 1
我会使用CSS的flexbox布局:
<div class="container">
<div class="row">
<div class="d-flex text-center">
<img id="myPic" src="./me.png" alt="Me" class="img-fluid">
<h1>Somebody Jone| Does some crazy stuff!</h1>
</div>
</div>
</div>
如果你想让图片和文本居中,你可以添加justify-content-center
,像这样:
<div class="container">
<div class="row">
<div class="d-flex justify-content-center text-center">
<img id="myPic" src="./me.png" alt="Me" class="img-fluid">
<h1>Somebody Jone| Does some crazy stuff!</h1>
</div>
</div>
</div>
这符合你的要求吗?
英文:
I would use css flexbox:
<div class="container">
<div class="row">
<div class="d-flex text-center">
<img id="myPic" src="./me.png" alt="Me" class="img-fluid">
<h1>Somebody Jone| Does some crazy stuff!</h1>
</div>
</div>
</div>
If you want the image and text to be in the middle you can add justify-content-center
like this:
<div class="container">
<div class="row">
<div class="d-flex justify-content-center text-center">
<img id="myPic" src="./me.png" alt="Me" class="img-fluid">
<h1>Somebody Jone| Does some crazy stuff!</h1>
</div>
</div>
</div>
Is that what you wanted it to look like?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论