html - 文字和图片相邻

标签 html css

enter image description here

enter image description here

我有这个网站,我使用 html 和 css,我想做一个元素和我自己的作品集,网站上的其中一个部分是“关于”,在这个部分中接下来应该有文字和图像就文字而言,正如第一张图片所示。

正如第一张图片所示,有一张图片,旁边有一个“关于”部分的特殊文字。

在第二张图片中,这是这篇文章的主要问题,因为我无法将图片和文字放在一起。

index.html:

<html DOCTYPE="html5">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Training with navbar</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="css/all.min.css">
</head>

<body>

<header>
    <nav>
        <div class="logo">
            <p>Portfolio</p>
        </div>

        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Contact</a></li>
        </ul>

        <label class="check">
            <i class="fas fa-bars fa-lg icon" id="menu"></i>
        </label>

    </nav>

    <div class="header-content">
        <h2>Welcome to Portfolio</h2>
        <p>Let's chat</p>

        <div class="btn">
            <button class="btn-1">Book Now</button>
        </div>
    </div>
</header>

<section class="about">
    <div class="row">

        <div class="about-text">
            <img src="images/women%20image.jpeg" class="personal-img" alt="about me picture">
            <h2>About</h2>
            <p>I am Hiba Youssef, I am studying <br>Information Technology Engineering <br>at Damascus university.
                I have three years experience in React.
            </p>


        </div>

    </div>

</section>
<script src="app.js"></script>
</body>

</html>

css.样式:

@import url('https://fonts.googleapis.com/css2?family=Tangerine:wght@700&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    --spacing: 10px;
    --primary: #141414;
    --secondary: #b80003;
    --third: #ffffff;
    font-size: 12px;
}

/*img{*/
/*    width: 100%;*/
/*    margin: auto;*/
/*}*/

.row {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: auto;
}

a {
    text-decoration: none;
}

ul {
    list-style: none;
}

body {
    background-color: var(--primary);
}

header {
    height: 100vh;
    background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url('images/mountain.jpg');
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

nav {
    background-color: var(--primary);
    display: flex;
    justify-content: space-between;
    padding: calc(2 * var(--spacing)) calc(8 * var(--spacing));
    position: sticky;
    top: 0;
    z-index: 1;
}

.logo p {
    font-family: 'Tangerine', cursive;
    font-size: 4.8rem;
    font-weight: bold;
    color: var(--secondary);
}

nav ul li {
    padding-top: calc(1.5 * var(--spacing));
    display: inline-block;
}

nav ul li a {
    color: var(--third);
    font-size: 1.2rem;
    font-weight: 400;
    /*padding:calc(1.5*var(--spacing)) 0 0 calc(3.6*var(--spacing));*/
    padding: 0 calc(var(--spacing));
    text-transform: uppercase;
    letter-spacing: 1.5px;
}

nav ul li a:hover {
    color: var(--secondary);
}

.icon {
    padding-top: calc(1.5 * var(--spacing));
    color: var(--third);
    cursor: pointer;
}

.check {
    display: none;
}

@media only screen and (max-width: 768px) {
    html {
        font-size: 8px;
    }

    nav {
        /*padding: 0 50px;*/
        flex-direction: column;
        position: relative;
    }

    .check {
        display: block;
        position: absolute;
        top: calc(2 * var(--spacing));
        right: calc(4 * var(--spacing));
    }

    nav ul {
        width: 100%;
        height: 100vh;
        background-color: ghostwhite;
        display: none;
        position: fixed;
    }

    .showmenu {
        display: block;
    }

    nav ul li {
        display: block;
        padding: calc(2 * var(--spacing));
    }

    nav ul li a {
        color: black;
    }
}

.header-content {
    text-align: center;
    /*display: flex;*/
    /*justify-content: center;*/
    /*align-items: center;*/
    position: relative;
    top: calc(15 * var(--spacing));

}

.header-content h2 {
    font-size: 4rem;
    color: var(--third);
    margin: calc(1.3 * var(--spacing));
}

.header-content p {
    font-size: 1.8rem;
    color: var(--third);
    font-weight: 500;
}

.btn {
    /*display: flex;*/
    /*justify-content: space-evenly;*/
    /*flex-direction: row;*/
    align-items: center;
    text-align: center;
    margin: calc(2 * var(--spacing));

}

.btn-1 {
    background-color: var(--secondary);
    color: var(--third);
    padding: calc(var(--spacing)) calc(2 * var(--spacing));
    border: none;
    letter-spacing: 1px;
    outline: none;
    border-radius: 1.5px;
}

.about-space{
    /*display: inline-block;*/

}

.about {
    margin: calc(6*var(--spacing));
    display: flex;
    align-items: center;
    justify-content: center;
}

.about-text {
    padding: calc(6*var(--spacing)) calc(6*var(--spacing));
    background-color: var(--secondary);
}

.about-text h2{
    padding: calc(2*var(--spacing)) 0;
    font-size: 2.4rem;
    color: var(--third);
}

.about-text p{
    font-size: 1.3rem;
    color: var(--third);
}

.about-text img{
    object-fit: contain;
    display: inline-block;
    width: calc(48*var(--spacing));
    height: calc(44*var(--spacing));
    /*position: absolute;*/
}

.personal-img{
    display: flex;
    position: relative;
    left: 70%;
    object-fit: contain;
}

最佳答案

about-text位置属性设置为relative并将图像位置属性设置为absolute以使其正常工作:

@import url('https://fonts.googleapis.com/css2?family=Tangerine:wght@700&display=swap');
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  --spacing: 10px;
  --primary: #141414;
  --secondary: #b80003;
  --third: #ffffff;
  font-size: 12px;
}


