简体   繁体   English

将div内的文字和图片居中

[英]Center a text inside a div with an image also

I'm developing a web site and I to show something like this: 我正在开发一个网站,并展示如下内容:

在此处输入图片说明

My current html code is this: 我当前的html代码是这样的:

<body>
    <div id="about">
        <img src="images/aboutCardIcon.png"/>
        <span>About</span>
    </div>
</body>

Is this html code correct? 这个HTML代码正确吗? Can I use span tag? 我可以使用跨度标签吗?

How can I center in the middle About text? 如何在“关于文本”中间居中?

I would use the <figure> and <figcaption> elements which are perfect for what you're trying (html5). 我会使用<figure><figcaption>元素,这些元素非常适合您尝试的内容(html5)。

Here's an example 这是一个例子

The code used 使用的代码

HTML 的HTML

<figure>
    <img src="image.jpg" alt="Potato">
    <figcaption>A Potato</figcaption>
</figure>

CSS 的CSS

figure {
    border:     1px solid black;
    display:    inline-block;
    text-align: center;
    padding:    20px;
}

figure img {
    float:  left;
    height: 126px;
}

figcaption {
    float:       right;
    height:      126px;
    line-height: 126px;
    margin-left: 50px;
    font-family: Arial, sans-serif;
    font-size:   300%;
}

Most of the CSS is purely cosmetic, the important parts are the floats places on the image and the <figcaption> , and the set height and line-height on the <figcaption> . 大部分的CSS的纯粹是化妆品中,重要的部分是在图像上的漂浮位置和<figcaption>和设定高度和行高的<figcaption>

PS the reason I made it white and not black like your example, is because the image I chose had a black background, and I didn't want it to look hideous. PS我之所以将其设置为白色而不是黑色的原因是因为我选择的图像具有黑色背景,并且我不希望它看起来很丑。 :) :)

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

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