HTML-CSS: Display table centering
HTML-CSS : Exercise-26 with Solution
Using HTML, CSS vertically and horizontally centers a child element within its parent element, using display: table.
- Use display: table to make the .center element behave like a <table> element.
- Set height and width to 100% to make the element fill the available space within its parent element.
- Use display: table-cell on the child element to make it behave like a <td> elements.
- Use text-align: center and vertical-align: middle on the child element to center it horizontally and vertically.
- The outer parent (.container) must have a fixed width and height.
HTML Code:
Explanation:
- This HTML code creates a simple webpage with a container div and a child element to be centered.
- The <!--License: https://bit.ly/3GjrtVF--> comment indicates the license information for the code.
- <!DOCTYPE html> declares the document type and version of HTML, which is HTML5.
- <html> marks the start of the HTML document.
- <head> contains meta-information about the HTML document, such as character encoding and viewport settings.
- The <title> element sets the title of the webpage.
- <body> contains the content of the HTML document.
- Within the w3rcontainer div, there's a center div containing a span element.
CSS Code:
Explanation:
- .w3rcontainer styles the container for the centered child element, setting its border, height, and width.
- .center styles the centering element, setting it to be displayed as a table with full height and width.
- .center > span styles the span element within the centering element, displaying it as a table cell and centering its text horizontally and vertically.
HTML-CSS Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.