How to style horizontally and vertically? - html

<div class="flex-container">
<div class="flex-row">
<li class="org-li" v-for="o in orgs">
<div class="flex-item">
<button v-b-modal.link_printer #click="click_org(o.id)" class="org-btn">
<img class="org-img" :src="`http://localhost:8080/orgs/org_pictures/${o.img}`" alt=""/>
</button>
</div>
<li class="printer-li" v-for="p in printers[o.id]">
<div class="flex-item">
<button variant="outline-primary" class="printer-btn" v-bind:org_id="`${o.id}`" v-bind:printer_id="`${p.id}`" #click="onClick(o.id, p.id, p.serial_number)" >
<img class="printer-img" :src="`http://localhost:8080/printers/printer_pictures/${p.img}`" alt=""/>
</button>
</div>
</li>
</li>
</div>
</div>
I want the org-img to be 55px x 55px and I want the printer-img to be 25px x 25px. Each org-btn and printer-btn should be centered vertically on the screen.
I have been having difficulties with the printer-img being slightly offset from being vertically aligned.

you can do it that way. This adds the CSS styles to your component's block. The .org-img and .printer-img styles will define the width and height of the images, and the .flex-item style will center the buttons within the div elements that contain them. And the .flex-container will wrap the content inside on overflow.
You can make adjustments as needed to match your component's specific structure and class naming.
<template>
<div class="flex-container">
<div class="flex-row">
<li class="org-li" v-for="o in orgs">
<div class="flex-item">
<button v-b-modal.link_printer #click="click_org(o.id)" class="org-btn">
<img class="org-img" :src="`http://localhost:8080/orgs/org_pictures/${o.img}`" alt=""/>
</button>
</div>
<li class="printer-li" v-for="p in printers[o.id]">
<div class="flex-item">
<button variant="outline-primary" class="printer-btn" v-bind:org_id="`${o.id}`" v-bind:printer_id="`${p.id}`" #click="onClick(o.id, p.id, p.serial_number)" >
<img class="printer-img" :src="`http://localhost:8080/printers/printer_pictures/${p.img}`" alt=""/>
</button>
</div>
</li>
</li>
</div>
</div>
</template>
<style>
.org-img {
width: 55px;
height: 55px;
}
.printer-img {
width: 25px;
height: 25px;
}
.flex-item {
display: flex;
align-items: center;
justify-content: center;
}
.flex-container {
display: flex;
flex-wrap: wrap;
}
</style>

Related

Div elements do not wrap on the next line

I defined flexbox properties of container display: flex; flex-wrap: wrap; justify-content: center; but there are always 3 divs on the first row and 2 divs on the second. How to make it wrap divs when the browser resizes?
I tried almost everything (changed the width and height of parent container, changed width to min-width/max-width, set margin of parent container margin: 0 auto;).
<!-- HTML -->
<div class="parent">
<div class="headline">
<h2>Tea of the Month</h2>
<h4>What's Stepping at The Tea Cozy?</h4>
</div>
<div class="container">
<img src="..." class="image">
<p>Fall Berry Blitz Tea</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Spiced Rum Tea</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Seasonal Donuts</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Myrtle Ave Tea</p>
</div>
</div>
<div class="container">
<img src="..." class="image">
<p>Bedford Bizarre Tea</p>
</div>
</div>
<!-- URL of images is correct -->
/* CSS */
.parent {
display: flex;
flex-wrap: wrap;
justify-content: center;
width: 1000px;
margin: 0 auto;
}
.headline {
display: block;
width: 100%;
}
.container {
margin: 0px 10px;
}
.image {
width: 300px;
height: 200px;
}
I expect the divs would wrap on next line. But there always are 3 divs on first and 2 divs on second line.
Your HTML contained some invalid closure tags, please validate your HTML you can check out: https://validator.w3.org/
Also removed the fixed width of 1000px, you want to have a fluid parent so it sizes acording to the device or browser width.
/* CSS */
.parent {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin: 0 auto;
/* changed */
width: 100%;
}
.headline {
display: block;
width: 100%;
}
.container {
margin: 0px 10px;
}
.image {
width: 300px;
height: 200px;
}
<!-- HTML -->
<div class="parent">
<div class="headline">
<h2>Tea of the Month</h2>
<h4>What's Stepping at The Tea Cozy?</h4>
</div>
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Fall Berry Blitz Tea</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Spiced Rum Tea</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Seasonal Donuts</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Myrtle Ave Tea</p>
</div>
<!-- </div> REMOVED -->
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Bedford Bizarre Tea</p>
</div>
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Bedford Bizarre Tea</p>
</div>
<div class="container">
<img src="https://placehold.it/300x300" class="image">
<p>Bedford Bizarre Tea</p>
</div>
</div>
It happens because you set width:1000px on your .parent. This means that no matter the device size the parent will always be 1000px and there will always be 3 elements on the first row.
To solve this, set .parent {width: 100%; max-width: 1000px;}
cheers

