如何使用 CSS3 并排放置 HTML 元素
How to position HTML element Side by Side using CSS3
我刚开始编程,我正在尝试创建一个基于运球设计的简单网站。
我想在一个部分中并排放置图像和文本,以便在调整大小时,它们在彼此下方,但我不明白,你能帮我吗?
目前我的网站是这样的:
我要这样运球设计离开
我的HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./new.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,300;0,400;0,700;1,300&display=swap" rel="stylesheet">
<title>Document</title>
</head>
<body>
<header>
<a href="#" class="brand">MYBRAND</a>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Tasks</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">School</a></li>
<li><a href="#">Contact</a></li>
</ul>
</header>
<section class="banner">
<div class="container">
<div class="item">
<h1>Create, <br>and Collect Data</h1>
<div class="text">Change your mind, <br> extract and collect any data for you business.</div>
<button href="#" class="btn-contact btn-style-one">Talk to me</button>
</div>
<div class="item">
<figure class="image">
<img src="1.png" alt="">
</figure>
</div>
</div>
</section>
<script type="text/javascript">
window.addEventListener("scroll", function() {
let header = document.querySelector("header");
header.classList.toggle("sticky", window.scrollY > 390);
})
</script>
</body>
</html>
我的css代码:
* {
outline: none;
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
min-height: 200vh;
background-color: black;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.6s;
padding: 40px 100px;
z-index: 100000;
}
header .brand {
position: relative;
font-weight: 700;
color: #fff;
text-decoration: none;
font-size: 2em;
text-transform: uppercase;
letter-spacing: 2px;
transition: 0.6s;
}
header.sticky {
padding: 5px 100px;
background-color: #fff;
}
header.sticky .brand,
header.sticky ul li a {
color: #000;
}
header ul {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
header ul li {
position: relative;
list-style: none;
}
header ul li a {
position: relative;
margin: 0 15px;
text-decoration: none;
color: #fff;
letter-spacing: 2px;
font-weight: 500px;
transition: 0.5s;
}
.banner {
position: relative;
width: 100%;
height: 100vh;
background: url(13.png);
background-size: cover;
background-repeat: no-repeat;
}
.container {
display: flex;
width: 100%;
margin: 0 auto;
flex-wrap: wrap;
}
.item {
flex: 1;
margin: 5px;
padding: 0 10px;
text-align: center;
}
btn-contact {
display: inline-block;
}
.btn-style-one {
position: relative;
text-decoration: none;
line-height: 24px;
color: #ffffff;
font-size: 15px;
cursor: pointer;
font-weight: 600;
border-radius: 50px;
border-style: none;
background-color: #ff8048;
text-transform: capitalize;
padding: 16px 32px 16px 32px;
font-family: "Poppins", sans-serif;
box-shadow: 0px 0px 20px rgba(23, 23, 23, 0.15);
background-image: -webkit-gradient(
linear,
left top,
right top,
color-stop(0, #f434a1),
color-stop(100, #ff8442)
);
background-image: -webkit-linear-gradient(left, #f434a1 0%, #ff8442 100%);
background-image: linear-gradient(to right, #f434a1 0%, #ff8442 100%);
}
这些是我使用的图像:
您是否考虑过使用 css 网格?
.grid-container {
display: inline-grid;
}
将height: 100%
添加到.container
并更改CSS。
* {
outline: none;
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
min-height: 200vh;
background-color: black;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.6s;
padding: 40px 100px;
z-index: 100000;
}
header .brand {
position: relative;
font-weight: 700;
color: #fff;
text-decoration: none;
font-size: 2em;
text-transform: uppercase;
letter-spacing: 2px;
transition: 0.6s;
}
header.sticky {
padding: 5px 100px;
background-color: #fff;
}
header.sticky .brand,
header.sticky ul li a {
color: #000;
}
header ul {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
header ul li {
position: relative;
list-style: none;
}
header ul li a {
position: relative;
margin: 0 15px;
text-decoration: none;
color: #fff;
letter-spacing: 2px;
font-weight: 500px;
transition: 0.5s;
}
.banner {
position: relative;
width: 100%;
height: 100vh;
background: url('https://i.imgur.com/mTHpmbP.png');
background-size: cover;
background-repeat: no-repeat;
}
.container {
display: flex;
width: 100%;
height: 100%;
margin: 0 auto;
flex-wrap: wrap;
}
.item {
flex: 1;
margin: auto;
padding: 0 10px;
}
.item-text {
width: max-content;
margin: auto;
}
.item-text h1 {
color: #fff;
font-size: 40px;
line-height: 1.2;
}
.item-text .text {
color: #fff;
margin: 10px 0;
font-size: 14px;
}
.btn-contact {
display: inline-block;
}
.btn-style-one {
position: relative;
text-decoration: none;
line-height: 24px;
color: #ffffff;
font-size: 15px;
cursor: pointer;
font-weight: 600;
border-radius: 50px;
border-style: none;
background-color: #ff8048;
text-transform: capitalize;
padding: 16px 32px 16px 32px;
font-family: "Poppins", sans-serif;
box-shadow: 0px 0px 20px rgba(23, 23, 23, 0.15);
background-image: -webkit-gradient(
linear,
left top,
right top,
color-stop(0, #f434a1),
color-stop(100, #ff8442)
);
background-image: -webkit-linear-gradient(left, #f434a1 0%, #ff8442 100%);
background-image: linear-gradient(to right, #f434a1 0%, #ff8442 100%);
}
@media only screen and (max-width: 767px) {
.banner {
padding-top: 130px;
}
header {
padding: 20px;
flex-direction: column;
}
header ul li a {
margin: 0 10px;
letter-spacing: 0;
}
.container {
flex-direction: column;
flex-wrap: unset;
}
.item {
margin: 0;
padding: 0 30px;
}
.item img {
max-width: 100%;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./new.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,300;0,400;0,700;1,300&display=swap" rel="stylesheet">
<title>Document</title>
</head>
<body>
<header>
<a href="#" class="brand">MYBRAND</a>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Tasks</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">School</a></li>
<li><a href="#">Contact</a></li>
</ul>
</header>
<section class="banner">
<div class="container">
<div class="item">
<div class="item-text">
<h1>Create, <br>and Collect Data</h1>
<div class="text">Change your mind, <br> extract and collect any data for you business.</div>
<button class="btn-contact btn-style-one">Talk to me</button>
</div>
</div>
<div class="item">
<figure class="image">
<img src="https://i.imgur.com/5Zvfdoe.png" alt="">
</figure>
</div>
</div>
</section>
</body>
</html>
我刚开始编程,我正在尝试创建一个基于运球设计的简单网站。
我想在一个部分中并排放置图像和文本,以便在调整大小时,它们在彼此下方,但我不明白,你能帮我吗?
目前我的网站是这样的:
我要这样运球设计离开
我的HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./new.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,300;0,400;0,700;1,300&display=swap" rel="stylesheet">
<title>Document</title>
</head>
<body>
<header>
<a href="#" class="brand">MYBRAND</a>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Tasks</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">School</a></li>
<li><a href="#">Contact</a></li>
</ul>
</header>
<section class="banner">
<div class="container">
<div class="item">
<h1>Create, <br>and Collect Data</h1>
<div class="text">Change your mind, <br> extract and collect any data for you business.</div>
<button href="#" class="btn-contact btn-style-one">Talk to me</button>
</div>
<div class="item">
<figure class="image">
<img src="1.png" alt="">
</figure>
</div>
</div>
</section>
<script type="text/javascript">
window.addEventListener("scroll", function() {
let header = document.querySelector("header");
header.classList.toggle("sticky", window.scrollY > 390);
})
</script>
</body>
</html>
我的css代码:
* {
outline: none;
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
min-height: 200vh;
background-color: black;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.6s;
padding: 40px 100px;
z-index: 100000;
}
header .brand {
position: relative;
font-weight: 700;
color: #fff;
text-decoration: none;
font-size: 2em;
text-transform: uppercase;
letter-spacing: 2px;
transition: 0.6s;
}
header.sticky {
padding: 5px 100px;
background-color: #fff;
}
header.sticky .brand,
header.sticky ul li a {
color: #000;
}
header ul {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
header ul li {
position: relative;
list-style: none;
}
header ul li a {
position: relative;
margin: 0 15px;
text-decoration: none;
color: #fff;
letter-spacing: 2px;
font-weight: 500px;
transition: 0.5s;
}
.banner {
position: relative;
width: 100%;
height: 100vh;
background: url(13.png);
background-size: cover;
background-repeat: no-repeat;
}
.container {
display: flex;
width: 100%;
margin: 0 auto;
flex-wrap: wrap;
}
.item {
flex: 1;
margin: 5px;
padding: 0 10px;
text-align: center;
}
btn-contact {
display: inline-block;
}
.btn-style-one {
position: relative;
text-decoration: none;
line-height: 24px;
color: #ffffff;
font-size: 15px;
cursor: pointer;
font-weight: 600;
border-radius: 50px;
border-style: none;
background-color: #ff8048;
text-transform: capitalize;
padding: 16px 32px 16px 32px;
font-family: "Poppins", sans-serif;
box-shadow: 0px 0px 20px rgba(23, 23, 23, 0.15);
background-image: -webkit-gradient(
linear,
left top,
right top,
color-stop(0, #f434a1),
color-stop(100, #ff8442)
);
background-image: -webkit-linear-gradient(left, #f434a1 0%, #ff8442 100%);
background-image: linear-gradient(to right, #f434a1 0%, #ff8442 100%);
}
这些是我使用的图像:
您是否考虑过使用 css 网格?
.grid-container {
display: inline-grid;
}
将height: 100%
添加到.container
并更改CSS。
* {
outline: none;
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
min-height: 200vh;
background-color: black;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.6s;
padding: 40px 100px;
z-index: 100000;
}
header .brand {
position: relative;
font-weight: 700;
color: #fff;
text-decoration: none;
font-size: 2em;
text-transform: uppercase;
letter-spacing: 2px;
transition: 0.6s;
}
header.sticky {
padding: 5px 100px;
background-color: #fff;
}
header.sticky .brand,
header.sticky ul li a {
color: #000;
}
header ul {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
header ul li {
position: relative;
list-style: none;
}
header ul li a {
position: relative;
margin: 0 15px;
text-decoration: none;
color: #fff;
letter-spacing: 2px;
font-weight: 500px;
transition: 0.5s;
}
.banner {
position: relative;
width: 100%;
height: 100vh;
background: url('https://i.imgur.com/mTHpmbP.png');
background-size: cover;
background-repeat: no-repeat;
}
.container {
display: flex;
width: 100%;
height: 100%;
margin: 0 auto;
flex-wrap: wrap;
}
.item {
flex: 1;
margin: auto;
padding: 0 10px;
}
.item-text {
width: max-content;
margin: auto;
}
.item-text h1 {
color: #fff;
font-size: 40px;
line-height: 1.2;
}
.item-text .text {
color: #fff;
margin: 10px 0;
font-size: 14px;
}
.btn-contact {
display: inline-block;
}
.btn-style-one {
position: relative;
text-decoration: none;
line-height: 24px;
color: #ffffff;
font-size: 15px;
cursor: pointer;
font-weight: 600;
border-radius: 50px;
border-style: none;
background-color: #ff8048;
text-transform: capitalize;
padding: 16px 32px 16px 32px;
font-family: "Poppins", sans-serif;
box-shadow: 0px 0px 20px rgba(23, 23, 23, 0.15);
background-image: -webkit-gradient(
linear,
left top,
right top,
color-stop(0, #f434a1),
color-stop(100, #ff8442)
);
background-image: -webkit-linear-gradient(left, #f434a1 0%, #ff8442 100%);
background-image: linear-gradient(to right, #f434a1 0%, #ff8442 100%);
}
@media only screen and (max-width: 767px) {
.banner {
padding-top: 130px;
}
header {
padding: 20px;
flex-direction: column;
}
header ul li a {
margin: 0 10px;
letter-spacing: 0;
}
.container {
flex-direction: column;
flex-wrap: unset;
}
.item {
margin: 0;
padding: 0 30px;
}
.item img {
max-width: 100%;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./new.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,300;0,400;0,700;1,300&display=swap" rel="stylesheet">
<title>Document</title>
</head>
<body>
<header>
<a href="#" class="brand">MYBRAND</a>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Tasks</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">School</a></li>
<li><a href="#">Contact</a></li>
</ul>
</header>
<section class="banner">
<div class="container">
<div class="item">
<div class="item-text">
<h1>Create, <br>and Collect Data</h1>
<div class="text">Change your mind, <br> extract and collect any data for you business.</div>
<button class="btn-contact btn-style-one">Talk to me</button>
</div>
</div>
<div class="item">
<figure class="image">
<img src="https://i.imgur.com/5Zvfdoe.png" alt="">
</figure>
</div>
</div>
</section>
</body>
</html>