I'm trying to recreate something like this but now I'm running into some issues. The first problem I had was to center my 'main' div, I did this:
body{ background: linear-gradient(110deg, #fdcd3b 60%, #ffed4b 60%); -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } .main { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); }
But because of 'position: fixed', my background is no longer showing, and I added background in body.
Another problem I ran into was that on the website I wanted to recreate, there were two p elements next to each other, but I couldn't get my two p elements together using this code:
p{ margin: 0; display:inline; float:left; }
This is what my p element and the entire html looks like:
<!DOCTYPE html> <html> <head> <title>练习网站</title> <link rel="stylesheet" href="style.css"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous"> <script src="https://kit.fontawesome.com/c934cb6477.js" crossorigin="anonymous"></script> </head> <body> <main class="container"> <!-- <header> <div class="header"> </div> </header> --> <div class="main"> <h1>嘿,我是测试</h1> <h2>学生软件开发者。</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur aliquam arcu et urna placerat dignissim. Nulla nec sollicitudin lorem, at maximus lorem. Cras porta quis sapien ut ornare. Nullam nec sem molestie, scelerisque sapien sit amet, dignissim ex. Sed eget felis gravida, maximus risus a, sodales libero. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur aliquam arcu et urna placerat dignissim. Nulla nec sollicitudin lorem, at maximus lorem. Cras porta quis sapien ut ornare. Nullam nec sem molestie, scelerisque sapien sit amet, dignissim ex. Sed eget felis gravida, maximus risus a, sodales libero. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p> </div> <footer class="bg-dark text-center text-white footer fixed-bottom"> <div class="container p-4 pb-0"> <section class="mb-4"> <!-- Linkedin --> <a class="btn btn-outline-light btn-floating m-1" href="#" target="_blank" role="button" ><i class="fab fa-linkedin-in"></i ></a> <!-- Github --> <a class="btn btn-outline-light btn-floating m-1" href="#" target="_blank" role="button" ><i class="fab fa-github"></i ></a> <!-- CV --> <a class="btn btn-outline-light btn-floating m-1" href="#" target="_blank" role="button" ><i class="fa-regular fa-address-card"></i ></a> </section> </div> <!-- 版权 --> <div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.2);"> © <span id="current-year"></span> 版权所有: <a class="text-white">测试</a> </div> </footer> </main> <script src="/Files/Js/function.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8" crossorigin="anonymous"></script> </body> </html>
P粉2084690502023-09-10 09:04:18
Instead of absolutely positioning your main content, you can apply display: flex to the body element and set it to 100vh so it covers the viewport. Then the background will reappear.
For your p elements, similarly, wrap them in a div with display: flex and then place them side by side like the example below.
body { background: linear-gradient(110deg, #fdcd3b 60%, #ffed4b 60%); background-size: cover; height: 100vh; display: flex; /*display flex allows you the center the main element in the middle of your page */ align-items: center; justify-content: center; } .main { /* not needed */ } .inline-container { display: flex; gap: 1rem; } .inline { display: block; }
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous"> <script src="https://kit.fontawesome.com/c934cb6477.js" crossorigin="anonymous"></script> <main class="container"> <!-- <header> <div class="header"> </div> </header> --> <div class="main"> <h1>嘿,我是测试</h1> <h2>学生软件开发人员。</h2> <div class="inline-container"> <p class="inline">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur aliquam arcu et urna placerat dignissim. Nulla nec sollicitudin lorem, at maximus lorem. Cras porta quis sapien ut ornare. Nullam nec sem molestie, scelerisque sapien sit amet, dignissim ex. Sed eget felis gravida, maximus risus a, sodales libero. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p> <p class="inline">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur aliquam arcu et urna placerat dignissim. Nulla nec sollicitudin lorem, at maximus lorem. Cras porta quis sapien ut ornare. Nullam nec sem molestie, scelerisque sapien sit amet, dignissim ex. Sed eget felis gravida, maximus risus a, sodales libero. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p> </div> </div> <footer class="bg-dark text-center text-white footer fixed-bottom"> <div class="container p-4 pb-0"> <section class="mb-4"> <!-- Linkedin --> <a class="btn btn-outline-light btn-floating m-1" href="#" target="_blank" role="button"><i class="fab fa-linkedin-in"></i></a> <!-- Github --> <a class="btn btn-outline-light btn-floating m-1" href="#" target="_blank" role="button"><i class="fab fa-github"></i></a> <!-- CV --> <a class="btn btn-outline-light btn-floating m-1" href="#" target="_blank" role="button"><i class="fa-regular fa-address-card"></i></a> </section> </div> <!-- 版权 --> <div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.2);"> © <span id="current-year"></span> 版权所有: <a class="text-white">测试</a> </div> </footer> </main> <script src="/Files/Js/function.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8" crossorigin="anonymous"></script>
P粉5556967382023-09-10 00:25:46
I understand your line of thinking, but unfortunately, it's not entirely correct. You see, the design of the initial page is completely different from the beginning:
The creator did not center the main div, but added a padding to the body of 64px
, and then set the width of the wrapper div to 100%
and the height to 100% - 64px * 2
. So, to do this:
body{ padding:64px; } .wrapper{ height:calc(100vh - 64px * 2); width:100%; border: 1px solid black; }
<div class="wrapper"></div>