样式标签为什么没有应用在这里?

huangapple go评论54阅读模式
英文:

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 = `
&lt;style&gt;
table tr:hover {
background-color: #f5f5f5;
}
&lt;/style&gt;
&lt;header style=&quot;padding: 20px; text-align: center;position: relative&quot;&gt;
&lt;div class=&quot;contact-info&quot; style=&quot;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;&quot;&gt;
Contact: +91-8491064038, +91-9622259639&lt;br&gt;
Email: info@azeestravels.com
&lt;/div&gt;
&lt;div style =&quot;padding: 20px;text-align: center;background-color: #333;&quot;&gt;
&lt;h1 style=&quot;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;&quot;&gt;Azees Travel&lt;/h1&gt;
&lt;p id=&quot;tag&quot; style=&quot;font-family: Arial, sans-serif; font-size: 16px; color: white; font-weight: bold;&quot;&gt;Dream it, visit it&lt;/p&gt;&lt;/div&gt;
&lt;div&gt;
&lt;p id = &quot;greet&quot; style=&quot;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;&quot;&gt;&lt;span id=&quot;gretD&quot; style=&quot;font-size:29px&quot;&gt;
Greetings from Azees Travel!&lt;/span&gt;&lt;br&gt;
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 &amp; we would love to hear from you upon final package confirmation.
Thank you .....!&lt;/p&gt;
&lt;/div&gt;
&lt;h3 style=&quot;text-align:left;color: red; text-decoration: underline;&quot;&gt;Summary:&lt;/h3&gt;
&lt;table style=&quot;border-collapse: collapse; width: 100%; margin-bottom: 20px; background-color: #f8f8f8; font-size: 16px; color: #333;&quot;&gt;
&lt;tr&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;&lt;Tour Duration:&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;5 Days&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;Adults:&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;Tour Start Date:&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;April 1, 2023&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;Kids:&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;h3 style=&quot;text-align:left;color: red; text-decoration: underline;&quot;&gt;The Travel Itinerary:&lt;/h3&gt;
&lt;/header&gt;
&lt;/form&gt;`;

答案1

得分: 1

两个问题:

  1. 你看不到颜色变化,因为 tdtr 前面,遮挡了背景。
  2. 内联样式具有更高的特异性,将始终覆盖样式(因此不建议使用它们)。

修复:

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:

  1. You don't see the color change because td is in front tr and obscures the background
  2. 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 = `
&lt;style&gt;
table tr:hover td {
background-color: #f5f5f5 !important;
}
&lt;/style&gt;
&lt;header style=&quot;padding: 20px; text-align: center;position: relative&quot;&gt;
&lt;div class=&quot;contact-info&quot; style=&quot;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;&quot;&gt;
Contact: +91-8491064038, +91-9622259639&lt;br&gt;
Email: info@azeestravels.com
&lt;/div&gt;
&lt;div style =&quot;padding: 20px;text-align: center;background-color: #333;&quot;&gt;
&lt;h1 style=&quot;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;&quot;&gt;Azees Travel&lt;/h1&gt;
&lt;p id=&quot;tag&quot; style=&quot;font-family: Arial, sans-serif; font-size: 16px; color: white; font-weight: bold;&quot;&gt;Dream it, visit it&lt;/p&gt;&lt;/div&gt;
&lt;div&gt;
&lt;p id = &quot;greet&quot; style=&quot;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;&quot;&gt;&lt;span id=&quot;gretD&quot; style=&quot;font-size:29px&quot;&gt;
Greetings from Azees Travel!&lt;/span&gt;&lt;br&gt;
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 &amp; we would love to hear from you upon final package confirmation.
Thank you .....!&lt;/p&gt;
&lt;/div&gt;
&lt;h3 style=&quot;text-align:left;color: red; text-decoration: underline;&quot;&gt;Summary:&lt;/h3&gt;
&lt;table style=&quot;border-collapse: collapse; width: 100%; margin-bottom: 20px; background-color: #f8f8f8; font-size: 16px; color: #333;&quot;&gt;
&lt;tr&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;&lt;Tour Duration:&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;5 Days&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;Adults:&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;Tour Start Date:&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;April 1, 2023&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;Kids:&lt;/td&gt;
&lt;td style=&quot;padding: 10px; text-align: left; border-bottom: 1px solid #ddd; background-color: #e6e6e6; font-weight: bold;&quot;&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;h3 style=&quot;text-align:left;color: red; text-decoration: underline;&quot;&gt;The Travel Itinerary:&lt;/h3&gt;
&lt;/header&gt;
&lt;/form&gt;`;
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

&lt;style&gt;
table tr:hover td {
background-color: #f5f5f5 !important;
}
&lt;/style&gt;

huangapple
  • 本文由 发表于 2023年3月7日 15:38:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定