首页 > 其他分享 >css01 CSS Introduction

css01 CSS Introduction

时间:2024-05-28 20:33:57浏览次数:22  
标签:css01 width Introduction padding color background font CSS

https://www.w3schools.com/css/css_intro.asp

 

CSS is the language we use to style a Web page.


What is CSS?

  • CSS stands for Cascading Style Sheets
  • CSS describes how HTML elements are to be displayed on screen, paper, or in other media
  • CSS saves a lot of work. It can control the layout of multiple web pages all at once
  • External stylesheets are stored in CSS files

CSS Demo - One HTML Page - Multiple Styles!

Here we will show one HTML page displayed with four different stylesheets. Click on the "Stylesheet 1", "Stylesheet 2", "Stylesheet 3", "Stylesheet 4" links below to see the different styles:

<html>

<head>
   <style>
      /* Stylesheet 1: */
      body {
         font: 100% Lucida Sans, Verdana;
         margin: 20px;
         line-height: 26px;
      }

      .container {
         xmin-width: 900px;
      }

      .wrapper {
         position: relative;
         overflow: auto;
      }

      #top,
      #sidebar,
      #bottom,
      .menuitem {
         border-radius: 4px;
         margin: 4px;
      }

      #top {
         background-color: #04AA6D;
         color: #ffffff;
         padding: 15px;
      }

      #menubar {
         width: 200px;
         float: left
      }

      #main {
         padding: 10px;
         margin: 0 210px;
      }

      #sidebar {
         background-color: #32a4e7;
         color: #ffffff;
         padding: 10px;
         width: 180px;
         bottom: 0;
         top: 0;
         right: 0;
         position: absolute;
      }

      #bottom {
         border: 1px solid #d4d4d4;
         background-color: #f1f1f1;
         text-align: center;
         padding: 10px;
         font-size: 70%;
         line-height: 14px;
      }

      #top h1,
      #top p,
      #menulist {
         margin: 0;
         padding: 0;
      }

      .menuitem {
         background-color: #f1f1f1;
         border: 1px solid #d4d4d4;
         list-style-type: none;
         padding: 2px;
         cursor: pointer;
      }

      .menuitem:hover {
         background-color: #ffffff;
      }

      .menuitem:first-child {
         background-color: #04AA6D;
         color: white;
         font-weight: bold;
      }

      a {
         color: #000000;
         text-decoration: underline;
      }

      a:hover {
         text-decoration: none;
      }


      @media (max-width: 800px) {
         #sidebar {
            width: auto;
            position: relative;
         }

         #main {
            margin-right: 0;
         }

      }

      @media (max-width: 600px) {
         #menubar {
            width: auto;
            float: none;
         }

         #main {
            margin: 0;
         }
      }
   </style>

   <style>
      /* Stylesheet 2: */
      body {
         font-family: Arial;
         background-color: #d14836;
         line-height: 20px;
      }

      .container {
         xmin-width: 900px;
      }

      .wrapper {
         position: relative;
         overflow: auto;
      }

      #top {
         color: #ffffff;
         padding: 15px;
         font-size: 30px;
         line-height: 26px;
      }

      #top h1 {
         margin: 0;
         line-height: 50px;
      }

      #menubar {
         width: 190px;
         float: right;
      }

      #main {
         padding: 10px;
         background-color: #ffffff;
         font: 80% Verdana;
      }

      #main h1,
      #main h2 {
         color: #d14836;
      }

      #sidebar {
         background-color: #F6DAD7;
         color: #d14836;
         padding: 10px;
      }

      #bottom {
         text-align: center;
         padding: 10px;
         font-size: 70%;
         color: #ffffff;
      }

      #menulist {
         padding: 0;
         font: 16px verdana;
      }

      .menuitem {
         width: 155px;
         background-color: #d14836;
         border: 1px solid #d14836;
         border-radius: 40px;
         color: #ffffff;
         list-style-type: none;
         margin: 10px;
         padding: 5px;
         text-align: center;
         cursor: pointer;
      }

      .menuitem:nth-child(2) {
         background-color: white;
         color: #d14836;
         font-weight: bold;
      }

      .menuitem:hover {
         background-color: #ffffff;
         color: #d14836;
      }

      a {
         color: #d14836;
         text-decoration: none;
      }

      a:hover {
         text-decoration: underline;
      }
   </style>

   <style>
      /* Stylesheet 3: */
      body {
         font: 100% Verdana;
         margin: 20px;
         line-height: 26px;
      }

      .container {
         xmin-width: 900px;
      }

      .wrapper {
         position: relative;
         overflow: auto;
      }

      #sidebar {
         background-color: #f1f1f1;
         border: 1px solid #d4d4d4;
         padding-left: 10px;
      }

      #bottom {
         text-align: center;
         padding: 10px;
         font-size: 70%;
         line-height: 14px;
      }

      h1,
      h2,
      h3 {
         color: #4CAF50;
      }

      #menulist {
         padding: 0;
         position: relative;
         overflow: auto;
      }

      .menuitem {
         width: 165px;
         float: left;
         background-color: #555555;
         color: #ffffff;
         list-style-type: none;
         margin: 4px;
         padding: 2px;
         text-align: center;
         cursor: pointer;
      }

      .menuitem:nth-child(3) {
         background-color: #04AA6D;
      }

      .menuitem:hover {
         background-color: #000;
      }

      a {
         color: #000000;
      }

      a:hover {
         color: #84c754;
      }
   </style>
   <style>
      /* Stylesheet 4: */
      body {
         font: 100% Courier New;
         margin: 20px;
         line-height: 26px;
         background-color: #000000;
      }

      .container {
         xmin-width: 900px;
      }

      .wrapper {
         position: relative;
         overflow: auto;
      }

      #top {
         color: #84c754;
         padding: 15px;
      }

      #main {
         padding: 10px;
         color: #84c754;
      }

      #sidebar {
         color: #ffffff;
         border: 1px solid #ffffff;
         border-radius: 4px;
         padding: 10px;
         width: 320px;
         top: 0;
         right: 0;
         position: absolute;
         font-size: 80%;
         line-height: 20px;
      }

      #bottom {
         border: 1px solid #ffffff;
         border-radius: 4px;
         color: #ffffff;
         text-align: center;
         padding: 10px;
         font-size: 70%;
         line-height: 14px;
      }

      #top h1,
      #top p {
         margin: 0;
      }

      .menuitem {
         color: #84c754;
         cursor: pointer;
      }

      .menuitem:nth-child(4) {
         color: white;
         font-weight: bold;
      }

      .menuitem:hover {
         color: #ffffff;
      }

      a {
         color: #ffffff;
      }

      a:hover {
         color: #84c754;
      }

      @media (max-width: 600px) {
         #sidebar {
            width: auto;
            margin-bottom: 10px;
            position: relative;
         }
      }
   </style>

