I am trying to learn html and css the page I created below has an issue, whenever I add sentences to the paragraph the image size increases. I watched videos, and searched Google, but found no fix. Can someone please help me with the code, and explain what was wrong?
body {
background: #222831;
;
padding: auto;
margin: 0;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.info {
justify-content: center;
margin-top: 100px;
display: flex;
flex-direction: column;
}
.info img {
display: block;
margin: auto;
border-radius: 50%;
max-width: 50%;
}
.info h3 {
text-align: center;
}
.info h4 {
text-align: center;
}
.navigaton {
display: flex;
}
.navigaton a {
margin-left: auto;
text-decoration: none;
color: white;
text-transform: uppercase;
opacity: 25%;
}
.navigaton a:hover {
text-shadow: 0 0 15px cyan;
opacity: 100%;
color: cyan;
}
.navabt {
justify-content: center;
align-items: center;
margin: 0%;
}
<div class="container">
<div class="info">
<img src="https://upload.wikimedia.org/wikipedia/commons/a/ac/Default_pfp.jpg" alt="error">
<h3>Anirban Roy</h3>
<h4>Btech in cse</h4>
</div>
<div class="navigaton">
Summary
Key Skills
Education
</div>
<div class="navabt">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Nihil perspiciatis eveniet quod enim. Sed nam tenetur eveniet, nostrum iusto libero.
</p>
</div>
</div>
I want the paragraph to be aligned to the centre, and whenever I add paragraph sentences the image size should not increase
I have found your issue, in CSS the body element was having most of its CSS rules ignored due the the extra colon and because of this the width wasnt been set a size which allowed the page to keep growing, and then due to the img being set half the size of the page it was also growing.
This should now work hope this helps.
body {
background: #222831;
padding: auto;
margin: 0;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.info {
justify-content: center;
margin-top: 100px;
display: flex;
flex-direction: column;
}
.info img {
display: block;
margin: auto;
border-radius: 50%;
max-width: 50%;
}
.info h3 {
text-align: center;
}
.info h4 {
text-align: center;
}
.navigaton {
display: flex;
}
.navigaton a {
margin-left: auto;
text-decoration: none;
color: white;
text-transform: uppercase;
opacity: 25%;
}
.navigaton a:hover {
text-shadow: 0 0 15px cyan;
opacity: 100%;
color: cyan;
}
.navabt {
justify-content: center;
align-items: center;
margin: 0%;
}
Trying to place a button into flex container and make its height to be percentage of sibling's (.task) height. It works as expected except that button horizontally overflows parent (.task-buttons) and ancestor (.task). I have no idea why it happens.
I'd like .task-buttons to have the same width as its content and fit into .task, shrinkin .task-todo if needed. Exactly .task-todo as its parent (.task-info) may contain additional items that shouldn't be shrunk.
*,
::before,
::after {
box-sizing: border-box;
padding: 0;
margin: 0;
font-size: 22px;
}
.container {
position: relative;
left: 30%;
top: 50px;
width: 40%;
padding: 8px 16px;
border-radius: 30px;
box-shadow: 0px 1px 4px #00000029;
}
.task {
display: flex;
justify-content: space-between;
width: 100%;
}
.task-info {
display: flex;
align-items: center;
white-space: nowrap;
overflow: hidden;
}
.task-todo {
overflow: hidden;
text-overflow: ellipsis;
}
.task-buttons {
display: flex;
align-items: center;
position: relative;
}
.task-inc-button {
font-size: 18px;
color: white;
height: 80%;
aspect-ratio: 1;
border: none;
border-radius: 50%;
background-color: #9BE0DB;
cursor: pointer;
}
<div class="container">
<div class="task">
<div class="task-info">
<div class="task-todo">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis magna a sem posuere vestibulum.</div>
</div>
<div class="task-buttons">
<button type="button" class="task-inc-button">+</button>
</div>
</div>
</div>
Replace aspect-ratio: 1; with width: 20px; in .task-inc-button CSS, I hope it'll help you out. Thank You
*,
::before,
::after {
box-sizing: border-box;
padding: 0;
margin: 0;
font-size: 22px;
}
.container {
position: relative;
left: 30%;
top: 50px;
width: 40%;
padding: 8px 16px;
border-radius: 30px;
box-shadow: 0px 1px 4px #00000029;
}
.task {
display: flex;
justify-content: space-between;
width: 100%;
}
.task-info {
display: flex;
align-items: center;
white-space: nowrap;
overflow: hidden;
}
.task-todo {
overflow: hidden;
text-overflow: ellipsis;
}
.task-buttons {
display: flex;
align-items: center;
position: relative;
}
.task-inc-button {
font-size: 18px;
color: white;
height: 80%;
border: none;
border-radius: 50%;
background-color: #9BE0DB;
cursor: pointer;
width: 20px;
}
<div class="container">
<div class="task">
<div class="task-info">
<div class="task-todo">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis magna a sem posuere vestibulum.</div>
</div>
<div class="task-buttons">
<button type="button" class="task-inc-button">+</button>
</div>
</div>
</div>
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;
}
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 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>