【问题标题】:How to make a heading with image and text horizontally center?如何使图像和文本水平居中的标题?
【发布时间】:2016-03-09 19:17:16
【问题描述】:

我有一张图片和一段文字。我需要文本应该连续跟随在图像之后。 并像 [image]Time tracker 一样将两者并排对齐后,使两个内容在屏幕的水平中心作为当前页面的标题。 下面给出了我尝试做的事情。

 <div class="row">
      <div class="col-80 col-offset-10">

          <div class="appheadingcenter">

            <img src="img/clock.png" alt="clock" class="clockwidth"></img>
            <div class="timesheettext2">
              <h1 class="timesheettext">
                <b>Timesheet Tracking</b>
              </h1>
            </div>
          </div>

        </div>

        </div>

【问题讨论】:

  • 我建议你创建一个 jsfiddle 或类似的东西,在那里你可以轻松地使用 css 来实现你想要的那些东西..

标签: html css ionic-framework


【解决方案1】:

<div class="appheadingcenter" style="margin:auto;width:454px;">

                <img src="https://upload.wikimedia.org/wikipedia/commons/3/33/Vanamo_Logo.png" 
        alt="clock" class="clockwidth" style="float: left;max-width: 100px;max-height: 100px;">
                <div class="timesheettext2" style="width: 341px;float: left;">
                  <h1 class="timesheettext">
                    <b>Timesheet Tracking</b>
                  </h1>
                </div>
    </div>
	

更新答案

如果您不想固定宽度,请使用以下解决方案

<div class="appheadingcenter" style="text-align:center;">

      <div style="display: inline-block;">
                <img src="https://upload.wikimedia.org/wikipedia/commons/3/33/Vanamo_Logo.png" 
        alt="clock" class="clockwidth" style="float: left;max-width: 100px;max-height: 100px;">
                <div class="timesheettext2" style="width: 341px;float: left;">
                  <h1 class="timesheettext">
                    <b>Timesheet Tracking</b>
                  </h1>
                </div>
              </div>
    </div>

【讨论】:

  • 我不认为将宽度设置为 454 px 是一个好主意,我认为它不会在每个设备中提供精确的中心。
  • 这背后的逻辑是什么?你怎么能确定它的绝对中心?
  • 在我的解决方案中,text-align:center; 样式负责将所有内容放在中心。但它不适用于普通 div,所以我在其子 div 中添加了 display: inline-block; 以应用中心样式。而且我还在图像和文本上应用了float:left; 样式,以便两者都在同一行。
【解决方案2】:

这是一种使用display: flex;的技术

注意:点击整页查看此演示的大图

	.header-frame {
		display: flex;
		align-items: center;
        top: 50%;
        left: 50%;
        position: absolute;
        transform: translate(-50%, -50%);
	}

	.image-frame {
		display: inline-block;
		background: url("https://bobagaming.files.wordpress.com/2015/10/league-of-legends-world-championship-logo-eps-vector-image.png") no-repeat center;
		width: calc(4vw + 4vh);
		height: calc(4vw + 4vh);
		background-size: cover;
	}

	.text-frame {
		font-family: 'Arial';
		font-size: calc(2.5vw + 2.5vh);
		display: inline-block;
		margin: 1vh 1vw;
	}
<div class="header-frame">
	<span class="image-frame"></span>
	<h3 class="text-frame">World Championships</h3>
</div>

【讨论】:

  • 使其恰好位于屏幕中心
  • 立即查看。我将它准确地放在屏幕上(x = 50,y = 50),对吗?或者你希望它居中于顶部 (x = 50, y = 0) ?
【解决方案3】:

只需给出以下 css。 display:flex 将图像和文本并排放置,justify-content: center; 将使其水平居中。

.appheadingcenter {
    display: flex;
    justify-content: center;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 2013-05-18
相关资源
最近更新 更多