In writing styles, we can often see this situation The code is as follows <div style="width: 300px;border: 4px solid #000;margin: 20px;padding: 2px;"> Parent element <div style="border: 1px solid blue;height: 100px;white-space: nowrap;"> <span>child elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elementschild elements</span> </div> </div> If you think about this phenomenon carefully, why? You might ask, shouldn’t the child element expand the width of the parent element? Just like expanding the height of the parent element. Why? So how can we make the parent element of this child element expand to this width? There are two solutions here. 1. display: inline-block The layout style is as follows <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <style> #box1 { width: 500px; height: 200px; border: 2px solid blue; padding: 10px; } #box2 { white-space: nowrap; display: inline-block; } #box3 { width: 300px; height: 200px; background-color: blueviolet; display: inline-block; vertical-align: middle; } #box4 { width: 400px; height: 200px; background-color: black; display: inline-block; vertical-align: middle; } </style> <body> <div id="box1"> <div id="box2"> <div id="box3"></div> <div id="box4"></div> </div> </div> </body> </html> The result is as shown in the figure. Box3 and box4 expand the width of box2. 2. display: inline-flex The layout style is as follows <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <style> #box1 { width: 500px; height: 200px; border: 2px solid blue; padding: 10px; } #box2 { white-space: nowrap; display: inline-flex; } #box3 { width: 300px; height: 200px; background-color: blueviolet; vertical-align: middle; } #box4 { width: 400px; height: 200px; background-color: black; vertical-align: middle; } </style> <body> <div id="box1"> <div id="box2"> <div id="box3"></div> <div id="box4"></div> </div> </div> </body> </html> The effect is as follows This is the end of this article about how to use pure CSS to make child elements exceed the width limit of parent elements. For more relevant CSS child elements exceeding the width of parent elements, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! |
<<: Two ways to prohibit clearing the input text input cache in html
>>: MySQL changes the default engine and character set details
Table of contents Business Logic Data table struc...
The project test environment database data is los...
Exploiting a newly discovered sudo vulnerability ...
<br /> The website access speed can directly...
This article shares the installation and configur...
tomcat official website tomcat is equivalent to a...
For commercial databases, database upgrade is a h...
Basics In a relational database, each data table ...
This article shares the specific code of MySQL 8....
Configure service startup and shutdown in Linux s...
Preface In many management and office systems, tr...
Data cleaning (ETL) Before running the core busin...
After being tortured by the front-end cross-domai...
Pull the image docker pull season/fastdfs:1.2 Sta...
MySQL Users and Privileges In MySQL, there is a d...