如何在图像后面创建圆形背景?

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

How to create circular background behind an image?

问题

我试图重新创建这个图像。

如何在图像后面创建圆形背景?

然而,我无法弄清楚如何在图像后面创建橙色圆圈。我尝试过使用相对和绝对定位,但没有效果,而且会将其他元素推到一侧。我甚至尝试过给包裹图像的 div 元素上色并给它添加边框半径,但只是导致一些难看的线条在两侧可见。

这是我的代码:

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@100;400;700;800;900&display=swap');

:root{
    --background: #f7f7ff;
    --headerBackgroundColour: #ff9101;
    --titles: #0b0b15;
    --text: #555555;
    --borders: #bbbbbb;
    --dividers: #eeeeee;

    --spacing: 1em;
    --sectionSpacing: 4vw;
}

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

    font-family: 'Roboto';
    text-decoration: none;

    color: var(--text);
    font-weight: 400;
}

.PageHeader{
    width: 100%;

    display: flex;
    flex-direction: row;
    justify-content: center;

}

.PageHeader div:nth-child(1){
    width: 50%;

    padding-left: var(--sectionSpacing);
    padding-top: var(--sectionSpacing);
    padding-bottom: var(--sectionSpacing);
}

.PageHeader div:nth-child(1) p:nth-child(1){
    color: var(--titles);

}

.PageHeader div:nth-child(1) h1{

    padding: 0;
    font-weight: 900;
    color: var(--titles);
    font-size: 6em;
}

.PageHeader div:nth-child(1) h2{
    color: var(--headerBackgroundColour);
    font-weight: 700;
    font-size: 2em;
}

.NavBar ul{
    margin-top: var(--spacing);
    display: flex;
    flex-direction: row;
    justify-content: space-evenly;
    align-items: center;
}

    .NavBar li{
        list-style: none;
    }

    .NavBar li a{

        padding: var(--spacing);
        border-radius: 5px;
    }

.PageHeader .ImageContainer{
    width: 50%;

    display: flex;
    flex-direction: column;

    padding-right: var(--sectionSpacing);
}

    .PageHeader .ImageContainer div{
        width: 1000px;
        height: 1000px;

        background-color: var(--headerBackgroundColour);
        position: absolute;
        display: block;

        border-radius: 50%;
    }


    .PageHeader .ImageContainer .Image{
        display: inline-block;
        width: 250px;
        height: 250px;
        background-color: red;

        margin-left: var(--sectionSpacing);
    }

.BioContainer{
    background-color: whitesmoke;
}
<!DOCTYPE html>
<html lang="en">
<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">

    <link rel="stylesheet" href="./style.css"/>


    <title>Joe Doe</title>
</head>
<body>
        <header class="PageHeader">
            <div>

                <p>Hello, My name is</p>
                <h1>Joe Doe</h1>
                <h2>Full-Stack Developer</h2>
                <p>I design and develop services for customers of all sizes, specializing in creating stylish, modern websites, web services and online stores</p>
                
                <nav class='NavBar'>
                    <ul>
                        <li><a href='#Biography'>Biography</a></li>
                        <li><a href='#MySkills'>What I Do?</a></li>
                        <li><a href='#Awards'>Awards</a></li>
                        <li><a href='#Experience'>Experience</a></li>
                        <li><a href='#Portfolio'>My Portfolio</a></li>
                        <li><a href='#Contact'>Contact</a></li>
                    </ul>
                </nav>
            </div>

            <div class="ImageContainer">
                <div></div>
                <p class="Image">IMAGE</p>
            </div>
    
        </header>

</body>
</html>
英文:

I am trying to recreate this image.

如何在图像后面创建圆形背景?

I however can't figure out how to create the orange circle behind the image. I've trid using relative and absolute position but that did nothing and pushed other elements to the side. I have even tried to color the div parent element and give it border radius but that only caused some ugly lines to be visible on the sides.

This is my code.

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

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

@import url(&#39;https://fonts.googleapis.com/css2?family=Roboto:wght@100;400;700;800;900&amp;display=swap&#39;);
:root{
--background: #f7f7ff;
--headerBackgroundColour: #ff9101;
--titles: #0b0b15;
--text: #555555;
--borders: #bbbbbb;
--dividers: #eeeeee;
--spacing: 1em;
--sectionSpacing: 4vw;
}
*{
box-sizing: border-box;
padding: 0;
margin: 0;
font-family: &#39;Roboto&#39;;
text-decoration: none;
color: var(--text);
font-weight: 400;
}
.PageHeader{
width: 100%;
display: flex;
flex-direction: row;
justify-content: center;
}
.PageHeader div:nth-child(1){
width: 50%;
padding-left: var(--sectionSpacing);
padding-top: var(--sectionSpacing);
padding-bottom: var(--sectionSpacing);
}
.PageHeader div:nth-child(1) p:nth-child(1){
color: var(--titles);
}
.PageHeader div:nth-child(1) h1{
padding: 0;
font-weight: 900;
color: var(--titles);
font-size: 6em;
}
.PageHeader div:nth-child(1) h2{
color: var(--headerBackgroundColour);
font-weight: 700;
font-size: 2em;
}
.NavBar ul{
margin-top: var(--spacing);
display: flex;
flex-direction: row;
justify-content: space-evenly;
align-items: center;
}
.NavBar li{
list-style: none;
}
.NavBar li a{
padding: var(--spacing);
border-radius: 5px;
}
.PageHeader .ImageContainer{
width: 50%;
display: flex;
flex-direction: column;
padding-right: var(--sectionSpacing);
}
.PageHeader .ImageContainer div{
width: 1000px;
height: 1000px;
background-color: var(--headerBackgroundColour);
position: absolute;
display: block;
border-radius: 50%;
}
.PageHeader .ImageContainer .Image{
display: inline-block;
width: 250px;
height: 250px;
background-color: red;
margin-left: var(--sectionSpacing);
}
.BioContainer{
background-color: whitesmoke;
}

<!-- 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;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;./style.css&quot;/&gt;
&lt;title&gt;Joe Doe&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;header class=&quot;PageHeader&quot;&gt;
&lt;div&gt;
&lt;p&gt;Hello, My name is&lt;/p&gt;
&lt;h1&gt;Joe Doe&lt;/h1&gt;
&lt;h2&gt;Full-Stack Developer&lt;/h2&gt;
&lt;p&gt;I design and develop services for customers of all sizes, specializing in creating stylish, modern websites, web services and online stores&lt;/p&gt;
&lt;nav class=&#39;NavBar&#39;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#39;#Biography&#39;&gt;Biography&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#39;#MySkills&#39;&gt;What I Do?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#39;#Awards&#39;&gt;Awards&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#39;#Experience&#39;&gt;Experience&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#39;#Portfolio&#39;&gt;My Portfolio&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#39;#Contact&#39;&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;/div&gt;
&lt;div class=&quot;ImageContainer&quot;&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p class=&quot;Image&quot;&gt;IMAGE&lt;/p&gt;
&lt;/div&gt;
&lt;/header&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 2

这是一个基本的想法,您可以通过在一个块级元素上设置圆角,并在父元素中隐藏溢出来实现它。

.wrapper {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.wrapper::after {
  position: absolute;
  z-index: 0;
  display: block;
  content: '';
  top: -150px;
  right: -100px;
  border-radius: 50%;
  width: 400px;
  height: 400px;
  background-color: blue;
}

.row {
  display: flex;
  flex-direction: row;
  width: 100%;
  display: flex;
  height: 200px;
}

.column {
  display: flex;
  flex-direction: column;
  flex-basis: 100%;
  flex: 1;
  z-index: 1;
}

.column.image {
  background-image: url('https://placekitten.com/75/150');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center bottom;
}
<div class="wrapper">
  <div class='row'>
    <div class='column'>
      <h2>Hello World</h2>
      <p>Basic idea</p>
    </div>
    <div class='column image'>
    </div>
  </div>
</div>

另一个选项是使用SVG或剪裁路径来修剪元素。

英文:

This is a basic idea on how you could accomplish it with a block element that has a border radius set on it and with the parent hiding the overflow.

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

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

.wrapper {
position: relative;
height: 200px;
overflow: hidden;
}
.wrapper::after {
position: absolute;
z-index: 0;
display: block;
content: &#39;&#39;;
top: -150px;
right: -100px;
border-radius: 50%;
width: 400px;
height: 400px;
background-color: blue;
}
.row {
display: flex;
flex-direction: row;
width: 100%;
display: flex;
height: 200px;
}
.column {
flex-direction: column;
flex-basis: 100%;
flex: 1;
z-index: 1;
}
.column.image {
background-image: url(&#39;https://placekitten.com/75/150&#39;);
background-repeat: no-repeat;
xbackground-attachment: fixed;
background-position: center bottom;
}

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

&lt;div class=&quot;wrapper&quot;&gt;
&lt;div class=&#39;row&#39;&gt;
&lt;div class=&#39;column&#39;&gt;
&lt;h2&gt;Hello World&lt;/h2&gt;
&lt;p&gt;Basic idea&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#39;column image&#39;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Other option is using an SVG or a clip path to trim the element.

答案2

得分: 1

通常你可以使用 position: absolute 属性并设置 topbottom 以及 leftright 属性来定位元素。你可以在 https://www.w3schools.com/css/css_positioning.asp 上了解更多相关信息。

你可以使用 z-index 属性来调整哪个元素在另一个元素之上。你可以在 https://www.w3schools.com/cssref/pr_pos_z-index.php 上了解更多相关信息。

我已经为你的图片添加了 z-index,并调整了图片和圆圈的 topright 属性。

.PageHeader .ImageContainer .Image{
    display: inline-block;
    width: 250px;
    height: 250px;
    background-color: red;
    z-index: 999;
    position: absolute;
    top: 25%;
    right: 15%;
    margin-left: var(--sectionSpacing);
}
英文:

Usually you can position the elements with position: absolute and setting the top or bottom and left or right property. You can learn more about it on https://www.w3schools.com/css/css_positioning.asp

You can adjust which element is over the other with the property z-index. You can learn more about it on https://www.w3schools.com/cssref/pr_pos_z-index.php

I have added a z-index to your image and adjusted the top and right properties for both the image and the circle.

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

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

@import url(&#39;https://fonts.googleapis.com/css2?family=Roboto:wght@100;400;700;800;900&amp;display=swap&#39;);
:root{
--background: #f7f7ff;
--headerBackgroundColour: #ff9101;
--titles: #0b0b15;
--text: #555555;
--borders: #bbbbbb;
--dividers: #eeeeee;
--spacing: 1em;
--sectionSpacing: 4vw;
}
*{
box-sizing: border-box;
padding: 0;
margin: 0;
font-family: &#39;Roboto&#39;;
text-decoration: none;
color: var(--text);
font-weight: 400;
}
.PageHeader{
width: 100%;
display: flex;
flex-direction: row;
justify-content: center;
}
.PageHeader div:nth-child(1){
width: 50%;
padding-left: var(--sectionSpacing);
padding-top: var(--sectionSpacing);
padding-bottom: var(--sectionSpacing);
}
.PageHeader div:nth-child(1) p:nth-child(1){
color: var(--titles);
}
.PageHeader div:nth-child(1) h1{
padding: 0;
font-weight: 900;
color: var(--titles);
font-size: 6em;
}
.PageHeader div:nth-child(1) h2{
color: var(--headerBackgroundColour);
font-weight: 700;
font-size: 2em;
}
.NavBar ul{
margin-top: var(--spacing);
display: flex;
flex-direction: row;
justify-content: space-evenly;
align-items: center;
}
.NavBar li{
list-style: none;
}
.NavBar li a{
padding: var(--spacing);
border-radius: 5px;
}
.PageHeader .ImageContainer{
width: 50%;
display: flex;
flex-direction: column;
padding-right: var(--sectionSpacing);
}
.PageHeader .ImageContainer div{
width: 1000px;
height: 1000px;
background-color: var(--headerBackgroundColour);
position: absolute;
display: block;
border-radius: 50%;
top: -40%;
}
.PageHeader .ImageContainer .Image{
display: inline-block;
width: 250px;
height: 250px;
background-color: red;
z-index: 999;
position: absolute;
top: 25%;
right: 15%;
margin-left: var(--sectionSpacing);
}
.BioContainer{
background-color: whitesmoke;
}

<!-- 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;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;./style.css&quot;/&gt;
&lt;title&gt;Joe Doe&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;header class=&quot;PageHeader&quot;&gt;
&lt;div&gt;
&lt;p&gt;Hello, My name is&lt;/p&gt;
&lt;h1&gt;Joe Doe&lt;/h1&gt;
&lt;h2&gt;Full-Stack Developer&lt;/h2&gt;
&lt;p&gt;I design and develop services for customers of all sizes, specializing in creating stylish, modern websites, web services and online stores&lt;/p&gt;
&lt;nav class=&#39;NavBar&#39;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#39;#Biography&#39;&gt;Biography&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#39;#MySkills&#39;&gt;What I Do?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#39;#Awards&#39;&gt;Awards&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#39;#Experience&#39;&gt;Experience&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#39;#Portfolio&#39;&gt;My Portfolio&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#39;#Contact&#39;&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;/div&gt;
&lt;div class=&quot;ImageContainer&quot;&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p class=&quot;Image&quot;&gt;IMAGE&lt;/p&gt;
&lt;/div&gt;
&lt;/header&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定