将内部CSS移到单独的文件中时,它的工作方式不同 – 现在已解决。

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

When moving internal CSS to separate file, it works different -- now resolved

问题

I cut and pasted the <style> section from the code below into a different CSS file by replacing the section with <link rel="stylesheet" href="styles.css">, but it renders differently. Please tell me how to secure the original style even when I move them into a .css file.

现在我看到它正常工作了。对于混淆,对不起。

英文:

From the code below, I cut and pasted the &lt;style&gt; section into different css file by replacing the section with &lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot;&gt;, but it renders differently. Please tell me how do I secure original style even when I moved them into a .css file.

--> Now I see it works fine. Sorry for the confusion folks.

Disclaimer: This code was generated by ChatGPT.

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;title&gt;Data Provider Site&lt;/title&gt;
  &lt;style&gt;
    /* Add your custom styles here */
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }

    header {
      background-color: #ffffff;
      color: #e2b931;
      padding: 20px;
      text-align: center;
    }

    h1 {
      margin: 0;
    }

    nav {
      background-color: #c8beb8;
      padding: 10px;
    }

    nav ul {
      list-style-type: none;
      margin: 0;
      padding: 0;
      overflow: hidden;
    }

    nav ul li {
      display: inline-block;
    }

    nav ul li a {
      display: block;
      padding: 10px;
      text-decoration: none;
      color: #333;
    }

    nav ul li a:hover {
      background-color: #ddd;
    }

    main {
      padding: 20px;
    }

    footer {
      background-color: #333;
      color: #fff;
      padding: 10px;
      text-align: center;
    }
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;header&gt;
    &lt;h1&gt;Data Provider Site&lt;/h1&gt;
  &lt;/header&gt;
  &lt;nav&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Services&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Pricing&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/nav&gt;
  &lt;main&gt;
    &lt;!-- Add your content here --&gt;
    &lt;h2&gt;Welcome to Our Data Provider Site&lt;/h2&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et iaculis arcu. Duis sed nulla eu enim ullamcorper ultricies.&lt;/p&gt;
  &lt;/main&gt;
  &lt;footer&gt;
    &lt;p&gt;&amp;copy; 2023 Your Company. All rights reserved.&lt;/p&gt;
  &lt;/footer&gt;
&lt;/body&gt;
&lt;/html&gt;

Original page
将内部CSS移到单独的文件中时,它的工作方式不同 – 现在已解决。

Page with separate .css file
将内部CSS移到单独的文件中时,它的工作方式不同 – 现在已解决。

styles.css

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
}

header {
  background-color: #1e155e;
  color: #fff;
  padding: 20px;
  text-align: center;
}

h1 {
  margin: 0;
}

nav {
  background-color: #f4f4f4;
  padding: 10px;
}

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

nav ul li {
  display: inline-block;
}

nav ul li a {
  display: block;
  padding: 10px;
  text-decoration: none;
  color: #333;
}

nav ul li a:hover {
  background-color: #ddd;
}

main {
  padding: 20px;
}

footer {
  background-color: #333;
  color: #fff;
  padding: 10px;
  text-align: center;
}

index.html

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;title&gt;Temp&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot; type=&quot;text/css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;header&gt;
    &lt;h1&gt;Temp&lt;/h1&gt;
  &lt;/header&gt;
  &lt;nav&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;data.html&quot;&gt;Data&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;lesson.html&quot;&gt;Lesson&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;about.html&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/nav&gt;
  &lt;main&gt;
    &lt;!-- Add your content here --&gt;
    &lt;h2&gt;Welcome to Our Data Provider Site&lt;/h2&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et iaculis arcu. Duis sed nulla eu enim ullamcorper ultricies.&lt;/p&gt;
  &lt;/main&gt;
  &lt;footer&gt;
    &lt;p&gt;&amp;copy; 2023 . All rights reserved.&lt;/p&gt;
  &lt;/footer&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 1

您的代码对我很有效。这是我附上的工作代码和从代码生成的屏幕截图。

您可以尝试清除浏览器缓存。有时浏览器缓存也可能是问题。

将内部CSS移到单独的文件中时,它的工作方式不同 – 现在已解决。

<!-- 开始代码片段:js 隐藏:false 控制台:true babel:false -->

<!-- 语言:lang-css -->

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
}

header {
  background-color: #1e155e;
  color: #fff;
  padding: 20px;
  text-align: center;
}

h1 {
  margin: 0;
}

nav {
  background-color: #f4f4f4;
  padding: 10px;
}

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

nav ul li {
  display: inline-block;
}

nav ul li a {
  display: block;
  padding: 10px;
  text-decoration: none;
  color: #333;
}

nav ul li a:hover {
  background-color: #ddd;
}

main {
  padding: 20px;
}

footer {
  background-color: #333;
  color: #fff;
  padding: 10px;
  text-align: center;
}

<!-- 语言:lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;title&gt;Temp&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot; type=&quot;text/css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;header&gt;
    &lt;h1&gt;Temp&lt;/h1&gt;
  &lt;/header&gt;
  &lt;nav&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;data.html&quot;&gt;Data&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;lesson.html&quot;&gt;Lesson&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;about.html&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/nav&gt;
  &lt;main&gt;
    &lt;!-- 在此处添加您的内容 --&gt;
    &lt;h2&gt;欢迎访问我们的数据提供者网站&lt;/h2&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et iaculis arcu. Duis sed nulla eu enim ullamcorper ultricies.&lt;/p&gt;
  &lt;/main&gt;
  &lt;footer&gt;
    &lt;p&gt;&amp;copy; 2023 . 版权所有。&lt;/p&gt;
  &lt;/footer&gt;
&lt;/body&gt;

<!-- 结束代码片段 -->

英文:

Your code is working fine for me. Here I have attached my working code and the screenshot generated from the code.

You can try clearing the browser cache. Sometimes browser cache is the problem too.

将内部CSS移到单独的文件中时,它的工作方式不同 – 现在已解决。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
}

header {
  background-color: #1e155e;
  color: #fff;
  padding: 20px;
  text-align: center;
}

h1 {
  margin: 0;
}

nav {
  background-color: #f4f4f4;
  padding: 10px;
}

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

nav ul li {
  display: inline-block;
}

nav ul li a {
  display: block;
  padding: 10px;
  text-decoration: none;
  color: #333;
}

nav ul li a:hover {
  background-color: #ddd;
}

main {
  padding: 20px;
}

footer {
  background-color: #333;
  color: #fff;
  padding: 10px;
  text-align: center;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;title&gt;Temp&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot; type=&quot;text/css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;header&gt;
    &lt;h1&gt;Temp&lt;/h1&gt;
  &lt;/header&gt;
  &lt;nav&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;data.html&quot;&gt;Data&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;lesson.html&quot;&gt;Lesson&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;about.html&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/nav&gt;
  &lt;main&gt;
    &lt;!-- Add your content here --&gt;
    &lt;h2&gt;Welcome to Our Data Provider Site&lt;/h2&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et iaculis arcu. Duis sed nulla eu enim ullamcorper ultricies.&lt;/p&gt;
  &lt;/main&gt;
  &lt;footer&gt;
    &lt;p&gt;&amp;copy; 2023 . All rights reserved.&lt;/p&gt;
  &lt;/footer&gt;
&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月28日 21:12:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351675.html
匿名

发表评论

匿名网友

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

确定