How to put a header in between of two images on the same line (using the <div> blocks)?

I am trying to create a webpage for an imaginary pizzeria as practice. I want to insert the header of the webpage in between two images(same) such that the three(image, header, image) elements are in line. But with the code below I am getting a screen like this. How do I get them all on one line?
<body>
<div style='float:left'>
<img src='body.png' style='width:100px; height:100px;'>
</div>
<div style='font-family:amita; font-size:20px;'>
<h1 align='center' style='margin-bottom:5px;'>Ralph's Pizzeria</h1>
</div>
<div style='float:right'>
<img src='body.png' style='width:100px; height:100px;'>
</div>
<hr style='clear:both;' />
</body>
This can be done using display: flex and justify-content: space-between:
.header {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
<div class="header">
<div>
<img src='body.png' style='width:100px; height:100px;'>
</div>
<div style='font-family:amita; font-size:20px;'>
<h1 align='center' style='margin-bottom:5px;'>Ralph's Pizzeria</h1>
</div>
<div>
<img src='body.png' style='width:100px; height:100px;'>
</div>
</div>
I've added a container div and given it the class header and applied the flexbox to it.
You can simply do this :
header {
text-align: center;
}
h1 {
font-size: 25px;
display: inline-block;
vertical-align: top;
margin-top: 25px;
}
img {
display: inline-block;
width: 100px;
height: 100px;
margin:0 10px;
}
<header>
<img src='https://lorempixel.com/100/100/'>
<h1>Ralph's Pizzeria</h1>
<img src='https://lorempixel.com/100/100/'>
</header>
<hr>
Here's another solution if you want to continue to work with float.
Simply move the 2nd image to before the title.
<body>
<div style='float:left'>
<img src='body.png' style='width:100px; height:100px;'>
</div>
<div style='float:right'> <!-- Just moved this up here -->
<img src='body.png' style='width:100px; height:100px;'>
</div>
<div style='font-family:amita; font-size:20px;'>
<h1 align='center' style='margin-bottom:5px;'>Ralph's Pizzeria</h1>
</div>
<hr style='clear:both;'/>
</body>
However, I suggest using flex-box if you don't have to support older browsers.

Flexbox grid with multi-width columns

I have a 4 column flexbox grid that is used to display products. All products have a width of 25% but how can I ensure the grid doesn't break if just 1 product gets a width of 75% for example so that I can display 2 products on one row?
Is there a way, using flexbox, to give the wider 75% grid item priority so that it automatically adjusts/shifts the grid to ensure it doesn't break like it does at the moment?
jsFiddle (Code at the bottom)
https://jsfiddle.net/kdjkc15g/
This is what I'm trying to achieve:
CSS
.grid-uniform {
display: -ms-flex;
display: -webkit-flex;
display: flex;
flex-wrap: wrap;
margin-left: -20px;
}
.grid__item {
width: 25%;
display: -ms-flex;
display: -webkit-flex;
display: flex;
padding-left: 20px;
position: relative;
margin-bottom: $gutter*2;
}
.grid__item.featured-product {
width: 75%;
}
HTML
<div class="flex-grid">
<div class="grid-uniform">
<a href="#" class="product grid__item">
<div class="flex-content">
<div class="grid__image">
<img src="http://via.placeholder.com/250x350">
</div>
<div class="grid product-info">
<p>
This is a title
</p>
</div>
</div>
</a>
...
</div>
</div>
Edit your CSS to this. It's fully responsive now.
The CSS
* {box-sizing: border-box;}
.flex-grid {
overflow: hidden;
padding: 0;
}
.grid-uniform {
display: flex; display: -webkit-flex;
flex-wrap:wrap; -webkit-flex-wrap: wrap;
}
.grid__item {
flex-grow: 1; -webkit-flex-grow: 1;
flex-basis: 25%;
}
.grid__item.featured-product {
flex-basis: 50%;
}
.grid__image {
background:#f4f4f4;
padding-top:$gutter;
padding-bottom:$gutter;
overflow: hidden;
}
Link to fiddle
A rather easy way to do that type of layout is through CSS Grid module. But since you've asked it for Flexbox; here is my take:
The flexbox-based grid here is required to be adjusted on width (not exactly the width property, but flex-basis). Using flex-basis makes it more easier to size up the flexbox-children, therefore I avoided giving them a width.
The adjustment I made below is with calc(), substracting the (n-1) times of the gutter width from each .grid__item.
Why (n-1) times?
We've been giving a left margin to each of the .grid__item and want it not to collapse. So, we discount the last cell in a row.
/* Box-sizing reset */
*,
*:before,
*:after {
box-sizing: border-box;
}
.flex-grid {
max-width: 1120px;
margin: 2em auto;
}
.grid-uniform {
display: flex;
flex-wrap: wrap;
margin-left: -20px;
}
.grid__item {
display: flex;
/* Used `flex` instead of `width` */
flex: 1 0 calc(25% - 60px); /* (4-1) * 20px = 60px
/* Used `margin` instead of `padding` */
margin: 0 0 20px 20px;
position: relative;
/* Gave it a size in order to make it look "uniform" */
max-height: 350px;
}
.grid__item.featured-product {
flex-basis: calc(75% - 60px); /* (4-1) * 20px = 60px */
}
.flex-content {
/* Made the content take up the whole available width and hide bleed-outs, if any */
width: 100%;
overflow: hidden;
}
.grid__image img {
/* Made the dummy images look okay */
width: 100%;
object-fit: cover;
object-position: center;
}
<div class="flex-grid">
<div class="grid-uniform">
<a href="#" class="product grid__item">
<div class="flex-content">
<div class="grid__image">
<img src="http://via.placeholder.com/250x350">
</div>
<div class="grid product-info">
<p>
This is a title
</p>
</div>
</div>
</a>
<a href="#" class="product grid__item">
<div class="flex-content">
<div class="grid__image">
<img src="http://via.placeholder.com/250x350">
</div>
<div class="grid product-info">
<p>
This is a title
</p>
</div>
</div>
</a>
<a href="#" class="product grid__item">
<div class="flex-content">
<div class="grid__image">
<img src="http://via.placeholder.com/250x350">
</div>
<div class="grid product-info">
<p>
This is a title
</p>
</div>
</div>
</a>
<a href="#" class="product grid__item">
<div class="flex-content">
<div class="grid__image">
<img src="http://via.placeholder.com/250x350">
</div>
<div class="grid product-info">
<p>
This is a title
</p>
</div>
</div>
</a>
<a href="#" class="product grid__item">
<div class="flex-content">
<div class="grid__image">
<img src="http://via.placeholder.com/250x350">
</div>
<div class="grid product-info">
<p>
This is a title
</p>
</div>
</div>
</a>
<a href="#" class="product grid__item featured-product">
<div class="flex-content">
<div class="grid__image">
<img src="http://via.placeholder.com/550x350">
</div>
<div class="grid product-info">
<p>
This is a title
</p>
</div>
</div>
</a>
<a href="#" class="product grid__item">
<div class="flex-content">
<div class="grid__image">
<img src="http://via.placeholder.com/250x350">
</div>
<div class="grid product-info">
<p>
This is a title
</p>
</div>
</div>
</a>
<a href="#" class="product grid__item">
<div class="flex-content">
<div class="grid__image">
<img src="http://via.placeholder.com/250x350">
</div>
<div class="grid product-info">
<p>
This is a title
</p>
</div>
</div>
</a>
</div>
</div>
I hope it was helpful. Cheers!

Control wrapping alignment in flexbox

I'd like all divs to be centered, both horizontally and vertically, but have the headings on the left (Posters, Lobby Cards and Misc) remain centered no matter how many rows may be needed next to them on their right.
And I want to do it all with Flexbox!
Here is what I have:
Here is what I'd like to have:
#hrthick {
color: #800000;
background-color: #800000;
width: 90%;
height: 10px;
}
.EPWrapper {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
align-items: center;
}
.EPHeader {
width: 200px;
}
.EPContent {
-webkit-flex-direction: row;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
text-align: center;
}
<div class="EPButtonInner">
<div class="EPWrapper">
<div class="EPHeader">
<img src="http://i.imgur.com/A7NvL35.png">
</div>
<div class="EPContent">
<a href="http://imgur.com/VtTQdEg">
<img src="http://i.imgur.com/VtTQdEgm.jpg">
</a>
</div>
<div class="EPContent">
<a href="http://imgur.com/ELV6u2i">
<img src="http://i.imgur.com/ELV6u2im.jpg">
</a>
</div>
<div class="EPContent">
<a href="http://imgur.com/1XoSRx1">
<img src="http://i.imgur.com/1XoSRx1m.jpg" title="source: imgur.com" />
</a>
</div>
</div>
<!-- EPWrapper -->
<hr id="hrthick">
<div class="EPWrapper">
<div class="EPHeader">
<img src="http://i.imgur.com/ZJiFNlg.png">
</div>
<div class="EPContent">
<a href="http://imgur.com/5SmCQPE">
<img src="http://i.imgur.com/5SmCQPEm.jpg">
</a>
</div>
<div class="EPContent">
<a href="http://imgur.com/sXj4N3W">
<img src="http://i.imgur.com/sXj4N3Wm.jpg">
</a>
</div>
<div class="EPContent">
<a href="http://imgur.com/6eBofBD">
<img src="http://i.imgur.com/6eBofBDm.jpg">
</a>
</div>
<div class="EPContent">
<a href="http://imgur.com/7zDGNgk">
<img src="http://i.imgur.com/7zDGNgkm.jpg">
</a>
</div>
<div class="EPContent">
<a href="http://imgur.com/VjpjUSu">
<img src="http://i.imgur.com/VjpjUSum.jpg">
</a>
</div>
</div>
<!-- EPWrapper -->
<hr id="hrthick">
<div class="EPWrapper">
<div class="EPHeader">
<img src="http://i.imgur.com/oaTM0xH.png">
</div>
<div class="EPContent">
<a href="http://imgur.com/kD1U5i3">
<img src="http://i.imgur.com/kD1U5i3m.jpg">
</a>
<br>(Herald)</div>
<div class="EPContent">
<a href="http://imgur.com/skg6N8u">
<img src="http://i.imgur.com/skg6N8um.jpg">
</a>
</div>
<div class="EPContent">
<a href="http://imgur.com/WidGPsF">
<img src="http://i.imgur.com/WidGPsFm.jpg">
</a>
</div>
</div>
<!-- EPWrapper -->
</div>
<!-- EPButtonInner -->
Thank You in advance :)
For each section (.EPWrapper), you have the heading and the images wrapped together in the same flex container.
That means that when the images begin to wrap, they will naturally start the next row in the first column (right under the heading).
To achieve the layout you want, you would need to force the images to wrap, but start the next row in the second column. This also frees up the space in the first column for the heading to move around vertically.
One solution is to put the heading and images in separate flex containers, aligned side-by-side.
Here's an example using your "Lobby Cards" section:
/* new */
.headings {
display: inline-flex; /* inline-level flex container */
align-items: center;
justify-content: center;
}
/* new */
.images {
display: inline-flex; /* inline-level flex container */
align-items: center;
justify-content: center;
flex-wrap: wrap; /* images are allowed to wrap in this container */
}
/* original code */
#hrthick {
color: #800000;
background-color: #800000;
width: 90%;
height: 10px;
}
.EPWrapper {
display: flex;
/* flex-wrap: wrap; remove; this would allow image container to wrap
under the heading container */
justify-content: flex-start;
align-items: center;
}
.EPHeader {
width: 200px;
}
.EPContent {
-webkit-flex-direction: row;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
text-align: center;
}
<div class="EPButtonInner">
<div class="EPWrapper">
<section class="headings">
<div class="EPHeader"><img src="http://i.imgur.com/ZJiFNlg.png"></div>
</section>
<section class="images">
<div class="EPContent">
<img src="http://i.imgur.com/5SmCQPEm.jpg">
</div>
<div class="EPContent">
<img src="http://i.imgur.com/sXj4N3Wm.jpg">
</div>
<div class="EPContent">
<img src="http://i.imgur.com/6eBofBDm.jpg">
</div>
<div class="EPContent">
<img src="http://i.imgur.com/7zDGNgkm.jpg">
</div>
<div class="EPContent">
<img src="http://i.imgur.com/VjpjUSum.jpg">
</div>
</section>
</div>
</div>

