I have a little website programmed containing an area with three divs centered in the middle. I am trying to put a <h2> over it like this:
But best I could do was to place the Locations <h2> along with the divs in the center using display: flex; and flex-direction: column; in the #locationsbg but for now I just replaced it and let the Locations away.
* {
background-color: black;
font-family: Helvetica;
color: seashell;
opacity: 0.9;
font-size: 22px;
text-align: center;
}
#locations {
display: inline-flex;
background-color: transparent;
}
#locationsbg {
margin: 0 auto;
height: 500px;
width: 1200px;
background-image: url(https://content.codecademy.com/courses/freelance-1/unit-4/img-locations-background.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
display: flex;
align-items: center;
justify-content: center;
}
#locationsbg h2 {
background-color: transparent;
position: relative;
top: 40px;
}
#locations div {
max-height: 300px;
width: 300px;
display: flex;
flex-direction: column;
margin-left: 40px;
opacity: 1;
}
<section id="locationsbg">
<!--<h2>Locations</h2> This should be over the centered divs (not in the real center-->
<div id="locations">
<div>
<h3>Downtown</h3>
<p>384 West 4th St</p>
<p>Suite 108</p>
<p>Portland, Maine</p>
</div>
<div>
<h3>East Bayside</h3>
<p>3433 Phisherman's Avenue</p>
<p>(Northwest Corner)</p>
<p>Portland, Maine</p>
</div>
<div>
<h3>Oakdale</h3>
<p>515 Crescent Avenue</p>
<p>Second Floor</p>
<p>Portland, Maine</p>
</div>
</div>
</section>
Now there is a slightly awkward structure that is difficult to work with. Wrap H2 tag with div and use any properties to move the elements inside it. Highly recommend using a grid for the elements inside #locations. As far as I understand, you will need the following code fpr example:
.heading-wrapper{
width:100%;
text-align:center;
padding:15px 0;
}
<section id="locationsbg">
<div class="heading-wrapper">
<h2>Locations</h2>
</div>
<div id="locations">
<!-- ... -->
</div>
</section>
or use the same grid for splitting and centering. But I still don't understand why you use such structure, it can be made easier with less code.
Add flex-direction: column to the #locationsbg element and then remove the positioning in the h2 element. Remove this position: relative; top: 40px; from => #locationsbg h2.
* {
background-color: black;
font-family: Helvetica;
color: seashell;
opacity: 0.9;
font-size: 22px;
text-align: center;
}
#locations {
display: inline-flex;
background-color: transparent;
}
#locationsbg {
margin: 0 auto;
height: 500px;
width: 1200px;
background-image: url(https://content.codecademy.com/courses/freelance-1/unit-4/img-locations-background.jpg);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#locationsbg h2 {
background-color: transparent;
}
#locations div {
max-height: 300px;
width: 300px;
display: flex;
flex-direction: column;
margin-left: 40px;
opacity: 1;
}
<section id="locationsbg">
<h2>Locations</h2>
<div id="locations">
<div>
<h3>Downtown</h3>
<p>384 West 4th St</p>
<p>Suite 108</p>
<p>Portland, Maine</p>
</div>
<div>
<h3>East Bayside</h3>
<p>3433 Phisherman's Avenue</p>
<p>(Northwest Corner)</p>
<p>Portland, Maine</p>
</div>
<div>
<h3>Oakdale</h3>
<p>515 Crescent Avenue</p>
<p>Second Floor</p>
<p>Portland, Maine</p>
</div>
</div>
</section>
Related
I created a cards-based horizontal scroller. And the cards are nicely scrolling inside the wrapper. The issue I'm having is that even after I applied the z-index to our member-owner-card-image, the photos still go under the card when I want to put them on the top of each card.
Is there any solution so that I can add the image on top of the card? I'm trying to fix it, but no solution has been found.
.scrolling-wrapper {
-webkit-overflow-scrolling: touch;
height: 331px;
width: 100%;
padding-inline: 40px;
position: relative;
display: flex;
flex-wrap: nowrap;
overflow-x: auto;
z-index: 0;
}
.scrolling-wrapper::-webkit-scrollbar {
display: none;
}
.card {
width: 100%;
flex: 0 0 auto;
background-color: green;
border-radius: 20px;
position: relative;
margin-right: 10px;
}
.our-member-owner-card-image {
position: absolute;
top: -30px;
z-index: 10;
}
.card-content {
position: absolute;
padding-top: 38px;
}
.member-detail {
padding-top: 55px;
line-height: 1.7;
}
.member-detail h3 {
text-align: center;
color: #263244;
font-weight: 700;
font-family: 'Lato';
}
.member-detail p {
text-align: center;
color: #737C89;
}
.member-description {
padding-inline: 20px;
color: #263244;
line-height: 1.6;
padding-top: 9px;
font-weight: 500;
font-size: 17px;
}
.member-description span {
color: red;
text-decoration: underline;
}
<div class="scrolling-wrapper">
<div class="card">
<div class="our-member-owner-card-image">
<img width="220px" src="https://images.unsplash.com/photo-1661961110144-12ac85918e40?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" />
</div>
<div class="card-content">
<div class="member-detail">
<h3>Sohaib</h3>
<p>Chairman</p>
</div>
<div class="member-description">
Sohaib Ashraf has extensive work experience during his career
of more than 25 years in the financial services sector.<span
>Read more</span
>
</div>
</div>
</div>
<div class="card">
<div class="our-member-owner-card-image">
<img width="220px" src="https://images.unsplash.com/photo-1661961110144-12ac85918e40?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" />
</div>
<div class="card-content">
<div class="member-detail">
<h3>Sohaib Ashraf</h3>
<p>Chairman</p>
</div>
<div class="member-description">
Sohaib Ashraf has extensive work experience during his career
of more than 25 years in the financial services sector.<span
>Read more</span
>
</div>
</div>
</div>
</div>
You could add the image as a background-image to the card. I added few examples how you can use background-image:
.card{
width: 400px;
height: 300px;
margin: 10px;
border-radius: 6px;
background-color: gray;
}
.card-1, .card-2 .image, .card-3 .image{
/* Here, we use background-image to set the image */
background-image: url("https://images.unsplash.com/photo-1661961110144-12ac85918e40?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80");
/* The background should be a cover photo,
so it fills the whole card: */
background-size: cover;
/* We don't want the image to repeat itself */
background-repeat: no-repeat;
/* When the ratio of the image changes, it will zoom into
this point, which we want to happen in the center of the image */
background-position: center;
}
.card-2, .card-3{
display: grid;
grid-template-rows: 1fr 1fr;
}
.card-2 .image{
border-radius: 6px 6px 0px 0px;
}
.card-3{
padding: 10px;
}
/* This is to demonstrate: */
h2{
color: white;
display: flex;
justify-content: center;
font-family: sans-serif;
}
<div class="card-1 card">
<h2>Test title</h2>
</div>
<div class="card-2 card">
<div class="image"></div>
<div class="content">
<h2> Test title</h2>
</div>
</div>
<div class="card-3 card">
<div class="image"></div>
<div class="content">
<h2> Test title</h2>
</div>
</div>
I have a bunch of text and an image. The text is in a h2. The text I have aligned left, and the image I have aligned right on the same line using a flex container.
The alignment is perfect until I add the form in. I'm using a form with formaction as the button goes to an external site.
The form pushes the image out of view from the viewport. I have tried adding an inline-block class to the form but I am unable to solve this.
I am using EJS so below is the snippet I am rendering
body {
font-family: 'Raleway', sans-serif;
color: white;
background: linear-gradient(to left, #6ca4a1 35vw, #1d7874 0);
background-repeat: no-repeat;
background-repeat: repeat-y;
position: sticky;
background-size: cover;
height: 100%;
max-width: 100vw;
}
.display-flex {
height: 100%;
display: flex;
min-height: 0;
}
.display-flex>div {
margin: auto;
}
.padding-top {
padding-top: 7vh;
}
.inline-block {
display: inline-block;
}
<div class="display-flex">
<div>
<h2>Psychology <br> Services </h2>
<h2>Tim Carey</h2>
<h2>Earlwood <br> NSW</h2>
<form class="padding-top inline-block">
<button class="btn" formaction="/>Book
a
session</button>
</form>
</div>
<div>
<img src="/images/tim-compressed.png" alt="photo of psychologist" id="tim">
</div>
</div>
try to use the code i have made, does it match what you expect?
body {
font-family: 'Raleway', sans-serif;
color: white;
background: linear-gradient(to left, #6ca4a1 35vw, #1d7874 0);
background-repeat: no-repeat;
background-repeat: repeat-y;
position: sticky;
background-size: cover;
height: 100%;
max-width: 100vw;
}
.display-flex {
height: 100%;
display: flex;
min-height: 0;
flex-wrap: wrap;
}
.left-side{
flex-basis : 65%;
}
.right-side{
flex-basis : 35%;
display : flex;
flex-direction : column;
}
.padding-top {
padding-top: 7vh;
}
.inline-block {
display: inline-block;
}
img{
width : 100%
}
<div class="display-flex">
<div class="left-side">
<h2>Psychology <br> Services </h2>
<h2>Tim Carey</h2>
<h2>Earlwood <br> NSW</h2>
<form class="padding-top inline-block">
<button class="btn"
formaction="https://www.halaxy.com/book/tim-carey/location/443171/select-time">Book
a
session</button>
</form>
</div>
<div class="right-side">
<img src="https://images.unsplash.com/photo-1661956602153-23384936a1d3?ixlib=rb-1.2.1&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2070&q=80" alt="photo of psychologist" id="tim">
</div>
</div>
you can remove margin:auto;, and add justify-content: space-between in the wrapper that wraps the element
body {
font-family: 'Raleway', sans-serif;
color: white;
background: linear-gradient(to left, #6ca4a1 35vw, #1d7874 0);
background-repeat: no-repeat;
background-repeat: repeat-y;
position: sticky;
background-size: cover;
height: 100%;
max-width: 100vw;
}
.display-flex {
height: 100%;
display: flex;
min-height: 0;
justify-content: space-between;
}
/* .display-flex > div {
margin: auto;
} */
.padding-top {
padding-top: 7vh;
}
.inline-block {
display: inline-block;
}
<div class="display-flex">
<div>
<h2>Psychology <br> Services </h2>
<h2>Tim Carey</h2>
<h2>Earlwood <br> NSW</h2>
<form class="padding-top inline-block">
<button class="btn" formaction="https://www.halaxy.com/book/tim-carey/location/443171/select-time">Book
a
session</button>
</form>
</div>
<div>
<img src="/images/tim-compressed.png" alt="photo of psychologist" id="tim">
</div>
</div>
You have a syntax error formaction="/> no closing quote. Correcting the error formaction=""/> appears to fix your layout:
body {
font-family: 'Raleway', sans-serif;
color: white;
background: linear-gradient(to left, #6ca4a1 35vw, #1d7874 0);
background-repeat: no-repeat;
background-repeat: repeat-y;
position: sticky;
background-size: cover;
height: 100%;
max-width: 100vw;
}
.display-flex {
height: 100%;
display: flex;
min-height: 0;
}
.display-flex>div {
margin: auto;
}
.padding-top {
padding-top: 7vh;
}
.inline-block {
display: inline-block;
}
<div class="display-flex">
<div>
<h2>Psychology <br> Services </h2>
<h2>Tim Carey</h2>
<h2>Earlwood <br> NSW</h2>
<form class="padding-top inline-block">
<button class="btn" formaction="" />Book a session
</button>
</form>
</div>
<div>
<img src="https://www.fillmurray.com/200/250" alt="photo of psychologist" id="tim">
</div>
</div>
This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 10 months ago.
<header className='header'>
<div className='header--center'>
<h1>Title of the website</h1>
</div>
<div className='header--right'>
<p>Some text pushed to the right</p>
</div>
</header>
I want "Title of the website" to be centered and I want "Some text pushed to the right" to be pushed to the right of the container without messing up the centering of "Title of the website"
You can use flex boxes:
.header--right{display: flex;
justify-content: end;}
and:
.header--center{display: flex;
justify-content: center;}
For reference: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Also, you can use text-align on the p tag and the h1 tag
One way is
*,
*::before,
*::after {
box-sizing: border-box;
margin:0;
}
body{
min-height: 100vh;
overflow: hidden;
background-color: bisque;
}
p{
font-size: 3rem;
font-weight: 900;
}
.header-container{
position: relative;
display: flex;
flex-direction: row;
justify-content: center;
}
.text-right{
margin-left: auto;
}
.text-center{
position: absolute;
}
.container{
position: absolute;
width: 50vw;
height: 100vh;
border: 5px solid black;
}
<div class="header-container">
<div class="text-center">
<p>CENTER</p>
</div>
<div class="text-right">
<p>RIGHT</p>
</div>
</div>
<div class="container"></div>
If you don't want to use position:absolute here is another way.
*,
*::before,
*::after {
box-sizing: border-box;
margin:0;
}
body{
min-height: 100vh;
overflow: hidden;
background-color: bisque;
}
p{
font-size: 3rem;
font-weight: 900;
}
.header-container{
display: flex;
flex-direction: row;
justify-content: center;
}
.text-right{
margin-left: auto;
}
.text-center{
margin-left: 50%;
transform: translate(-50%);
}
.container{
position: absolute;
width: 50vw;
height: 100vh;
border: 5px solid black;
}
<div class="header-container">
<div class="text-center">
<p>CENTER</p>
</div>
<div class="text-right">
<p>RIGHT</p>
</div>
</div>
<div class="container"></div>
I'm trying to center a div vertically using flexbox but align-items:center; only changes the contents inside the div. I tried adding height:100% but still the same. what should I add/change in the code?
body {
background: url(images/pattern-background-mobile.svg);
background-repeat: no-repeat;
background-size: cover;
margin: 0;
height: 100%;
}
.container {
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
border: 2px solid red;
padding: 0;
margin: 2em;
width: 80%;
}
<body>
<div class="main">
<div class="container">
<div class="hero">
</div>
<h1 class="title">Order Summary</h1>
<p>You can now listen to millions of songs, audiobooks, and podcasts on any device anywhere you like!
</p>
<div class="container">
<h4>Annual Plan</h4>
<p>$59.99/year</p>
Change
</div>
<button>Proceed to Payment</button>
Cancel Order
</div>
</div>
</body>
Do you want to bring the whole thing? Actually I don't clearly understand your question. Still providing a solution. I guess it will help you.
body {
background: url(images/pattern-background-mobile.svg);
background-repeat: no-repeat;
background-size: cover;
margin: 0;
height: 100vh;
}
.container {
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
border: 2px solid red;
padding: 0;
margin: 2em;
width: 80%;
}
.main{
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
<body>
<div class="main">
<div class="container">
<div class="hero">
</div>
<h1 class="title">Order Summary</h1>
<p>You can now listen to millions of songs, audiobooks, and podcasts on any
device anywhere you like!
</p>
<div class="container">
<h4>Annual Plan</h4>
<p>$59.99/year</p>
Change
</div>
<button>Proceed to Payment</button>
Cancel Order
</div>
</div>
</body>
This should help. You have to add the flex classes to the container that surrounds the elements you want to center.
body {
background: url(images/pattern-background-mobile.svg);
background-repeat: no-repeat;
background-size: cover;
margin: 0;
height: 100%;
box-sizing: border-box;
}
main {
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
padding: 0 10%;
}
.container {
border: 2px solid red;
width: 80%;
padding: 2% 5%;
margin-bottom: 2%;
}
<body>
<div class="main">
<div class="container">
<div class="hero">
</div>
<h1 class="title">Order Summary</h1>
<p>You can now listen to millions of songs, audiobooks, and podcasts on any
device anywhere you like!
</p>
<div class="container">
<h4>Annual Plan</h4>
<p>$59.99/year</p>
Change
</div>
<button>Proceed to Payment</button>
Cancel Order
</div>
</div>
</body>
[How to position text on the image correctly? i got three blocks of text which i want to position them on the same line on the image but i struggle with it :(
#banner {
justify-content: center;
height: 600px;
margin-top: 100px;
margin-left: 10%; }
.banner-text {
color: white;
justify-content: center;
justify-content: space-around;
text-align: center;
display: inline-block;
position: absolute;
flex-direction: column; }
/*DownTown*/
.flex-text {
background-color: grey;
text-align: center;
}
html
<div id="banner"><img src="2525.jpeg">
<div class="banner-text">
<div class="flex-text text1">
<h1><b>DownTown</b> 384 West 4th St Suite 108</h1>
<div class="flex-text text2">
<h1><b>East Bayside</b> 3433 Phisherman Avenue </h1>
<div class="flex-text text3">
<h1><b>Oakdale</b> 515 Crecent avenue Second Floor </h1>
</div>
</div>
</div>
</div>
</div>
here is how it should look
]2
and that's how i did it -_-
Your html markup is not correct and instead of adding image as an img element in the #banner, add the image as a background image using css. That way, you don't need position absolute to place text over the image. After that, use flexbox for aligning the elements.
#banner {
background-image: url(https://picsum.photos/500);
background-repeat: no-repeat;
background-size: cover;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.banner-text {
color: white;
justify-content: space-around;
display: flex;
width: 100%;
flex-wrap: wrap;
}
.flex-text {
background-color: #222;
display: flex;
align-items: center;
flex-direction: column;
justify-content: space-between;
height: 180px;
padding: 20px;
width: 200px;
margin: 5px;
}
h1 {
margin: 0;
}
<div id="banner">
<div class="banner-text">
<div class="flex-text text1">
<h1>DownTown</h1>
<span>384 West 4th St</span>
<span>Suite 108</span>
<span>Portland, Maine</span>
</div>
<div class="flex-text text1">
<h1>DownTown</h1>
<span>384 West 4th St</span>
<span>Suite 108</span>
<span>Portland, Maine</span>
</div>
<div class="flex-text text1">
<h1>DownTown</h1>
<span>384 West 4th St</span>
<span>Suite 108</span>
<span>Portland, Maine</span>
</div>
</div>
</div>
Your markup is not right. So I changed it a bit. And I also changed the css accordingly and use flex for aliigning items. And instead of using img in html I use background property.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
max-width: 1200px;
height: 100vh;
margin: auto;
overflow: hidden;
padding: 1rem;
background: #333;
}
#banner {
margin-top: 100px;
}
.banner-text {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
color: white;
}
/*DownTown*/
.flex-text {
background-color: rgba(0, 0, 0, 0.384);
line-height: 4rem;
padding: 4rem;
margin: 0rem 2rem;
text-align: center;
}
<div class="container">
<div id="banner">
<!-- <img src="2525.jpeg" /> -->
<div class="banner-text">
<div class="flex-text text1">
<h1>DownTown</h1>
<p>384 West 4th St</p>
<p>Suite 108</p>
</div>
<div class="flex-text text2">
<h1>East Bayside</h1>
<p>3433 Phisherman Avenue</p>
<p>(Norway Corner)</p>
</div>
<div class="flex-text text3">
<h1>Oakdale</h1>
<p>
515 Crecent avenue
</p>
<p>
Second Floor
</p>
</div>
</div>
</div>
</div>
The issue you have is the root diff i.e #banner has two child. The properties on #banner doesn't indicate in any way that the two items should overlap. You are trying to achieve that through position:absolute. Which is also an approach. But here's how you can achieve what you want in two ways:
Put that image as the background property for #banner and provide the flexbox properties to banner.
body {
margin: 0;
max-width: 100%;
}
#banner {
position: relative;
height: 600px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#banner img {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
z-index: 1;
}
.banner-text {
position: relative;
width: 100%;
z-index: 2;
display: flex;
justify-content: space-around;
}
.flex-text {
background: #efefef;
}
h1 {
font-size: 17px;
}
<div id="banner">
<img src="https://cdn.slashgear.com/wp-content/uploads/2019/07/coffee_main_envat-1280x720.jpg">
<div class="banner-text">
<div class="flex-text text1">
<h1><b>DownTown</b><br/> 384 West 4th St Suite 108</h1>
</div>
<div class="flex-text text2">
<h1><b>East Bayside</b><br/> 3433 Phisherman Avenue </h1>
</div>
<div class="flex-text text3">
<h1><b>Oakdale</b><br/> 515 Crecent avenue Second Floor </h1>
</div>
</div>
</div>
Position your banner div relative. Then position your image as absolute so that it occupies whole width and banner-text should have relative positioning. One other thing you need to do if the text doesn't come to center is provide 100% width to the banner-text.
Hope this solves the problem