英文:
Why is not the style tag applying here
问题
I am trying to style the html code but the css of style tag inside the const htmlContent is not applying to the html code below. Is there any other alternate way to do this which is easier. No styles are working
<style>
table tr:hover {
background-color: #f5f5f5;
}
</style>
<header style="padding: 20px; text-align: center; position: relative">
<div class="contact-info" style="position: absolute; margin-top: 21px; margin-right: 25px; top: 0; right: 0; font-family: Arial, sans-serif; font-size: 13px; color: #fff; text-align: right">
Contact: +91-8491064038, +91-9622259639<br>
Email: info@azeestravels.com
</div>
<div style="padding: 20px; text-align: center; background-color: #333;">
<h1 style="display: inline-block; font-family: Arial, sans-serif; font-size: 48px; font-weight: bold; margin: 0; color: #333; text-shadow: 2px 2px #ccc; background-image: linear-gradient(to bottom, #ffcc00, #ff6600); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">Azees Travel</h1>
<p id="tag" style="font-family: Arial, sans-serif; font-size: 16px; color: white; font-weight: bold;">Dream it, visit it</p>
</div>
<div>
<p id="greet" style="font-family: Georgia, serif; font-size: 16px; line-height: 2.5; max-width: 1232px; padding: 30px; background-color: #fff; color: #333; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); margin: 0; margin-top: 10px;">
<span id="gretD" style="font-size: 29px;">
Greetings from Azees Travel!
</span><br>
Great to hear you are interested in visiting Kashmir. You can take a look at our scheduled upcoming tour for you. All of our Kashmir Tours include visits to Pahalgam, Gulmarg, Sonamarg, and Srinagar. It also includes accommodation and transfers when you arrive from the airport and depart to the airport. We use quality accommodation throughout the tour and make use of new air-conditioned 4-wheelers. You are in good hands with our certified speaking guides, all of whom work for us full time. We guarantee every tour and are committed to making sure you have a great experience. We wish you a very happy journey, and we would love to make your trip more comfortable and enjoyable. Team AZEES TRAVEL wishes you a great journey, and we would love to hear from you upon final package confirmation.
Thank you .....!
</p>
</div>
<h3 style="text-align: left; color: red; text-decoration: underline;">Summary:</h3>
<table style="border-collapse: collapse; width: 100%; margin-bottom: 20px; background-color: #f8f8f8; font-size: 16px; color: #333;">
<tr>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Tour Duration:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">5 Days</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Adults:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">8</td>
</tr>
<tr>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Tour Start Date:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">April 1, 2023</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Kids:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">8</td>
</tr>
</table>
<h3 style="text-align: left; color: red; text-decoration: underline;">The Travel Itinerary:</h3>
</header>
</form>
英文:
I am trying to style the html code but the css of style tag inside the const htmlContent is not applying to the html code below.Is there any other alternate way to do this which is easier .No styles are working
const htmlContent = `
<style>
table tr:hover {
background-color: #f5f5f5;
}
</style>
<header style="padding: 20px; text-align: center;position: relative">
<div class="contact-info" style="position: absolute; margin-top: 21px; margin-right: 25px; top: 0; right: 0; font-family: Arial, sans-serif; font-size: 13px; color: #fff; text-align: right;">
Contact: +91-8491064038, +91-9622259639<br>
Email: info@azeestravels.com
</div>
<div style ="padding: 20px;text-align: center;background-color: #333;">
<h1 style="display: inline-block;font-family: Arial, sans-serif; font-size: 48px; font-weight: bold; margin: 0; color: #333; text-shadow: 2px 2px #ccc; background-image: linear-gradient(to bottom, #ffcc00, #ff6600); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">Azees Travel</h1>
<p id="tag" style="font-family: Arial, sans-serif; font-size: 16px; color: white; font-weight: bold;">Dream it, visit it</p></div>
<div>
<p id = "greet" style="font-family: Georgia, serif; font-size: 16px; line-height: 2.5; max-width: 1232px; padding: 30px; background-color: #fff; color: #333; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); margin: 0; margin-top: 10px;"><span id="gretD" style="font-size:29px">
Greetings from Azees Travel!</span><br>
Great to hear you are interested in visiting Kashmir.You can take a look at our scheduled upcoming tour for you. All of our Kashmir Tours include visits to Pahalgam, Gulmarg,
Sonamarg and Srinagar.It also includes accommodation and transfers, when you arrive from airport and depart to Airport.We use quality accommodation throughout the tour and make
use of new air-conditioned 4-wheelers. You are in good hands with our certified speaking guides all of whom work for us full time.We guarantee every tour and are committed to
making sure you have a great experience. We wish you a very happy journey and we would love to make your trip more comfortable and enjoyable.Team AZEES TRAVEL wishes you a great
journey & we would love to hear from you upon final package confirmation.
Thank you .....!</p>
</div>
<h3 style="text-align:left;color: red; text-decoration: underline;">Summary:</h3>
<table style="border-collapse: collapse; width: 100%; margin-bottom: 20px; background-color: #f8f8f8; font-size: 16px; color: #333;">
<tr>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;"><Tour Duration:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">5 Days</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Adults:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">8</td>
</tr>
<tr>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Tour Start Date:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">April 1, 2023</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Kids:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">8</td>
</tr>
</table>
<h3 style="text-align:left;color: red; text-decoration: underline;">The Travel Itinerary:</h3>
</header>
</form>`;
答案1
得分: 1
两个问题:
- 你看不到颜色变化,因为
td
在tr
前面,遮挡了背景。 - 内联样式具有更高的特异性,将始终覆盖样式(因此不建议使用它们)。
修复:
table tr:hover td {
background-color: #f5f5f5 !important;
}
添加 td
修复了遮挡问题。
添加 !important
修复了内联样式问题。
<!-- 开始代码段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言:lang-js -->
const htmlContent = `
<style>
table tr:hover td {
background-color: #f5f5f5 !important;
}
</style>
<header style="padding: 20px; text-align: center;position: relative">
<div class="contact-info" style="position: absolute; margin-top: 21px; margin-right: 25px; top: 0; right: 0; font-family: Arial, sans-serif; font-size: 13px; color: #fff; text-align: right;">
Contact: +91-8491064038, +91-9622259639<br>
Email: info@azeestravels.com
</div>
<div style ="padding: 20px;text-align: center;background-color: #333;">
<h1 style="display: inline-block;font-family: Arial, sans-serif; font-size: 48px; font-weight: bold; margin: 0; color: #333; text-shadow: 2px 2px #ccc; background-image: linear-gradient(to bottom, #ffcc00, #ff6600); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">Azees Travel</h1>
<p id="tag" style="font-family: Arial, sans-serif; font-size: 16px; color: white; font-weight: bold;">Dream it, visit it</p>
</div>
<div>
<p id="greet" style="font-family: Georgia, serif; font-size: 16px; line-height: 2.5; max-width: 1232px; padding: 30px; background-color: #fff; color: #333; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); margin: 0; margin-top: 10px;"><span id="gretD" style="font-size:29px;">
Greetings from Azees Travel!</span><br>
Great to hear you are interested in visiting Kashmir.You can take a look at our scheduled upcoming tour for you. All of our Kashmir Tours include visits to Pahalgam, Gulmarg,
Sonamarg and Srinagar.It also includes accommodation and transfers, when you arrive from airport and depart to Airport.We use quality accommodation throughout the tour and make
use of new air-conditioned 4-wheelers. You are in good hands with our certified speaking guides all of whom work for us full time.We guarantee every tour and are committed to
making sure you have a great experience. We wish you a very happy journey and we would love to make your trip more comfortable and enjoyable.Team AZEES TRAVEL wishes you a great
journey & we would love to hear from you upon final package confirmation.
Thank you .....!</p>
</div>
<h3 style="text-align:left;color: red; text-decoration: underline;">Summary:</h3>
<table style="border-collapse: collapse; width: 100%; margin-bottom: 20px; background-color: #f8f8f8; font-size: 16px; color: #333;">
<tr>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Tour Duration:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">5 Days</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Adults:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">8</td>
</tr>
<tr>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Tour Start Date:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">April 1, 2023</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Kids:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">8</td>
</tr>
</table>
<h3 style="text-align:left;color: red; text-decoration: underline;">The Travel Itinerary:</h3>
</header>
</form>`;
document.body.innerHTML = htmlContent
<!-- 结束代码段 -->
英文:
Two things:
- You don't see the color change because
td
is in fronttr
and obscures the background - Inline style have higher specificity and will always override styles (so it's not recommended to use them)
Fix:
table tr:hover td {
background-color: #f5f5f5 !important;
}
Adding td
fixes the obscuring problem
Adding !important
fixes the inline style issue
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const htmlContent = `
<style>
table tr:hover td {
background-color: #f5f5f5 !important;
}
</style>
<header style="padding: 20px; text-align: center;position: relative">
<div class="contact-info" style="position: absolute; margin-top: 21px; margin-right: 25px; top: 0; right: 0; font-family: Arial, sans-serif; font-size: 13px; color: #fff; text-align: right;">
Contact: +91-8491064038, +91-9622259639<br>
Email: info@azeestravels.com
</div>
<div style ="padding: 20px;text-align: center;background-color: #333;">
<h1 style="display: inline-block;font-family: Arial, sans-serif; font-size: 48px; font-weight: bold; margin: 0; color: #333; text-shadow: 2px 2px #ccc; background-image: linear-gradient(to bottom, #ffcc00, #ff6600); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">Azees Travel</h1>
<p id="tag" style="font-family: Arial, sans-serif; font-size: 16px; color: white; font-weight: bold;">Dream it, visit it</p></div>
<div>
<p id = "greet" style="font-family: Georgia, serif; font-size: 16px; line-height: 2.5; max-width: 1232px; padding: 30px; background-color: #fff; color: #333; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5); margin: 0; margin-top: 10px;"><span id="gretD" style="font-size:29px">
Greetings from Azees Travel!</span><br>
Great to hear you are interested in visiting Kashmir.You can take a look at our scheduled upcoming tour for you. All of our Kashmir Tours include visits to Pahalgam, Gulmarg,
Sonamarg and Srinagar.It also includes accommodation and transfers, when you arrive from airport and depart to Airport.We use quality accommodation throughout the tour and make
use of new air-conditioned 4-wheelers. You are in good hands with our certified speaking guides all of whom work for us full time.We guarantee every tour and are committed to
making sure you have a great experience. We wish you a very happy journey and we would love to make your trip more comfortable and enjoyable.Team AZEES TRAVEL wishes you a great
journey & we would love to hear from you upon final package confirmation.
Thank you .....!</p>
</div>
<h3 style="text-align:left;color: red; text-decoration: underline;">Summary:</h3>
<table style="border-collapse: collapse; width: 100%; margin-bottom: 20px; background-color: #f8f8f8; font-size: 16px; color: #333;">
<tr>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;"><Tour Duration:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">5 Days</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Adults:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">8</td>
</tr>
<tr>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Tour Start Date:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">April 1, 2023</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">Kids:</td>
<td style="padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;">8</td>
</tr>
</table>
<h3 style="text-align:left;color: red; text-decoration: underline;">The Travel Itinerary:</h3>
</header>
</form>`;
document.body.innerHTML = htmlContent
<!-- end snippet -->
答案2
得分: 0
根据评论所示,缺少了td。此外,将!important添加到CSS中,否则它不会覆盖表格中使用的background-color。
<style>
table tr:hover td {
background-color: #f5f5f5 !important;
}
</style>
英文:
as the comments implied, the td was missing. But also add the !important to the css, otherwise it won't overwrite your background-color used in the table
<style>
table tr:hover td {
background-color: #f5f5f5 !important;
}
</style>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论