Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮     
     ❯   

CSS Tutorial

CSS HOMECSS IntroductionCSS SyntaxCSS SelectorsCSS How ToCSS CommentsCSS ErrorsCSS ColorsCSS BackgroundsCSS BordersCSS MarginsCSS PaddingCSS Height/WidthCSS Box ModelCSS OutlineCSS TextCSS FontsCSS IconsCSS LinksCSS ListsCSS TablesCSS DisplayCSS Max-widthCSS PositionCSS Z-indexCSS OverflowCSS FloatCSS Inline-blockCSS AlignCSS CombinatorsCSS Pseudo-classesCSS Pseudo-elementsCSS OpacityCSS Navigation BarsCSS DropdownsCSS Image GalleryCSS Image SpritesCSS Attr SelectorsCSS FormsCSS CountersCSS UnitsCSS InheritanceCSS SpecificityCSS !importantCSS Math FunctionsCSS OptimizationCSS AccessibilityCSS Website Layout

CSS Advanced

CSS Rounded CornersCSS Border ImagesCSS BackgroundsCSS ColorsCSS Color KeywordsCSS GradientsCSS ShadowsCSS Text EffectsCSS Custom FontsCSS 2D TransformsCSS 3D TransformsCSS TransitionsCSS AnimationsCSS TooltipsCSS Image StylingCSS Image ModalCSS Image CenteringCSS Image FiltersCSS Image ShapesCSS object-fitCSS object-positionCSS MaskingCSS ButtonsCSS PaginationCSS Multiple ColumnsCSS User InterfaceCSS VariablesCSS @propertyCSS Box SizingCSS Media QueriesCSS MQ Examples

CSS Flexbox

Flexbox IntroFlex ContainerFlex ItemsFlex Responsive

CSS Grid

Grid IntroGrid ContainerGrid ItemsGrid 12-column LayoutCSS @supports

CSS Responsive

RWD IntroRWD ViewportRWD Grid ViewRWD Media QueriesRWD ImagesRWD VideosRWD FrameworksRWD Templates

CSS SASS

SASS Tutorial

CSS Examples

CSS TemplatesCSS ExamplesCSS EditorCSS SnippetsCSS QuizCSS ExercisesCSS WebsiteCSS SyllabusCSS Study PlanCSS Interview PrepCSS BootcampCSS Certificate

CSS References

CSS ReferenceCSS SelectorsCSS CombinatorsCSS Pseudo-classesCSS Pseudo-elementsCSS At-rulesCSS FunctionsCSS Reference AuralCSS Web Safe FontsCSS AnimatableCSS UnitsCSS PX-EM ConverterCSS ColorsCSS Color ValuesCSS Default ValuesCSS Browser Support

CSSThe display Property


The CSS display Property

Thedisplay property is an important CSS property for controlling layout. It specifies whether an HTML element is treated as a block or an inline element.

Every HTML element has a default display value, depending on what type of element it is. The default display value for most elements isblock orinline.

Thedisplay property is used to change the default display behavior of HTML elements.


Block-level Elements

A block-level element ALWAYS starts on a new line and takes up the full width available (stretches out to the left and right as far as it can).

The <div> element is a block-level element.

Examples of block-level elements:

  • <div>
  • <h1> - <h6>
  • <p>
  • <form>
  • <header>
  • <footer>
  • <section>

Inline Elements

An inline element DOES NOT start on a new line and only takes up as much width as necessary.

This isan inline <span> element inside a paragraph.

Examples of inline elements:

  • <span>
  • <a>
  • <img>


Common display Values

The CSSdisplay property has many values. The following table lists the most commonly used:

ValueDescription
inlineDisplays an element as an inline element
blockDisplays an element as a block element
contentsMakes the container disappear, making its child elements children of the element the next level up in the DOM
flexDisplays an element as a block-level flex container
gridDisplays an element as a block-level grid container
inline-blockDisplays an element as an inline-level block container. The element itself is formatted as an inline element, but you can apply height, width, padding, and margin values
noneThe element is completely hidden from the document flow (does not take up any space).

CSS display: none;

When usingdisplay: none; the element is completely hidden from the document flow and does not take up any space.

It is commonly used with JavaScript to hide or show elements without deleting and recreating them.

Click to show hidden panel

This panel contains a <div> element, which is hidden by default, with display: none.

We use JavaScript to show it (change it to display: block).

Example

How to use CSS and JavaScript to show a hidden element on a click event:

<style>
#panel {
  display: none;
}
</style>

<script>
function myFunction() {
  document.getElementById("panel").style.display = "block";
}
</script>
Try it Yourself »

Example

How to use CSS and JavaScript to toggle between show and hide on a click event:

<style>
#panel {
  display: none;
}
</style>

<script>
function myFunction() {
  var x = document.getElementById("panel");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
</script>
Try it Yourself »

Override the Default Display Value

Thedisplay property is used to change the default display behavior of HTML elements.

Changing an inline element to a block element, or vice versa, can be useful for making the page look a specific way, and still follow the web standards.

A common example is to change<li> elements to inline, to create a horizontal menu:

Example

li {
  display: inline;
}
Try it Yourself »

Note: Setting the display property of an element only changeshow the element is displayed, NOT what kind of element it is. So, an inline element withdisplay: block; is not allowedto have other block elements inside it.

The following example displays <span> elements as block elements:

Example

span {
  display: block;
}
Try it Yourself »

The following example displays <a> elements as block elements:

Example

a {
  display: block;
}
Try it Yourself »

Example of More Display Values

The following example demonstrates some more display values:

Example

p.ex1 {display: none;}
p.ex2 {display: inline;}
p.ex3 {display: block;}
p.ex4 {display: inline-block;}
p.ex5 {display: flex;}
p.ex6 {display: grid;}
Try it Yourself »

Hide an Element - Use display:none or visibility:hidden?

display:none

Italy

visibility:hidden

Forest

Reset

Lights

Try it Yourself »

Hiding an element can be done by setting thedisplay property tonone. The element will be hidden, and the page will be displayed as if the element is not there:

Example

h1.hidden {
  display: none;
}
Try it Yourself »

You can also usevisibility:hidden; to hide an element.

However, with this property, the element will be hidden, but it will still take up the same space as if it was visible:

Example

h1.hidden {
  visibility: hidden;
}
Try it Yourself »


CSS Display/Visibility Properties

PropertyDescription
displaySpecifies how an element should be displayed
visibilitySpecifies whether or not an element should be visible


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp