html - 水平对齐图像和段落

标签 html css alignment

我正在尝试将我的图像与网页中的标题和副标题对齐。我希望图像位于左侧,标题及其副标题位于中间。

 <header>
    <div class="title_div">
        <img src="pictures/logo.png" alt="logo"/>
        <p>
           <h1>Title</h1>
           <h2>Subtitle</h2>
        </p>
     </div>
 </header>

我尝试使用 float:left 属性,但图像从标题中消失了......我看到了,因为我使用了以下 css 属性:

header{
    background:#f1f1f1;
    border: black solid;
}

我想达到类似 https://www.ted.com/ 的目的但在“值得传播的想法”下有一个副标题。

最佳答案

为您的 Logo 和文本使用单独的 div 使其更清晰:

<header>
<div class="logo">
  <img src="pictures/logo.png" alt="logo"/>
</div>

<div class="title_div">
 <h1>Title</h1>
 <h2>Subtitle</h2>
</div>
</header>

CSS:

header{
 float:left;
}

.logo{
 float:left;
 margin:20px;
}
.title_div{
 float:left;
}

注意:请注意,您对 HTML 的使用非常不当。 P 标签用于段落,H1 用于标题,因此您不能将标题放在P 标签内。重新构建您的 HTML。

关于html - 水平对齐图像和段落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35139082/

相关文章:

javascript - 当所有按钮具有相同值时如何检查单击了哪个按钮

javascript - 调用 onBlur 时 relatedTarget 在 Firefox 中为 null

html - CSS 网格为每个 html 标签创建一个新框

android - ImageView 和 TextView float (左/右)Android

html - WebRTC:数据通道丢弃消息?

html - 如何在图像的同一行显示<p>

javascript - 一个函数不与 jQuery 无限轮播构建一起运行

html - 悬停效果不能通过使用选择器应用于上一个

ios - 文本字段中的对齐不起作用

xcode - 如何在 Xcode 中获取智能选项卡 ("indent with tabs, align with spaces") 行为?