I made a div with text and a description alongside an image. They look fine, but when I resize my browser and make it smaller, the image gets smaller. I eventually intend to make this responsive, so how can I make the image not resize when I change the browser size? Here's an example:
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none;
background-color: #F6F6F6;
}
.course {
cursor: pointer;
margin-top: 10px;
display: flex;
justify-content: space-between;
align-items: center;
background-color: #ffffff;
width: 70%;
margin-left: 15%;
margin-right: 15%;
padding-right: 10%;
}
.course__header {
display: flex;
align-items: center;
}
.course-brief{
font-weight: normal;
}
.course__header img {
max-width: 30%;
max-height: 30%;
width: 30%;
display: block;
vertical-align: middle;
padding-right: 5%;
}
<div class="course">
<div class="course__header">
<img src="https://media.npr.org/assets/img/2013/10/29/gardiner_haussmann_wide-c1eb4ea2508483430321f3d003e8ddda0e1e9324.jpg?s=1400" class="course-image">
<div class="course__info">
<h3 class="course-header">THIS IS SOME TEXT</h3>
<h5 class="course-brief">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Cras tincidunt lobortis feugiat vivamus at</h5>
</div>
</div>
<button class="course-button">
Sign up!
</button>
</div>
Now, when you run the example, you can see that the image doesn't fit the top, unless you full screen it. Is there a css way to make it fit the top, and not resize when your browser resizes? (I also want it to work with many images, not just this one).
Thanks so much!!!
I've encountered this many times and this is the solution I always land on:
apply the image as a background image to a container,
set the background image to cover, and display: flex; will fill it vertically and horizontally.
place the inline image in the container, but hide it for desktop. Then on mobile, show the inline-image and hide the background-image.
The drawback is focus on the image might not always be in the same place for every image, which depending on your application, might not be important. At this point, you can be granular with placement after all are set, using css. But just depends on your specific situation.
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none;
background-color: #F6F6F6;
}
.course {
cursor: pointer;
margin-top: 10px;
display: flex;
justify-content: space-between;
align-items: center;
background-color: #ffffff;
width: 70%;
margin-left: 15%;
margin-right: 15%;
padding-right: 10%;
}
.course__header {
display: flex;
align-items: stretch;
}
.course-brief{
font-weight: normal;
}
.course__header img {
max-width: 30%;
max-height: 30%;
width: 30%;
display: block;
vertical-align: middle;
padding-right: 5%;
}
.course-image {
background-image: url(https://i.imgur.com/Cn6k0bM.jpg);
background-size: cover;
width: 25%;
background-position: center center;
margin-right: 1rem;
}
.course-image img {
display: none;
width: 100%;
max-width: unset;
}
#media(max-width: 768px){
.course {
flex-direction: column;
width: 100%;
margin-left: 0;
margin-right: 0;
padding-right: 0;
}
.course__header {
flex-direction: column;
align-items: unset;
}
.course-image {
margin: 0;
background-image: unset;
width: 100%;
}
.course-image img {
display: block;
margin-bottom: 1rem;
}
}
<div class="course">
<div class="course__header">
<div class="course-image"><img src="https://i.imgur.com/Cn6k0bM.jpg"></div>
<div class="course__info">
<h3 class="course-header">THIS IS SOME TEXT</h3>
<h5 class="course-brief">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Cras tincidunt lobortis feugiat vivamus at</h5>
</div>
</div>
<button class="course-button">
Sign up!
</button>
</div>
In that case you need to set a fixed width to the image since currently you define its width as 30% of its container that in its turn depends on the window size.
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none;
background-color: #F6F6F6;
}
.course {
cursor: pointer;
margin-top: 10px;
display: flex;
justify-content: space-between;
align-items: center;
background-color: #ffffff;
width: 70%;
margin-left: 15%;
margin-right: 15%;
padding-right: 10%;
}
.course__header {
display: flex;
align-items: center;
}
.course-brief{
font-weight: normal;
}
.course__header img {
width: 200px;
display: block;
vertical-align: middle;
padding-right: 5%;
}
<div class="course">
<div class="course__header">
<img src="https://media.npr.org/assets/img/2013/10/29/gardiner_haussmann_wide-c1eb4ea2508483430321f3d003e8ddda0e1e9324.jpg?s=1400" class="course-image">
<div class="course__info">
<h3 class="course-header">THIS IS SOME TEXT</h3>
<h5 class="course-brief">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Cras tincidunt lobortis feugiat vivamus at</h5>
</div>
</div>
<button class="course-button">
Sign up!
</button>
</div>
The image is resizing according to your screen size because you've used percentage for your image. If you don't want your image resizing, just use a fixed height and width for your image. Example:
.course__header img {
width: 200px;
height: 200px;
width: 30%;
display: block;
vertical-align: middle;
padding-right: 5%;
}
Fixed width is required
width: 200px;
instead of
width: 30%;
If you want to have a fixed height without messing up the aspect ratio, an easy way would be to use the background-image property:
img-wrapper {
background-image: url("https://media.npr.org/assets/img/2013/10/29/gardiner_haussmann_wide-c1eb4ea2508483430321f3d003e8ddda0e1e9324.jpg?s=1400");
background-positon: center;
background-size: cover;
background-repeat: no-repeat;
}
Related
Here's a diagram:
I'm trying to make a web page that have a banner (the grey box) with a height of 80vh (when it's possible).
This banner contains a div with a text inside (the blue box) that must be vertically centered inside this banner.
In addition, I have a navigation menu with a height of 100px (the pink line), this menu is positioned in absolute at the top of the banner.
How can I obtain a layout in which the blue box is vertically centered but it cannot cross the navigation menu (pink line) and in which the banner (grey box) cannot be smaller than the height of the blue box + the height of the navigation menu?
I wish I could obtain this result in CSS only.
Here's a code with the partial layout:
html, body {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.banner {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: url("https://picsum.photos/id/1015/1920/1080");
height: 80vh;
min-height: 100px; /* + the box inside :( */
}
nav {
position: absolute;
top: 0;
width: 100%;
border-bottom: solid 4px #f5989d;
height: 100px;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
nav li {
float: left;
}
nav a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 18px;
}
.content {
font-size: 18px;
color: white;
text-align: center;
max-width: 400px;
padding: 20px;
background: #6dcff6dd;
border: solid 1px black;
}
<section class="banner">
<nav>
<ul>
<li>Home</li>
<li>Products</li>
<li>Contact</li>
</ul>
</nav>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</section>
Not sure if it's possible to have all the requirement with CSS only so here is a try with almost all the requirement (missing only the last one). I will simply consider position:sticky
html, body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
.banner {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: url("https://picsum.photos/id/1015/1920/1080");
height: 80vh;
min-height: 100px; /* + the box inside :( */
}
nav {
position: absolute;
top: 0;
width: 100%;
border-bottom: solid 4px #f5989d;
height: 100px;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
nav li {
float: left;
}
nav a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 18px;
}
.content {
font-size: 18px;
color: white;
text-align: center;
max-width: 400px;
padding: 20px;
background: #6dcff6dd;
border: solid 1px black;
/* the trick start here */
position:sticky;
top:100px;
margin:-100px auto;
}
<section class="banner">
<nav>
<ul>
<li>Home</li>
<li>Products</li>
<li>Contact</li>
</ul>
</nav>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</section>
You can visually hack the last requirement like below:
html, body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
.banner {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: url("https://picsum.photos/id/1015/1920/1080") fixed;
height: 80vh;
min-height: 100px; /* + the box inside :( */
}
nav {
position: absolute;
top: 0;
width: 100%;
z-index:2;
border-bottom: solid 4px #f5989d;
height: 100px;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
nav li {
float: left;
}
nav a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 18px;
}
.content {
font-size: 18px;
color: white;
text-align: center;
max-width: 400px;
padding: 20px;
background: #6dcff6dd;
border: solid 1px black;
/* the trick start here */
position:sticky;
top:100px;
margin:-100px auto;
transform-style:preserve-3d;
}
.content::before {
content:"";
position:absolute;
bottom:-2px;
top:0;
left:-50vw;
right:-50vw;
background: url("https://picsum.photos/id/1015/1920/1080") fixed;
transform:translateZ(-1px);
}
body {
overflow-x:hidden;
}
<section class="banner">
<nav>
<ul>
<li>Home</li>
<li>Products</li>
<li>Contact</li>
</ul>
</nav>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</section>
I'm working on a component that is supposed to look like this image (this is what it looks like in Chrome and Firefox):
.team-members {
background-color: lightblue;
padding-top: 5em;
padding-bottom: 7.5em;
}
.team-member {
background-color: white;
max-width: 80%;
margin-right: auto;
margin-left: auto;
display: grid;
grid-gap: 2em;
align-items: center;
grid-template-columns: 0.25em 1fr 2fr 0.25em;
}
.team-member__headshot {
width: 100%;
height: 115%;
grid-column: 2;
grid-row: 1;
margin: -1em 0;
box-shadow: 10px 20px 21px rgba(0,0,0,.16);
object-fit: cover;
}
.team-member__text {
grid-column: 3;
grid-row: 1;
padding: 4.5em 0 2.5em 5em;
}
.team-member__quote {
color: darkblue;
font-weight: 600;
font-size: 30px;
margin-bottom: 1.5em;
line-height: 1.3;
}
.team-member__name,
.team-member__position {
color: blue;
margin: 0;
font-family: serif;
line-height: 1.3;
}
.team-member__name {
font-weight: 700;
}
<section class="team-members">
<div class="team-member">
<img class="team-member__headshot" src="https://d31u1j2vbx6ya5.cloudfront.net/gei-assets/uploads/2019/08/pro-headshots-photography-tips.jpg" alt="{{ team.headshot.first().title }}">
<div class="team-member__text">
<p class="team-member__quote">Lorem ipsum dolor sit amet, consectr adipiscing elit, empor nar incididunt ut labore et dolore magna aliqua elit, empor nar.</p>
<p class="team-member__name">Lorem Ipsum</p>
<p class="team-member__position">Lorem ipsum</p>
</div>
</section>
On Safari, however, the headshot is stretching vertically with the browser window height, which consequently stretches the container (the white box):
Any idea what I can do to fix this in Safari? Thank you!
u can use object-fit for fill responsive image ..
add object-fit css attrribute
.team-member__headshot{
object-fit:cover;
}
This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I want my image to be in the middle of the height in the box.
This my code (image)(The code is below the article)
I want(image)(width screen < 767px)
I want it to be the same on every screen (responsive)
Thank you
.question-answer {
background: #ffffff 0% 0% no-repeat padding-box;
border-radius: 29px;
border: 1px solid #C9C9C9;
cursor: pointer;
color: #303030;
}
.question-answer .question {
display: flex;
justify-content: space-between;
padding-right: 30px;
}
.question-answer .question .content {
padding: 0 34px;
text-align: left;
font-size: 14px;
font-weight: bold;
font-family: Open Sans;
letter-spacing: 0px;
color: #686868;
opacity: 1;
letter-spacing: 0px;
position: relative;
padding-right: 30px;
min-height: 57px;
display: flex;
align-items: center;
}
.question-answer .question img {
max-width: 24px;
max-height: 24px;
margin-top: 17px;
}
#media (max-width: 767px){
.question-answer .question .content {
padding-top: 15px;
padding-bottom: 15px;
padding-right: 0px;
}
.faq-page .question-answer .question img {
}
}
<div class="question-answer">
<div class="question">
<div class="content">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore</div>
<img class="img-down" src="https://i.ibb.co/pRztJtW/arrow-circle-down-solid.png" alt="">
</div>
</div>
Well, you can simply do it with flexbox. All you have to do is to assign the display: flex; to the parent element of your text and image, then with the flex properties like align-items: center; make them align vertically. You also have to remove the margin-top from the image element itself to make it perfectly centre.
So the final code should be something like this:
.question {
display: flex;
align-items: center;
}
.question-answer {
background: #ffffff 0% 0% no-repeat padding-box;
border-radius: 29px;
border: 1px solid #C9C9C9;
cursor: pointer;
color: #303030;
}
.question-answer .question {
display: flex;
justify-content: space-between;
padding-right: 30px;
}
.question-answer .question .content {
padding: 0 34px;
text-align: left;
font-size: 14px;
font-weight: bold;
font-family: Open Sans;
letter-spacing: 0px;
color: #686868;
opacity: 1;
letter-spacing: 0px;
position: relative;
padding-right: 30px;
min-height: 57px;
display: flex;
align-items: center;
}
.question-answer .question img {
max-width: 24px;
max-height: 24px;
}
#media (max-width: 767px) {
.question-answer .question .content {
padding-top: 15px;
padding-bottom: 15px;
padding-right: 0px;
}
}
<div class="question-answer">
<div class="question">
<div class="content">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore</div>
<img class="img-down" src="https://i.ibb.co/pRztJtW/arrow-circle-down-solid.png" alt="">
</div>
</div>
Please add this style. It will make your image vertically center.
.question {align-items: center;}
.question-answer .question img{margin: 0;}
I am trying to create a webpage with a vertically and horizontally centred div, with defined height and width, split down the middle. Almost like an open book.
I have no problems with achieving this in Chrome/Firefox/Safari but cannot get this to reflect in IE11 as half of the 'book' has a greater height than the other half, which leads me to believe the 'height: 863px' property in .loginContainer is causing the issue as it looks marginally better once I move this.
Not totally sure but I think I need to specify an explicit height for the container to stop the content inside becoming squashed - simply removing this property allows the container to be too small so content looks cramped once I put text in there.
I have tried to remove the property and use padding on the content inside to create a bit of space but I don't feel this is the right approach and makes it appear totally different to the mock ups I'm following.
I have created a JSFiddle found here;
https://jsfiddle.net/e02cqdr6/2/
and think the issue lies at;
.loginContainer {
display: flex;
justify-content: flex-start;
flex-grow: 0;
flex-shrink: 1;
margin: 0 auto;
width: 1338px;
height: 863px;
border-radius: 15px;}
html,
body {
background-color: rgb(27, 27, 27);
height: 100%;
min-height: 100%;
/* for firefox */
}
.pageContainer {
display: flex;
flex-direction: column;
justify-content: center;
height: 100%;
}
.loginContainer {
display: flex;
justify-content: flex-start;
flex-grow: 0;
flex-shrink: 1;
margin: 0 auto;
width: 1338px;
height: 863px;
border-radius: 15px;
}
.leftContainer {
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
text-align: center;
background-color: green;
width: 621px;
border-radius: 15px 0 0 15px;
}
.rightContainer {
display: flex;
flex-direction: column;
background-color: #ffffff;
align-items: center;
width: 717px;
border-radius: 0 15px 15px 0;
}
.titleContainer {
display: flex;
flex-direction: column;
width: 100%;
}
input {
height: 30px;
width: 220px;
padding: 0 10px 0 10px;
box-shadow: none !important;
border: 1px solid rgb(243, 241, 241);
border-radius: 15px;
background-color: rgb(243, 241, 241);
color: grey;
}
a {
font-size: 0.7rem;
color: orange;
text-decoration: none;
}
.nextBtn {
margin-top: 10px;
background-color: rgb(134, 200, 223);
color: #ffffff;
}
.assetContainer {
display: flex;
flex-direction: column;
justify-content: center;
align-content: center;
}
.descContainer {
display: flex;
flex-direction: column;
text-align: center;
width: 100%;
}
.descHeading {
margin: 0 auto;
color: #fff;
font-family: Arial, Helvetica, sans-serif;
font-size: 36px;
font-style: normal;
font-weight: 100;
padding-bottom: 5px;
}
.productDesc {
margin: 0 auto;
color: #fff;
font-family: Arial, Helvetica, sans-serif;
font-size: 17px;
font-style: normal;
font-weight: 100;
width: 440px;
}
.assetLogoBlack {
width: 100%;
height: 100%;
}
.logoContainer {
width: 100%;
}
.servicesLogo {
width: 30%;
}
<div class="pageContainer">
<div class="loginContainer">
<div class="leftContainer">
<div class="assetContainer">
<div class="logoContainer">
<img src="/assets/ilogo.svg" class="servicesLogo">
<img src="/assets/cslogo.svg" class="servicesLogo">
</div>
<div class="descContainer">
<p class="descHeading">Lorem ipsum dolor sit amet</p>
<p class="productDesc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi volutpat sodales arcu id tincidunt. Ut a laoreet risus. Suspendisse potenti. Curabitur in ultricies risus. Vivamus convallis non libero commodo malesuada. Cras eu neque vulputate
lectus sagittis ullamcorper sit amet vitae ante. Integer pellentesque neque eget molestie vehicula. </p>
</div>
</div>
</div>
<div class="rightContainer">
</div>
</div>
</div>
So I expect it to look like this: https://ibb.co/T2Cc59V
but instead it looks like this: https://ibb.co/M8zZ9rs
To reiterate, the problem only exists in IE11.
Nothing stands out as obvious and I have no errors thrown so am totally stumped as to how to resolve this.
Thank you for all the input, I have ended up stripping this back to bare bones to see what is causing the issue as it does work fine on the jsfiddle. It turns out it is the white space around the asset I need to use for the background makes it appear as though it doesn't fill the parent div.
I have a child element inside a parent container, the child has element a width of 50%, and a min-width of 30rem.
When I bring the window size in the from the right, after the child element hits its min-width of 30rem it starts to break its containing / parent element, despite there being plenty of available space.
Is there anyway of setting it so the min-width value of 30rem remains, but when the window is reduced in size it still slides inside the parent element (like it does before the min-width value is hit)?
It's sending me nuts. (In the code StackOverflow code-snippet you'll probably need to view full screen to see the issue)
Codepen: https://codepen.io/emilychews/pen/wXBdvz
body {margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
}
.tl {color: white;}
.section {
position: relative;
display: flex;
margin: 0 auto;
width: 100%;
box-sizing: border-box;
padding: 2.48832rem 0;
}
.row {
position: relative;
justify-content: center;
margin: auto;
width: 80%;
box-sizing: border-box;
background: blue;
width: 90%;
right: 5%;
justify-content: flex-start;
padding: 4.299rem 0;
}
.one-col.col-1 {
position: relative;
width: 50%;
margin: 0;
padding: 3.583rem 2rem;
left: 40%;
background: #172731;
min-width: 30rem;
top: 7rem;
color: white;
}
<section class="section">
<div class="row">
<div class="one-col col-1">
<h3 class="tl">Title</h3>
<h3 class="tl"><span id="customerbase">Do your thing</span></h3>
<hr>
<p class="tl">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<p><a class="seework" href="#">SEE WORK</a></p>
</div>
</div>
</section>
You'll have to use calc to adjust the left positioning. This is not a perfect solution but I think it achieves what you are after.
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
}
.tl {
color: white;
}
.section {
position: relative;
display: flex;
margin: 0 auto;
width: 100%;
box-sizing: border-box;
padding: 2.48832rem 0;
}
.row {
position: relative;
justify-content: center;
margin: auto;
width: 80%;
box-sizing: border-box;
background: blue;
width: 90%;
right: 5%;
justify-content: flex-start;
padding: 4.299rem 0;
}
.one-col.col-1 {
position: relative;
width: 50%;
margin: 0;
padding: 3.583rem 2rem;
left: calc(60% - 30rem);
background: #172731;
min-width: 30rem;
top: 7rem;
color: white;
}
<section class="section">
<div class="row">
<div class="one-col col-1">
<h3 class="tl">Title</h3>
<h3 class="tl"><span id="customerbase">Do your thing</span></h3>
<hr>
<p class="tl">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<p><a class="seework" href="#">SEE WORK</a></p>
</div>
</div>
</section>