</head>

<body>

   <div class="container wrapper">
      <div id="top">
         <h1>Welcome to My Homepage</h1>
         <p>Use the menu to select different Stylesheets</p>
      </div>
      <div class="wrapper">
         <div id="menubar">
            <ul id="menulist">
               <li class="menuitem" onclick="reStyle(0)">Stylesheet 1
               </li>
               <li class="menuitem" onclick="reStyle(1)">Stylesheet 2
               </li>
               <li class="menuitem" onclick="reStyle(2)">Stylesheet 3
               </li>
               <li class="menuitem" onclick="reStyle(3)">Stylesheet 4
               </li>
               <li class="menuitem" onclick="noStyles()">No Stylesheet
               </li>
            </ul>
         </div>
         <div id="main">
            <h1>Same Page Different Stylesheets</h1>
            <p>This is a demonstration of how different stylesheets can change the layout of your HTML page. You can
               change the layout of this page by selecting different stylesheets in the menu, or by selecting one of the
               following links:<br>
               <a href="#" onclick="reStyle(0);return false">Stylesheet1</a>,
               <a href="#" onclick="reStyle(1);return false">Stylesheet2</a>,
               <a href="#" onclick="reStyle(2);return false">Stylesheet3</a>,
               <a href="#" onclick="reStyle(3);return false">Stylesheet4</a>.
            </p>
            <h2>No Styles</h2>
            <p>This page uses DIV elements to group different sections of the HTML page. Click here to see how the page
               looks like with no stylesheet:<br><a href="#" onclick="noStyles();return false">No Stylesheet</a>.</p>
         </div>
         <div id="sidebar">
            <h3>Side-Bar</h3>
            <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
               laoreet dolore magna aliquam erat volutpat.</p>
         </div>
      </div>


      <div id="bottom">
         Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet
         dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
         suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in
         vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan
         et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla
         facilisi.
      </div>
   </div>

   <script>
      function noStyles() {
         document.styleSheets[0].disabled = true;
         document.styleSheets[1].disabled = true;
         document.styleSheets[2].disabled = true;
         document.styleSheets[3].disabled = true;
      }

      function reStyle(n) {
         noStyles()
         document.styleSheets[n].disabled = false;
      }

      function closeBlackdiv() {
         var blackdiv, stylediv;
         blackdiv = document.getElementById("blackdiv")
         blackdiv.parentNode.removeChild(blackdiv);
         stylediv = document.getElementById("stylediv")
         stylediv.parentNode.removeChild(stylediv);
      }

      function showStyle(n) {
         var div, text, blackdiv;
         blackdiv = document.createElement("DIV");
         blackdiv.setAttribute("style", "background-color:#000000;position:absolute;width:100%;height:100%;top:0;opacity:0.5;margin-left:-20px;");
         blackdiv.setAttribute("id", "blackdiv");
         blackdiv.setAttribute("onclick", "closeBlackdiv()");
         document.body.appendChild(blackdiv);
         div = document.createElement("DIV");
         div.setAttribute("id", "stylediv");
         div.setAttribute("style", "background-color:#ffffff;padding-left:5px;position:absolute;width:auto;height:auto;top:100px;bottom:50px;left:200px;right:200px;overflow:auto;font-family: monospace; white-space: pre;line-height:16px;");
         text = document.createTextNode(document.getElementsByTagName("STYLE")[n].innerHTML);
         div.appendChild(text);
         document.body.appendChild(div);
         //alert(document.getElementsByTagName("STYLE")[n].innerHTML);
      }
      reStyle(0);
   </script>



