4.Front End Development‐CSS - YukaKoshiba/MyknowledgeDocs GitHub Wiki
Front End Development_CSS @English Version
Create Date:2025/07/09
Last Update Date:2025/07/09
Basic Syntax Terminology Specifying Each Element @media Comments Changing Style Priority CSS Global Variables (:root)
Color Models
Units in CSS
Functions Used in CSS
Font Settings
Element Borders
Elements
Backgrounds
Adjusting Layout
Horizontal Alignment
Adding Gradients
Images
Adding Blur/Color Change
Adding Shadows
Rotate/Scale/Skew/Translate
Bullet Points
Navigation Menu Links (=Anchor Links)
Inserting Text Before/After String
Preventing Layout Collapse Changing Scroll Behavior Screen Reader Creating Animations
Sass
Variable Declaration and Usage・
Basic Syntax・
Nesting・
Mixin
Style Inheritance・
Importing Styles from Other Sass Files
An acronym for Cascading Style Sheets, it's a computer language used to arrange the layout of web pages (HTML/XML).
* Although CSS is a language, it is not a programming language because it does not have variables or functions.
The W3C (World Wide Web Consortium) is an international non-profit organization that promotes the standardization of web technologies. By using jigsaw.w3.org/css-validator/, you can check whether your web page's CSS code complies with W3C's standard specifications.
You can also check HTML and XHTML on the W3C site (validator.w3.org).
Described with properties containing key-value pairs.
target_element or .class_name or #id_name {
property: value;
property: value;
}
- CSS Pseudo-elements
A mechanism for treating elements that do not exist in HTML as if they do.
It expands design possibilities without directly modifying the HTML structure.
::before,::after,::first-line, etc. - CSS Pseudo-classes
A mechanism for applying styles when an element is in a specific state.
:hover,:active,:focus, etc.
| Element | Scope |
|---|---|
| * | All elements (Universal Selector) |
| *, "::before", "::after" | All elements and their pseudo-elements (pseudo-selectors) |
| Element1, Element2, Element3, … e.g.) menu, h1, h2 |
Specify multiple elements e.g.) Multiple selection of menu, h1, h2 |
| Element[class/id etc. = "value"] e.g. 1) div[class = "name"] e.g. 2) input[type="submit"] |
Matches exactly the specified value within the target element e.g. 1) div element exactly matching .name e.g. 2) input element where type is submit |
| Element[class/id ~= "value"] e.g.) div[class ~= "name"] |
Matches partially the class/id name within the target element e.g.) div element partially matching .name |
| Element class/id name e.g.) div .name |
Child element of the target element with the specified class/id name e.g.) Child element of div element that is .name |
| Element > class/id name e.g.) div > .name |
Direct child element of the target element with the specified class/id name e.g.) Direct child element of div element that is .name |
| Element:not(class/id name etc.) e.g.) div:not(#name) |
All elements within the target element except the specified one e.g.) All elements within the div element except #name |
| Element:first/last-of-type | Only the first/last of the target elements |
| Element:nth-of-type(number) | The element at the specified number within the target elements |
| Target element:hover e.g.) a:hover |
When the mouse cursor is over the target element e.g.) When the mouse cursor is placed over a link |
| Target element:active e.g.) a:active |
When the target element is actually clicked e.g.) When a link is clicked |
| Target element:visited e.g.) a:visited |
When the user has visited e.g.) When the user has visited a link |
Applies flexible CSS based on user environment (such as device, color inversion, scrolling settings, etc.).
/* Apply only to users who prefer smooth motion */
@media (prefers-reduced-motion: no-preference) {
* {
scroll-behavior: smooth;
}
/* For screen sizes below smartphone */
@media (max-width: 480px) {
nav ul {
display: none;
}
}
/* For tablet and larger screen sizes */
@media (min-width: 768px) {
.container {
width: 70%;
}
}
/* 縦向き(portrait)/横向き(landscape)で表示を変更する */
@media (orientation: landscape) {
.image {
width: 100%;
}
}
/* For non-handheld devices */
@media (not handheld) {
.sidebar {
width: 20%;
}
}
/* Process for users who prefer dark mode */
@media (prefers-color-scheme: dark) {
html {
class: "dark-mode";
}
}
/* What you want to comment out */To prevent unintended style overrides or to extremely boost the priority of a specific style during an urgent fix, use !important.
.example {
color: red !important;
}
Utilize the pseudo-class :root.
Property values used repeatedly can be set as custom properties, which makes redundant declarations easier to manage.
Additionally, by not directly specifying property values multiple times, it helps ensure layout consistency.
The custom properties you create can be used with var().
:root {
--background-color: #f0f0f0;
--font-color: #000000:
}
/* Use the created custom properties */
main {
backfround-color: var(--background-color);
color: var(----font-color);
}
background-color: red; /* Specified by color name */
background-color: rgb(255, 0, 0); /* Specified by rgb function */
background-color: #00FF00; /* Specified by hexadecimal/hexadecimal value */
background-color: hsl(240, 100%, 50%); /* Specified by hsl function */
background-color: rgba(0, 255, 255, 0.5); /* rgba function + opacity */
| Unit | Description |
|---|---|
| % | Percentage relative to the parent element's size. If the parent element is 16px, 100% → 16px, 50% → 8px |
| rem | Relative size based on the font size specified for the root (html tag). If html is set to 16px, 1rem → 16px, 2rem → 32px |
| em | Percentage relative to the parent element's size. 1em = 100% (same as parent size) |
| px | Specifies size in pixels. This is a fixed value, so it depends on the screen resolution. |
| vw | Relative length unit based on the viewport width. When the browser window's width changes, the element's size also changes accordingly. The user's screen width is considered 100vw. 5vw = 5% of the viewport width |
| vh | Abbreviation for viewport height. Relative length unit based on the viewport height. When the browser window's height changes, the element's size also changes accordingly. The user's screen height is considered 100vh. 5vh = 5% of the viewport height |
| Function | Description | Usage |
|---|---|---|
| calc() | Calculates based on other values. | padding: calc(1.25rem + 2px) ; |
| Parameter | Description | Value | Notes |
|---|---|---|---|
| font | Shorthand for font properties | Various parameter values | There is no strict order, but it's often written as: font-style, font-weight, font-size, line-height, font-family |
| color | Text color | Keyword/rgb/rgba/HEX value | |
| font-style | Specifies text style | normal: Normal italic: Italic oblique: Oblique |
|
| font-size | Text size | Number + unit | |
| font-weight | Text thickness | normal (initial value) bold Numeric value (100-900) Can be specified in units of 100, 700 or more is bold |
|
| font-family | Text font | sans-serif (Gothic) serif (Mincho) monospace (Monospaced font), etc. |
Multiple fonts can be specified The first font is usually applied, but if it's not installed on the user's system, the second font is applied |
| text-decoration: none | Underline setting | none: Removes underline underline: Adds underline |
Setting text-decoration: underline dotted red; also specifies the line type and color |
| line-height | Line spacing | Number + em/%/unitless/px If font size is 12px, 1em/ 100%/ 1 -> 12px |
|
| letter-spacing | Letter spacing | Number + em/(px: deprecated) Initial value is normal (letter-spacing: 0;) |
|
| text-align | Horizontal alignment of inline elements (text and images) | left: Align to the left (start: initial value = left) center: Align to the center right: Align to the right (end: Align to the end = right) |
|
| text-transform | Specifies uppercase representation of text | capitalize: First letter of each word capitalized uppercase: All uppercase lowercase: All lowercase, etc. |
|
| text-indent | Sets the width of the empty space (indentation) before the lines of text within a block | Value + unit (mm/px/%/em) Keyword value or global value |
|
| white-space | Specifies whitespace (spaces/tabs/line breaks) | Reference | |
| justify-content | Specifies the alignment of content (text) |
Use the border property.
In addition to the below, you can also specify only certain lines for the top, bottom, right, or left borders, such as border-top/bottom/right/left.
| Parameter | Setting Content | Value | Notes |
|---|---|---|---|
| border-width | Border thickness | No line: none or 0 Specified with keyword values / global values Number + unit (px/em/rem/no unit) |
Reference |
| border-style | Line style | none, solid, dotted, dashed, double, etc. | Reference |
| border-color | Line color | Specified with keywords/rgb/rgba/HEX values | Specify transparent to make it transparent |
| border-radius | Corner roundness | Number + unit (%/px) | Different values can be specified for the 4 corners 1 value: All corners 2 values: Top-left/Bottom-right, Bottom-left/Top-right 3 values: 4 values: Clockwise from top-left |
| border | Batch setting for the above | Number + unit (%/px) | Different values can also be specified for each of the 4 corners To remove the border, specify border:0 |
You can specify padding and margin for only a few sides, such as top, bottom, right, or left.
For example: padding or margin-top/bottom/right/left, etc.
| Parameter | Setting Content | Value | Notes |
|---|---|---|---|
| width and height | Width and height of the element | Number + Unit (%/vw/vh/px) | |
| padding | Inner spacing inside the border | Number + Unit (%/vw/vh/px) Initial value is auto |
|
| margin | Outer spacing outside the border | Number + Unit (%/vw/vh/px) | Can set multiple values 1 value: top, right, bottom, left 2 values: top/bottom, left/right 3 values: top, left/right, bottom 4 values: clockwise from top |
| overflow | Handling of child elements overflowing the parent element | visible: displayed as is (initial value) scroll: overflowing part hidden/scrollable hidden: overflowing part hidden/not scrollable auto: behaves like scroll if necessary |
|
| clip-path | Clips the display area of the element | Reference | Specifying with clip only allows specifying rectangles |
Specified with the background property.
| Parameter | Setting Content | Value | Notes |
|---|---|---|---|
| background | Shorthand for all background properties of an element | Background color, image, etc. | Multiple background colors can be specified. Gradients are possible using linear-gradient(). |
| background-color | Background color of an element | Keyword/rgb/rgba/HEX value | Only one color can be specified; gradients are not possible. |
| background-image | Background image | url(...path.jpeg etc.) | |
| background-attachment | How the background image displays when scrolling | Specified by keyword value or global value | |
| background-clip | The area where the background image is displayed | Keyword value or global value | |
| background-size | Display size of the background image | Keyword value or global value Or, number + unit (%/em/px) |
|
| background-repeat | Repetition of the background image | Keyword value or global value | |
| background-repeat | Background image | url(...path.jpeg) | |
| opacity | Opacity of the background color (background-color) | Specified from 0 to 1 or 0 to 100% |
| Parameter | Setting Content | Value |
|---|---|---|
| float | Floats an element within its parent, allowing text/other elements to wrap around it. | none: default If left, etc. is specified, elements will align horizontally, wrapping around each other. |
| position | Positioning of an element within the document. | static: default relative: Positioned relative to its normal position absolute: Fixed to a specific position Always visible even when scrolling fixed: Fixes an element in position Doesn't move with scrolling sticky: Toggles between relative and fixed based on scroll position If anything other than static is specified, it is affected by top/right/left/bottom. |
| flex-direction | CSS property that specifies how child elements are laid out within a flex container. Allows flexible creation of various layouts, such as horizontal/vertical alignment of child elements, or reversing their order. |
|
| flex-wrap | Item wrapping settings | wrap: Wraps items to the next line/column nowrap: Prevents item wrapping and shrinks as needed *default |
| justify-content | Adjusts the alignment of child elements along the main axis within a flex container. Can choose to align horizontally/vertically, to the left/center/right of the parent element. |
|
| flex-direction | Refers to the direction perpendicular to the specified main axis. | row: Cross-axis is vertical (top/bottom) column: Cross-axis is horizontal (left/right) |
| gap | When using grid/flexbox, can specify the gap between child elements collectively. | Length (px/em, etc.), % |
| object-fit | Specifies how replaced elements (<img> or <video>) are displayed within their parent. Used to maintain image aspect ratio or fit into a specific area. |
|
| z-index | Specifies the stacking order of overlapping elements. | Number/Global value |
| grid-template | Provides fine-grained control over element placement. Allows building complex layouts more flexibly and intuitively than float/flexbox. |
Refer to external sites for grid-template-columns, etc. |
| display | Determines the display format of an element. | none: Not displayed on the browser, as if it doesn't exist on the page. Does not affect the layout of other elements, but is accessible via JavaScript. flex: Child elements placed within the container can be freely arranged/resized. |
There are two methods for adjusting spacing with CSS:
(1) margin-bottom
(2) specifying pre with white-space
Method (2) is deprecated, so method (1) should be used.
Utilize the linear-gradient function.
linear-gradient(gradientDirection, color1 color-stop, color2 color-stop, ...);
/* Gradient from left to right, red to green */
background: linear-gradient(90deg, rgb(255, 0, 0),rgb(0, 255, 0));
/* Gradient from top to bottom, red to green, with red covering 75% */
background: linear-gradient(180deg, rgb(255,0,0) 75%, rgb(0, 255, 0));
gradientDirection: The direction of the gradient (90deg: left → right, 180deg: top → bottom *default)
color-stop: Specifies the position where the gradient should end (0% to 100%)
To repeat a gradient, use repeating-linear-gradient.
| Parameter | Setting Content | Value | Notes |
|---|---|---|---|
| width: max(fixed width, relative) | Maximum size of the image | (e.g.) max(250px, 25vw) | By default, the image is displayed at its original size. Flexibly adjusts element width in responsive design. |
| aspect-ratio | Adjusts the ratio of width to height | ・width/height aspect-ratio: 35/4; ・numeric value (width relative to height) aspect-ratio: 0.5; |
Effective for responsive design |
Utilize `filter`.
| Value | Setting Content |
|---|---|
| blur(blur radius) | Sets a Gaussian blur. Blur radius units are px, rem, etc. |
| brightness(number/%) | Specifies brightness. 1/100%: original brightness 2/200%: twice the original brightness |
| contrast(%) | Specifies contrast. |
| grayscale(%) | Converts to grayscale. |
Utilize `box-shadow`.
box-shadow: offsetX offsetY blurRadius spreadRadius color;
/* Adds a 5px red shadow to the right and top */
box-shadow: 5px -5px 5px red;
/* Emits a 20px red glow */
box-shadow: 0 0 20px 0 red; /* 0 can be omitted */
offsetX: + right - left
offsetY: + down - up
blurRadius: excessive roundness (none/0: square, other values: rounded corners)
spreadRadius: degree of shadow spread (0/none: original shadow size, number + px: specifies spread range, +: outward -: inward)
Utilize `transform`.
| Value | Setting Content |
|---|---|
| transform-origin | Sets the origin for element transformations (transform). 1 value: center/bottom/left/right 2 values: center/left/right + center/bottom/top 3 values: 2 values + Z-offset |
| rotate(rotation direction numerical deg) | Rotation direction: -: counter-clockwise +: clockwise x/y/z: each axis direction Can also be specified with rotate(vector and angle value) |
| skew(skew angle) | Represents the amount of distortion applied to each axis. 1 value specified: X-axis distortion 2 values specified: X-axis distortion, Y-axis distortion |
You can remove bullets or change their shape.
nav > ul {
/* Removes bullet points */
list-style: none;
}
Color/Alignment Adjustment
li > a {
/* Makes the anchor link text color the same as the parent element's (list item's) text color */
color: inherit;
/* Removes the default underline for anchor links */
text-decoration: none;
/* Prevents overflow */
flex-wrap: wrap;
align-items: center;
padding-inline-start: 0;
margin-block: 0;
height: 100%;
}
```Change the color when the user hovers over the menu.
nav > ul > li:hover {
background-color: #dfdfe2; /* Change background color */
color: #1b1b32; /* Change text color */
cursor: pointer; /* Change cursor to a hand shape */
}
Used when you want to arrange menus or other elements horizontally.
Specifies the display format of elements, such as block-level elements, inline elements, and flex items.
nav > ul
{
display: flex;
padding: 0;
}
/* Add space as needed */
/* Add ★ before and ! after all text within tags */
p::before {
content: "★ ";
}
p::after {
content: "!";
}
When adapting to various screen sizes, such as in responsive design, properly setting the box-sizing property can prevent layout collapse.
/* For all elements, adjust width and height to their content's width and height */
*{
box-sizing: content-box; /* Element's width and height do not include padding or border size */
box-sizing: border-box; /* Element's width and height include padding and border size */
}
When adapting to various screen sizes, such as in responsive design, properly setting the box-sizing property can prevent layout collapse.
scroll-behavior:smooth;
/* For all elements, adjust width and height to their content's width and height */
.sr-only{
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
}
You can create animations with CSS by using @keyframes.
While you can't create complex animations like with JavaScript,
it offers high performance as it doesn't require changing the HTML structure.
It's processed separately from JavaScript's main thread, allowing for parallel processing.
Basic syntax: @keyframes rule-name { specify animation movement }
You can specify the animation's behavior at 0% (from) for the start, 100% (to) for the end, and numerical percentages for intermediate actions.
/* Create an animation named 'wheel' */
@keyframes wheel {
/* Rotate 360 degrees */
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/* Use the created animation */
Target element {
/* Animation to apply */
animation-name: wheel;
/* Animation duration */
animation-duration: 10s;
/* Specify animation speed */
animation-timing-function: linear; /* Proceeds at the same speed from start to finish */
animation-timing-function: ease-in-out; /* Slows down the animation at the beginning and end */
/* Repeat animation infinitely */
animation-iteration-count: infinite;
}
/* The above specifications can be combined into one line */
.wheel {
animation: wheel 10s linear infinite;
}
A CSS framework used for front-end development to create responsive web pages and applications.
It includes frequently used features, allowing for the rapid development of visually appealing websites.
Originally developed by Twitter, it was called Twitter Bootstrap, but
it became open-source and independent from Twitter, and its name was changed to Bootstrap.
Official Web Site in English
Example
Background of its Birth
・The expansion of app usage led to a demand for rapid development.
・There was a growing need for responsive web design compatible with mobile devices, not just PCs.
By specifying the class names pre-defined in Bootstrap for elements,
you can arrange the layout without having to write CSS yourself.
= Can be used even without prior knowledge
You can customize Bootstrap's styles by using Sass variables.
Various templates are available, ranging from free to paid options.
To implement Bootstrap, use a CDN (Content Delivery Network) and specify the CSS file with an HTML `` tag.
-- HTML
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</head>
Styles are applied immediately by simply applying predefined CSS classes to HTML elements like <div> according to the format.
<footer>
<div class="text-bg-secondary p-3">Copyright © 2025 Yuka Koshiba</div>
</footer>
Text Color
text-primary: Changes to primary color (usually blue)
text-secondary: Secondary color (usually gray)
text-success: Color indicating success (usually green)
text-danger: Color indicating danger (usually red)
text-warning: Color indicating warning (usually yellow)
text-info: Color for information display (usually a bluish hue)
text-light: Light color
text-dark: Dark color
Text Alignment
text-center: Centers the text
Object Arrangement
row: Arranges elements horizontally on a web page
col-screen-size-column-width: Specifies the width of columns to be placed within a row
・Screen size: sm, md, lg, xl
・Column width: Integer from 1 to 12 = occupies n/12ths of the screen width
<div class="row">
<div class="col-xs-6"></div>
<div class="col-xs-4"></div>
</div>
well: Provides visual depth
btn: Default button style
btn-default: Extends the btn class; used with the btn class to give buttons a standard appearance
btn-primary: Emphasized button used for primary action buttons
btn-secondary: Button used for secondary action buttons
btn-success: Button used for success action buttons
btn-danger: Button used for dangerous action buttons
btn-warning: Button used for action buttons requiring caution
btn-info: Button used for informational buttons
btn-light: Light-colored button
btn-dark: Dark-colored button
An extension language for CSS.
It allows you to write reusable functions, nesting, and conditional statements, which improves code readability compared to regular CSS.
The method for importing Sass varies depending on the development environment, such as VS Code or CodePen.
Variable declaration differs from regular CSS; you declare them using $variable-name: value;.
// Variable declaration
$main-fonts: Arial, sans-serif;
$headings-color: green;
// Use variables to change the style of target elements
h1 {
font-family: $main-fonts;
color: $headings-color;
}
Nesting can improve code readability.
// Regular CSS
article {
height: 200px;
}
article p {
color: white;
}
article ul {
color: blue;
}
// When using Sass nesting
article {
height: 200px;
p {
color: white;
}
ul {
color: blue;
}
}
A group of CSS declarations that can be reused throughout the style sheet.
Mixin definitions are written using `@mixin` + a custom name.
If there are arguments, specify them, and write the style settings within `{ }`.
To apply a mixin, write `@include` + custom name.
// mixin definition
@mixin reset-list($parm1, $parm2) {
margin: 0;
padding: 0;
list-style: none;
}
// mixin application
nav ul {
@include reset-list(10px, 10px);
}
// if statement
@mixin border-stroke($val) {
@if $val == light {
border: 1px solid black;
}
@else if $val == medium {
border: 3px solid black;
}
@else if $val == heavy {
border: 6px solid black;
}
@else {
border: none;
}
}
// for loop
@for $i from 1 through 12 {
.col-#{$i} { width: 100%/12 * $i; }
}
// for each loop
@each $color in blue, red, green {
.#{$color}-text {color: $color;}
}
You can inherit already specified styles using `@extend` + the name of the element to inherit from.
// Inherit styles specified for .info-important to .info
.info-important {
background-color: magenta;
@extend .info;
}
A feature useful for code reuse and modularization.
// Import the Sass file "_mixins.scss"
@import 'mixins'
mdn web docs
ProEngineer
W3CScholl