gpt4 book ai didi

html - 如何将不同的背景图像 URL 添加到一个类中?

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:08 25 4
gpt4 key购买 nike

我有这些框,.partyboxFlag 这是一个类,因为它们在分辨率等方面都相同。但是,我希望在 HTML 标记中创建的每个框都具有一个不同的 URL。

<html>

<head>
<title>Learn which parties are doing what</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>

<body>

<div id = "header">

<div id = "logo"></div>

</div>

<div id = "navbar">
<ul>
<li><a href="index.html">Major Parties</a></li>
<li><a href="minor.html">Minor Parties</a></li>
<li><a href="whyvote.html">Why Vote</a></li>
<li><a href="about.html">About Us</a></li>
</ul>
</div>

<div id = "wrapper">

<div class = "topbox">

<h1>The Conservative Party</h1>

<div id = "listBox">
<h2>Plan</h2>
<p>The Conservatives have a long-term economic plan, which they intend to use to build a stronger, healthier economy.</p>
<h2>Policies</h2>
<ul>
<li>Reducing the Deficit</li>
<li>Cutting income tax and freezing fuel duty</li>
<li>Creating more jobs</li>
<li>Capping welfare and working to control immigration</li>
<li>Delivering the best schools and skills for young people</li>
</ul>
</div>

<div id = "topboxFlag"></div>

</div>

<div class = "partybox">

<h1>The Labour Party</h1>

<div class = "partyboxFlag"></div>

</div>

<div class = "partybox">

<h1>The Liberal Democrats</h1>

<div class = "partyboxFlag"></div>

</div>

<div class = "partybox">

<h1>The UK Independence Party</h1>

<div class = "partyboxFlag"></div>

</div>

<div class = "partybox">

<h1>The Green Party</h1>

<div class = "partyboxFlag"></div>

</div>

<div class = "partybox">

<h1>The Scottish National Party</h1>

<div class = "partyboxFlag"></div>

</div>

</div>



</body>

</html>

根据上面的代码,我希望每个部分都有不同的图像。

<div class = "partybox">

<h1>The Liberal Democrats</h1>

<div class = "partyboxFlag"></div>

</div>

<div class = "partybox">

<h1>The UK Independence Party</h1>

<div class = "partyboxFlag"></div>

</div>

body
{
background-color: #C6E0F5;
}

#wrapper
{
position: relative;
margin: auto;
width: 100%;
height: 2900px;
max-width: 1000px;
background-color: #C6E0F5;
}

#header
{
position: relative;
width: 100%;
height: 170px;
background-color: #00247D;
}

#navbar
{
position: relative;
width: auto;
bottom: 20px;
height: 35px;
text-align: center;
background-color: #CF142B;
}
#navbar ul li
{
list-style-type: none;
padding: 20px;
text-align: center;
font-family: "Trebuchet MS";
font-size: 25px;
display: inline;
}
#navbar ul li a
{
text-decoration: none;
font-weight: bold;
color: #fff;
}

.topbox
{
height: 400px;
width: 100%;
margin: auto;
background-color: #00247D;
}

.topbox h1
{
font-size: 35px;
font-family: "Trebuchet MS";
text-align: center;
color: #fff;
width: 500px;
background-color: #CF142B;
margin:0;
float: left;
}

.topbox #topboxFlag
{
float: right;
width: 300px;
height: 400px;
background-image: url(../img/conservativesflag.png);

}

.topbox #listBox
{
position: absolute;
top: 30px;
left: 10px;
float: left;
width: 500px;
height: 300px;

}

.topbox #listbox ul li
{
font-size: 18px;
font-family: "Trebuchet MS";
color: #fff;
}

.topbox #listbox h2
{
font-size: 20px;
font-family: "Trebuchet MS";
color: #fff;
}

.topbox #listbox p
{
font-size: 18px;
font-family: "Trebuchet MS";
color: #fff;
}


.partybox
{
height: 400px;
width: 100%;
margin: auto;
background-color: #00247D;
margin-top: 15px;
}

.partybox h1
{
font-size: 35px;
font-family: "Trebuchet MS";
text-align: center;
color: #fff;
width: 500px;
background-color: #CF142B;
margin:0;
float: left;
}

.partybox .partyboxFlag
{
float: right;
width: 300px;
height: 400px;
background-color: #fff;
}

最佳答案

如果我正确理解了你的问题(我想我理解了,但你永远不知道),你想要每个 <div class = "partyboxFlag"></div>有不同的背景图像,对吗?

因此,要做到这一点,我会使用一个 ID 来指示这个特定的 URL(您也可以使用另一个类,但由于您的背景似乎是独一无二的,ID 似乎是可行的方法)。也就是说,您的标记将是:

<div class ="partyboxFlag" id="labour-party"></div>
<div class ="partyboxFlag" id="liberal-party"></div>
<div class ="partyboxFlag" id="green-party"></div>

还有你的 CSS:

#labour-party{background-image: url('/somefile.jpeg')}
#liberal-party{background-image: url('/someotherfile.jpeg')}
#green-party{background-image: url('/yetanotherfile.jpeg')}

这样,您就可以随意使用您的标记,标志顺序将由您决定。 @Nick Delaney 的解决方案有效,但标志出现的顺序(我假设图像将是标志)由子项在标记中显示的顺序设置。如果您想在第一个和第二个之间放置一个新标志,那么您的逻辑就是这样。考虑到这一点,我认为使用 ID 更容易,因为它可以让您轻松维护代码。

关于html - 如何将不同的背景图像 URL 添加到一个类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29514941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com