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?

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

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;
}

输出结果图片:
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?

英文:

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:
    &lt;!-- Header image and information --&gt;
&lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;row&quot;&gt;
              &lt;div class=&quot;col-md-4 text-center&quot;&gt;
                    &lt;img id=&quot;myPic&quot; src=&quot;images/Me.jpg&quot; alt=&quot;Me&quot; class=&quot;img-fluid&quot;&gt;
              &lt;/div&gt;
              &lt;div class=&quot;col-md-8 text-center&quot;&gt;
                     &lt;h1&gt;Somebody Jone| Does some crazy stuff!&lt;/h1&gt;
              &lt;/div&gt;
        &lt;/div&gt;
  &lt;/div&gt;

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:

&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;d-flex text-center&quot;&gt;
            &lt;img id=&quot;myPic&quot; src=&quot;./me.png&quot; alt=&quot;Me&quot; class=&quot;img-fluid&quot;&gt;
            &lt;h1&gt;Somebody Jone| Does some crazy stuff!&lt;/h1&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

If you want the image and text to be in the middle you can add justify-content-center like this:

&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;d-flex justify-content-center text-center&quot;&gt;
            &lt;img id=&quot;myPic&quot; src=&quot;./me.png&quot; alt=&quot;Me&quot; class=&quot;img-fluid&quot;&gt;
            &lt;h1&gt;Somebody Jone| Does some crazy stuff!&lt;/h1&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

Is that what you wanted it to look like?

huangapple
  • 本文由 发表于 2023年8月9日 07:38:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863739.html
匿名

发表评论

匿名网友

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

确定