简体   繁体   English

将h1对齐浮动图像的右侧

[英]Aligning h1 to the right of a floated image

I have the following code: 我有以下代码:

<div>
    <img class="left" src="http://dummyimage.com/64x64/0088cc/ffffff.gif&amp;text=.img">
    <h1>
    Biografie
    </h1>
    <p>
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
    </p>
</div>

And this is the CSS I'm using: 这是我正在使用的CSS:

img { float:left; width:150px; margin-right:1em; margin-top:5px; }
h1 { background:red; }
p { overflow:hidden;}

I'm struggling on how to get the h1-element getting displayed right from the image regarding the margin-right:1em; 我正在努力研究如何从图像右侧显示h1元素margin-right:1em; (like the p-element does) without using padding-left on the h1-element (because the size of the image varies due to responsiveness, etc.). (就像p元素一样)不在h1元素上使用padding-left (因为图像的大小因响应性而变化等)。

Here's a fiddle: https://jsfiddle.net/ukwn43y2/1/ 这是一个小提琴: https//jsfiddle.net/ukwn43y2/1/

And hint on how to do that? 并提示如何做到这一点?

EDIT: I'm looking for a "pure" css-way without editing the html; 编辑:我正在寻找一个“纯粹的”CSS方式而不编辑html; adding a container-div for the text-part is possible, but not what I'm looking for... 为文本部分添加容器div是可能的,但不是我正在寻找的...

You can try just hiding the overflow from the image and the h1 : 您可以尝试隐藏imageh1的溢出:

h1 { background:red; overflow:hidden;}

https://jsfiddle.net/ukwn43y2/5/ https://jsfiddle.net/ukwn43y2/5/

Have you tried setting display:inline; 你试过设置显示:内联; on the h1 element? 在h1元素?

https://jsfiddle.net/tz8fs40x/ https://jsfiddle.net/tz8fs40x/

h1 { background:red; display:inline; }

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

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