简体   繁体   English

将图像放在 h1 旁边

[英]Put image next to h1

How do I put the image next to the h1 text?如何将图像放在 h1 文本旁边? The image on the left and the text on the right.左边是图片,右边是文字。 Thanks in advance!提前致谢!

 img { width: 150px; height: 150px; } h1 { padding: 25px; border: 0px; margin: 0px; height: 30%; width: 70%; }
 <header class="head"> <img src="https://static.nike.com/a/images/f_jpg,q_auto:eco/61b4738b-e1e1-4786-8f6c-26aa0008e80b/swoosh-logo-black.png" class="pic"> <h1 class="home" id="home">Hi</h1>

It may be most efficient to use flexbox to resolve this layout issue.使用 flexbox 来解决这个布局问题可能是最有效的。 Adding display: flex;添加显示:flex; to the parent element will create the row layout you're looking for and some additional CSS properties will center the children elements if desired.到父元素将创建您正在寻找的行布局,如果需要,一些额外的 CSS 属性将使子元素居中。 Check out the complete guide here to really control the position of every element if needed.在此处查看完整指南,以便在需要时真正控制每个元素的位置。

 .head { display: flex; justify-content: center; } img { width: 150px; height: auto; max-width: 100%; } h1 { padding: 25px; border: 0; margin: 0; align-self: center; }
 <header class="head"> <img src="https://static.nike.com/a/images/f_jpg,q_auto:eco/61b4738b-e1e1-4786-8f6c-26aa0008e80b/swoosh-logo-black.png" class="pic"> <h1 class="home" id="home">Hi</h1> </header>

Either add float: right to h1 :float: right添加到h1

 img { width: 150px; height: 150px; } h1 { float: right; padding: 25px; border: 0px; margin: 0px; height: 30%; width: 70%; }
 <header class="head"> <img src="https://static.nike.com/a/images/f_jpg,q_auto:eco/61b4738b-e1e1-4786-8f6c-26aa0008e80b/swoosh-logo-black.png" class="pic"> <h1 class="home" id="home">Hi</h1>

Or display: inline-block to h1 :display: inline-blockh1

 img { width: 150px; height: 150px; } h1 { display: inline-block; padding: 25px; border: 0px; margin: 0px; height: 30%; width: 70%; }
 <header class="head"> <img src="https://static.nike.com/a/images/f_jpg,q_auto:eco/61b4738b-e1e1-4786-8f6c-26aa0008e80b/swoosh-logo-black.png" class="pic"> <h1 class="home" id="home">Hi</h1>

Keep in mind that the image has a fixed width and the text has a relative width, so it will automatically flow over to the next line if the total width allowance is exceeded.请记住,图像具有固定宽度,而文本具有相对宽度,因此如果超出总宽度限制,它将自动流到下一行。 To prevent this, simply set width: fit content on the h1 .为了防止这种情况,只需在h1上设置width: fit content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM