简体   繁体   English

如何在同一元素中居中放置文本和右对齐图像?

[英]How can I have centered text and right-aligned image in the same element?

I have some th elements with text in them that should be centered and they also contain images: 我有一些th在他们的文本元素应该居中,它们也包含图片:

替代文字

The up/down arrow graphic is a separate image. 向上/向下箭头图形是单独的图像。

My question is, what is the simplest reliable way to position that image over to the right side of the th element while keeping the text centered? 我的问题是,在保持文本居中的同时将图像定位在th元素右侧的最简单可靠的方法是什么?

I'm open to using jQuery/JavaScript if there's a reasonably simple way to do it. 如果有相当简单的方法,我愿意使用jQuery / JavaScript。

One caveat: I need the up/down graphic to be a separate image, not part of the header background. 一个警告:我需要上下图形作为单独的图像,而不是页眉背景的一部分。

        <th> 
          Title
          <img src='/images/sort_unsorted.jpg' /> 
        </th>

Just float the image to the right: 只需将图像向右浮动:

th img {
    float: right;
}

DEMO: http://jsfiddle.net/marcuswhybrow/ZxUMY/ 演示: http : //jsfiddle.net/marcuswhybrow/ZxUMY/

Exactly how did you want the text to be centered, equidistant from the left/right edges of the th or from the left edge of the th to the left side of the image? 究竟你是怎么想的文本为中心,由左/右边缘等距th或从左侧边缘th到图像的左侧? There is a small difference between the two since your image is relatively small, but still a discernible difference. 两者之间的差异很小,因为您的图像相对较小,但仍然可以识别。

Equidistant to image, the solution is easy (as Marcus answered): 与图像等距,解决方案很容易(如Marcus回答):

th img { float:right; }

Equidistant to th edges requires a bit more, since you need to break the image out of the page flow: 等距th边缘需要多一点的,因为你需要打破图像出来的页面流:

th {
  display:block;
  position:relative;
  text-align:center;
}
th img {
  position:absolute;
  right:0;
  /* optional, for vertically centering the image
  top:50%;
  margin-top:-[image height/2]px;
  */
}

EDIT: fixed for positioning in a table cell (thanks Brandon), and here's a simple fiddle: http://jsfiddle.net/brillyfresh/4LDZ9/ 编辑:固定在表单元格中的位置(感谢布兰登),这是一个简单的小提琴: http : //jsfiddle.net/brillyfresh/4LDZ9/

To get a good handle on positioning (if you don't want to use a background image) you'll have to wrap the content in a block level element, like a DIV. 为了更好地定位(如果您不想使用背景图像),您必须将内容包装在块级元素中,例如DIV。 You can't use position: absolute; 您不能使用position: absolute; on child elements of a table cell. 在表格单元格的子元素上。

So, I recommend something like this: 所以,我推荐这样的东西:

HTML: HTML:

<table cellpadding="0" cellspacing="0">
    <thead>
        <th>
            <div class="positioning">
                Left
                <div class="img">&nbsp;</div>
            </div>
        </th>
        <th>
            <div class="positioning">
                Center
                <div class="img">&nbsp;</div>
            </div>
        </th>
        <th>
            <div class="positioning">
                Right
                <div class="img">&nbsp;</div>
            </div>
        </th>
    </thead>
    <tbody>
        <td>Left</td>
        <td>Center</td>
        <td>Right</td>
    </tbody>
</table>

Imagine <div class="img">&nbsp;</div> is just a 12px x 12px image. 想象<div class="img">&nbsp;</div>只是一个12px x 12px的图像。

CSS 的CSS

table {
    border: 10px solid rgb(230,230,230);
    color: white;
}
table thead th,
table tbody td {
    background: black;
    border: solid 1px white;
    font: bold 12px Helvetica, Arial, sans-serif;
    padding: 10px;
}
table thead th {
    padding: 0;
    text-align: center;
}
table thead .positioning {
    padding: 10px 32px;
    position: relative;
}
table thead .img {
    background: red;
    height: 12px;
    margin-top: -6px;
    position: absolute;
    right: 10px;
    top: 50%;
    width: 12px;
}

The important thing to remember here is that you give the left and right sides of the positioning div enough padding to make room for the image (ie: padding: 10px 32px; ). 这里要记住的重要一点是,您要给positioning div的左侧和右侧提供足够的填充,以为图像腾出空间(即: padding: 10px 32px; )。 Otherwise the image will overlap your text. 否则,图像将与您的文本重叠。

Here's a fiddle: http://jsfiddle.net/brandondurham/nXQeR/ 这是一个小提琴: http : //jsfiddle.net/brandondurham/nXQeR/

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

相关问题 将文字右对齐 - Put the text right-aligned 如果标题包含右对齐的文本,则jQueryUI对话框将隐藏关闭(X)按钮。 我怎样才能解决这个问题? - jQueryUI dialog hides close (X) button if title contains right-aligned text. How can I fix this? 缩放和居中不同尺寸的图像,在 CSS 中使用右对齐的文本 - Scaling and centering image of varying dimensions, with right-aligned text in CSS 半居中,输入框右对齐,文本左对齐 - How do a semi center, with input boxes right-aligned and text left-aligned Items flex item左对齐和右对齐,当浏览器window缩小时如何居中? - Items flex item is left-aligned and right-aligned, how to center it when the browser window is resized down? 如何使底部工具提示与其元素的右端对齐? - How can I keep a bottom tooltip aligned with the right end of its element? 我怎样才能在中间有一个文本并且文本是右对齐的 - how can i have a text in the middle and text that is right align 我想从同一个 position 开始文本,并且连续右对齐 - I want to start text from same position and also right aligned in a row 如何将一些文本对齐在textarea中右对齐? - How to align some text as right aligned in textarea? 输入元素内的左右对齐文本(名称:值)+灵活 - left and right aligned text inside input element (name: value) + flexible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM