英文:
Why can't I put my desired text next to the image, underneath the title?
问题
我刚刚接触CSS和HTML,这是我制作的第一个网站,我在页面上设置文本位置时遇到了问题。我希望它在页面中间,靠近图像旁边,但无论我尝试什么,它都不让我将其移动更高。
这是页面的CSS。
a {
text-transform: uppercase;
display: inline-block;
padding: 5px;
margin: 35px 30px;
text-decoration: none;
}
nav {
background-color: rgba(255, 255, 255, 1);
position: relative;
width: 100%;
height: 100px;
}
.navigation ul {
margin: 0;
padding: 0;
text-align: center;
}
.navigation li {
font-weight: 100;
letter-spacing: 2px;
list-style-type: none;
color: #8D7B68;
font-size: larger;
}
body {
background-color: #ffffff;
margin: 0 auto;
}
li:hover {
color: #ed2323;
}
#banner {
background-color: rgba(164, 144, 124, .8);
background-size: cover;
background-position: bottom center;
height: 500px;
width: 100%;
}
#banner .content h1 {
border: 3px solid white;
position: relative;
top: 50px;
width: 400px;
margin: 0 auto;
}
h1 {
color: white;
font-size: 42px;
font-weight: 600;
text-align: center;
padding: 10px;
letter-spacing: 1px;
}
.milk {
display: flex;
margin: 0 0 0 150px;
width: 350px;
height: 350px;
border: rgba(164, 144, 124, .9) 3px solid;
}
.milkp {
margin: 0 675px 0 675px;
text-align: center;
max-width: 400px;
}
p {
color: rgb(0, 0, 0);
font-size: 15px;
font-weight: 300;
padding: 10px;
letter-spacing: 1px;
line-height: 25px;
}
我尝试使用外边距来移动它,但它不允许我将其向上移动,只能向下、右或左移动。我尝试将定位设置为不同的值,将显示设置为其他内容,但没有任何效果。
英文:
I just got into CSS and HTML, this is my first website I'm making and I'm having trouble positioning my text on the page. I want it to be in the middle, next to the image, but whatever I try it doesn't let me move it any more upwards.
Here's the CSS for the page.
here is what it looks like right now
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
a {
text-transform: uppercase;
display: inline-block;
padding: 5px;
margin: 35px 30px;
text-decoration: none;
}
nav {
background-color: rgba(255, 255, 255, 1);
position: relative;
width: 100%;
height: 100px
}
.navigation ul {
margin: 0;
padding: 0;
text-align: center;
}
.navigation li {
font-weight: 100;
letter-spacing: 2px;
list-style-type: none;
color: #8D7B68;
font-size: larger;
}
body {
background-color: #ffffff;
margin: 0 auto;
}
li:hover {
color: #ed2323;
}
#banner {
background-color: rgba(164, 144, 124, .8);
background-size: cover;
background-position: bottom center;
height: 500px;
width: 100%;
}
#banner .content h1 {
border: 3px solid white;
position: relative;
top: 50px;
width: 400px;
margin: 0 auto;
}
h1 {
color: white;
font-size: 42px;
font-weight: 600;
text-align: center;
padding: 10px;
letter-spacing: 1px;
}
.milk {
display: flex;
margin: 0 0 0 150px;
width: 350px;
height: 350px;
border: rgba(164, 144, 124, .9) 3px solid;
}
.milkp {
margin: 0 675px 0 675px;
text-align: center;
max-width: 400px;
}
p {
color: rgb(0, 0, 0);
font-size: 15px;
font-weight: 300;
padding: 10px;
letter-spacing: 1px;
line-height: 25px;
}
<!-- language: lang-html -->
<nav class="navigation">
<ul>
<a href="./index.html">
<li>početna stranica</li>
</a>
<a href="./proizvodi.html">
<li>proizvodi</li>
</a>
<a href="./kontakt.html">
<li>kontakt</li>
</a>
</ul>
</nav>
<div id="banner">
<div class="content">
<h1>mliječna čokolada</h1>
<img src="https://i.pinimg.com/564x/47/99/71/47997186f6af47d86181766d78d55f47.jpg" alt="choco" class="milk">
<p class="milkp">
Prvi niz, upućen voljenom mladiću, bolje je uređen i sadrži nekoliko kraćih nizova koji se tiču njihova odnosa ili obrađuju pripadne ideje. Tako soneti 117 potiču mladića da nađe životnu družicu i dobije djecu; soneti 41 i 42 upozoravaju da je pjesnikova
ljubavnica zavela mladića tijekom pjesnikova izbivanja; soneti 7886 opisuju pjesnikarivala koji je očevidno pokušao pridobiti mladića za sebe; u sonetima 8790 iskazana je zabrinutost da je mladić potpuno zaboravio pjesnika
</p>
</div>
</div>
<!-- end snippet -->
I tried moving it using margins but it won't let me move it upwards, just down right or left.
Tried setting the positioning to be different, display to something else but it doesn't do anything.
答案1
得分: 0
CSS Grid是一个用于布局的强大工具。我已经在CSS的每一行上提供了解释,以描述它的作用。这里有一个关于CSS Grid的良好介绍,css tricks,以及来自Kevin Powell的一段很棒的视频,帮助你入门。祝你好运!
a {
text-transform: uppercase;
display: inline-block;
padding: 5px;
margin: 35px 30px;
text-decoration: none;
}
nav {
background-color: rgba(255, 255, 255, 1);
position: relative;
width: 100%;
height: 100px;
}
.navigation ul {
margin: 0;
padding: 0;
text-align: center;
}
.navigation li {
font-weight: 100;
letter-spacing: 2px;
list-style-type: none;
color: #8D7B68;
font-size: larger;
}
body {
background-color: #ffffff;
margin: 0 auto;
}
li:hover {
color: #ed2323;
}
#banner {
background-color: rgba(164, 144, 124, .8);
background-size: cover;
background-position: bottom center;
}
#banner .content h1 {
border: 3px solid white;
width: 400px;
margin: 50px auto;
}
.title {
grid-area: header;
}
h1 {
color: white;
font-size: 42px;
font-weight: 600;
text-align: center;
padding: 10px;
letter-spacing: 1px;
}
.milk {
margin: auto 50px;
width: 350px;
height: 350px;
border: rgba(164, 144, 124, .9) 3px solid;
grid-area: img;
place-self: center;
}
.milkp {
grid-area: milkp;
justify-self: center;
text-align: center;
max-width: 400px;
}
p {
color: rgb(0, 0, 0);
font-size: 15px;
font-weight: 300;
padding: 10px;
letter-spacing: 1px;
line-height: 25px;
}
.content {
display: grid;
grid-template-columns: fit-content(0) 1fr;
grid-template-areas: "img header"
"img milkp";
}
英文:
CSS grid is a great for layout. I've put explainers on each line of CSS to describe what it does. Here's a good introduction on css tricks and a nice video from Kevin Powell to get you started. Good luck!
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
a {
text-transform: uppercase;
display: inline-block;
padding: 5px;
margin: 35px 30px;
text-decoration: none;
}
nav {
background-color: rgba(255, 255, 255, 1);
position: relative;
width: 100%;
height: 100px
}
.navigation ul {
margin: 0;
padding: 0;
text-align: center;
}
.navigation li {
font-weight: 100;
letter-spacing: 2px;
list-style-type: none;
color: #8D7B68;
font-size: larger;
}
body {
background-color: #ffffff;
margin: 0 auto;
}
li:hover {
color: #ed2323;
}
#banner {
background-color: rgba(164, 144, 124, .8);
background-size: cover;
background-position: bottom center;
/* height: 500px; removed this as resizing your window may cause the text and/or image to overflow */
/*width: 100%; not needed */
}
#banner .content h1 {
border: 3px solid white;
/*position: relative; got rid of this, we'll use the grid to position this */
/*top: 50px; got rid of this, we'll use the margin below to position this */
width: 400px;
margin: 50px auto; /* changed this from margin: 0 auto; to add a bit of space between the title and the container */
}
.title {
grid-area: header; /* added this to place it in the 'header' area. */
}
h1 {
color: white;
font-size: 42px;
font-weight: 600;
text-align: center;
padding: 10px;
letter-spacing: 1px;
}
.milk {
/* display: flex; removed this - not necessary */
margin: auto 50px; /* changed this from margin: 0 0 0 150px; to put a nice border around your image*/
width: 350px;
height: 350px;
border: rgba(164, 144, 124, .9) 3px solid;
grid-area: img; /* put this in the image grid area (see .content below) */
place-self: center; /* place the image in the center of the grid element */
}
.milkp {
/*margin: 0 675px 0 675px; removed this - we'll use the grid to position this content */
grid-area: milkp; /* put this in the milkp grid area (see .content below) */
justify-self: center; /* put the text in the middle of the container */
text-align: center;
max-width: 400px;
}
p {
color: rgb(0, 0, 0);
font-size: 15px;
font-weight: 300;
padding: 10px;
letter-spacing: 1px;
line-height: 25px;
}
/* added this to use css grid to position your elements */
.content {
display: grid;
grid-template-columns: fit-content(0) 1fr;
grid-template-areas: "img header"
"img milkp";
}
<!-- language: lang-html -->
<nav class="navigation">
<ul>
<a href="./index.html">
<li>početna stranica</li>
</a>
<a href="./proizvodi.html">
<li>proizvodi</li>
</a>
<a href="./kontakt.html">
<li>kontakt</li>
</a>
</ul>
</nav>
<div id="banner">
<div class="content">
<div class='title'><!-- added this to act as a wrapper for your header -->
<h1>mliječna čokolada</h1>
</div>
<img src="https://i.pinimg.com/564x/47/99/71/47997186f6af47d86181766d78d55f47.jpg" alt="choco" class="milk">
<p class="milkp">
Prvi niz, upućen voljenom mladiću, bolje je uređen i sadrži nekoliko kraćih nizova koji se tiču njihova odnosa ili obrađuju pripadne ideje. Tako soneti 117 potiču mladića da nađe životnu družicu i dobije djecu; soneti 41 i 42 upozoravaju da je pjesnikova
ljubavnica zavela mladića tijekom pjesnikova izbivanja; soneti 7886 opisuju pjesnikarivala koji je očevidno pokušao pridobiti mladića za sebe; u sonetima 8790 iskazana je zabrinutost da je mladić potpuno zaboravio pjesnika
</p>
</div>
</div>
<!-- end snippet -->
答案2
得分: 0
以下是代码更改的翻译:
这种类型的代码更改对您可能有效。以下是CSS:
a {
text-transform: uppercase;
display: inline-block;
padding: 5px;
margin: 35px 30px;
text-decoration: none;
}
nav {
background-color: rgba(255, 255, 255, 1);
position: relative;
width: 100%;
height: 100px
}
.navigation ul {
margin: 0;
padding: 0;
text-align: center;
}
.navigation li {
font-weight: 100;
letter-spacing: 2px;
list-style-type: none;
color: #8D7B68;
font-size: larger;
}
body {
background-color: #ffffff;
margin: 0 auto;
}
li:hover {
color: #ed2323;
}
#banner {
background-color: rgba(164, 144, 124, .8);
background-size: cover;
background-position: bottom center;
height: 500px;
width: 100%;
}
.content-title {
display: inline-block;
width: 100%;
}
.content-title h1 {
color: white;
font-size: 42px;
font-weight: 600;
text-align: center;
padding: 10px;
letter-spacing: 1px;
border: 3px solid white;
position: relative;
width: 400px;
margin: 0 auto;
margin-top: 40px;
margin-bottom: 10px;
}
.milk {
width: 350px;
height: 350px;
border: rgba(164, 144, 124, .9) 3px solid;
margin-left: 150px;
float: left;
}
.milkp {
max-width: 400px;
display: inline-block;
width: auto;
float: left;
margin-top: 25px;
margin-left: 25px;
}
p {
color: rgb(0, 0, 0);
font-size: 15px;
font-weight: 300;
padding: 10px;
letter-spacing: 1px;
line-height: 25px;
}
.content {
display: inline-block;
width: 100%;
}
以下是HTML:
<body>
<nav class="navigation">
<ul>
<a href="./index.html">
<li>početna stranica</li>
</a>
<a href="./proizvodi.html">
<li>proizvodi</li>
</a>
<a href="./kontakt.html">
<li>kontakt</li>
</a>
</ul>
</nav>
<div id="banner">
<div class="content">
<div class="content-title">
<h1>mliječna čokolada</h1>
</div>
<img src="https://i.pinimg.com/564x/47/99/71/47997186f6af47d86181766d78d55f47.jpg" alt="choco" class="milk">
<p class="milkp">
Prvi niz, upućen voljenom mladiću, bolje je uređen i sadrži nekoliko kraćih
nizova koji se tiču njihova odnosa ili obrađuju pripadne ideje. Tako soneti 117
potiču mladića da nađe životnu družicu i dobije djecu; soneti 41 i 42 upozoravaju
da je pjesnikova ljubavnica zavela mladića tijekom pjesnikova izbivanja; soneti
7886 opisuju pjesnikarivala koji je očevidno pokušao pridobiti mladića za
sebe; u sonetima 8790 iskazana je zabrinutost da je mladić potpuno zaboravio
pjesnika
</p>
</div>
</div>
</body>
这对您有效。
英文:
Please Try This types of code changes might be working for you.
Here is CSS
a {
text-transform: uppercase;
display: inline-block;
padding: 5px;
margin: 35px 30px;
text-decoration: none;
}
nav {
background-color: rgba(255, 255, 255, 1);
position: relative;
width: 100%;
height: 100px
}
.navigation ul {
margin: 0;
padding: 0;
text-align: center;
}
.navigation li {
font-weight: 100;
letter-spacing: 2px;
list-style-type: none;
color: #8D7B68;
font-size: larger;
}
body {
background-color: #ffffff;
margin: 0 auto;
}
li:hover {
color: #ed2323;
}
#banner {
background-color: rgba(164, 144, 124, .8);
background-size: cover;
background-position: bottom center;
height: 500px;
width: 100%;
}
.content-title {
display: inline-block;
width: 100%;
}
.content-title h1 {
color: white;
font-size: 42px;
font-weight: 600;
text-align: center;
padding: 10px;
letter-spacing: 1px;
border: 3px solid white;
position: relative;
width: 400px;
margin: 0 auto;
margin-top: 40px;
margin-bottom: 10px;
}
.milk {
width: 350px;
height: 350px;
border: rgba(164, 144, 124, .9) 3px solid;
margin-left: 150px;
float: left;
}
.milkp {
max-width: 400px;
display: inline-block;
width: auto;
float: left;
margin-top: 25px;
margin-left: 25px;
}
p {
color: rgb(0, 0, 0);
font-size: 15px;
font-weight: 300;
padding: 10px;
letter-spacing: 1px;
line-height: 25px;
}
.content {
display: inline-block;
width: 100%;
}
Here is HTML
<body>
<nav class="navigation">
<ul>
<a href="./index.html">
<li>početna stranica</li>
</a>
<a href="./proizvodi.html">
<li>proizvodi</li>
</a>
<a href="./kontakt.html">
<li>kontakt</li>
</a>
</ul>
</nav>
<div id="banner">
<div class="content">
<div class="content-title">
<h1>mliječna čokolada</h1>
</div>
<img src="https://i.pinimg.com/564x/47/99/71/47997186f6af47d86181766d78d55f47.jpg" alt="choco" class="milk">
<p class="milkp">
Prvi niz, upućen voljenom mladiću, bolje je uređen i sadrži nekoliko kraćih
nizova koji se tiču njihova odnosa ili obrađuju pripadne ideje. Tako soneti 117
potiču mladića da nađe životnu družicu i dobije djecu; soneti 41 i 42 upozoravaju
da je pjesnikova ljubavnica zavela mladića tijekom pjesnikova izbivanja; soneti
7886 opisuju pjesnikarivala koji je očevidno pokušao pridobiti mladića za
sebe; u sonetima 8790 iskazana je zabrinutost da je mladić potpuno zaboravio
pjesnika
</p>
</div>
</div>
</body>
This works well for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论