I want to display images inside a container that has a colored background.
I want the background of the container is rounded, and the image is placed at the center.
Also, there is space between the border of the background and the image.
This is the goal:
img
The code so far:
.circle{
border-radius: 50%;
background: #2e374f;
display: flex;
justify-content: center;
align-items: center;
height: 50px;
width: 50px;
display: table-cell;
vertical-align: middle;
}
.title{
color: #ffffff;
font-size: 10px;
text-align: center;
margin-top: 5px;
opacity: 0.5;
}
<div class="circle">
<img src="https://www.fillmurray.com/50/50" class="mx-auto d-block">
<div class="title">
TITLE
</div>
</div>
this is what I achieved so far:
img
you need to remove display: table-cell rule and additionaly you can shrink the image.
.circle{
border-radius: 50%;
background: #2e374f;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 150px;
width: 150px;
}
.title{
color: #ffffff;
font-size: 10px;
text-align: center;
margin-top: 5px;
opacity: 0.5;
}
.img {
max-width: 50%;
max-height: 50%;
}
<div class="circle">
<img src="https://picsum.photos/100/100?grayscale" class="mx-auto d-block img">
<div class="title">
TITLE
</div>
</div>
Please check.
.container-box {
width: 400px;
height: 400px;
background-color: black;
}
.container-circle {
display:inline-block;
margin: 50px;
width: 300px;
height: 300px;
border-radius: 150px;
background-color: gray;
}
.image {
background: url(https://upload.wikimedia.org/wikipedia/commons/c/ce/Font_Awesome_5_solid_arrow-circle-right.svg);
width: 100px;
height: 100px;
display: inline-block;
margin: 100px;
}
<div class="container-box">
<div class="container-circle">
<div class="image">
</div>
</div>
</div>
`
If I see correctly you want the image to be round and not the container. In this case this should help you out:
.circle img {
border-radius: 50%;
max-width: 100%;
}
.circle{
background: #2e374f;
display: flex;
justify-content: center;
align-items: center;
flex-flow: column;
height:100px;
width: 100px;
}
.title{
color: #ffffff;
font-size: 10px;
text-align: center;
margin-top: 5px;
opacity: 0.5;
}
<div class="circle">
<img src="https://www.fillmurray.com/50/50" class="mx-auto d-block">
<div class="title">
TITLE
</div>
</div>
Related
I have a main container that contains an image and a div. What I want to achieve is to center the div on top of the image without having to use absolute or relative positioning on it. How can I achieve this? Thanks in advance.
.imgCon {
display: flex;
width: 95%;
height: 95%;
margin: auto;
background-color: blue;
}
.imgCon img {
width: 95.5%;
height: 95%;
margin: auto;
border-radius: inherit;
}
.iconCon {
display: flex;
justify-content: center;
align-items: center;
width: 30%;
height: 30%;
margin: auto;
color: #ffffff;
border-radius: 50%;
background-color: #000000;
}
<div class="imgCon">
<!--center this-->
<div class="iconCon">
C
</div>
<img src="https://www.vitalground.org/wp-content/uploads/2021/11/Bart-the-Bear-II--e1637176991443.jpg" />
</div>
The only way I can think of without using absolute positioning or changing your markup is to use a CSS Grid and make both elements occupy the same cell.
.imgCon {
display: flex;
width: 95%;
height: 95%;
margin: auto;
background-color: blue;
display: grid;
}
.imgCon img {
width: 95.5%;
height: 95%;
margin: auto;
border-radius: inherit;
grid-column-start: 1;
grid-row-start: 1;
}
.iconCon {
display: flex;
justify-content: center;
align-items: center;
width: 4rem;
height: 4rem;
margin: auto;
color: #ffffff;
border-radius: 50%;
background-color: #000000;
grid-column-start: 1;
grid-row-start: 1;
}
<div class="imgCon">
<img src="https://www.vitalground.org/wp-content/uploads/2021/11/Bart-the-Bear-II--e1637176991443.jpg" />
<!--center this-->
<div class="iconCon">
C
</div>
</div>
You could use background-image property and tiny styling changes.
.imgCon {
display: flex;
justify-content: center;
align-items: center;
width: 20rem;
height: 20rem;
margin: auto;
background-color: blue;
background-image: url("https://www.vitalground.org/wp-content/uploads/2021/11/Bart-the-Bear-II--e1637176991443.jpg");
background-position: center center;
background-repeat: no-repeat;
background-size: 100% 80%;
}
.iconCon {
display: flex;
justify-content: center;
align-items: center;
width: 3rem;
height: 3rem;
margin: auto;
color: #ffffff;
border-radius: 50%;
background-color: #000000;
}
.container {
display: flex;
justify-content: center;
align-item: center;
}
<div class="imgCon">
<div class="container">
<!--center this-->
<div class="iconCon">
C
</div>
</div>
</div>
I really don't know what I'm doing wrong here. I want the image inside the box to stay centered when the window shrinks. Furthermore, I would have thought that align-items: center; would work, but apparently not. The colors are only relevant for me, so I understand what's going on. I don't know if there is a solution for this either, but I hope so. And please ignore the naming and order of the individual classes, I couldn't do better ...:)
.megadiv {
max-width: 1600px;
margin: auto;
text-align: center;
}
.centerbox {
display: flex;
justify-content: space-between;
}
.left {
width: 64%;
background-color: red;
justify-content: space-between;
border: 2px solid gray;
display: flex;
}
.insideleft {
width: 20%;
background-color: yellow;
align-items: center;
text-align: center;
align-content: center;
}
.insideright {
width: 78%;
background-color: purple;
float: right;
padding-top: 2%;
text-align: left;
border-left: 2px ridge #ffa54f;
padding-left: 2%;
padding-bottom: 1%;
}
.picture {
width: 80%;
border-radius: 1%;
margin-top: 10%;
margin-bottom: 8%;
}
.right {
width: 34%;
border: 2px solid gray;
height: 20px;
}
h7 {
color: rgb(0, 153, 158);
font-size: large;
font-family: sans-serif;
}
.textpart {
margin-bottom: 0.5%;
}
<div class="megadiv">
<div class="centerbox">
<div class="left">
<div class="insideleft">
<h20>
<a href="">
<img class="picture" src="https://images-na.ssl-images-amazon.com/images/I/71hi8fWdX2L.jpg"> </a>
</h20>
</div>
<div class="insideright">
<h7>Headline</h7><br>
<h4>
<div class="textpart">Authors</div>
<div class="textpart">Views <a class="" href="">Chapter 2</a></div>
<div class="textpart">Genres: Action - Adventure - Comedy</div>
<div class="textpart">Rating: ⭐⭐⭐⭐⭐</div>
</h4>
</div>
<div class="right">
wawaeaweew
</div>
</div>
</div>
h4 and h20 are empty
You're pretty close to getting the image vertically aligned as you wanted. Try this out, and see if this works the way you would like:
.megadiv {
max-width: 1600px;
margin: auto;
text-align: center;
}
.centerbox {
display: flex;
justify-content: space-between;
}
.left {
width: 64%;
background-color: red;
justify-content: space-between;
border: 2px solid gray;
display: flex;
}
.insideleft {
display: flex;
width: 20%;
background-color: yellow;
align-items: center;
text-align: center;
align-content: center;
}
.insideright {
width: 78%;
background-color: purple;
float: right;
padding-top: 2%;
text-align: left;
border-left: 2px ridge #ffa54f;
padding-left: 2%;
padding-bottom: 1%;
}
.picture {
width: 80%;
border-radius: 1%;
margin-top: 10%;
margin-bottom: 8%;
}
.right {
width: 34%;
border: 2px solid gray;
height: 20px;
}
h7 {
color: rgb(0, 153, 158);
font-size: large;
font-family: sans-serif;
}
.textpart {
margin-bottom: 0.5%;
}
<div class="megadiv">
<div class="centerbox">
<div class="left">
<div class="insideleft">
<a href="">
<img class="picture" src="https://images-na.ssl-images-amazon.com/images/I/71hi8fWdX2L.jpg"> </a>
</div>
<div class="insideright">
<h7>Headline</h7><br>
<h4>
<div class="textpart">Authors</div>
<div class="textpart">Views <a class="" href="">Chapter 2</a></div>
<div class="textpart">Genres: Action - Adventure - Comedy</div>
<div class="textpart">Rating: ⭐⭐⭐⭐⭐</div>
</h4>
</div>
<div class="right">
wawaeaweew
</div>
</div>
</div>
I saw you used align-items: center; in the .insideleft CSS selector which is for aligning a container's children to the center like you want, you'll just want to make this a flexbox to make this work. To do this, simply add display: flex; to the .insideleft selector like in the example. I also removed the <h20> tag from the HTML as this is not valid or necessary.
As for the image shrinking down when the screen width is shrinked - this is because you're using percentages for the widths for all the containers and the image. If you want the image to stop shrinking after a certain point, you can add min-width: 80px; /* (this can be any number of pixels) */ to your .picture selector to make the image stop shrinking once it gets to a certain width of pixels.
Flexbox is super useful for position elements in CSS and I'd recommend looking into this more to have a better understanding. Check out this link here if you'd like an overview of the different flexbox CSS properties.
I am not 100% sure on your intent - Here I changed the class names a bit for clarity and adjusted the markup for a left-middle-right
Not a huge fan of % for padding and margin sizing myself (em feels more clear since it is based on the current font size)
Not strictly needed but I added the containing element class in a few places in CSS for clarity example: .left-pane .picture-container
.page-container {
max-width: 1600px;
text-align: center;
}
.container-box {
display: flex;
align-content: space-between;
}
.container-box .left-pane {
width: 20em;
display: flex;
align-items: center;
justify-content: center;
background-color: #FF0000;
border: 2px solid gray;
}
.left-pane .picture-container {
width: 30%;
background-color: yellow;
align-items: center; /* vertical */
align-content: center; /* horizontal */
}
.left-pane .picture-container .picture {
width: 80%;
border-radius: 1%;
margin-top: 10%;
margin-bottom: 8%;
}
.container-box .middle-pane {
width: 70em;
background-color: #FFDDDD;
padding-top: 2%;
padding-left: 2%;
padding-bottom: 1%;
border-left: 2px ridge #ffa54f;
}
.middle-pane .headline {
color: rgb(0, 153, 158);
font-size: 1.5em;
font-family: sans-serif;
margin-bottom: 1em;
background-color: #eeeeee;
}
.middle-pane .textpart {
margin-bottom: 0.5em;
}
.container-box .right-pane {
height: 20px;
border: 2px solid gray;
}
<div class="page-container">
<div class="container-box">
<div class="left-pane">
<div class="picture-container">
<div>
<a href="">
<img class="picture" src="https://images-na.ssl-images-amazon.com/images/I/71hi8fWdX2L.jpg"> </a>
</div>
</div>
</div>
<div class="middle-pane">
<div class="headline">Headline</div>
<h4>
<div class="textpart">Authors</div>
<div class="textpart">Views <a class="" href="">Chapter 2</a></div>
<div class="textpart">Genres: Action - Adventure - Comedy</div>
<div class="textpart">Rating: ⭐⭐⭐⭐⭐</div>
</h4>
</div>
<div class="right-pane">
wawaeaweew
</div>
</div>
</div>
So I have flexbox items inside of a flex container, inside of these items there are image and button as shown in the following picture:
But for some reason when I try to do that, the button goes above the image as per the below screenshot:
My css code:
.navbar .ps_container{ /*flex container*/
justify-content: space-between;
display: flex;
flex-wrap: wrap;
width:100%;
height: auto;
background-color: blue;
padding-bottom: 50px;
padding-top: 50px;
}
.ps_container>div{ /* flexbox items*/
display: flex;
flex:1 1 auto;
justify-content: center;
margin:20px;
padding-inline: 40px;
width:200px;
height:200px;
background-color: white;
}
.ps_container>div img{ /* image style*/
max-height: 300px;
max-width: 300px;
object-fit: contain;
vertical-align: middle;
margin: 0 auto;
}
.ps_container>div a{ /* button style*/
background-color: #20d8da;
-webkit-transition-duration: 500ms;
transition-duration: 500ms;
position: relative;
z-index: 1;
display: inline-block;
min-width: 160px;
height: 50px;
color: #fff;
border-radius: 0;
padding: 0 30px;
font-size: 14px;
line-height: 50px;
font-weight: 500;
text-transform: capitalize;
text-align: center;
}
<div class="ps_container">
<div class="block">
<img src="https://dummyimage.com/600x400/000/fff">
view Games
</div>
<div class="block">
<img src="https://dummyimage.com/600x400/000/fff">
view Games
</div>
<div class="block">
<img src="https://dummyimage.com/600x400/000/fff">
view Games
</div>
</div>
I thought maybe it has to do with img not being display:block, but adding that didnt seem to work either.
I guess you are searching for flex-direction:column; in your div.block.
Doc : https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction
After your block goes on each other because you are setting container size (200x200) smaller than img (300x300) inside it...
DEMO:
.navbar .ps_container{ /*flex container*/
justify-content: space-between;
display: flex;
flex-wrap: wrap;
width:100%;
height: auto;
background-color: blue;
padding-bottom: 50px;
padding-top: 50px;
}
.ps_container>div{ /* flexbox items*/
display: flex;
flex-direction:column; /***** ADDED *****/
flex:1 1 auto;
justify-content: center;
margin:20px;
padding-inline: 40px;
width:200px;
height:200px;
background-color: white;
}
.ps_container>div img{ /* image style*/
max-height: 300px;
max-width: 300px;
object-fit: contain;
vertical-align: middle;
margin: 0 auto;
}
.ps_container>div a{ /* button style*/
background-color: #20d8da;
-webkit-transition-duration: 500ms;
transition-duration: 500ms;
position: relative;
z-index: 1;
display: inline-block;
min-width: 160px;
height: 50px;
color: #fff;
border-radius: 0;
padding: 0 30px;
font-size: 14px;
line-height: 50px;
font-weight: 500;
text-transform: capitalize;
text-align: center;
}
<div class="ps_container">
<div class="block">
<img src="https://dummyimage.com/600x400/000/fff">
view Games
</div>
<div class="block">
<img src="https://dummyimage.com/600x400/000/fff">
view Games
</div>
<div class="block">
<img src="https://dummyimage.com/600x400/000/fff">
view Games
</div>
</div>
You might wanna use flex-direction: column to achieve it.
.ps_container {
/*flex container*/
justify-content: space-between;
display: flex;
flex-wrap: wrap;
width: 100%;
height: auto;
background-color: blue;
padding-bottom: 50px;
padding-top: 50px;
}
.ps_container > div {
/* flexbox items*/
display: inline-flex;
flex-direction: column;
justify-content: center;
height: 200px;
background-color: white;
}
.ps_container > div img {
/* image style*/
max-height: 300px;
max-width: 300px;
object-fit: contain;
vertical-align: middle;
margin: 0 auto;
}
.ps_container > div a {
/* button style*/
background-color: #20d8da;
-webkit-transition-duration: 500ms;
transition-duration: 500ms;
position: relative;
z-index: 1;
display: inline-block;
height: 50px;
color: #fff;
border-radius: 0;
padding: 0 30px;
font-size: 14px;
line-height: 50px;
font-weight: 500;
text-transform: capitalize;
text-align: center;
}
<div class="ps_container">
<div class="block">
<img src="https://dummyimage.com/600x400/000/fff" />
view Games
</div>
<div class="block">
<img src="https://dummyimage.com/600x400/000/fff" />
view Games
</div>
<div class="block">
<img src="https://dummyimage.com/600x400/000/fff" />
view Games
</div>
</div>
EDIT
Updated the answer including the fix to the parent to wrap the boxes in a single row.
Changes I done:
Added flex-direction: column to the box .ps_container > div so that the button will come below the image.
Remove width:200px; from .ps_container>div.
Replaced display: flex with display: inline-flex in .ps_container>div.
By giving width in static, you will have to readjust the same when you are using the site in responsive mode, without that, the width and details will update according to the window width, and that's what I will prefer.
in spite of my research on the internet I did not succeed in doing what I wanted to do.
I want to put my text to the left of my text or my text to the right of my image but I can't do it.
Here is the code :
HTML :
<div class="surfooter">
<div class="flex-container">
<div class="flex-item">
<img src="img/Nike.png" alt="Nike" class="img-responsive"/>
<p>Nike.com</p>
</div>
<div class="flex-item">
<img src="img/Adidas.png" alt="Adidas" class="img-responsive" />
<p>Adidas.com</p>
</div>
</div>
</div>
CSS :
.surfooter{
padding-bottom: 50px;
padding-top: 50px;
background-color: #466964;
color: white;
font-size: 16px;
position: relative;
}
.flex-container {
display: flex;
flex-flow: row wrap;
justify-content: space-between;
height: 200px;
position: relative;
}
.flex-item {
margin: auto;
width: auto;
height: auto;
}
.flex-item p{
color: white;
font-size: 1.41em;
font-weight: bold;
line-height: 1.3em;
width: 80%;
margin: 0 auto;
}
.flex-item img{
object-fit: cover;
border-radius: 100%;
width: 150px;
height: 150px;
}
Thank you for reading
Since you're trying to change your text's, you have to change your flex-item div, which is:
In your Css script, put this:
.flex-item{
display:flex;}
Set display:flex; on your flex-item div.
HTML:
<div class="flex-item">
<img src="img/Nike.png" alt="Nike" class="img-responsive"/>
<p>Nike.com</p>
</div>
CSS:
.flex-item{
display:flex;
}
Change your .flex-item class to this hope this solves your problem.
.flex-item {
display:flex;
flex-direction:row;
}
I tried like this. It works.
<div class="surfooter">
<div class="flex-container">
<div class="flex-item">
<img src="img/Nike.png" alt="Nike" class="img-responsive"/>
<p>Nike.com</p>
</div>
<div class="flex-item">
<img src="img/Adidas.png" alt="Adidas" class="img-responsive" />
<p>Adidas.com</p>
</div>
</div>
</div>
css
.surfooter{
padding-bottom: 50px;
padding-top: 50px;
background-color: #466964;
color: white;
font-size: 16px;
position: relative;
}
.flex-container {
display: flex;
flex-flow: row wrap;
justify-content: space-between;
height: 200px;
position: relative;
}
.flex-item {
margin: auto;
width: auto;
height: auto;
display: flex;
justify-content: center;
align-items: center;
}
.flex-item p{
color: white;
font-size: 1.41em;
font-weight: bold;
line-height: 1.3em;
width: 80%;
margin: 0 auto;
}
.flex-item img{
object-fit: cover;
border-radius: 100%;
}
There are a few divs. I want to set width of black as needed and put it on the middle (horizontally) of red. Then put some elements in black in one line and position them on the middle (vertically) of black.
The final result should looks like:
There is a problem with center vertically.
My code is:
<html>
<body>
<div id="mainContainer">
<div id="singleOptions">
<div id="myObject"></div>
<div id="mySecondObject"></div>
</div>
</div>
</body>
<style>
#mainContainer {
background: red;
width: 100px;
height: 100px;
margin-top: 50px;
text-align: center;
}
#singleOptions {
height: 100%;
background: black;
display: inline-block;
}
#myObject {
width: 10px;
display: inline-block;
height: 10px;
background: green;
}
#mySecondObject {
width: 10px;
display: inline-block;
height: 10px;
background: yellow;
}
</style>
</html>
How is it possible to get this effect?
You can try it like this
#mainContainer {
background: red;
width: 100px;
height: 100px;
margin-top: 50px;
text-align: center;
}
#singleOptions {
height: 100%;
background: black;
display: flex;
margin: 0 auto;
justify-content: space-between;
width: min-content;
align-items: center;
}
#myObject {
width: 10px;
display: inline-block;
height: 10px;
margin-right: 5px;
background: green;
}
#mySecondObject {
width: 10px;
display: inline-block;
height: 10px;
background: yellow;
}
<html>
<body>
<div id="mainContainer">
<div id="singleOptions">
<div id="myObject"></div>
<div id="mySecondObject"></div>
</div>
</div>
</body>
</html>
You can achieve this using flexbox:
#mainContainer {
background: red;
width: 100px;
height: 100px;
text-align: center;
display: flex;
justify-content: center;
}
#singleOptions {
height: 100%;
background: black;
display: flex;
align-items: center;
}
#myObject {
width: 10px;
display: inline-block;
height: 10px;
background: green;
}
#mySecondObject {
width: 10px;
display: inline-block;
height: 10px;
background: yellow;
}
<div id="mainContainer">
<div id="singleOptions">
<div id="myObject"></div>
<div id="mySecondObject"></div>
</div>
</div>
This is solved easily with Flexbox.
Change your CSS for #singleOptions as follows.
#singleOptions {
height: 100%;
background-color: black;
display: flex;
justify-content: space-between;
align-items: center;
}