How to make p tags parallel in CSS
How to make p tags parallel in CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display p tags side by side</title>
<style>
.inline-p {
display: inline-block;
width: 30%;
margin: 0 10px;
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<p class="inline-p">This is the first paragraph</p>
<p class="inline-p">This is the second paragraph</p>
<p class="inline-p">This is the third paragraph</p>
</body>
</html>
Code running result:
In the above example, we added the class name inline-p
to the <p>
tags and set the display: inline-block;
property to display the three <p>
tags side by side.
Method 2: Using float: left;
Another common method is to use the float: left;
property to display the <p>
tags side by side. Here is a sample code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display p tags side by side</title>
<style>
.float-p {
float: left;
width: 30%;
margin: 0 10px;
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<p class="float-p">This is the first paragraph</p>
<p class="float-p">This is the second paragraph</p>
<p class="float-p">This is the third paragraph</p>
</body>
</html>
Code execution results:
In the above example, we added the class name float-p
to the <p>
tags and set the float: left;
attribute, causing the three <p>
tags to display side by side.
Method 3: Using flexbox
Using flexbox
layout is also a common method to achieve side-by-side display of elements. Here is a sample code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display p tags side by side</title>
<style>
.flex-container {
display: flex;
}
.flex-p {
flex: 1;
margin: 0 10px;
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<div class="flex-container">
<p class="flex-p">This is the first paragraph</p>
<p class="flex-p">This is the second paragraph</p>
<p class="flex-p">This is the third paragraph</p>
</div>
</body>
</html>
Code Running Result:
In the above example, we use the flexbox
layout. By setting the display: flex;
and flex: 1;
properties, the three <p>
tags are displayed side by side.
Method 4: Using grid
Using a grid
layout is also a common method for displaying elements side by side. Here is a sample code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display p tags side by side</title>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.grid-p {
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<div class="grid-container">
<p class="grid-p">This is the first paragraph</p>
<p class="grid-p">This is the second paragraph</p>
<p class="grid-p">This is the third paragraph</p>
</div>
</body>
</html>
Code running results:
In the above example, we use the grid
layout. By setting the display: grid;
property and the grid-template-columns: repeat(3, 1fr);
property, the three <p>
tags are displayed side by side.
Through the above methods, we can achieve parallel display of <p>
tags, making the page look more beautiful. In actual projects, you can choose the appropriate method to achieve parallel display of elements based on specific needs.