I am trying to create a simple element consisting of 3 columns, the 3. column being an icon-image.
I want the element to be responsive, but the icon-image should always have the same size (e.g. 40x40 px).
So, the 1. and 2. column should have % values, the 3. column should have width 40px. I don't want the icon-image to resize on smaller screens. The icon should always have width 40px.
.container {
width: 80%;
}
.text1 {
float: left;
width: 30%;
}
.text2 {
float: left;
width: 50%;
}
.image {
float: right;
width: 120px;
}
<div class="container">
<p class="text1">Some Titel</p>
<h3 class="text2">Some Text and some more text...</h3>
<img src="image1.jpeg" alt="" class="image" />
</div>
https://jsfiddle.net/mkrizanek/usfmk6r7
Kind regards,
Milan
I have tweaked a little bit with HTML and CSS. But I hope you will get the logic. I have changed the display-type of container to flex. You can change the with of elements inside container as per your requirements.
.container {
background-color: #DDDDDD;
width: 80%;
display: flex;
}
.text {
background-color: #BBBBBB;
width: 50%;
}
.heading {
background-color: #999999;
width: 50%;
}
img {
max-width: 120px;
max-height: 120px;
}
<div class="container">
<p class="text">Some Text</p>
<h3 class="heading">Heading</h3>
<img src="https://www.w3.org/html/logo/downloads/HTML5_Logo_512.png" class="image" />
</div>
Your best option is probably to use flex.
Set "display: flex; justify-content: space-between" on the container div, and "flex: 1;" on each section that you want responsive.
If you want the text in the second div to push all the way to the image, you can also add "text-align: right;" to it.
.container {
display: flex;
justify-content: space-between;
}
.text1 {
flex: 1;
}
.text2 {
flex: 1;
}
.image {
width: 40px;
height: 40px;
background: #088;
}
<div class="container">
<p class="text1">Some Title</p>
<h3 class="text2">Some Text and some more text...</h3>
<img alt="" class="image" />
</div>
Here is a good resource for flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/.
Related
I have two divs present inside a parent div. The first div has an absolutely positioned image, whereas the other has some text content. I want the content of the parent div to wrap. So this is what I set:
.item-content {
display: flex;
flex-wrap: wrap;
}
.image-container {
flex-basis: 48%;
position: relative;
border: 2px solid red;
}
.image {
display: block;
position: absolute;
left: -100px;
}
.text-content {
margin: 0px auto;
flex-basis: 43%;
}
<div className="item-content">
<div className="image-container">
<img src={desktopImage} alt="" className="image " />
</div>
<div className="text-content">
<h2>{title}</h2>
<p>{subtitleText}</p>
<div className="divider" />
<h5>The challenge</h5>
<p className="description">{description}</p>
</div>
</div>
But, this doesn't make the content wrap when the screen size changes. How can I make the 'text-content' class wrap?
If you want all the content inside <div class="text-content"> to wrap then you just need to add the flex-wrap property to all the elements inside.
.text-content *{
display:flex;
flex-wrap:wrap;
}
If you guys take a look at the code, when I add squared images (first row), the grid will fit perfectly just like instagram. I want to make different shaped images fits in this same squared width and height when I add them (second row).
The problem is, if I just try to make it a square by changing the width and height of the .column > img selector from 100% to 300px for example, the images won't resize in page responsiveness and everything becomes a mess of images overlaying each other.
body {
font-family: "Open Sans", Arial, sans-serif;
}
* {
box-sizing: border-box;
}
.container {
width: 100%;
max-width: 930px;
margin-right: auto;
margin-left: auto;
}
.column {
width: 33.33%;
margin-right: 30px;
}
.row {
display: flex;
flex-direction: row;
justify-content: center;
margin-bottom: 28px;
}
.column>img {
width: 100%;
height: 100%;
}
#media (max-width: 760px) {
.row {
margin-bottom: 3px;
}
.column {
margin-right: 3px;
}
}
#media (max-width: 450px) {
.row {
flex-direction: column;
justify-content: center;
}
.column {
width: 100%;
margin-bottom: 6px;
}
}
<div class="container">
<div class="row">
<div class="column">
<img src="https://images.unsplash.com/photo-1498471731312-b6d2b8280c61?w=500&h=500&fit=crop">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1515023115689-589c33041d3c?w=500&h=500&fit=crop">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1502630859934-b3b41d18206c?w=500&h=500&fit=crop">
</div>
</div>
<div class="row">
<div class="column">
<img src="https://images.unsplash.com/photo-1523354177913-be035fcee55e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1567&q=80">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1563281746-48bb478e9b2a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1400&q=80">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1563170446-9c3c0622d8a9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1534&q=80">
</div>
</div>
</div>
Is there a way to make all images a square and at the same time keep the responsiveness of the width and height when resizing the page? Thanks in advance.
I made a solution. In my solution I've used object-fit property to resize image. Check it. Hope it will be helpful to you.
body {
font-family: "Open Sans", Arial, sans-serif;
}
* {
box-sizing: border-box;
}
.container {
width: 100%;
max-width: 930px;
margin-right: auto;
margin-left: auto;
}
.column {
width: 33.33vw; /*These properties are changed*/
height: 33.33vw; /*These properties are changed*/
margin-right: 30px;
}
.row {
display: flex;
flex-direction: row;
justify-content: center;
margin-bottom: 28px;
}
.column>img {
width: 33.33vw; /*These properties are changed*/
height: 33.33vw; /*These properties are changed*/
object-fit: cover; /*These properties are changed*/
}
#media (max-width: 760px) {
.row {
margin-bottom: 3px;
}
.column {
margin-right: 3px;
}
}
#media (max-width: 450px) {
.row {
flex-direction: column;
justify-content: center;
}
.column {
width: 100%;
margin-bottom: 6px;
}
}
<div class="container">
<div class="row">
<div class="column">
<img src="https://images.unsplash.com/photo-1498471731312-b6d2b8280c61?w=500&h=500&fit=crop">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1515023115689-589c33041d3c?w=500&h=500&fit=crop">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1502630859934-b3b41d18206c?w=500&h=500&fit=crop">
</div>
</div>
<div class="row">
<div class="column">
<img src="https://images.unsplash.com/photo-1523354177913-be035fcee55e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1567&q=80">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1563281746-48bb478e9b2a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1400&q=80">
</div>
<div class="column">
<img src="https://images.unsplash.com/photo-1563170446-9c3c0622d8a9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1534&q=80">
</div>
</div>
</div>
You can abuse padding for this.
tl;dr: https://jsfiddle.net/Hoargarth/u247vz0k/ Just look at the fiddle, pretty self explanatory
First of all we are using flexboxes, for this we need a flex container and flex items in it.
<div class="flex-container">
<div class="flex-item">
<div class="image-wrapper">
<div class="image" style="background-image: url('https://www.url.to/image.png');">
</div>
</div>
</div>
<!-- Any number of flex items inside container -->
</div>
I'm not explayning the flex-system here, I guess you should know about it.
But for everyone not knowing about flex, here is a pretty good explanation: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
So we start at the flex-item:
position: relative because we need the next element
(image-wrapper) to be absolute positioned to flex-item
width: 32%
becasue you want 3 images per row, don't forget to give your
flex-container a width
padding-bottom: 32% the padding is the key, 32% padding is relative to the width of the flex-container. So if you use padding-bottom: 100%, the flex-item's height would be exact same like flex-container width. In our case we want a quadratic image, so we use 32%
.flex-item {
position: relative;
width: 32%;
padding-bottom: 32%;
}
Next is image-wrapper:
position: absolute because we don't wanna take this container (or any other container inside this one) to take up any more space.
overflow: hidden to make sure nothing can escape our quadratic image and overlaps to another container
.image-wrapper {
position: absolute;
overflow: hidden;
width: 100%;
height: 100%;
}
And last, the image itself:
No magic here, just add your background image as background-image: url() in html so you don't have to create a lot of extra classes.
position: relative I don't remember why I added it, maybe some crossbrowser related stuff. But you can try to remove it, it's working on Firefox without.
background *-size: cover so all the quadratic space gets filled, no matter what size the image is; *-position: 50% to completely center the image (vertically and horizontally); *-repeat: no-repeat you don't really need it since background-size: cover fills the whole space, I just added it so there is no special case where the image would be repeated
.image {
position: relative;
height: 100%;
background-size: cover;
background-position: 50%;
background-repeat: no-repeat;
}
This solution is responsive out of the box, but you can change the number of Images per Row with simple Media Queries:
Example:
/*Window Width smaller 800px with two images per row*/
.flex-item {
position: relative;
width: 48%;
padding-bottom: 48%;
}
/*Window Width larger or equal 800px with three images per row*/
#media (min-width: 800px){
.flex-item {
width: 32%;
padding-bottom: 32%;
}
}
I hope it's understandable, if not let me know!
Okay using flex to align the image (purple) and the content (yellow) side by side. However I am getting extra space between and is throwing off the image to the right.
See here: http://imgur.com/a/nl0ZJ
It's supposed to be:
image should be inside the blue div next to the yellow div
the yellow div should be 60% width (These are flex items)
I've checked padding and margin, however it hasn't worked for me. Any ideas?
Here is the html
<div class="sec-1">
<h2>Introducing 'Keeva' world's smartest assistant.</h2>
<div class="flex-box">
<div>
<p class="sales-copy">Keeva smartphone app helps you organize your work schedule, meetings, project deadlines and much more.</p>
<!-- Download Buttons -->
<img class="download-btns" src="img/playstore-1.png">
<img class="download-btns" src="img/iphone-1.png">
</div>
<!-- Phone 0 image -->
<img class="phone-img" src="img/iphone-cut.png" alt="phone image">
</div>
</div>
CSS
#media screen and (min-width: 599px) {
.sec-1 h2 {
font-size: 1.2em;
background-color: green;
}
.sec-1 p {
font-size: 1.1em;
width: 50%;
background-color: yellow;
}
.sec-1 .phone-img {
position: relative;
top: 10%;
left: 30%;
background-color: purple;
}
.download-btns {
position: relative;
right: 25%;
background-color: orange;
}
.sec-1 .sales-copy {
width: 50%;
}
.flex-box {
display: flex;
justify-content: flex-end;
}
}
Since display: flex makes its immediate descendants flex items, it is only the div that wraps the the p/img that becomes one.
So to make this work, and since img doesn't behave normal when being flex items, move that div wrapper to the img and it will flow as intended.
I also changed width: 50% to flex-basis: 60% so the yellow element becomes 60%, and added flex-grow: 1 to the div wrapper, so it takes the remaining space.
Updated based on a comment
Changed to an outer and an inner Flexbox wrapper, so the buttons are located below the yellow element and the image to the right
.sec-1 h2 {
font-size: 1.2em;
background-color: green;
}
.sec-1 p {
margin: 0;
font-size: 1.1em;
background-color: yellow;
}
.sec-1 .phone-img {
position: relative;
top: 10%;
left: 30%;
background-color: purple;
}
.flex-box-outer {
display: flex;
}
.flex-box-outer > div {
flex-grow: 1;
}
.flex-box-inner {
flex-basis: 60%;
display: flex;
flex-direction: column;
align-items: center;
}
<div class="sec-1">
<h2>Introducing 'Keeva' world's smartest assistant.</h2>
<div class="flex-box-outer">
<div class="flex-box-inner">
<p class="sales-copy">Keeva smartphone app helps you organize your work schedule, meetings, project deadlines and much more.</p>
<!-- Download Buttons -->
<div>
<img class="download-btns" src="http://placehold.it/100x50/?text=playstore">
<img class="download-btns" src="http://placehold.it/100x50/?text=iphone">
</div>
</div>
<!-- Phone 0 image -->
<div>
<img class="phone-img" src="http://placehold.it/100x50/?text=iphone cut" alt="phone image">
</div>
</div>
</div>
The issue I am facing here has to do with the fact that the total width of the child divs is larger than the width of the parent div. I need the child divs to float to the left of each other even if they overflow the parent div.
I have this HTML:
<div class="gallery_container">
<div id="viewport">
<div class="gallery_img_container">
<img src="images/1/1.png" class="gallery_img">
</div>
<div class="gallery_img_container">
<img src="images/1/2.png" class="gallery_img">
</div>
<div class="gallery_img_container">
<img src="images/1/3.png" class="gallery_img">
</div>
</div>
</div>
And this CSS:
.gallery_container {
width: 70%;
height: 100%;
float: left;
background-color: #171717;
}
#viewport {
height: 100%;
width: 100%;
}
.gallery_img_container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.gallery_img {
height: 95%;
}
How can I get all divs with of class gallery_img_container to float to the left of each other? I've tried adding float:left .gallery_img_container but it doesn't do anything. They go underneath each other instead of side by side.
Another note is that .gallery_img_container must have the display:flex property
Thanks in advance for any help!
EDIT: .gallery_img_container must have the width:100% property
You have "width:100%;" applied to the ".gallery_img_container" class causing each container to break into next line, remove this and add "float: left;" then it will work.
Edited based on comments.
.gallery_container {
width: 70%;
height: 100%;
background-color: #171717;
}
#viewport {
height: 100%;
width: 100%;
display: flex;
}
.gallery_img_container {
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
justify-content: flex-start;
flex-wrap: wrap;
}
.gallery_img {
height: 95%;
}
<div class="gallery_container">
<div id="viewport">
<div class="gallery_img_container">
<img src="images/1/1.png" class="gallery_img">
</div>
<div class="gallery_img_container">
<img src="images/1/2.png" class="gallery_img">
</div>
<div class="gallery_img_container">
<img src="images/1/3.png" class="gallery_img">
</div>
</div>
</div>
Farasat,
I'm not entirely sure what you are trying to accomplish. Let me see if I can confirm what I think you are seeking. You want to achieve a result where you have a set of images which are, collectively, wider than #viewport, but you want them to keep "piling up" to the right. That is, you don't want them to wrap. If we consider [] the bounds of the viewport and # to be an image, you want (for example), something like this:
[#]##
Is this right?
If so, I think a key thing to note is that your gallery_img_container is of display flex, which means it is a block, not inline, style. This will cause the gallery_img_containers to stack, not flow to the right.
I think all you need to do is to say "white-space: nowrap;" in #viewport (to keep things from wrapping), and then to make gallery_img_container to be of type inline-flex, so that they do not stack.
I'm attaching an example to hopefully demonstrate what I mean (notes: I just grabbed a random image off the web to make this more concrete. Also note that since your gallery_img_container is 100%, each image is the size of the #viewport. I'm not sure if that's what you want).
.gallery_container {
width: 70%;
height: 100%;
background-color: #171717;
}
#viewport {
height: 100%;
width: 100%;
white-space: nowrap;
}
.gallery_img_container {
width: 100%;
height: 100%;
display: inline-flex;
justify-content: center;
align-items: center;
}
.gallery_img {
height: 95%;
}
<div class="gallery_container">
<div id="viewport">
<div class="gallery_img_container">
<img src="https://wildfiregames.com/forum/uploads/monthly_2016_07/elephant.png.d12017f872cf14f8b046706f497701ba.png" class="gallery_img">
</div>
<div class="gallery_img_container">
<img src="https://wildfiregames.com/forum/uploads/monthly_2016_07/elephant.png.d12017f872cf14f8b046706f497701ba.png" class="gallery_img">
</div>
<div class="gallery_img_container">
<img src="https://wildfiregames.com/forum/uploads/monthly_2016_07/elephant.png.d12017f872cf14f8b046706f497701ba.png" class="gallery_img">
</div>
</div>
</div>
I am new to html/css and I am stumped trying to set up a two column design where the left column is a column of text that has a fixed width of 300px and the right column is a photo that has a width of 40% of the screen. Also, and this is where I am getting hung up, the two columns should always maintain a fixed margin between them, even at different screen sizes. I want it so any excess screen width beyond the 300px that the text column takes up and the 40% of the screen that the photo takes up is evenly distributed to the left and right edges of the screen, instead of going in between the two columns. Currently, when I expand the screen all the excess space goes in between the two columns (while I want there to instead be a fixed margin between them and the excess space to go to the edges). What would be the best way to do this?
I have tried a couple different methods, but here is my code for the closest I have gotten:
<div class="group">
<div class="f">
<h1> Header Here</h1>
<p>Paragraph with text here. Paragraph with text here.</p>
</div>
<img class="pic" src="img/picture.jpg"/>
</div>
.pic {
width: 40%;
margin: 0;
padding: 0;
display: inline-block;
}
.f {
float: left;
margin: 0;
padding: 0;
width: 300px;
text-align: justify;
display: inline-block;
}
.group:after {
content: "";
display: table;
clear: both;
}
Thanks so much for any help. I've tried floating it, using in-line blocks, and relative/absolute.
If I understood your question correctly, you want a fixed width between the text and picture? And any extra space evenly spread to the sides?
* {
margin: 0;
padding: 0;
text-align: center;
}
.group {
display: flex;
justify-content: center;
}
.f {
width: 300px;
height: 200px;
border: 1px solid black;
}
.middle {
width: 10%;
}
.pic {
width: 40%;
height: 200px;
background-color: green;
}
<div class="group">
<div class="f">
fixed width: 300px;
</div>
<div class="middle">
fixed width: 10%;
</div>
<div class="pic">
fixed width: 40%;
</div>
</div>
Is this something like you're trying to achieve?
.pic {
max-width: 100%;
margin: 0;
padding: 0;
}
.group {
width: 100%;
display: table;
table-layout: fixed;
}
.group > * {
display: table-cell;
vertical-align: top;
}
.group .f {
width: 300px;
}
<div class="group">
<div class="f">
<h1> Header Here</h1>
<p>Paragraph with text here. Paragraph with text here.</p>
</div>
<img class="pic" src="http://lorempixel.com/output/people-q-c-640-480-6.jpg" />
</div>