简体   繁体   English

如何将文字和图片垂直居中对齐

[英]How do I center aligned vertically the text with my image

I'm trying to vertically centered align the text with my image. 我正在尝试将文字与图片垂直居中对齐。 Currently, the text looks like it's aligned at the bottom of the image. 当前,文本看起来像在图像底部对齐。

Here's my jsfiddle: http://jsfiddle.net/huskydawgs/L5Le0w37/7/ 这是我的jsfiddle: http : //jsfiddle.net/hus​​kydawgs/L5Le0w37/7/

Here's my HTML: 这是我的HTML:

    <div class="column-resources-box">
    <a href="http://en.wikipedia.org/wiki/Apple"><img alt="Apples" height="50" src="http://www.clipartbest.com/cliparts/acq/ezj/acqezjKcM.jpeg" width="50" /></a>

<h4 class="title-bar">Apple<br>
    Center</h4>

<ul>
    <li>Gala</li>
    <li>Pink Lady</li>
    <li>Fuji</li>
</ul>
</div>

Here's my CSS:

        .column-resources-box {
        width: 200px;
        float: left;
        margin: 15px;
    }

        .column-resources-box img {
    margin:0 2%;
    float:left;
    height:50px;
    width:50px;
}
        }

h4.title-bar {
    color: #2251a4;
    background: none;
    font-family: 'Arial', inherit;
    font-weight: normal;
    text-transform: none;
    line-height: normal;
    margin: 0;
    padding: 0;
    text-align: left;
}

Try this out. 试试看 I wrapped the two items you want centered in the div, and then centered the image. 我包装了两个要在div中居中的项目,然后将图像居中。

 .wrap { display:inline } .apple_image { vertical-align:middle } .column-resources-box { width: 200px; float: left; margin: 15px; } .column-resources-box img { margin:0 2%; float:left; height:50px; width:50px; } } h4.title-bar { color: #2251a4; background: none; font-family: 'Arial', inherit; font-weight: normal; text-transform: none; line-height: normal; margin: 0 0 0 0; padding: 0; text-align: left; } 
 <div class="column-resources-box"> <div class="wrap"> <a class="apple_image" href="http://en.wikipedia.org/wiki/Apple"> <img alt="Apples" height="50" src="http://www.clipartbest.com/cliparts/acq/ezj/acqezjKcM.jpeg" width="50" /> </a> <h4 class="title-bar">AppleCenter</h4> </div> <ul> <li>Gala</li> <li>Pink Lady</li> <li>Fuji</li> </ul> </div> 

There's a syntax error in your CSS. CSS中存在语法错误。 Here's your CSS, excerpted from the top: 这是您的CSS,摘自顶部:

        .column-resources-box {
        width: 200px;
        float: left;
        margin: 15px;
    }

        .column-resources-box img {
    margin:0 2%;
    float:left;
    height:50px;
    width:50px;
}
        }

Notice the extraneous close brace. 注意无关紧要的括号。 That seems to be preventing the browser from getting to the remaining CSS. 这似乎阻止了浏览器访问其余的CSS。

Fixed: http://jsfiddle.net/L5Le0w37/13/ 固定: http : //jsfiddle.net/L5Le0w37/13/

You can move it down a little to center it with position:relative; top:7px; 您可以将其向下移动一点以使其与position:relative; top:7px;居中position:relative; top:7px; position:relative; top:7px; :

Centered: http://jsfiddle.net/L5Le0w37/16/ 居中: http : //jsfiddle.net/L5Le0w37/16/

You could use absolute positioning to put things exactly where you want them. 您可以使用绝对定位将事物准确放置在所需的位置。

Fiddle: http://jsfiddle.net/t8rL871j/ 小提琴: http : //jsfiddle.net/t8rL871j/

  .column-resources-box { width: 200px; float: left; margin: 15px; position: relative; } .column-resources-box img { margin:0 2%; height:50px; width:50px; position: absolute; } h4.title-bar { color: #2251a4; background: none; font-family:'Arial', inherit; font-weight: normal; text-transform: none; line-height: normal; margin: 0 0 0 0; padding: 0; text-align: left; position: absolute; top: 10px; left: 60px; } .column-resources-box ul { margin:60px 2%; height:50px; width:70px; position: absolute; } 
 <div class="column-resources-box"> <a href="http://en.wikipedia.org/wiki/Apple"><img alt="Apples" height="50" src="http://www.clipartbest.com/cliparts/acq/ezj/acqezjKcM.jpeg" width="50" /></a> <h4 class="title-bar">Apple<br> Center</h4> <ul> <li>Gala</li> <li>Pink Lady</li> <li>Fuji</li> </ul> </div> 

I rewrote your code a bit but here's another possible way using top padding.. 我稍微重写了您的代码,但这是使用顶部填充的另一种可能方式。

vertical-align: top;
padding: 4px 0px 0px 0px; /* adjust top padding */

http://jsfiddle.net/Hastig/mj5yzsr7/3/ http://jsfiddle.net/Hastig/mj5yzsr7/3/

You can adjust the spacing between Apple and Center with h4.title-bar { line-height: 25px; } 您可以调整与苹果中心之间的间距h4.title-bar { line-height: 25px; } h4.title-bar { line-height: 25px; } then adjust the top padding to compensate. h4.title-bar { line-height: 25px; }然后调整顶部填充以进行补偿。

Wrap your text and image inside of a div and style it like this: 将您的文本和图像包装在div内,并设置其样式,如下所示:

HTML HTML

<div class="appleWrapper">
    <a href="http://en.wikipedia.org/wiki/Apple"><img alt="Apples" height="50" src="http://www.clipartbest.com/cliparts/acq/ezj/acqezjKcM.jpeg" width="50" /></a>
    <h4 class="title-bar">Apple<br>Center</h4>
</div>

CSS CSS

.appleWrapper {
    height: 50px;
}
.title-bar {
    margin: 0;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

Check out the online example here 在此处查看在线示例

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

相关问题 如何使图像相对于左侧的动态div在中心垂直对齐? - How can I get my image to be vertically aligned in the center with respect to a dynamic div on it's left? 如何将我的项目符号列表移动到中心,同时让它们在 CSS 中垂直对齐? - How do I move my bullet list to the center while having them vertically aligned in CSS? 如何在导航栏中垂直居中显示文本? - How do I Vertically Center Text in My Navigation Bar? 如何首先居中放置图像,然后在该图像的任一侧垂直居中放置文本? - How do I first center an image and then vertically center text on either side of that image? 如何在div中垂直居中放置图像? - How do I vertically center an image in a div? 如何将带有段落的图像垂直居中? - How do I vertically center an image with a paragraph? li跨度的中心背景图像,文本居中并在底部垂直对齐 - center background image of a span in li with text centered and vertically aligned at bottom 为什么我的文本是垂直对齐的,而不是水平对齐的? - How come my text is aligned vertically, but not hoizontally? 如何将Outlook电子邮件中的左对齐文本居中? - How do I Center Left-aligned Text in an Outlook Email? 如何在div的中心向左对齐文本? - How do I have left-aligned text in the center of a div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM