当页面处于最小视口尺寸时,我如何包装图像?

huangapple go评论61阅读模式
英文:

How can I wrap an image only when the page is at minimum viewport size?

问题

以下是代码部分的翻译:

html, body {
    margin: 0;
    padding: 0;
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    box-sizing: border-box;
}

/* 蓝色容器,第一个容器 */
.dark-blue {
    background-color: #1f2937;
    display: flex;
    flex: 1 1 0;
    padding-left: 10%;
    padding-right: 10%;
}

.header {
    display: flex;
    justify-content: space around;
    align-items: center;
    padding-bottom: 5%;
}

.logo {
    color: #F9FAF8;
    font-size: 24px;
    font-weight: bolder;
    display: flex;
    flex: 1 1 auto;
}

.links {
    font-size: 18px;
    display: flex;
    flex-shrink: 1;
    gap: 1rem;
    flex-wrap: wrap;
}

.link-text {
    font-size: 18px;
    color: #e5e7eb;
}

a {
    text-decoration: none;
}
ul {
    list-style-type: none;
}

.about {
    display: flex;
    justify-content: flex-start;
    padding-bottom: 5%;
}

.main-text {
    color: #F9FAF8;
    font-size: 48px;
    font-weight: bolder;
    width: 95%;
    display: flex;
    flex: 1 1 auto;
    padding-bottom: 1rem;
}

.secondary-text {
    padding-bottom: .5rem;
    font-size: 18px;
    max-width: 80%;
    color: #e5e7eb;
    display: flex;
    flex: 1 1 auto;
    justify-content: start;
    padding-bottom: 1rem;
}

.about-text {
    padding-bottom: 5%;
    display: flex;
    flex-direction: column;
}

.button-one {
    background-color: #1DB954;
    color: #F9FAF8;
    font-weight: bold;
    border-radius: 10px;
    width: 92px;
    height: 20px;
    padding: 8px;
    text-align: center;
}

.button-one:hover {
    color:  #F9FAF8;
    font-weight: bold;
    background-color: #126e32;
    border-radius: 10px;
    box-shadow: 0 5px 8px 0 rgba(224, 224, 224, 0.020), 0 7px 10px 0 rgba(238, 238, 238, 0.025);
    width: 92px;
    height: 20px;
    padding: 8px;
    text-align: center;
}

.button-text {
    color: #F9FAF8;
}

.button-text:hover {
    color: #F9FAF8;
}