</body>

</html>

 

 


Why Use CSS?

CSS is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.

CSS Example

body {
  background-color: lightblue;
}

h1 {
  color: white;
  text-align: center;
}

p {
  font-family: verdana;
  font-size: 20px;
}

CSS Solved a Big Problem

HTML was NEVER intended to contain tags for formatting a web page!

HTML was created to describe the content of a web page, like:

<h1>This is a heading</h1>

<p>This is a paragraph.</p>

When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a nightmare for web developers. Development of large websites, where fonts and color information were added to every single page, became a long and expensive process.

To solve this problem, the World Wide Web Consortium (W3C) created CSS.

CSS removed the style formatting from the HTML page!

 


CSS Saves a Lot of Work!

The style definitions are normally saved in external .css files.

With an external stylesheet file, you can change the look of an entire website by changing just one file!

 

标签:css01,width,Introduction,padding,color,background,font,CSS
From: https://www.cnblogs.com/emanlee/p/18218792

相关文章

  • css02 CSS Syntax
    https://www.w3schools.com/css/css_syntax.aspACSSruleconsistsofaselectorandadeclarationblock.CSSSyntaxTheselectorpointstotheHTMLelementyouwanttostyle.Thedeclarationblockcontainsoneormoredeclarationsseparatedbysemicolo......
  • html+CSS部分基础运用8
    1.P147实验1,完成页面制作效果。图7-1木兰花令效果图2.P147实验2,完成页面制作效果。项目1<!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <linktype="text/css">  <title>木兰花令</title>......
  • html+CSS部分基础运用7
    项目1 设计简易灯箱画廊1.实验所需素材在trees文件夹中提供一个MP3文件和18个JPG文件,设计页面时可以使用。2.编程实现简易灯箱画廊,鼠标单击任一个图像超链接,在底部浮动框架中显示大图像,效果如图4-1所示的页面。图4-1简易灯箱画廊项目2 设计支持音频、视频播放的......
  • CSS 之 自定义属性(变量)
    一、简介​CSS的自定义属性,又称为CSS变量或级联变量,用于定义一个带有值的、可重复使用的CSS属性(变量)。其包含的值可以在其作用域内的任意属性上重复使用,在使用时需要借助var()函数获取自定义属性的值。当自定义属性的值发生变化时,所有使用该自定义属性的CSS属性都会随之变......
  • dumi2.x如何支持css Module?
    在dumi2.x中支持CSSModules是比较直接的,因为dumi基于Umi,而Umi自身就对CSSModules提供了良好支持。以下是基本步骤和注意事项来在dumi项目中使用CSSModules:安装依赖:确保你的项目中已经安装了相应的加载器,如style-loader、css-loader以及可能的less-loader、s......
  • CSS设置水平垂直居中
    在CSS中,有多种方法可以实现元素的水平垂直居中。以下是一些常见的方法:1.使用FlexboxFlexbox是CSS3引入的一个强大的布局模型,可以轻松地实现元素的水平垂直居中。css.container{display:flex;justify-content:center;/*水平居中*/align-items:cente......
  • 自定义CSS属性(@property)解决自定义CSS变量无法实现过渡效果的问题
    且看下面的代码:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=1.0"/><title>demot</t......
  • 【scss】基于:empty伪类的空元素回显及加载状态
    mixin.scss/**空内容height:占位高度**/@mixinempty-block($height:220px){display:block;position:relative;width:100%;min-height:$height;//图片高度background-image:url('~@/assets/images/default-img/no-data.svg');background-repeat......
  • 代码雨(coderain)源码(html5+css3+javascript,原创)
     大家看过黑客帝国的代码雨吗?本人自己写了一个,效果还可以。演示效果请见https://www.lanbaoshi.site/coderain.htm下面上代码:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="htt......
  • SCSS入门指南:基本语法与高效用法
    关于SCSS(SassyCSS)基本使用的文章概述:###1.SCSS简介*SCSS是一种CSS的扩展语言,它允许开发者使用更强大、更灵活的语法来编写样式表。*SCSS提供了变量、嵌套规则、混合宏等高级功能,使得CSS代码更加模块化和可维护。*SCSS最终被编译成标准的CSS文件,以供浏览器解析。###2.安......