Home >Web Front-end >HTML Tutorial >How to use HTML and CSS to implement a fixed-width and centered layout
How to use HTML and CSS to implement a fixed-width and centered layout
In web design, fixed-width and centered layout allows the page to be maintained on monitors of different sizes. Uniform style and layout. This layout can be achieved using HTML and CSS. This article will give a specific example to demonstrate how to use HTML and CSS to implement a fixed-width and centered layout.
First, we need to create an HTML file. You can use any text editor, such as Notepad, to create a new HTML file. Enter the following content in the file:
<!DOCTYPE html> <html> <head> <title>定宽居中布局示例</title> <style> body { width: 100%; background-color: #f3f3f3; font-family: Arial, sans-serif; } .container { width: 800px; margin: 0 auto; background-color: #fff; padding: 20px; box-shadow: 0 0 5px rgba(0, 0, 0, 0.1); } h1 { text-align: center; } </style> </head> <body> <div class="container"> <h1>这是一个定宽居中布局示例</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In dapibus ante ac dapibus interdum. Sed faucibus, erat eget laoreet consequat, metus mi tincidunt lorem, nec tincidunt ex magna at arcu. Sed consequat tincidunt ante vel laoreet. Pellentesque ultrices est non elit tincidunt, nec rutrum quam efficitur. Fusce euismod orci in laoreet semper. Donec gravida magna id erat maximus sollicitudin eget ac turpis. Nulla suscipit augue ac pretium tincidunt. Sed vestibulum, tellus vel molestie iaculis, est tellus consectetur nisi, id rutrum urna turpis vel ligula.</p> <p>Sed bibendum vehicula est, et pellentesque ex laoreet id. Cras ut ligula vitae mi tincidunt imperdiet vitae sit amet lorem. Morbi porta justo et lacinia elementum. Praesent eu dapibus neque. Ut mollis quis risus eget rutrum. Aliquam erat volutpat. Aliquam tristique ultrices vestibulum. Nulla facilisi.</p> </div> </body> </html>
In the above code, we define a div element named container, which will be used to wrap the content of the page. In the CSS, we set a width of 800 pixels for the container and set the margin to "0 auto", which means it will be centered horizontally on the page. We also added some background color, padding and shadow effects to the container to increase the aesthetics of the page.
Finally, we use h1 and two paragraphs to display some content that will be displayed in the container.
Save the file as a .html file and double-click to open it. You will see that the page is wrapped in a fixed width and displayed horizontally in the center. The background color of the page is gray and the background color of the content area is white. Both the title and text are also centered.
Through this example, we can see how to use HTML and CSS to implement a fixed-width and centered layout. You can modify this example to suit your own project.
Hope this article is helpful to you!
The above is the detailed content of How to use HTML and CSS to implement a fixed-width and centered layout. For more information, please follow other related articles on the PHP Chinese website!