search
HomeWeb Front-endCSS TutorialHow to use CSS to separate content and design?

How to use CSS to separate content and design?

A good website always consists of a perfectly structured HTML document and a beautiful design that attracts the user's attention. This kind of website can enhance the overall interactivity of the website and make it more attractive.

When applying styles to our web pages, we use Cascading Style Sheets, or CSS for short. By using CSS, we can make our website beautiful more easily. It distinguishes between the structure of an HTML document and its presentation, which refers to the elements that users see and interact with.

Unlike bland websites created simply using HTML, having a unique and creative style has become a must-have feature for websites.

Merge CSS

We can incorporate CSS into our website in three different ways -

  • Inline Styles - When we apply styles to every individual HTML tag, it is called inline styles.

  • Embedded Styles - The head tag is contained within the style tag and it looks like the CSS has been merged into the HTML file. Then use the term "embedded style".

  • External Styles - This is the most recommended technique for using CSS as it is used to separate CSS from HTML. The HTML content is connected to a CSS file that contains all styling information. Using this styling approach we can attach many CSS files.

By using separate files and grouping similar types of formats into the same attribute, we can significantly reduce the length of the code used. This helps maintain the code because less code makes the bug-finding process easier and greatly enhances the overall readability of the code.

Another advantage of using separate CSS files is that you can reuse the same file as many times as needed; compared to traditional formatting, where you have to repeat the formatting in each page separately. You can reuse the file by importing it or linking the worksheet.

As already discussed, we can separate the content (structure) of a web page from its design (formatting and style) by using a .CSS file linked to an HTML document. We can link CSS files to HTML files using the html tag.

We use the link tag to specify how two documents relate to each other. It is an empty element, which means that it neither has any opening or closing tags nor is it a self-closing tag. All the information it needs is stored in its properties. There are many attributes that can be used in link tags, but the ones we need to use are defined below.

  • Rel - It is a must-have attribute of the link tag that specifies how the document currently being used is related to the document we are trying to link to. Usually, we just tend to think of stylesheets or favicons as relationships.

  • Href - Used to specify the URL of the file to be linked.

The link tag is used in the head tag. Within the same document, each tag can have multiple instances of the link tag; pointing to different files. Given below is the syntax for linking CSS files in HTML using link tags -

<link rel=”the Relationship” href=”source” >

Example

Let’s look at an example of using link tags to separate the content and design parts of a web page.

HTML part

<html>
<head>
   <title>Separating Content and Design using the link HTML tag</title>
   <style>
      body {
         background-color: rgb(230, 228, 228);
      }
      h1 {
         color: rgb(125, 211, 125);
      }
      h3 {
         color: rgb(40, 15, 57);
      }
      h1,
      h3{
         text-align: right;
         font-family: fantasy;
      }
      .formattedContent {
         text-align: justify;
         font-family: sans-serif;
         margin-left: 2%;
         margin-right: 2%;
         color: rgb(149, 84, 72);
      }
   </style>
</head>
<body>
   <div>
      <h1 id="Example-of-separating-content-and-design-using-external-styleheet-with-link-tag">Example of separating content and design using external styleheet with link tag.</h1>
      <h3 id="External-Stylesheet-and-link-tags">External Stylesheet and link tags</h3>
      <p class="FormattedContent">
         Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsam earum eligendi, dignissimos nobis a nam similique animi tenetur expedita  pariatur nulla recusandae repellendus sequi quisquam corporis,  voluptates nisi iure perferendis?
      </p>
   </div>
</body>
</html>

Use import declaration

We can also use the import statement in CSS to separate content from design. We use this statement whenever we have to import styles stored in separate files. We just need to provide the URL or source path where the CSS file is located within quotes.

We can also choose to use media queries with this statement. This is a flexible statement that also supports cascading of style sheets.

The only thing we need to change when using this to separate design and content is to remove the link tag from the example code above and add the following statement in its place.

<style>
   @import(‘LinkedStyle.css’)
</style>
The output of the

code will be the same as the output of the example code above.

in conclusion

In summary, CSS is a powerful tool for web designers, allowing them to separate content from design. By using CSS, designers can create a consistent look across multiple websites and devices while keeping content organized and easy to update. With the right understanding of HTML and CSS, any designer can easily leverage this feature to create stunning designs without compromising on usability or accessibility.

The above is the detailed content of How to use CSS to separate content and design?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
CSS Animation LibrariesCSS Animation LibrariesApr 19, 2025 am 10:46 AM

There are an awful lot of libraries that want to help you animate things on the web. These aren't really libraries that help you with the syntax or the

Color Inputs: A Deep Dive into Cross-Browser DifferencesColor Inputs: A Deep Dive into Cross-Browser DifferencesApr 19, 2025 am 10:40 AM

In this article, we'll be taking a look at the structure inside elements, browser inconsistencies, why they look a certain way in a certain browser, and how

Restricting a (pseudo) element to its parent's border-boxRestricting a (pseudo) element to its parent's border-boxApr 19, 2025 am 10:39 AM

Have you ever wanted to ensure that nothing of a (pseudo) element gets displayed outside its parent's border-box? In case you're having trouble picturing what

ToastToastApr 19, 2025 am 10:30 AM

One day, all the sudden, I started hearing jokes about toast. I had no idea what the context was. I assumed some friends just got started telling toast jokes,

Protecting Vue Routes with Navigation GuardsProtecting Vue Routes with Navigation GuardsApr 19, 2025 am 10:29 AM

Authentication is a necessary part of every web application. It is a handy means by which we can personalize experiences and load content specific to a

Different Approaches for Creating a Staggered AnimationDifferent Approaches for Creating a Staggered AnimationApr 19, 2025 am 10:28 AM

Animating elements, at its most basic, is fairly straightforward. Define the keyframes. Name the animation. Call it on an element.

Hey, let's create a functional calendar app with the JAMstackHey, let's create a functional calendar app with the JAMstackApr 19, 2025 am 10:22 AM

Hey, let's create a functional calendar app with the JAMstack

The Fight Against Layout JankThe Fight Against Layout JankApr 19, 2025 am 10:20 AM

A web page isn't locked in stone just because it has rendered visually. Media assets, like images, can come in and cause the layout to shift based on their

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.