Top 12 CSS Code Tips
Top 12 CSS Code Tips
<style>
/* Target elements with a specific class */
.highlight {
background-color: yellow;
font-weight: bold;
}
/* Target elements with a specific ID */
#header {
background-color: lightgray;
padding: 20px;
text-align: center;
}
</style>
<body>
<div id="header">
<h1>Welcome to my website</h1>
</div>
<p>This is some example text.</p>
<p class="highlight">This text is highlighted.</p>
</body>
<style>
/* Define a CSS class for the parent element */
.container {
font-family: Arial, sans-serif;
font-size: 16px;
background-color: #f2f2f2;
}
/* Child elements will inherit the styles from the
parent */
.item {
background-color: lightblue;
padding: 20px;
text-align: center;
}
</style>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<style>
.container {
padding: 20px;
background-color: lightgray;
}
.item {
width: 200px;
height: 100px;
margin: 10px;
background-color: lightblue;
}
</style>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
</div>
<style>
.container {
padding: 20px;
background-color: lightgray;
}
.item {
margin: 10px 20px;
background-color: lightblue;
}
</style>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
</div>
In this example, the margin property for the .item class uses
shorthand to specify a top and bottom margin of 10px and left
and right margins of 20px.
<style>
/* Use a CSS reset stylesheet */
/* http://meyerweb.com/eric/tools/css/reset/ */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* Add your own styles */
body {
<style>
/* Define a general style for all links */
a {
text-decoration: none;
color: blue;
}
<style>
.container {
display: flex;
flex-wrap: wrap;
}
<style>
/* Use a simple selector */
.box {
background-color: lightblue;
padding: 20px;
}
/* Avoid using complex expressions */
In this example, the selector for the .box class is simple, using
only the class name. The selector for the style that changes the
background color of every third box uses the nth-child
pseudo-class, which is less complex than using multiple class
names or multiple selectors.
<style>
/* Default styles */
.box {
width: 100%;
padding: 20px;
text-align: center;
background-color: lightblue;
In this example, the default styles for the .box class set its width
to 100% and its background color to lightblue. The media query
checks if the screen width is at least 600px wide, and if so, sets
the width of the .box to 50%. This means that the box will take
up half of the screen on devices with a screen width of 600px or
more, and will take up the full screen on smaller devices.
<style>
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
In this example, the .container class is set to use the display: flex
property to activate Flexbox. The flex-direction property is set to
row to arrange the elements in a row. The justify-content
property is set to space-between to distribute the space between
the elements evenly. The align-items property is set to center to
align the elements vertically in the center of the container. The
.box class sets the width of each box to 30% and the height to
100px. This results in the three boxes being arranged in a row,
with equal space between them, and centered vertically.
<style>
.box {
width: 100px;
height: 100px;
background-color: lightblue;
transition: width 2s, height 2s;
}
.box:hover {
width: 200px;
height: 200px;
}
</style>
<body>
<div class="box">Hover over me</div>
</body>
In this example, the .box class sets the width and height of a box
to 100px and the background color to lightblue. The transition
property is set to width 2s, height 2s to animate changes to the
width and height over 2 seconds. The .box:hover selector targets
the box when the user hovers over it, and changes the width and
height to 200px. As a result, when the user hovers over the box,
it will smoothly animate from 100px to 200px in both width and
height over 2 seconds.