简体   繁体   English

将跨度内的图像对齐到表格单元格的右侧

[英]Align image within a span to be to the right of a table cell

I made a JS Fiddle to show you what is currently happening.我制作了一个 JS Fiddle 来向您展示当前正在发生的事情。

https://jsfiddle.net/70z5Lm2r/2/ https://jsfiddle.net/70z5Lm2r/2/

HTML HTML

<table>
    <tbody>
        <tr>
            <td class="first">

            </td>
            <td class="second">
                <span>
                    <span onClick={doSomething} class="underline-hover">
                        <img src="pic.png" />
                        This is some clickable data. Sometimes it overflows.
                    </span>

                </span>
            </td>
        </tr>
    </tbody>
</table>

CSS CSS

table {
    table-layout: fixed;
    word-wrap: break-word;
    width: 50%;
    max-width: 100%;
}

td {
    border: 1px solid black;
}

.first {
    width: 40%;
}

.second {
    width: 60%;
}

.underline-hover:hover {
    text-decoration: underline;
    cursor: pointer;
}

img {
    height: 20px;
    width: 20px;
    margin-top: 2px;
    margin-left: 8px;
    vertical-align: top;
    float: right;
}

As you can see, if you hover over the user icon, the underline affect happens on the text.如您所见,如果您将鼠标悬停在用户图标上,下划线会影响文本。 This is because both the text and the image are within the span that has the underline-hover class and the onClick handler applied.这是因为文本和图像都内的span具有underline-hover类和onClick应用处理器。

What I want to have happen is that when a hover happens on the text, only the text is highlighted.我想要发生的是,当悬停在文本上时,只有文本被突出显示。 When a hover happens on the icon, nothing happens.当悬停在图标上时,什么也没有发生。 However, I still want the icon to be flush to the right and the text to wrap accordingly as it currently is displayed.但是,我仍然希望图标向右齐平,并且文本在当前显示时相应地换行。

What I have tried is taking the img tag out of the inner span like so:我所尝试的是将img标签从内部span取出,如下所示:

<span>
    <span onClick={doSomething} class="underline-hover">
        This is some clickable data. Sometimes it overflows.
    </span>
    <img src="pic.png" />
</span>

This satisfies the behavior but the float property of the img tag causes it to be removed from the flow of the page, causing the text and icon to jumbled together.这满足行为,但img标签的float属性导致它从页面流中移除,导致文本和图标混杂在一起。

Any help achieving the behavior and look that I want would be greatly appreciated.任何帮助实现我想要的行为和外观都将不胜感激。

Your attempt was very close.你的尝试非常接近。 Because you're floating the img right, it needs to be before the <span> .因为您将img正确浮动,所以它需要在<span>之前。

<span>
    <img src="pic.png" />
    <span onClick={doSomething} class="underline-hover">
        This is some clickable data. Sometimes it overflows.
    </span>
</span>

Next, make a style for .underline-hover with a margin-right:20px;接下来,为.underline-hover设置一个margin-right:20px;的样式margin-right:20px; Currently the span fills the width of it's container, so the img isn't triggering the hover, the span is.目前跨度填充了它的容器的宽度,所以 img 不会触发悬停,跨度是。 We create a margin the same width as the floated img, and you should be good to go.我们创建一个与浮动 img 宽度相同的边距,你应该很高兴。

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

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