how to place two divs side by side

How can I place 2 divs side by side, both which have images. I want the divs to remain side by side and the images auto size ,with screen size.When I reduce the size of the screen the images re-position themselves one below the other.
How will be the css for the below html?
<div class="container">
<div class="wrapper">
<div class="left">
<img src="img1.png" />
</div>
<div class="right">
<img src="img2.png" />
</div>
</div>
</div>
You can use css float to get contained elements to sit either side
.container {
width: 100%;
}
.left, .right {
width: 50%;
float: left;
}
<div class="container">
<div class="left"><img src='http://placehold.it/350x150'/></div>
<div class="right"><img src='http://placehold.it/350x150'/></div>
</div>
Edit: You can use Flex-Box
.container {
width: 100vw;
display: inline-flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: center;
align-content: space-around;
}
.container > img {
align-self: auto;
margin: 4px;
}
<div class="container">
<img src='http://placehold.it/350x150'/>
<img src='http://placehold.it/350x150'/>
</div>
Try using bootstrap grid's. It can be something like this.
<div class="col-sm-12">
<div class="col-sm-6">
<img src="http://www.thebrandbite.com/wp-content/media/2015/07/apple-7.jpg" width="30%">
</div>
<div class="col-sm-6">
<img src="https://d3nevzfk7ii3be.cloudfront.net/igi/KRLMkuaBjm5mKDDP" width="30%">
</div>
</div>
Demo