CSS 背景图片放置

标签 css background-image

我这里有这个网站http://cowelllaserhair.com/index.php你会注意到有一个段落,周围环绕着一个 div,就像这样......

<div style="background-image:url(/images/newLocation.jpg);">
<p style="text-align:center;">
Text here
</p>
</div>

但它切断了图像,我也希望图像更高 like this

这可能吗?我该怎么做呢?

解决了图像被截断的问题,方法是在我的 div 中添加图像高度和背景图像 <div style="background-image:url(/images/newLocation.jpg); height:580px;">

现在唯一要做的就是在图像中获取文本。转到http://cowelllaserhair.com/index.php看看我在说什么。

最佳答案

你真的不需要'p'标签。

只需将div设置为与背景图片同宽同高,并居中对齐即可。

然后在 DIV 的顶部设置一些填充,以便您的文本位于您需要的位置(确保从 DIV 的高度中减去您使用的顶部填充量。

设置背景图片不重复,位置在左上。

简单。

div {
    width:XXXpx
    height:XXXpx
    padding-top:20px;
    background:url(/images/newLocation.jpg) no-repeat 0 0;
    text-align:center;
}

<div>
    Text here
</div>

关于CSS 背景图片放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12218881/

相关文章:

javascript - 展开四个 div 使它们一个接一个地打开

css - 允许站点内容在固定定位元素下方滚动

html - IE7 和 IE6 在使用无序列表(额外间距等)进行导航时遇到问题

html - 如何在适用于 Outlook 2007 的 HTML 电子邮件中执行背景重复?

css - 使用 % 居中绝对图像

css - 如何使用背景大小 : cover and background-position in CSS3 ?

css - 带有顶部导航的可折叠侧边栏

css - 复选框和标签之间的填充

html - 缩放时保持图像正确显示 (HTML)

jquery - CSS DIV 滚动条最小宽度问题