I am using bootstrap for layout, one of the difficulties I have run into is about how to put the text over image. I have tried all things I can do such as using background-image which does not work. One of the approaches is to place image as relative and put the text in the absolute position.
However for my issue, I can't use absolute position because I have so many images and texts' lengths are varied. So I have to solve issue this by using align-center..
If someone is familiar with this issue, could you give some advice for this problem.. I have to put the text over the image on the very center vertically and horizontally.
<div class="row justify-content-center">
<div class="col-sm-auto">
<img src="img/1.jpg">
</div>
<div class="col-sm-auto">
<img src="img/2.jpg">
</div>
<div class="col-sm-auto">
<img src="img/3.jpg">
</div>
</div>
If the image source isnt changing, then you can try setting background: url(img/1.jpg) from CSS and adjust its position using background-position
Check out the fiddle here.
http://jsfiddle.net/yktvod54/2/
html
<div class="row justify-content-center">
<div class="col-sm-auto image-1">
text1
</div>
<div class="col-sm-auto image-2">
text2
</div>
<div class="col-sm-auto image-3">
text3 with longer text
</div>
css
.col-sm-auto {
color: lightgreen;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
.col-sm-auto.image-1 {
width: 200px;
height: 150px;
background:
url("https://vignette.wikia.nocookie.net/disney/images/8/89/Cute-Cat.jpg/revision/latest?cb=20130828113117");
background-size: cover;
}
.col-sm-auto.image-2 {
width: 200px;
height: 200px;
background: url("https://vignette.wikia.nocookie.net/disney/images/8/89/Cute-Cat.jpg/revision/latest?cb=20130828113117");
background-size: cover;
}
.col-sm-auto.image-3 {
width: 250px;
height: 250px;
background:
url("https://vignette.wikia.nocookie.net/disney/images/8/89/Cute-Cat.jpg/revision/latest?cb=20130828113117");
background-size: cover;
}
Set the image as a background to a div, make that div display:flex; justify-content: center; align-items: center; and give it a width/height. The flex alignments will push everything to the center of the div, but it needs a height/width or the div will only be the size of the text. Add flex-wrap: wrap; and the text will wrap around, but there isnt a nice breakpoint like with word-break
I can't use absolute position because I have so many images and texts' lengths are varied
You can use relative lengths for positioning you texts so they will be centered whatever their size.
You will need the translate CSS function as well.
.image-wrapper {
width: 400px;
position: relative;
}
.image-wrapper img {
width: 100%;
height: auto;
}
.image-wrapper figcaption {
width: 100%;
position: absolute;
top: 50%;
transform: translateY(-50%);
text-align: center;
font-size: 2em;
color: white;
}
<figure class="image-wrapper">
<img src="https://image.ibb.co/n5wsKU/bg3.jpg" alt="A nice pic!">
<figcaption>A text that fits on one line</figcaption>
</figure>
In case you still don't want to use absolute positioning, here is another solution using a background-image instead of an img tag:
.image-wrapper {
width: 400px;
height: 300px;
display: flex;
}
.image-wrapper.image-01 {
background: url('https://image.ibb.co/n5wsKU/bg3.jpg') center center / cover;
}
.image-wrapper p {
margin: auto;
font-size: 2em;
color: white;
}
<div class="image-wrapper image-01">
<p>Some text</p>
</div>
Related
I'm trying to create a vertical line inside a flexbox container div, and am finding that unless I give either the line or the container a specific height (like 100px instead of a percentage), the line won't show. Examination with devtools shows that the line has 0 height, even though the container has a non-zero height. I'm guessing that maybe the rendering engine somehow doesn't know about the container's height at the time it's rendering the line? I'd like to find a way to make this work with percentages in order to make the container and line responsive.
jsfiddle: [https://jsfiddle.net/jjorsett/d0852yhx/25/][1] Set .line's height units to px and it will show up.
css and html:
<div class="container">
<img class="image" src="http://via.placeholder.com/100x250"/>
<div class="line">
</div>
</div>
.container {
background: yellow;
display: flex;
height: auto;
justify-content: space-around;
align-items: center;
}
.line {
border-left: .25em solid #f60;
height: 50%;
}
.image {
object-fit: contain;
width: 25%;
}
[1]: https://jsfiddle.net/jjorsett/d0852yhx/25/
Just remove align-items: center; from your container and height: 50% from your child and see the magic.
.container {
background: yellow;
display: flex;
height: auto;
justify-content: space-around;
}
.line {
border-left: .25em solid #f60;
}
.image {
object-fit: contain;
width: 25%;
}
<div class="container">
<img class="image" src="http://via.placeholder.com/100x250"/>
<div class="line">
</div>
</div>
When you align your item in the center it will automatically shrink your child container according to the children of your child div.
For the benefit of anyone else coming across this, I got the behavior I wanted by using javascript as follows (fiddle here: https://jsfiddle.net/jjorsett/d0852yhx/62/)
<div class="container">
<img class="image" src="http://via.placeholder.com/100x250"/>
<div id="line">
</div>
</div>
.container {
background: yellow;
display: flex;
height: auto;
justify-content: space-around;
align-items: center;
}
#line {
border-left: .25em solid #f60;
height: 50%;
}
.image {
object-fit: contain;
width: 25%;
}
window.onresize = adjustLineSize;
window.onload = adjustLineSize;
function adjustLineSize() {
var line = document.getElementById("line");
var desiredLineHeight = line.parentElement.clientHeight/2;
line.style.height= desiredLineHeight + "px";
}
I'm trying to redesign a category page on magento to display the category image full width with a max height and have the category title displayed in the same box centred on the image.
I've managed to move the category image and title into the page wrapper with the below code
<move element="category.image" destination="page.wrapper" before="main.content" />
<move element="page.main.title" destination="page.wrapper" before="main.content"/>
The HTML
<div class="amryw-container">
<!-- Lorem Ipsum -->
<div class="page-title-wrapper">
<h1 class="page-title" id="page-title-heading" aria-labelledby="page-title-heading toolbar-amount">
<span class="base" data-ui-id="page-title-wrapper">Classic Wax Melts</span>
</h1>
</div>
<div class="category-image"><img src="https://and-it.co.uk/test-store/pub/media/catalog/category/amryw-classic-wax-melts-banner.jpg" alt="Classic Wax Melts" title="Classic Wax Melts" class="image" /></div>
</div>
The CSS
.amryw-container {
display: contents;
}
.category-image {
margin-bottom: 20px;
align-self: center;
display: contents;
}
.category-image .image {
display: block;
max-width: 100%;
height: 340px;
object-fit: cover;
}
But can't figure out how to get the category title to be in the centre of the image.
Any advice would be greatly appreciated.
Page I'm trying to edit: https://and-it.co.uk/test-store/classic-wax-melts
Page I'm trying to replicate: https://amryw.co.uk/classic-wax-melts/
If the title is a text element, assign text-align:center; to its parent. If the title isn't a text element, you can force an element to behave this way by assigning it display:inline-block;
This is assuming you're using a background-image (which you should)
This might be an easier solution for you: https://jsfiddle.net/8reLz1gk/1/
For more info check out W3: https://www.w3schools.com/css/tryit.asp?filename=trycss_image_text_center2
Well you can set that image as background-image for your div. And then simply display the text in the middle of it.
index.html:
<div class="container">
<p>TEXT HERE</p>
</div>
style.css:
.container {
background-image: url("./img.jpg");
background-position: center;
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
}
.container p {
color: rgb(65, 150, 107);
font-size: 32px;
}
like this:
NEW ANSWER:
Well in my html, I put a div around my img, so I could give it a overflow: hidden;. I gave the 'img' and the container around it height :100%. With the overflow: hidden; the image stays within the container. Now I have an image, exactly the same size as my container.\
Now the for the 'p'. It took me some time to come up with this idea:
I first gave my 'p' height: 100%; so it has the same height as the div containing the img and the p tag.
Now I use transform: translateY(-100%);, which is moving up the p tag. Now the p tag and the img are in the same place, occupying the same space (both have same height).
And for centering my 'p', I simply use flex attributes.
index.html
<div class="container">
<div class="img-container">
<img src="./img.jpg" alt="">
</div>
<p>text</p>
</div>
style.css:
.container {
width: 400px;
height: 300px;
}
.container img {
width: 100%;
}
.img-container {
overflow: hidden;
height: 100%;
}
.container p {
height: 100%;
transform: translateY(-100%);
display: flex;
justify-content: center;
align-items: center;
}
Found the answer with the help of another user so posting it for others to use.
Achieved by removing the two basic blocks category.image and page.main.title and creating a custom block.
catalog_category_view.xml
create your PHTML inside your theme \ module : (Note, if you have a custom theme inherited from Luma you can just place it inside: Magento_Catalog\templates\category\view\custom_category_title.phtml,
<referenceContainer name="category.view.container">
<block class="Magento\Catalog\Block\Category\View" name="custom.category.top.view" template="category/view/custom_category_title.phtml"/>
</referenceContainer>
<referenceBlock name="category.image" remove="true"/>
<referenceBlock name="page.main.title" remove="true"/>
instead if you have only a custom module you'll have to place the Layout.xml [catalog_category_view] and the template [custom_category_title] inside your module: Vendor_Module\view\frontend\layout\catalog_category_view.xml and Vendor_Module\view\frontend\templates\category\view\custom_category_title.phtml)
\category\view\custom_category_title.phtml
<?php
/**
* Category view template
*
* #var $block \Magento\Catalog\Block\Category\View
*/
$_currentCat = $block->getCurrentCategory();
$categoryTitle = $_currentCat->getName();
$categoryImage = $_currentCat->getImageUrl();
?>
<div>
<div class="category-image-custom">
<img title="<?=$categoryTitle?>" alt="<?=$categoryTitle?>" src="<?=$categoryImage?>">
<h1><?=$categoryTitle?></h1>
</div>
</div>
And finally, the CSS
.category-image-custom img {
display: block;
width: 100%;
object-fit: cover;
object-position: top;
margin: auto;
height: 300px;
}
.category-image-custom h1 {
position: absolute;
bottom: 50%;
right: 40%;
font-size: 30px;
font: 400 50px/1.2 Poiret One, Helvetica Neue, Verdana, Arial, sans-serif;
color: #fff;
}
.category-image-custom {
margin-bottom: 20px;
width: 100%;
align-self: center;
position: relative;
flex-wrap: wrap;
display: flex;
}
Hope this helps anyone else looking to do the same!
I am trying to achieve the effect below in the example, so that the two images not in the middle are cut off purposely, so that when the browser is smaller, the user will actually see the full images. I have tried endless variations on this css:
width: 100%;
height: auto;
max-width: 670px;
overflow: hidden;
position: absolute;
and seem to be getting nowhere
Just use display: flex; for the container and align all the images in the middle.
.wrapper {
display: flex;
justify-content: center;
}
You can see and test this example here: https://codepen.io/bIropka/pen/WNramag
So I've used flexbox to get them all on the same line.
Added overflow: hidden; to .image-wrapper so the image can overflow without getting a scrollbar.
on the first .image-wrapper I've added direction: rtl; so it goes out of the box on the left instead of the right side.
.container .image-wrapper:nth-of-type(2n) {
min-width: max-content;
}
This is to make sure the middle wrapper will always be the full image size
.container {
display: flex;
}
.container .image-wrapper {
padding: 5px;
overflow: hidden;
min-height: max-content;
position: relative;
}
.container .image-wrapper:first-of-type {
direction: rtl;
}
.container .image-wrapper:nth-of-type(2n) {
min-width: max-content;
}
<div class="container">
<div class="image-wrapper">
<img src="https://via.placeholder.com/400" alt="">
</div>
<div class="image-wrapper">
<img src="https://via.placeholder.com/400" alt="">
</div>
<div class="image-wrapper">
<img src="https://via.placeholder.com/400" alt="">
</div>
</div>
I have a row with three coloumns. I want to have a image on each coloumn's divs with same height irrespective of image size in each div.
I want the images to get resized with in a div. But i get different size of divs as the image size of myImage1.png, myImage2.png and myImage3.png is different.
This is happening because i have fixed padding in my 'bg' class. Can somebody help me to fix this? Below is my HTML and CSS code.
<div class="row IDE_container">
<div class="col-lg-3 col-md-3 col-sm-3">
<div class="bg">
<img src="myImage1.png" class="IDE_div" />
</div>
</div>
<div class="col-lg-2 col-md-2 col-sm-2">
<div class="circle_bg circle_bg_sm">
<img src="myImage2.png" class="IDE_div" />
</div>
</div>
<div class="col-lg-3 col-md-3 col-sm-3">
<div class="circle_bg">
<img src="myImage3.png" class="IDE_div" />
</div>
</div>
</div>
.bg{
background-color: #ededed;
border-radius: 100%;
text-align: center;
padding: 35% 5%;
width: max-content;
max-width: 100%;
box-shadow: 0 0 3px 1px #DDD;
}
.IDE_div {
display: inline-block;
position: relative;
width: 100%;
height: 100%;
}
.IDE_container {
display: flex;
align-items: baseline;
}
For various kind of images, so can use object-fit property.
Add the CSS like this:
.IDE_div{
width: 100%;
object-fit: cover;
height: 300px; /* only if you want fixed height */
}
<div class="col-3">
<div class="circle_bg">
<img src="myImage3.png" class="IDE_div" />
</div>
</div>
You will find the details about object-fit and object-position here : https://css-tricks.com/on-object-fit-and-object-position/
You need to set a height or max-height on the divs containing the images, or they will adapt to the height of their content (the images). As you've set the images to take up 100% width and height, and haven't constrained their parents, they will get as large as they can based on the original image size.
So, for example :
.IDE_container {
display: flex;
align-items: baseline;
div.col-3 {
div {
height: 200px;
.IDE_div {
display: inline-block;
position: relative;
width: 100%;
height: 100%;
&::before {
content: "";
position: absolute;
top: 50%;
left: 50%;
width: var(--val);
padding-bottom: var(--val);
transform: translate(-50%, -50%);
z-index: -1;
border-radius: 50%;
}
}
}
}
}
You could give all containers the same class (e.g. fixed-height-bg) to style them more easily.
Also note: You don't need to give your columns individual classes for different screen sizes, as they always take up the same proportion. So, you can just use
<div class="col-3">
<div class="circle_bg">
<img src="myImage3.png" class="IDE_div" />
</div>
</div>
EDITED to add: if you want the images to maintain their proportions, you need to change your css to look like this:
img {
height: 100%; /* height of the parent container if specified, or of the image itself */
width: auto; /* will maintain the proportions of the image */
}
You can also switch those values, to height: auto; width: 100%; if you want the images to take the full width.
I'm trying to get a H1 vertically center aligned within a div that has a background image.
I tried this method I found: https://jsfiddle.net/vdqdpyc0/12/
But found that the full height of the banner was only visible if I specifically added a px height to the div, or added padding to either element. This meant that when I resized, there was lots of white space above and below the banner. This wouldn't be an issue if the background was intended to repeat, but it isn't.
The end product needs to look like this.
html,
body {
height: 100%;
}
.outer-wrapper {
background: url("http://paulmason.name/media/demos/full-screen-background-image/background.jpg") no-repeat center center;
background-size: cover;
max-height: 360px;
height: 100%;
}
.inner-wrapper {
display: table;
width: 100%;
height: 100%;
}
.header-wrapper {
display: table-cell;
text-align: center;
vertical-align: middle;
}
h1 {
color: #fff;
}
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="header-wrapper">
<h1>
Vertically aligned text
</h1>
</div>
</div>
</div>
I could go through reducing the padding for various responsive viewpoints, but I figured there has to be a more streamlined way of going about it.
You can achieve this much more easily these days with flexbox, e.g.
html,
body {
height: 100%;
}
.outer-wrapper {
background: url("http://paulmason.name/media/demos/full-screen-background-image/background.jpg") no-repeat center center;
background-size: cover;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
h1 {
color: #fff;
}
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="header-wrapper">
<h1>
Vertically aligned text
</h1>
</div>
</div>
</div>
Example: https://jsfiddle.net/0yxctLne/
You can use a flexbox for this. Here is a pretty simple guide to flexbox. For your issue, it should be enough to pass
display: flex;
flex-direction: column;
justify-content: center;
to the element surrounding the H1 tag, IF the surrounding element is the same height and width as the space you want the H1 tag to be centered in. This will define the main axis as vertical (read: column) and then center all content along the main axis. If you want, you can also add
align-items: center;
to have the text centered along the cross axis (as in horizontally).
Do you need something like this? Flexbox is a nice way to do:
.header-wrapper {
background: url("http://paulmason.name/media/demos/full-screen-background-image/background.jpg") no-repeat center center;
background-size: cover;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
}
h1 {
color: #fff;
}
<div class="outer-wrapper">
<div class="inner-wrapper">
<div class="header-wrapper">
<h1>
Vertically aligned text
</h1>
</div>
</div>
</div>
Fiddle