Add rectangle with content inside background image - html

I'm new to CSS, and I want to add a rectangle in the middle right of the image and add some content inside it
Fiddle
HTML:
<div class="MyClass__background"> </div>
SCSS:
.MyClass{
min-height: 100vh;
display: flex;
flex-direction: column;
&__background {
background-image: url(https://www.incimages.com/uploaded_files/image/1920x1080/getty_509107562_2000133320009280346_351827.jpg);
background-size: 100% 100%;
background-repeat: no-repeat;
height: 100vh;
}
}
So I want something like this:
Image
How can I achieve this? Regards

Your can find great docs here https://www.w3schools.com/css/default.asp and https://developer.mozilla.org/en-US/docs/Web/CSS
body {
padding: 0;
margin: 0;
}
.MyClass{
min-height: 100vh;
display: flex;
flex-direction: column;
/* add position relative this will prevent the children from moving out of the parent container */
position: relative;
}
.MyClass__background {
background-image: url(https://www.incimages.com/uploaded_files/image/1920x1080/getty_509107562_2000133320009280346_351827.jpg);
background-size: 100% 100%;
background-repeat: no-repeat;
height: 100vh;
}
.rectangle {
position: absolute; /* will move the rectangle to the top left corner */
right: 5px; /* move to the right with 5px space on the right with 5px space on the right */
bottom: 50%; /* this and translate center the image vertically */
transform: translateY(50%);
border: 2px solid red;
}
/* Center and display the image & text side by side */
.rectangle-content {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px;
}
.rectangle-content img {
margin-right: 50px;
max-width: 100% /*make the image fit in the container*/
}
<div class="MyClass__background">
<div class="rectangle">
<div class="rectangle-content">
<img src="https://picsum.photos/128/64" alt="">
<div>
<p>Some Text</p>
<p>Some Other Text</p>
</div>
</div>
</div>
</div>

First, you need to add .MyClass to the div. Then change your modifier class to use -- instead of __, as what you're doing is modifying the block, not creating a new element (assuming BEM)
Then, since you've already given it display:flex, all you need to do is add align-items:flex-end to move its contents to the right, and justify-content:center to vertically center its contents.
Then, add an overlay element inside the outer element. That's where you put your content.
https://jsfiddle.net/fgz8oab1/1/

Related

How can I scale something while centering it using justify-content: center?

I have a div element that I want to center in the middle of the page, but when I add transform: scale(3), it's not centered anymore. The div is 112px in width and height and I dont want to use position nor left/right/top/bottom.
body {
display: flex;
justify-content: center;
align-items: center;
}
.scale {
transform: scale(3);
}
div {
width: 112px;
height: 112px;
background: #333;
}
<body>
<div class="scale"></div>
</body>
How can I center the div while scaling it up?
You can change the transform point to the center. try add the css below to the scale element.
.scale{
transform-origin: center;
}
Transforming an element makes no difference to the amount of space it occupies. It is centered in the sense the height of the body has not changed.
If you experiment with say giving the body a height that isn't just dependent on the height of that element then you will see that the element remains centered more clearly. (Run the snippet in full page)
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.scale {
transform: scale(3);
}
div {
width: 112px;
height: 112px;
background: #333;
}
<body>
<div class="scale"></div>
</body>

How to hide borders when resizing images?

I put three photos inside a container but since I want there to be space between them, I couldn't leave the original size because they would take up the whole container without leaving the space I want.
To make them smaller I modified the height to 80%.
It worked but since I need to add the shadow to the box, I need it to match the edges of the image.
As you can see from the purple, the box is larger than the actual image. I would like to know how to get a box as big as the actual image, so without the purple section.
I added the background color only to the first pic, but the problem can be extended for all the three pics.
I post the code below.
.background {
height: 100vh;
display: flex;
justify-content: space-between;
}
.background * {
width: 100%;
height: 80%;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
<div class="background">
<div class="firstphoto"></div>
<div class="secondphoto"></div>
<div class="thirdphoto"></div>
</div>
Thanks all! ;)
You can take a look at object-fit property: https://www.w3schools.com/css/css3_object-fit.asp
Also, you should put:
.background > * {
flex: 1/3;
}
So that the boxes are taking the same space.
you should add this to each div that contains an image (if they have the same class)
The div would then be positoinned relatively to the image and you could then edit the box-shadow with the box-shadow property
.col{
position:relative;
top:0px;
left:0px;
width:100%;
height:100%;
margin-right:3.33%;
box-shadow: 10px 10px 10px 10px red;
}
Not sure why you're having the images as background images, but I would just use object-fit. Do note, I replaced the divs with image tags.
.background {
height: 100vh;
display: flex;
justify-content: space-between;
}
.background img {
width: 100%;
height: 80%;
padding: 1rem;
box-sizing: border-box;
object-fit: contain;
}
<div class="background">
<img src="https://picsum.photos/200/300" alt="200x300" title="200x300"/>
<img src="https://picsum.photos/50/150" alt="50x150" title="50x150"/>
<img src="https://picsum.photos/30/150" alt="30x150" title="30x150"/>
</div>

How to make only background color black and white in mix-blend-mode?

I am developing an app with vue.
However, I do not want to set black and white for the background-image on one page and not to set black and white for the elements above it, but it is set automatically and the whole is changed to black and white.
Any way to fix it??
//template
<div class="blank"> //root element -> I set the background image here
<div class="container"> // I don't want to set mix-blend-mode here.
...
</div>
</div>
style
.blank {
display: flex;
justify-content: center;
height: 659px;
background: url('../../assets/images/bg_gray.png') 100%;
mix-blend-mode: luminosity;
background-size: cover;
& .container {
padding: 0 16px;
display: flex;
flex-direction: column;
justify-content: space-between;
z-index: 0;
just use this code for background cover image,always use html and body for background in css in style
html {
height: 100%;
}
body {
background: url(''../../assets/images/bg_gray.png''); 100%;
background-size: cover;
}
never use div for background cover, image will not set as full screen
The definition of mix-blend-mode on w3schools says "The mix-blend-mode property specifies how an element's content should blend with its direct parent background". So, that means you must need a parent and since you don't want to set mix-blend-mode on .container you can wrap .blank in another div .main and set its background-color:black give it same height as .blank and there you go.
.main{
height: 659px;
background-color: black;
}
.blank {
display: flex;
justify-content: center;
height: 659px;
background: url('../../assets/images/bg_gray.png') 100%;
mix-blend-mode: luminosity;
background-size: cover;
}
.container {
padding: 0 16px;
display: flex;
flex-direction: column;
justify-content: space-between;
z-index: 0;
}
<div class="main">
<div class="blank"> //root element -> I set the background image here
<div class="container"> //I don't want to set mix-blend-mode here.
...
</div>
</div>
</div>

position: fixed prevents elements to be centered properly

I want to center .donut-graphs inside .dashboard horizontally, so the space between the right edge of the sidebar and the left edge of .donut-graphs is the same as the space from the right edge of .donut-graphs and the right edge of the screen. I have managed to do so, but I had to remove position: fixed from .navbar. The problem is, I can't do that because my sidebar has to stay on top of the screen when you scroll up/down, and with position: fixed on .navbar, the graphs aren't centered properly.
HTML:
<div class="container">
<div class="navbar">
</div>
<div class="content">
<div class="dashboard">
<div class="donut-graphs">
<div class="dashboard-income">
Div 1
</div>
<div class="dashboard-overall">
Div 2
</div>
<div class="dashboard-spent">
Div 3
</div>
</div>
</div>
</div>
</div>
CSS:
body {
margin: 0;
}
.container {
display: flex;
align-items: stretch;
max-width: 100%;
min-height: 100vh;
}
.navbar {
background-color: #ddd;
flex: 0 0 230px;
position: fixed;
height: 100vh;
width: 230px;
}
.content {
flex: 1;
overflow-x: auto;
text-align: center;
}
.donut-graphs {
display: inline-flex;
border: 1px solid;
margin: 50px auto 0;
position: relative;
text-align: left;
}
.dashboard-income,
.dashboard-overall,
.dashboard-spent {
height: 256px;
width: 357px;
display: inline-block;
}
.dashboard-income {
background-color: green;
}
.dashboard-overall {
background-color: blue;
}
.dashboard-spent {
background-color: red;
}
How can I overcome the issue?
Demo
position: fixed puts element above everything. That element won't attach to any element in body because it is the way that works. It only becomes dependent of viewport
What you want to achive could be done with position: absolute but parent (whose child you want to center) has to be position: relative for this to work.
Read more about positioning elements in css here
.content { padding-left:230px; }
Should do the trick.
Assigning your navbar a fixed position takes it out of the document flow, so when centering your donut graphs the browser doesn't take the navbar into account.
Giving the .content element a padding equivalent to the width of the navbar makes up for this.
The only problem with this approach is that if .navbar changes dimensions, you'll need to change the padding on .content to match.

Flexbox not vertically centering inline-block spans

I have a dashboard I am setting up that has wrapper blocks with a sprite icon that should be centered vertically and horizontally inside its parent.
I got the blocks to be placed how I want and wrapping how I want using flexbox.
I also have the icon centered horizontally using the text-align property, but for some reason justify-content: center doesn't do anything when I add it to the .view or .sprite-icon elements; adding it to the .views element also doesn't center things how I want and it breaks the padding between the .view blocks.
See my JSFiddle I set up showing the issue here: https://jsfiddle.net/efarley/k0m4nxny/
.views {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
}
.view {
height: 105px;
text-align: center;
width: 105px;
}
.sprite-icon {
background-image: url('http://67.media.tumblr.com/b2336d673e315081b6d657f8258c313d/tumblr_mv98xzSiJu1qhori9o1_500.jpg');
display: inline-block;
height: 35px;
width: 35px;
}
.sprite-icon.one {
background-position: 0 0;
}
.sprite-icon.two {
background-position: 0 35px;
}
.sprite-icon.three {
background-position: 0 70px;
}
<div class='views'>
<div class='view'>
<span class='sprite-icon one'></span>
</div>
<div class='view'>
<span class='sprite-icon two'></span>
</div>
<div class='view'>
<span class='sprite-icon three'></span>
</div>
</div>
You need to make your view a flex container, then add flex centering properties.
.view {
height: 105px;
text-align: center;
width: 105px;
display: flex; /* new */
align-items: center; /* new */
justify-content: center; /* new */
}
revised fiddle
Note that the justify-content property only applies to flex containers. In your question, you were applying it to a flex item. That's why it failed.
Learn more about flex centering:
How to Center Elements Vertically and Horizontally in Flexbox
Methods for Aligning Flex Items
Try these settings:
.sprite-icon {
display: block;
position: relative;
margin: 35px auto;
background-image: url('http://67.media.tumblr.com/b2336d673e315081b6d657f8258c313d/tumblr_mv98xzSiJu1qhori9o1_500.jpg');
height: 35px;
width: 35px;
}
margin 35px auto centers it horzontally (when position is relative) and moves it down 35px, which centers it vertically, but only since display is now block. I erased text-align: center in .view
Fiddle: https://jsfiddle.net/0t4vbzxb/1/