为什么 CSS 左右浮动不起作用?

问题描述 投票:0回答:1

我想让我的图像出现在文本的左侧,但是浮动似乎不起作用。我希望对其进行格式化,使图像浮动到 div 中文本的右侧。这是我的代码:

<div class="say-hello">
            <img src="images/say-hello.jpg" alt="Person placing macarons on top of cake">
            <div class="say-hello-text">
                <h2>Say Hello</h2>
                <p>In the mood for something sweet? We'd love to make your next event special with something Positively Delicious. Whether you're looking for a classic birthday cake or a unique cupcake assortment, we've got you covered.</p>
                <div class="say-hello-button">
                    <a class="button" href="#">Contact us</a>
                </div>
            </div>
        </div>
.say-hello {
    width: 80%;
    max-width: 100%;
    height: 100%;
    padding: 30px;
    margin: auto;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    border-radius: 8px;
  }
  .say-hello img {
    object-fit: cover;
    width: 50%;
    border: none;
    float: right;
    padding: none;
    margin-left: 0;
    margin-right: 0;
  }
  .say-hello-text {
    text-align: center;
    align-self: center;
    width: 40%;
    padding-right: 4%;
    float: left;
  }
html css css-float
1个回答
0
投票

flex 容器中的浮动属性被忽略:您在容器 div(类 .say-hello)中使用

"display:flex"
,只需删除 display flex 并且浮动将正常工作:

.say-hello {
    width: 80%;
    max-width: 100%;
    height: 100%;
    padding: 30px;
    margin: auto;
    /*display: flex;*/
    flex-wrap: wrap;
    justify-content: space-between;
    border-radius: 8px;
  }
  .say-hello img {
    object-fit: cover;
    width: 50%;
    border: none;
    float: right;
    padding: none;
    margin-left: 0;
    margin-right: 0;
  }
  .say-hello-text {
    text-align: center;
    align-self: center;
    width: 40%;
    padding-right: 4%;
    float: left;
  }
<div class="say-hello">
            <img src="https://assets.codepen.io/t-1/user-default-avatar.jpg?format=auto&version=0&width=80&height=80" alt="Person placing macarons on top of cake">
            <div class="say-hello-text">
                <h2>Say Hello</h2>
                <p>In the mood for something sweet? We'd love to make your next event special with something Positively Delicious. Whether you're looking for a classic birthday cake or a unique cupcake assortment, we've got you covered.</p>
                <div class="say-hello-button">
                    <a class="button" href="#">Contact us</a>
                </div>
            </div>
        </div>

© www.soinside.com 2019 - 2024. All rights reserved.