【问题标题】:attribute 'font-size' alters the 'background-img' size as well属性 'font-size' 也改变了 'background-img' 的大小
【发布时间】:2018-10-18 08:08:32
【问题描述】:

我正在尝试使用背景图片制作网站: (CSS)

body {
    text-align: center;
    background-image: url("../img/clouds.jpg");
    background-size: 100% 760%;
}

我在页面顶部也有这个: (HTML)

<div class="pageTitle">
    <p>Title of Page basically</p>
</div>

(CSS)

.pageTitle {
    font-size: 200%;
}

当我尝试使字体大小变大时,背景图像也会变大(基本上看起来像是放大了)。

我想这与百分比的使用有关,但即使使用em 表示法或px 我仍然得到相同的结果。 如何在不改变背景图片大小的情况下使 pageTitle 更大?

似乎使它起作用的是将background-size: 100% 760%;更改为background-size: 100% 250%;(在我的情况下),但是,无论何时更改元素,都需要将其作为一个整体进行修复。

代码片段:

var currentQuote = '';
var availableQuotes = [	'bla','blo','ble','foo','bar'
];

function setRandomQuote() {
	currentQuote = availableQuotes[Math.floor(Math.random() * availableQuotes.length)];

	$('#quote').html(currentQuote);
	$('#tweetQuote').attr('href', 'https://twitter.com/intent/tweet?text=' + currentQuote).attr('target', '_blank');
}

$(function () {
	$('#randomQuote').click(function () {
		setRandomQuote();
	});
});
.w3-tangerine {
	font-family: 'Tangerine', serif;
	font-size: 200%;
	margin-top: 10%;
	margin-bottom: 5%;
}
body {
	text-align: center;
	background-image: url("https://images.pexels.com/photos/850674/pexels-photo-850674.jpeg?cs=srgb&dl=agriculture-animal-photography-animals-850674.jpg&fm=jpg");
	background-size: 100% 250%;
}
p {
	font-size: 100%;
}
#randomQuote, #tweetQuote {
	margin: 3% 1% 3% 1%;
}
#quote {
	margin: 2% 2% 2% 2%;
}
<!DOCTYPE html>
<html lang="en" >

<head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>Random Quote Generator</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="css/style.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Tangerine">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <noscript>Sorry, your browser does not support JavaScript!</noscript>
    </head>

<body>

  <div class="w3-tangerine">
	<p>Just Another Random Quote Generator</p>
</div>
<div id="quote">
	Click on the button to generate a quote!
</div>
<div>
    <input id="randomQuote" type="button" class="btn btn-primary" value="Generate a Quote!">
    <a id="tweetQuote" class="btn btn-primary" href="#">Tweet this Quote  <i class="fa fa-twitter"></i></a>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script  src="js/index.js"></script>
</body>

</html>

【问题讨论】:

  • 你能制作一个JSFiddle 并在网上托管所有相关代码和图像吗?谢谢
  • 或者更好的是,直接在 StackOverflow 上创建一个代码 sn-p - 它内置在问题编辑器中。
  • 添加了 sn-p,但我似乎无法上传背景图片以显示问题。编辑:图片上传。
  • 上传到Imgur并使用图片链接。

标签: html css background-image font-size background-size


【解决方案1】:

如果不设置heightheight ,则backgroundheight 的百分比是作为一个常量的height。所以 760% background-heighttext-height*7.6。这就是为什么当你改变图像的大小时,图像的大小也会变大..
你可以做的是设置

 html, body{
    height:100%;
    }

然后它将计算整个页面大小的百分比(因此在您的情况下,您只会看到图像高度的 1/7.6)。

【讨论】:

  • 这解释了很多,也解决了这个问题!谢谢!
  • 我有同样的解决方案,这里是JSFiddle,如果你想看看,@Zap。
  • 非常感谢!现在,将正文设置为 background-size: 100% 100%; 而不是我用来掩盖问题的奇怪百分比也很有意义。
  • yap,老实说我不太了解这个问题的原因,只是回答问题)如果你想让它不掉线,你可以使用background-size:cover;
【解决方案2】:

var currentQuote = '';
var availableQuotes = [	'bla','blo','ble','foo','bar'
];

function setRandomQuote() {
	currentQuote = availableQuotes[Math.floor(Math.random() * availableQuotes.length)];

	$('#quote').html(currentQuote);
	$('#tweetQuote').attr('href', 'https://twitter.com/intent/tweet?text=' + currentQuote).attr('target', '_blank');
}

$(function () {
	$('#randomQuote').click(function () {
		setRandomQuote();
	});
});
.w3-tangerine {
	font-family: 'Tangerine', serif;
	font-size: 200%;
	margin-top: 10%;
	margin-bottom: 5%;
}
   

html, body {
    height: 100%;
    }
body {
	text-align: center;
	background-image: url("https://images.pexels.com/photos/850674/pexels-photo-850674.jpeg?cs=srgb&dl=agriculture-animal-photography-animals-850674.jpg&fm=jpg");
	background-size: cover;
}
p {
	font-size: 100%;
}
#randomQuote, #tweetQuote {
	margin: 3% 1% 3% 1%;
}
#quote {
	margin: 2% 2% 2% 2%;
}
<!DOCTYPE html>
<html lang="en" >

<head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>Random Quote Generator</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="css/style.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Tangerine">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <noscript>Sorry, your browser does not support JavaScript!</noscript>
    </head>

<body>

  <div class="w3-tangerine">
	<p>Just Another Random Quote Generator</p>
</div>
<div id="quote">
	Click on the button to generate a quote!
</div>
<div>
    <input id="randomQuote" type="button" class="btn btn-primary" value="Generate a Quote!">
    <a id="tweetQuote" class="btn btn-primary" href="#">Tweet this Quote  <i class="fa fa-twitter"></i></a>
</div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script  src="js/index.js"></script>
</body>

</html>

【讨论】:

  • 所以基本上你添加了html, body { height: 100%; },如上所示是吗?
  • 是的,还有 body { background-size: cover }
  • 所以我收到的两个回复的组合,很好!感谢您的贡献。
【解决方案3】:

有一个article,它描述了您遇到的确切问题。

解决方案之一是为body 提供以下属性。这将确保覆盖所有地方:

body {
  ...
  background-size: cover;
}

【讨论】:

  • 这意味着图像将尽可能地覆盖在屏幕上,但如果比例不一样怎么办?那么图像会拉伸以适应它可能缺少的尺寸吗?不过,感谢您的文章!
猜你喜欢
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 2012-05-20
  • 1970-01-01
  • 2013-07-16
  • 2011-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多