The @import At-rules in CSS

CSSWeb DevelopmentFront End Technology

The CSS @import rule is used for setting style information for a specific target media by specifying the media types after the URL of the imported style sheets.

Syntax

Following is the syntax −

@import url ("/* file path */") mediatypes {
   CSS-Code;
}

Let’s see an example for CSS @import rule:

HTML Document

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
@import url(screen.css) screen;
@import url(print.css) print;
</style>
</head>
<body>
<p> Vivamus commodo, dolor eu porttitor sagittis, orci nisl consectetur ipsum, vel volutpat nibh lectus at erat. Cras scelerisque faucibus tellus aliquam commodo.Donec sem urna, facilisis at ipsum id, viverra sollicitudin est. Nam rhoncus sollicitudin lorem, a accumsan purus varius eget. </p>
<p class="two">In ultrices lectus nisi. Nulla varius ex ut tortor congue viverra. Sed sodales vehicula leo, vitae interdum elit vehicula nec. Donec turpis nunc, iaculis et nisi sit amet, feugiat lacinia metus. </p>
<p>Suspendisse eget ligula et risus lobortis ornare id at elit. Suspendisse potenti. Vivamus pellentesque eleifend pellentesque. Vestibulum neque ante, iaculis a sagittis et, fermentum at metus.</p>
</body>
</html>

CSS document (screen.css)

p { color: navy; font-style: italic; }
.two { color: #c303c3; font-size: 20px; }
body { background-color: honeydew;}
CSS document (print.css):
p { color: red; font-style: italic;}
.two { color: #989898; font-size: 40px; }

Output

When document is visible in a screen mediatype −

When document is visible in a print mediatype −

Let’s see another example for CSS @import rule −

HTML Document

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>CSS @import rule</title>
<style type="text/css">
@import url(all.css) all;
</style>
</head>
<body>
<p>I am okay with shared space</p>
<p class="noneFloat">I want a private space</p>
<p>I am also okay with shared space</p>
<p>Me too</p>
</body>
</html>
CSS document (all.css):
p {
   float: left;
   margin: 10px;
   padding: 10px;
   color:white;
   background-color: #48C9B0;
   border: 4px solid #145A32;
}
p.noneFloat{
   float: none;
   clear: both;
   color: #34495E;
}

Output

raja
Updated on 06-Jan-2020 11:59:57

Advertisements