[英]How can I center align an element correctly when it contains a child element floated right?
I have a heading and an image. 我有一个标题和一个图像。 I want them to both appear on the same line as each other.
我希望它们都出现在同一行上。 I want the heading to be aligned center and the image to be aligned to the right.
我希望标题居中对齐,图像要向右对齐。
My attempts so far have resulted in the following: 到目前为止,我的尝试导致了以下结果:
As you can see the heading 'Generate a URL' is pushed slightly to the left due to the image. 如您所见,由于图像的原因,标题“生成URL”略微向左推。 How can I avoid this happening and keep it centered?
如何避免这种情况发生并保持居中?
My HTML mark up is the following: 我的HTML标记如下:
<h1 class="renault-title text-center bottom-margin-med">
Generate a URL
<img class="pull-right" src="{{ asset('bundles/urlBuilder/images/renault_english_logo_desktop.png') }}" style="height:49px;" alt="Renault logo">
</h1>
I am using Twitter Bootstrap. 我正在使用Twitter Bootstrap。 Appreciate any help.
感谢任何帮助。
Easiest way is to place img
with position: absolute
. 最简单的方法是将
img
放置在position: absolute
。
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'); h1 { position: relative; padding: 0 170px; // Add indents equal to image width to avoid text overlapping. font-size: 24px; } h1 img { position: absolute; right: 0; top: 0; }
<h1 class="renault-title text-center bottom-margin-med"> Generate a URL <img src="http://placehold.it/125x49/ffcc00/" style="height:49px;" alt="Renault logo"> </h1>
Depending on what the rest of your HTML looks like, you could use columns. 根据其余HTML的外观,您可以使用列。 CodePen .
CodePen 。
<div class="col-md-8 col-md-offset-2">
<h1 class="renault-title text-center bottom-margin-med">
Generate a URL
</h1>
</div>
<div class="col-md-2">
<img class="pull-right" src="{{ asset('bundles/urlBuilder/images/renault_english_logo_desktop.png') }}" style="height:49px;" alt="Renault logo">
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.