I want to have my the picture.png to be exactly on the very left of the title "ribbon", and the text to be centred about the ribbon (regardless of the presence of the picture). I've been struggling with CSS and flex boxes for a lot of time trying to achieve just this effect. Here I'm using some bootstrap classes, but .heading is my class.
That being said, is it actually possible to do all this alignment without using flexbox, but something more natural and simple? Such as maybe something from CSS1, without all those flex boxes?
.heading {
background-color: rgba(15, 156, 199, 0.829);
height: 5%;
display: flex;
text-align: center;
justify-content: space-between;
/* not helpful of course */
}
/* Here's what I'm trying to achieve. I made this by artificially adding the following CSS code: */
.name {
margin-right: 31%;
}
<div class="container-sm">
<div class="card-body">
<div class="heading">
<img class="rounded img-fluid" src="picture.png" alt="...">
<div class="name">
<h4>Web server monitoring and maintenance.</h4>
<div>Monitoring and upgrades.</div>
</div>
</div>
</div>
</div>
Of course I want this to be naturally centred, not artificially.
TL;DR. Make your .name also a flex container.
Please see the code snippet below. I didn't edit the HTML except the placeholder image. The main CSS change is in .name class to make it flex container and have its content vertically centered. Also, its parent .heading styles are a little adjusted to make it work properly.
.heading {
background-color: rgba(15, 156, 199, 0.829);
height: 5%;
display: flex;
text-align: center;
}
img {
width: 50px;
height: 50px;
flex: 0;
}
.name {
width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
h4, div {
margin: 0;
padding: 0;
line-height: 1;
}
<div class="container-sm">
<div class="card-body">
<div class="heading">
<img class="rounded img-fluid" src="https://picsum.photos/50" alt="...">
<div class="name">
<h4>Web server monitoring and maintenance.</h4>
<div>Monitoring and upgrades.</div>
</div>
</div>
</div>
</div>
Bootstrap relies on Flexbox for the alignment and layouts. So it will be odd to avoid using it.
Here is a code snippet that will help you achieve what you want to do.
Helpful links:
Bootstrap Flexbox: Flexbox utility classes
CSS Flexbox: MDN flexbox docs
.heading {
background-color: rgba(15, 156, 199, 0.829);
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div class="container-sm">
<div class="card-body">
<div class="heading d-flex text-center">
<img class="rounded img-fluid" src="picture.png" alt="...">
<div class="name flex-grow-1">
<h4>Web server monitoring and maintenance.</h4>
<p>Monitoring and upgrades.</p>
</div>
</div>
</div>
</div>
Note: Bootstrap is all about pre-made components and utility classes so try to avoid writing custom CSS as much as possible. Another downside of this is that you're repeating yourself and making Bootstrap useless in the manner you're using it.
using flexbox
img,.image {
height:50px;
}
.box {
display: flex;
align-items: center;
width: 100%;
text-align: center;
height:50px;
}
.text-wrapper{
background-color: rgba(15, 156, 199, 0.829);
width:100%;
height:50px;
display: flex;
flex-direction: column;
justify-content: center;
}
.container{
padding:5px;
}
*{
margin:0px;
padding:0px;
}
<div class="container">
<div class="box">
<div class="image">
<img src="https://placekitten.com/640/360" alt="...">
</div>
<div class="text-wrapper">
<div class="text">
<h4>Web server monitoring and maintenance.</h4>
<p>Monitoring and upgrades.</p>
</div>
</div>
</div>
</div>
Related
I have these 2 divs (footer-txt and footer-img), on the mobile it works perfectly because I want the text on top and the 2 images on the bottom, like side by side.
But when the screen gets wider, I want the 2 images side by side and also the text div. Like a row with the 2 images and the text.
Is there a way that I can do this?
#footer {
background-color: #98AFFF;
}
.footer-txt {
font-size: 14px;
color: #464646;
text-align: left;
}
.footer-img {
display: flex;
gap: 25px;
}
<footer class="text-center text-lg-start" id="footer">
<div class="p-4" id="footer-content">
<div class="pb-2 footer-txt">
PROTESTE
</div>
<div class="pt-4 pb-2 justify-content-center footer-img">
<a href="#">
<img src="images/pag1/Mask group (1).png" alt="Google">
</a>
<a href="#">
<img src="images/pag1/Mask group.png" alt="Google">
</a>
</div>
</div>
</footer>
Well, if i understand correctly you can put this css in id #footer-content:
#footer-content {
display: flex;
flex-direction: row;
}
That way you change the direction of your div to as if it were a row.
If you only need this for when you're not mobile, you can add the following line:
#media screen and (min-width: 960px) {
#footer-content {
display: flex;
flex-direction: row;
}
}
The desired solution can be achieved using either Flex-Box or Grid & Responsive CSS using Media Queries
Flex Box
Grid Layout
Media Query
#footer {
background-color: #98AFFF;
}
#footer-content {
display: flex;
align-items: center;
justify-content: flex-start;
gap: 25px;
}
.footer-txt {
font-size: 14px;
color: #464646;
text-align: left;
}
.footer-img {
display: flex;
gap: 25px;
}
/* Styling for Smaller Screens */
#media screen and (max-width: 500px) {
#footer-content {
flex-direction: column;
align-items: flex-start;
gap: 10px;
}
}
<footer class="text-center text-lg-start" id="footer">
<div class="p-4" id="footer-content">
<div class="pb-2 footer-txt">
PROTESTE
</div>
<div class="pt-4 pb-2 justify-content-center footer-img">
<a href="#">
<img src="images/pag1/Mask group (1).png" alt="Google">
</a>
<a href="#">
<img src="images/pag1/Mask group.png" alt="Google">
</a>
</div>
</div>
</footer>
You have to make the text and both of the images a flex item. You achive this by defining the flex property on the footer-content. All of its direct children are then flex-items.
Very good explanation of flex can be found here:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
If my solution is not what are you looking for you are maybe also looking for breakpoints and media queries for a responsive design.
#footer {
background-color: #98AFFF;
}
.footer-txt {
font-size: 14px;
color: #464646;
text-align: left;
}
#footer-content {
display: flex;
flex-wrap: wrap;
}
<footer id="footer">
<div id="footer-content">
<div class="footer-txt">
PROTESTE
</div>
<div class="footer-img">
<a href="#">
<img src="images/pag1/Mask group (1).png" alt="Google">
</a>
</div>
<div class="footer-img">
<a href="#">
<img src="images/pag1/Mask group.png" alt="Google">
</a>
</div>
</div>
</footer>
I am trying to align my 3 service divs next to each other but instead they are each slightly lower than the other one. I think it might be the images that are making so they do not align. I have tried the following without success:
.col-md-4 {
float:left;
}
Please advise what I am doing wrong and how I can align my divs, my code is as follows:
HTML
<section id="services">
<div class="container text-center">
<h1 CLASS="title">WHAT WE OFFER</h1>
<div class="row text-center">
<div class="col-md-4 services">
<img src="images/service1.png" class="service-img">
<h4>TITLE 1</h4>
<p>Add sentence here not sure what to say just yet but has to be good</p>
</div>
<div class="col-md-4 services">
<img src="images/service2.jpg" class="service-img">
<h4>TITLE 2</h4>
<p>Add sentence here not sure what to say just yet but has to be good</p>
</div>
<div class="col-md-4 services">
<img src="images/service3.png" class="service-img">
<h4>TITLE 3</h4>
<p>Add sentence here not sure what to say just yet but has to be good</p>
</div>
</div>
<button type="button"class="btn btn-primary"> All Services</button>
</div>
</section>
CSS
#services
{
padding: 80px 0;
}
.service-img
{
width: 100px;
margin-top: 20px;
}
.services
{
padding: 20px; /*service images and paragraphs do not want to align*/
}
.services h4
{
padding: 5px;
margin-top: 25px;
text-transform: uppercase;
}
.title::before
{
content: '';
background: #7b1798;
height:5px;
width: 300px;
margin-left: auto;
margin-right: auto;
display: block;
transform: translateY(63px);
}
#services .btn-primary
{
box-shadow: none;
padding: 8px 25px;
border: none;
border-radius: 20px;
background-image: linear-gradient(to right,#a517ba,#5f1782);
Instead of using the default flow layout, the flexbox layout can help you in this situation. CSS Tricks have a great guide about it here.
Applying display: flex to the parent div .row would render its children divs .services in the flexbox layout. Further, applying the CSS declaration align-items: center; should horizontally align its children elements .services in a single line.
The full CSS rule could be:
.row {
display: flex;
align-items: center;
}
Hey I'm new into CSS but I dont know how to make this work. Please help me on how to make this work.
The desired outcome.
My outcome.
The problem is how to make the heading come under the location tags. like in the figma design?
Here is the HTML.
import { GrLocation } from "react-icons/gr"
<div className="container">
<img src="https://source.unsplash.com/WLxQvbMyfas" className="main-img" alt="location-img" />
<div className="tags-colum">
<GrLocation />
<p>JAPAN</p>
<p className="underline-text">View on Google Maps</p>
<div className="container-text">
<h1>Mount Fuji</h1>
</div>
</div>
</div>
Here is the CSS.
.container {
display: flex;
align-items: center;
justify-content: center;
}
.main-img {
height: 168px;
width: 125px;
border-radius: 5px;
}
.tags-colum {
display: flex;
align-items: center;
margin: 20px 20px;
}
.container-text {
display: block;
}
.underline-text {
text-decoration: underline;
}
Your problem is .tags-column is display: flex, so you cannot group all 3 of those elements together. Because the default flexbox is row-based style which means it will align all elements on the same row
For the fix,
Create a group of that left image and all content elements (.container)
Separate the location icon and JAPAN to another group with flexbox (.tags-colum)
Put Mount Fuji separately (.container-text)
Note that, .new-group is just an alias name which I'm using for demonstration, and it has no specific styles
import { GrLocation } from "react-icons/gr"
<div className="container">
<img src="https://source.unsplash.com/WLxQvbMyfas" className="main-img" alt="location-img" />
<div className="new-group">
<div className="tags-colum">
<GrLocation />
<p>JAPAN</p>
<p className="underline-text">View on Google Maps</p>
</div>
<div className="container-text">
<h1>Mount Fuji</h1>
</div>
</div>
</div>
The issue is that you have used display flex to the .tags-colum ( which is the outermost parent) to fix this you can use flex-direction: column, yes adding this will make everything stacked up, so what's the solution?
group your elements like this
<div className="container">
<img src="https://source.unsplash.com/WLxQvbMyfas" className="main-img" alt="location-img" />
<div className="tags-colum">
<div className='gp1'>
<GrLocation />
<p>JAPAN</p>
<p className="underline-text">View on Google Maps</p>
</div>
<div className="container-text">
<h1>Mount Fuji</h1>
</div>
</div>
.tags-colum {
display: flex;
align-items: flex-start;
margin: 20px 20px;
flex-direction: column;
}
.gp1 {
display: flex;
align-items: center;
}
here is an example https://codesandbox.io/s/homepage-forked-g18lgm?file=/public/index.html:56-126
or you can completely remove the display flex from the tags-colum
I'm trying to recreate 2:1 layout from the photo below but I'm unable to distribute the right 2 photos vertically so they align perfectly with the photo on the left. I tried to put it in a flex but then they distribute vertically and I need them to be responsive.
Current Layout
How it should look like
img-row {
display: flex;
flex-wrap: wrap;
}
.gridImage1 {
width: 100%;
height: 100%;
object-fit: cover;
}
.right-photos-div {
justify-content:space-around;
}
.gridImage23 {
width:100%;
height:100%;
object-fit:cover;
justify-content: space-around;
}
<div class="container-fluid row img-row">
<div class="col-12 col-md-6">
<h5 class="grid-image1-title position-absolute"><b>GINA Smart | The new standard in filter coffee</b></h5>
<h5 class="grid-image1-price position-absolute">220€</h5>
<img src="Images/goat-story-gina-specialty-coffee-preparation-66_3_2x_cb3f02c4-e13e-4945-b38f-513e42091a4e.jpeg" class="gridImage1" alt="Gina">
</div>
<div class="col-12 col-md-6 right-photos-div">
<div class="col-12 d-inline-block">
<h5 class="grid-image2-title position-absolute"><b>COLD BREW KIT | Cold brew made easy</b></h5>
<h5 class="grid-image2-price position-absolute">22€</h5>
<img src="Images/20190902-A7R01337_2x_120dba37-6706-456a-89f5-fdfe1c56edef.jpeg" alt="" class="gridImage23">
</div>
<div class="col-12 d-inline-block">
<h5 class="grid-image2-title position-absolute"><b>GOAT MUG | Unique coffee cup on the go</b></h5>
<h5 class="grid-image2-price position-absolute">29,5€</h5>
<img src="Images/goat_mug_hemp_3_2x_dd7d99a2-21cd-4730-a623-786b47beab02.jpeg" alt="" class="gridImage23">
</div>
</div>
</div>
I managed to achieve what you're looking for with the following code:
.grid-test{
display:grid;
grid-template-columns: 1fr 1fr;
width: 100%;
height: 50vh;
}
img{
width: 100%;
object-fit: cover;
}
.left-side-image{
height: 100%;
}
.right-grid-container{
gap: 30px;
display: flex;
flex-wrap: wrap;
padding-left: 30px;
}
<div class="grid-test">
<div>
<img class="left-side-image" src="https://www.w3schools.com/css/img_lights.jpg">
</div>
<div class="right-grid-container">
<img class="right" src="https://www.w3schools.com/css/img_lights.jpg">
<img class="right" src="https://www.w3schools.com/css/img_lights.jpg">
</div>
</div>
By the logic I've changed, you should be able to do that by simply adding the following styles to your right-photos-div element:
gap: 30px;
display: flex;
flex-wrap: wrap;
padding-left: 30px;
Noting that you're transforming that element itself into a display flex element with flex-wrap so that you can add gap between items and adding a padding-left equivalent to that same gap (if you want to have equal spaces between your elements).
I used the styling from this thread to make a progress bar fill in the empty space in a div (only other item is a button).
The problem is now that align-items: center doesn't vertically center the progress bar. I tried using align-content: center on the child too, with no effect.
Here's the code in case you didn't open the link
.wrapper {
display: flex;
flex-direction: row;
align-items: center;
width: 100%;
height: 5em;
background: #ccc;
}
.wrapper > .left
{
background: #fcc;
}
.wrapper > .right
{
background: #ccf;
flex: 1;
}
Markup:
<div class="wrapper">
<div class="left">Left</div>
<div class="right">Right</div>
</div>
Can someone tell me what I'm doing wrong? Thanks in advance
This is how it looks:
I guess you can do the following to get it right:
There is a margin coming for the .progress element- first you can nullify it:
.wrapper > .left > .progress {
margin: 0;
}
Give 100% height for wrapper
I also removed height: 10vh for the container of wrapper to finish things up.
See revised fiddle here and snippet below:
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
#import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
}
.wrapper {
display: flex;
align-items: center;
width: 100%;
background: #ccc;
height: 100%;
}
.wrapper > .left {
background: #fcc;
flex: 1;
}
.wrapper > .right {
background: #ccf;
}
.wrapper > .left > .progress {
margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-s-6 col-s-offset-3" style="background:purple; position:relative; border-radius:10px;">
<div class="wrapper">
<div class="left">
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="35" aria-valuemin="0" aria-valuemax="100" style="width:35%">
</div>
</div>
</div>
<div class="right">
<button type="button" aside-menu-toggle="menu-1" class="btn btn-sm btn-default">Меню</button>
</div>
</div>
</div>
</div>
</div>
Let me know your feedback on this. Thanks!
A flex container will enable a flex context only to its direct children. Hence, your "progress-bar" div is out of reach. Likewise, your purple background bar is before flex container (wrapper), so don't expect it's content to be centered either. You can check this guide.
Check this code:
<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-s-6 col-s-offset-3" style="background:purple; height:10vh; position:relative; border-radius:10px;">
<div class="wrapper">
<div class="left">
left
</div>
<div class="right">
right
</div>
</div>
</div>
</div>
</div>
Pen based on your code here
Try adding
align-items: center;
justify-content: center;
to your .wrapper class.
Here is a good article on FlexBox positioning:
https://philipwalton.github.io/solved-by-flexbox/demos/vertical-centering/