.header-image {
    width: 45%;
    height: 18%;
    border-radius: 2px;
    display: flex;
    flex-shrink: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Black and White</title>
    <link rel="icon" href="images/blackandwhite.png">
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="header dark-blue">
        <div class="logo">黑白</div>
        <ul class="links">
            <li><a class="link-text" href="#">首页</a></li>
            <li><a class="link-text" href="#photos">照片</a></li>
            <li><a class="link-text" href="#sign-up">注册</a></li>
        </div>
    </div>
    <div class="about dark-blue">
        <div class="about-text">
            <div class="main-text">获取美丽的黑白照片</div>
            <div class="secondary-text">世界不是黑白的,有很多灰色的阴影。每个时代都有好事和坏事,盲目地说一个时代很美好是有限视野的,因为它确实很美好,但也有很多不好的地方。</div>
            <div class="button-one">
                 <a class="button-text" href="#">注册</a>
            </div>
        </div>
        <img class="header-image" src="./images/header-image.jpeg">
    </div>
    <!-- 其他部分未翻译 -->
</body>
</html>
英文:

I'm currently following TOP (The Odin Project) curriculum and working on my landing page project, so I'm trying to make it as responsive as possible.

How can I put the header's image below the sign up button once I minimize the page?

I've tried to use flex-wrap: wrap; but it'll not work because it wraps when the page is maximized.

.about {
display: flex;
justify-content: flex-start;
padding-bottom: 5%;
flex-wrap: wrap;
}

My code

html, body {
margin: 0;
padding: 0;
font-family: Verdana, Geneva, Tahoma, sans-serif;
box-sizing: border-box;
} 
/* blue container, first container */
.dark-blue {
background-color: #1f2937;
display: flex;
flex: 1 1 0;
padding-left: 10%;
padding-right: 10%;
}
.header {
display: flex;
justify-content: space-around;
align-items: center;
padding-bottom: 5%;
}
.logo {
color: #F9FAF8;
font-size: 24px;
font-weight: bolder;
display: flex;
flex: 1 1 auto;
}
.links {
font-size: 18px;
display: flex;
flex-shrink: 1;
gap: 1rem;
flex-wrap: wrap;
}
.link-text {
font-size: 18px;
color: #e5e7eb;
}
a {
text-decoration: none;
}
ul {
list-style-type: none;
}
.about {
display: flex;
justify-content: flex-start;
padding-bottom: 5%;
}
.main-text {
color: #F9FAF8;
font-size: 48px;
font-weight: bolder;
width: 95%;
display: flex;
flex: 1 1 auto;
padding-bottom: 1rem;
}
.secondary-text {
padding-bottom: .5rem;
font-size: 18px;
max-width: 80%;
color: #e5e7eb;
display: flex;
flex: 1 1 auto;
justify-content: start;
padding-bottom: 1rem;
}
.about-text {
padding-bottom: 5%;
display: flex;
flex-direction: column;
}
.button-one {
background-color: #1DB954;
color: #F9FAF8;
font-weight: bold;
border-radius: 10px;
width: 92px;
height: 20px;
padding: 8px;
text-align: center;
}
.button-one:hover {
color:  #F9FAF8;
font-weight: bold;
background-color: #126e32;
border-radius: 10px;
box-shadow: 0 5px 8px 0 rgba(224, 224, 224, 0.020), 0 7px 10px 0 rgba(238, 238, 238, 0.025);
width: 92px;
height: 20px;
padding: 8px;
text-align: center;
}
.button-text {
color: #F9FAF8;
}
.button-text:hover {
color: #F9FAF8;
}
.header-image {
width: 45%;
height: 18%;
border-radius: 2px;
display: flex;
flex-shrink: 0;
}
/* second container, information */
.information-header-text {
font-size: 36px; 
color: #1f2937;
font-weight: bolder;
text-align: center;
display: flex;
justify-content: center;
padding-top: 5rem;
padding-bottom: 2rem;
flex: 1 1 auto;
}
.image img {
width: 17rem;
height: 17rem;
border: none;
border-radius: 2px;
display: flex;
}
.image img:hover {
width: 17rem;
height: 17rem;
border: none;
border-radius: 2px;
box-shadow: 0 5px 12px 0px rgba(8, 8, 8, 0.199);
}
.subtext {
color: #1f2937;
width: 17rem;
text-align: center;
padding-top: .5rem;
padding-bottom: 1rem;
}
.image-subtext {
display: flex;
justify-content: center;
align-content: center;
gap: 1%;
flex-wrap: wrap;
padding-bottom: 8rem;
}
/* third container, quote container */
.quote {
background-color: #e5e7eb;
display: flex;
flex-wrap: wrap;
flex: 1 1 auto;
padding-left: 25%;
padding-right: 20%;
padding-bottom: 10%;
padding-top: 10%;
}
.quote-text {
font-size: 36px;
color: #1f2937;
font-style: italic;
font-weight: 100;
width: 100%;  
padding-bottom: 0;
}
.author {
font-size: 24px;
color: #1f2937;
font-weight: 900;
width: 90%;
display: flex;
flex-shrink: 1;
padding-left: 70%;
}
/* fourth container, box action */
.action {
background-color:#F9FAF8;
height: 30rem;
display: flex;
justify-content: center;
align-items: center;
flex: 1 1 auto;
}
.box-action {
background-color: #1DB954;
border-radius: 2px;
box-shadow: 0 5px 8px 0px rgba(8, 8, 8, 0.145);
display: flex;
justify-content: space-between;
align-items: center;
flex: 0 1 auto;
flex-wrap: wrap;
width: 50%;
padding: 5%;
padding-left: 10%;
padding-right: 10%;
gap: 2rem;
}
.action-title {
font-size: 32px;
color: #F9FAF8;
font-weight: bolder;
display: flex;
flex-shrink: 1;
}
.action-subtext {
color: #e5e7eb;
font-size: 18px;
font-weight: lighter;
display: flex;
flex-shrink: 1;
}
.button-two {
background-color: #1DB954;
color: #F9FAF8;
font-weight: bold;
border-radius: 10px;
border: 2px solid #F9FAF8;
width: 90px;
height: 20px;
padding: 5px;
text-align: center;
}
.button-two:hover {
color: #1DB954;
font-weight: bold;
background-color: #F9FAF8;
border-radius: 10px;
width: 90px;
height: 20px;
padding: 5px;
text-align: center;
}
.button-text-two {
color: #F9FAF8;
}
.button-text-two:hover {
color: #1DB954;
}
/* Footer */
footer {
color: #e5e7eb;
font-weight: lighter;
height: 5rem;
display: flex;
justify-content: center;
align-items: center;
}
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Black and White&lt;/title&gt;
&lt;link rel=&quot;icon&quot; href=&quot;images/blackandwhite.png&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;header dark-blue&quot;&gt;
&lt;div class=&quot;logo&quot;&gt;black and white&lt;/div&gt;
&lt;ul class=&quot;links&quot;&gt;
&lt;li&gt;&lt;a class=&quot;link-text&quot; href=&quot;#&quot;&gt;home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;link-text&quot; href=&quot;#photos&quot;&gt;photos&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;link-text&quot; href=&quot;#sign-up&quot;&gt;sign up&lt;/a&gt;&lt;/li&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;about dark-blue&quot;&gt;
&lt;div class=&quot;about-text&quot;&gt;
&lt;div class=&quot;main-text&quot;&gt;Get beautiful black and white pictures&lt;/div&gt;
&lt;div class=&quot;secondary-text&quot;&gt;The world is not black and white, there are lots of shades of grey. 
There are good things and bad things in every era, and 
it&#39;s kind of very blindfolded to say one era was wonderful, 
as it was wonderful, but there were a lot of bad things as well.&lt;/div&gt;
&lt;div class=&quot;button-one&quot;&gt;
&lt;a class=&quot;button-text&quot; href=&quot;#&quot;&gt;Sign up&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;img class=&quot;header-image&quot; src=&quot;./images/header-image.jpeg&quot;&gt;
&lt;/div&gt;
&lt;section id =&quot;photos&quot;&gt;&lt;/section&gt;
&lt;div class=&quot;information&quot;&gt;
&lt;div class=&quot;information-header-text&quot;&gt;Check some black and white photos&lt;/div&gt;
&lt;div class=&quot;image-subtext&quot;&gt;
&lt;div class=&quot;one&quot;&gt;
&lt;a href=&quot;https://images.unsplash.com/reserve/yZfr4jmxQyuaE132MWZm_stagnes.jpg?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1026&amp;q=80&quot;&gt;
&lt;div class=&quot;image&quot;&gt;&lt;img src=&quot;./images/rocks.jpeg&quot; alt=&quot;Monochrome coastal rocks&quot;&gt;&lt;/div&gt;
&lt;/a&gt;
&lt;div class=&quot;subtext&quot;&gt;Monochrome coastal rocks&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;two&quot;&gt;
&lt;a href=&quot;https://images.unsplash.com/photo-1574402897005-74dbf3f19be6?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=870&amp;q=80&quot;&gt;
&lt;div class=&quot;image&quot;&gt;&lt;img src=&quot;./images/stairway.jpeg&quot; alt=&quot;Stairway from metro station&quot;&gt;&lt;/div&gt;
&lt;/a&gt;
&lt;div class=&quot;subtext&quot;&gt;Stairway from metro station&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;three&quot;&gt;
&lt;a href=&quot;https://images.unsplash.com/photo-1603157259666-71adcb873119?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=870&amp;q=80&quot;&gt;
&lt;div class=&quot;image&quot;&gt;&lt;img src=&quot;./images/cat.jpeg&quot; alt=&quot;Cat eyes&quot;&gt;&lt;/div&gt;
&lt;/a&gt;
&lt;div class=&quot;subtext&quot;&gt;Cat eyes&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;four&quot;&gt;
&lt;a href=&quot;https://images.unsplash.com/photo-1452727333656-23ae1299777a?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=580&amp;q=80&quot;&gt;
&lt;div class=&quot;image&quot;&gt;&lt;img src=&quot;./images/truck.jpeg&quot; alt=&quot;Monochrome vintage truck&quot;&gt; &lt;/div&gt;
&lt;/a&gt;
&lt;div class=&quot;subtext&quot;&gt;Monochrome vintage truck&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;quote&quot;&gt;
&lt;div class=&quot;quote-text&quot;&gt;
Although humans see reality in colour, for me, 
black and white has always been connected to the image&#39;s deeper truth, 
to its most hidden meaning.
&lt;/div&gt;
&lt;div class=&quot;author&quot;&gt;
- Peter Lindbergh
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;section id =&quot;sign-up&quot;&gt;&lt;/section&gt;
&lt;div class=&quot;action&quot;&gt;
&lt;div class=&quot;box-action&quot;&gt;
&lt;div class=&quot;action-text&quot;&gt;
&lt;div class=&quot;action-title&quot;&gt;More photos?&lt;/div&gt;
&lt;div class=&quot;action-subtext&quot;&gt;Sign up for more beautiful black and white pictures!
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;button-two&quot;&gt;
&lt;a class=&quot;button-text-two&quot; href=&quot;#&quot;&gt;Sign up&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;footer class=&quot;dark-blue&quot;&gt;
Copyright &#169; The Odin Project 2023
&lt;/footer&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html, body {
margin: 0;
padding: 0;
font-family: Verdana, Geneva, Tahoma, sans-serif;
box-sizing: border-box;
} 
/* blue container, first container */
.dark-blue {
background-color: #1f2937;
display: flex;
flex: 1 1 0;
padding-left: 10%;
padding-right: 10%;
}
.header {
display: flex;
justify-content: space-around;
align-items: center;
padding-bottom: 5%;
}
.logo {
color: #F9FAF8;
font-size: 24px;
font-weight: bolder;
display: flex;
flex: 1 1 auto;
}
.links {
font-size: 18px;
display: flex;
flex-shrink: 1;
gap: 1rem;
flex-wrap: wrap;
}
.link-text {
font-size: 18px;
color: #e5e7eb;
}
a {
text-decoration: none;
}
ul {
list-style-type: none;
}
.about {
display: flex;
justify-content: flex-start;
padding-bottom: 5%;
}
.main-text {
color: #F9FAF8;
font-size: 48px;
font-weight: bolder;
width: 95%;
display: flex;
flex: 1 1 auto;
padding-bottom: 1rem;
}
.secondary-text {
padding-bottom: .5rem;
font-size: 18px;
max-width: 80%;
color: #e5e7eb;
display: flex;
flex: 1 1 auto;
justify-content: start;
padding-bottom: 1rem;
}
.about-text {
padding-bottom: 5%;
display: flex;
flex-direction: column;
}
.button-one {
background-color: #1DB954;
color: #F9FAF8;
font-weight: bold;
border-radius: 10px;
width: 92px;
height: 20px;
padding: 8px;
text-align: center;
}
.button-one:hover {
color:  #F9FAF8;
font-weight: bold;
background-color: #126e32;
border-radius: 10px;
box-shadow: 0 5px 8px 0 rgba(224, 224, 224, 0.020), 0 7px 10px 0 rgba(238, 238, 238, 0.025);
width: 92px;
height: 20px;
padding: 8px;
text-align: center;
}
.button-text {
color: #F9FAF8;
}
.button-text:hover {
color: #F9FAF8;
}
.header-image {
width: 45%;
height: 18%;
border-radius: 2px;
display: flex;
flex-shrink: 0;
}
/* second container, information */
.information-header-text {
font-size: 36px; 
color: #1f2937;
font-weight: bolder;
text-align: center;
display: flex;
justify-content: center;
padding-top: 5rem;
padding-bottom: 2rem;
flex: 1 1 auto;
}
.image img {
width: 17rem;
height: 17rem;
border: none;
border-radius: 2px;
display: flex;
}
.image img:hover {
width: 17rem;
height: 17rem;
border: none;
border-radius: 2px;
box-shadow: 0 5px 12px 0px rgba(8, 8, 8, 0.199);
}
.subtext {
color: #1f2937;
width: 17rem;
text-align: center;
padding-top: .5rem;
padding-bottom: 1rem;
}
.image-subtext {
display: flex;
justify-content: center;
align-content: center;
gap: 1%;
flex-wrap: wrap;
padding-bottom: 8rem;
}
.about {
display: flex;
justify-content: flex-start;
padding-bottom: 5%;
flex-wrap: wrap;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Black and White&lt;/title&gt;
&lt;link rel=&quot;icon&quot; href=&quot;images/blackandwhite.png&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;header dark-blue&quot;&gt;
&lt;div class=&quot;logo&quot;&gt;black and white&lt;/div&gt;
&lt;ul class=&quot;links&quot;&gt;
&lt;li&gt;&lt;a class=&quot;link-text&quot; href=&quot;#&quot;&gt;home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;link-text&quot; href=&quot;#photos&quot;&gt;photos&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;link-text&quot; href=&quot;#sign-up&quot;&gt;sign up&lt;/a&gt;&lt;/li&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;about dark-blue&quot;&gt;
&lt;div class=&quot;about-text&quot;&gt;
&lt;div class=&quot;main-text&quot;&gt;Get beautiful black and white pictures&lt;/div&gt;
&lt;div class=&quot;secondary-text&quot;&gt;The world is not black and white, there are lots of shades of grey. 
There are good things and bad things in every era, and 
it&#39;s kind of very blindfolded to say one era was wonderful, 
as it was wonderful, but there were a lot of bad things as well.&lt;/div&gt;
&lt;div class=&quot;button-one&quot;&gt;
&lt;a class=&quot;button-text&quot; href=&quot;#&quot;&gt;Sign up&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;img class=&quot;header-image&quot; src=&quot;./images/header-image.jpeg&quot;&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 0

如果你的意思是在减小页面尺寸时希望图像移动(而不是最小化它,即完全隐藏它),你可以在你的CSS中添加一些媒体查询。尝试添加以下内容:

@media screen and (max-width: 1200px) {
    .about {
        flex-direction: column;
    }
}

(设置像素断点以适应你想要的尺寸),然后查看是否有效!

英文:

If you mean that you want the image to move when you're reducing the size of the page (rather than minimizing it, i.e. hiding it completely), you could add some media queries to your CSS.

Try adding

@media screen and (max-width: 1200px) {
.about{
flex-direction: column;
}

(setting the px breakpoints for the size you want) and see if it works!

答案2

得分: 0

你可以添加一个媒体查询,在视口宽度达到特定阈值时切换flex方向,可能看起来像这样:

.about {
  display: flex;
  flex-direction: row;
}

@media (max-width: 600px) {
  .about {
    flex-direction: column;
  }
}

这在StackOverflow上很难演示,因为代码片段有框架,但如果你将上面的片段全屏并调整窗口大小,当窗口小于600px时,你将看到它切换到列方向。

如果你需要基于父元素宽度而不是整个视口进行调整,你也可以使用容器查询来实现。

英文:

You could add a media query that switches the flex direction between row and column when the viewport crosses a particular width threshold.

Might look something like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.about {
display: flex;
flex-direction: row;
}
@media (max-width: 600px) {
.about {
flex-direction: column;
}
}

<!-- language: lang-html -->

&lt;div class=&quot;about&quot;&gt;
&lt;div class=&quot;about-text&quot;&gt;
&lt;div class=&quot;main-text&quot;&gt;Get beautiful black and white pictures&lt;/div&gt;
&lt;div class=&quot;secondary-text&quot;&gt;The world is not black and white, there are lots of shades of grey. There are good things and bad things in every era, and it&#39;s kind of very blindfolded to say one era was wonderful, as it was wonderful, but there were a lot of bad things as well.&lt;/div&gt;
&lt;div class=&quot;button-one&quot;&gt;
&lt;a class=&quot;button-text&quot; href=&quot;#&quot;&gt;Sign up&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;img src=&quot;//placekitten.com/300/300&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

This is difficult to demonstrate on StackOverflow due to the snippet framing, but if you full-screen the snippet above and adjust your window size you'll see it snap to column orientation when your window is narrower than 600px.

You could also do this with a container query if you need to base it on a parent element width instead of the entire viewport.

huangapple
  • 本文由 发表于 2023年2月18日 02:45:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定