When using the border abbreviation attribute of CSS, you can set the width, style and color of the element border in one line of code at the same time. It contains three properties: border-width, border-style, border-color, which are usually written in this order, for example: border: 2px solid #333; If one of these is omitted, the browser will use the default value, and if the color is not specified, the default text color will be used. Common border styles include solid (solid line), dashed (dashed line), dotted (dotted line), double (double line) and none (borderless). Border-style must be set, otherwise the border will not be displayed. If you only need to set borders for one side, you can use border-top, border-right, border-bottom or border-left, such as border-bottom: 3px double red;. Abbreviation is suitable for setting all values at once, while long writing is suitable for modifying a certain item separately. For example, when performing animations or JavaScript operations, it is recommended to use long writing.
When you use the border
shorthand property in CSS, you're essentially setting the width, style, and color of an element's border all in one line. It's a neighbor way to keep your code clean without writing out each individual property.

What the border
shorthand includes
The border
shorthand combines three separate properties:
-
border-width
(how thick the border is) -
border-style
(like solid, dashed, etc.) -
border-color
(the color of the border)
You can write them in any order, but typically it's written as:
border: [width] [style] [color];

For example:
border: 2px solid #333;
This sets a 2-pixel wide solid border in dark gray around the element.

If you don't include a value for one of the properties, browsers usually fall back to the default. For instance, if you skip color like this:
border: 1px dashed;
The browser will use the current text color by default.
Common border styles you can use
There are several values you can use for border-style
. Some of the most common ones are:
-
solid
– a single solid line -
dashed
– a series of dashes -
dotted
– a series of dots -
double
– two solid lines -
none
– no border at all (useful when you want to override inherited borders)
You'll often see solid
and dashed
used in UI elements like buttons or cards to help visually separate sections on a page.
Also, note that border-style
must be set — otherwise, the border won't show up. If you just set width and color but forget the style, nothing appears.
Setting borders on specific sides
Sometimes you only want a border on one side of an element. You can still use shorthand, but not the main border
property.
Instead, use:
-
border-top
-
border-right
-
border-bottom
-
border-left
Each of these works the same way as the main border
shorthand.
For example:
border-bottom: 3px double red;
This gives only the bottom edge a red double border that's 3 pixels thick.
This is super handy when styling things like form inputs, where you might only want a bottom border to indicate a field.
When to use shorthand vs longhand
Using the shorthand is great when you want to set all values at once and keep your code concise. But sometimes you'll want to override just one part — say, change the color without touching the width or style. In those cases, using longhand properties like border-color
makes more sense.
Also, if you're animating borders or changing parts of the border with JavaScript, longhand properties tend to be more predictable.
But for most everyday uses — especially when starting out — the shorthand is perfectly fine and keeps your CSS easier to scan.
Basically that's it.
The above is the detailed content of Explain the `border` shorthand property. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

AnimatingSVGwithCSSispossibleusingkeyframesforbasicanimationsandtransitionsforinteractiveeffects.1.Use@keyframestodefineanimationstagesforpropertieslikescale,opacity,andcolor.2.ApplytheanimationtoSVGelementssuchas,,orviaCSSclasses.3.Forhoverorstate-b

Theconic-gradient()functioninCSScreatescirculargradientsthatrotatecolorstopsaroundacentralpoint.1.Itisidealforpiecharts,progressindicators,colorwheels,anddecorativebackgrounds.2.Itworksbydefiningcolorstopsatspecificangles,optionallystartingfromadefin

The scope of CSS custom properties depends on the context of their declaration, global variables are usually defined in :root, while local variables are defined within a specific selector for componentization and isolation of styles. For example, variables defined in the .card class are only available for elements that match the class and their children. Best practices include: 1. Use: root to define global variables such as topic color; 2. Define local variables inside the component to implement encapsulation; 3. Avoid repeatedly declaring the same variable; 4. Pay attention to the coverage problems that may be caused by selector specificity. Additionally, CSS variables are case sensitive and should be defined before use to avoid errors. If the variable is undefined or the reference fails, the fallback value or default value initial will be used. Debug can be done through the browser developer

Mobile-firstCSSdesignrequiressettingtheviewportmetatag,usingrelativeunits,stylingfromsmallscreensup,optimizingtypographyandtouchtargets.First,addtocontrolscaling.Second,use%,em,orreminsteadofpixelsforflexiblelayouts.Third,writebasestylesformobile,the

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

There are three ways to insert web pages in CSS: 1. Inline styles are suitable for single element modification or debugging, but are not conducive to maintenance and reuse; 2. Internal style sheets are suitable for small projects or test pages, and the current page style is unified but cannot be shared across pages; 3. External style sheets are most recommended, suitable for multi-page projects, centrally manage styles, improve maintenance and loading speed. In actual development, external style sheets are preferred, and the other two methods are considered in special circumstances.