/*img{*/


/*    width: 100%;*/


/*    margin: auto;*/


/*}*/

.row {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: auto;
}

a {
  text-decoration: none;
}

ul {
  list-style: none;
}

body {
  background-color: var(--primary);
}

header {
  height: 100vh;
  background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url('images/mountain.jpg');
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}

nav {
  background-color: var(--primary);
  display: flex;
  justify-content: space-between;
  padding: calc(2 * var(--spacing)) calc(8 * var(--spacing));
  position: sticky;
  top: 0;
  z-index: 1;
}

.logo p {
  font-family: 'Tangerine', cursive;
  font-size: 4.8rem;
  font-weight: bold;
  color: var(--secondary);
}

nav ul li {
  padding-top: calc(1.5 * var(--spacing));
  display: inline-block;
}

nav ul li a {
  color: var(--third);
  font-size: 1.2rem;
  font-weight: 400;
  /*padding:calc(1.5*var(--spacing)) 0 0 calc(3.6*var(--spacing));*/
  padding: 0 calc(var(--spacing));
  text-transform: uppercase;
  letter-spacing: 1.5px;
}

nav ul li a:hover {
  color: var(--secondary);
}

.icon {
  padding-top: calc(1.5 * var(--spacing));
  color: var(--third);
  cursor: pointer;
}

.check {
  display: none;
}

@media only screen and (max-width: 768px) {
  html {
    font-size: 8px;
  }
  nav {
    /*padding: 0 50px;*/
    flex-direction: column;
    position: relative;
  }
  .check {
    display: block;
    position: absolute;
    top: calc(2 * var(--spacing));
    right: calc(4 * var(--spacing));
  }
  nav ul {
    width: 100%;
    height: 100vh;
    background-color: ghostwhite;
    display: none;
    position: fixed;
  }
  .showmenu {
    display: block;
  }
  nav ul li {
    display: block;
    padding: calc(2 * var(--spacing));
  }
  nav ul li a {
    color: black;
  }
}

.header-content {
  text-align: center;
  /*display: flex;*/
  /*justify-content: center;*/
  /*align-items: center;*/
  position: relative;
  top: calc(15 * var(--spacing));
}

.header-content h2 {
  font-size: 4rem;
  color: var(--third);
  margin: calc(1.3 * var(--spacing));
}

.header-content p {
  font-size: 1.8rem;
  color: var(--third);
  font-weight: 500;
}

.btn {
  /*display: flex;*/
  /*justify-content: space-evenly;*/
  /*flex-direction: row;*/
  align-items: center;
  text-align: center;
  margin: calc(2 * var(--spacing));
}

.btn-1 {
  background-color: var(--secondary);
  color: var(--third);
  padding: calc(var(--spacing)) calc(2 * var(--spacing));
  border: none;
  letter-spacing: 1px;
  outline: none;
  border-radius: 1.5px;
}

.about-space {
  /*display: inline-block;*/
}

.about {
  margin: calc(6*var(--spacing));
  display: flex;
  align-items: center;
  justify-content: center;
}

.about-text {
  position: relative;
  padding: calc(6*var(--spacing)) calc(6*var(--spacing));
  background-color: var(--secondary);
}

.about-text h2 {
  padding: calc(2*var(--spacing)) 0;
  font-size: 2.4rem;
  color: var(--third);
}

.about-text p {
  font-size: 1.3rem;
  color: var(--third);
}

.about-text img {
  object-fit: contain;
  display: inline-block;
  width: calc(48*var(--spacing));
  height: calc(44*var(--spacing));
  /*position: absolute;*/
}

.personal-img {
  display: flex;
  position: absolute;
  left: 70%;
  object-fit: contain;
}
<html DOCTYPE="html5">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Training with navbar</title>
  <link rel="stylesheet" href="style.css">
  <link rel="stylesheet" href="css/all.min.css">
</head>

<body>

  <header>
    <nav>
      <div class="logo">
        <p>Portfolio</p>
      </div>

      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Contact</a></li>
      </ul>

      <label class="check">
            <i class="fas fa-bars fa-lg icon" id="menu"></i>
        </label>

    </nav>

    <div class="header-content">
      <h2>Welcome to Portfolio</h2>
      <p>Let's chat</p>

      <div class="btn">
        <button class="btn-1">Book Now</button>
      </div>
    </div>
  </header>

  <section class="about">
    <div class="row">

      <div class="about-text">
        <img src="https://wallpaperaccess.com/full/1376490.jpg" class="personal-img" alt="about me picture">
        <h2>About</h2>
        <p>I am Hiba Youssef, I am studying <br>Information Technology Engineering <br>at Damascus university. I have three years experience in React.
        </p>


      </div>

    </div>

  </section>
  <script src="app.js"></script>
</body>

</html>

基本上,当您将具有绝对定位的元素放入具有相对定位的元素时,内部元素将相对于外部元素定位自身。

关于html - 文字和图片相邻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69650220/

相关文章:

jquery - 如何获得默认外观

javascript - jquery 选择选项单击处理程序

html - 添加空格以列出连续的分隔单词

javascript - 浏览器检测 Opera 问题

javascript - 增加弹出窗口的宽度

python - BeautifulSoup python 3 css 麻烦

html - bootstrap 3 不应用 panel-primary 作为蓝色背景色

javascript - 具有固定页眉和页脚以及不固定宽度的可滚动正文的 HTML 表格

php - 如何从管道获取电子邮件正文以进行编程

html - 奇怪的 HTML 错误(停止包含 CSS 和 JS 文件)