Bootstrap Vertical Align Image - html

On this website: http://www.livenews.surf/ I want to make news images and text vertically middle align, how i can do that?

Use the following classes in your containing div.row instead of custom CSS as suggested for bootstrap 4.
d-flex flex-wrap align-items-center

The easiest solution is to use Flexbox (see spec. for more details on it's usage).
For this particular case, assign a custom class to each of your content containing div (currently it only has .col-md-11), for example class "content" and add this code to your stylesheet
.content {
display: flex;
align-items: center;
flex-wrap: wrap;
}
Small code explanation: align-items aligns the items vertically, since we left the default flex direction which is row and flex-wrap will allow our parent container to wrap children to next line (default is nowrap).
Keep in mind, that I haven't included vendor prefixes for the sake of this answer, however I would strongly recommend you to do so, using a tool such as Autoprefixer.

Well, As you are using bootstrap columns, so you will need to make by following a couple of steps as explained below:
As a general case html structure of your web page is as follows:
<div class="col-md-11">
<div class="col-md-5">
<a href="#">
<img src="images/image.jgp">
</a>
</div>
<div class="col-md-7">
// text goes here
</div>
</div>
First of all you will need to make the height of both columns (image + text) same. For this you can use jQuery matchHeight.
After that you can make your images vertically centered with the help of following change.
<div class="col-md-5 photo">
<a href="#">
<img src="images/image.jgp">
</a>
</div>
.photo {
display: table;
}
.photo a {
vertical-align: middle;
display: table-cell;
}
.photo img {
display: block;
height: auto;
width: 100%;
}
Here is Plnkr.

Related

Artist - title stick out div in mobile view media queries

I have a dynamic audio player with artist and title.
It works fine but on mobile view, artist and title stick out my div.
Here is a picture:
I'd like artist - title (when longer that user's screen) display just a line below.
I already tested by changing display, overflow-text, word-breakā€¦
Here is my code:
#media screen and (orientation: portrait) and (max-width: 750px) {
.titrage { display: inline-grid; }
}
<div class='titrage'>
<div class='now'>Your are listening <img class='cover' src="cover.png" alt='audio-cover'> : </div>
<div class='artist'>Loading </div>
<div class='separator'>-</div>
<div class='title'>...</div>
</div>
Use flex maybe? This should work fine without media queries. Or can with media queries too.
.titrage {
display: flex;
flex-direction: row;
align-items: center;
}
.pad {
padding: 10px;
}
<div class='titrage'>
<div class='now'>
<img class='cover' src="https://via.placeholder.com/75" alt='audio-cover'>
</div>
<div class="pad">
<span>You are Listening :</span>
<span class='artist'>Bee Gees </span>
<span class='separator'>-</span>
<span class='title'>Stayin' Alive (1977)</span>
</div>
</div>
Changing the divs to use display: inline-block; instead of inline grid will change the divs layout behavior.
Here is a link from MDN with more details on block and inline layout flow.
Right now with inline-grid each div is taking up a full row, regardless of its content's size, changing to inline-block will make each div take up roughly the amount of size that its content requires, resulting in them displaying on the same line if they can fit, demonstrated in code snippet below:
.titrage, .now, .artist, .separator, .title { display: inline-block; }
<div class='titrage'>
<div class='now'>Your are listening <img class='cover' src="cover.png" alt='audio-cover'> : </div>
<div class='artist'>Loading </div>
<div class='separator'>-</div>
<div class='title'>...</div>
</div>
This question about making divs layout on the same line is directly related.
Also this question about preferred line break points seems tangentially related.
Thanks for answering ;)
I finally found a solution.
I just put artist - title a line below 'You are listening'.
On .titrage a display: block. On .now a display: fixed. And on a new div .now (including .artist, .separator and .title) a display: flex
It's working for me :p

How to fix the problem of separating three side-by-side elements in HTML and CSS

I have three elements that are side-by-side on the personal website I am creating. I am wanting to structure them like so:
https://imgur.com/a/FMII42f
But instead, the middle element doesn't seem to create freely to editing the margins. Example, "margin-left" doesn't seem to have any effect at all.
THis is what my website currently looks like: https://imgur.com/a/kvSiJwI
I am beginning to think that I should have a much deeper grasp about HTML and its semantics, versus trying to learn experimentally and through playing around with the different <..>. Has anyone got any recommendations for this? I should like to say that I have learnt coding HTML and CSS and now JavaScript, purely through free resources, purely through freecodecamp.org
I've tried floating the outside comments, left and right respectively and the middle "center" but then the center element is acting oddly, again, by being relatively higher (has a margin-top value greater than the other two).
Here is the section pertaining to the three elements:
<div class="Services1">
<h2> Fine Art and Calligraphy</h2>
<a href="sites/blog.html">
<img src="images/FineArts.png" id="Fine">
</a>
</div>
<div class="Service2">
<h2> Chess and Mathematics Tutoring</h2>
<a href="sites/services.html">
<img src="images/Math.png" style="width: 300px">
</a>
</div>
<div class="Services3">
<h2> Web Dev and Design </h2>
<a href="sites/services.html">
<img src="images/Web-Developer-Skills-1.jpg" style="width: 400px;">
</a>
</div>
whereas the CSS that pertains to these is:
.Services1 {
float: left;
padding: 20px;
border: 5px solid red;
text-align: center
}
.Services2 {
display: inline-block;
margin: 20px;
}
.Services3 {
float: right;
margin-top: -330px;
margin-right: 90px;
border: 5px solid red;
text-align: center;
}
Please be aware, I have tried float: center; to the middle element without avail. Margin's don't seem to be working either. Also it seems stuck to the left element. Very strange.
The flex format might serve you better. You would need a parent element around your three columns but display: flex would give you a lot more control than using float.
CSS-Tricks has a very good reference guide to using flexbox.
.flex-parent {
display: flex;
justify-content: center;
}
[ class^="Service"] {
border: 3px solid black;
padding: 10px;
text-align: center;
margin: 0 20px;
}
<div class="flex-parent">
<div class="Services1">
<h2> Fine Art and Calligraphy</h2>
<img src="//placekitten.com/303/303" id="Fine">
</div>
<div class="Service2">
<h2> Chess and Mathematics Tutoring</h2>
<img src="//placekitten.com/303/303" id="Fine">
</div>
<div class="Services3">
<h2> Web Dev and Design </h2>
<img src="//placekitten.com/303/303" id="Fine">
</div>
</div>
Make a container <div> around all 3 elements
display: flex;
justify-content: space-between;
A quick way using flex-box would be to wrap your divs in a container div with class of e.g. flex-container:
<div class="flex-container">
<div class="Services1">
<h2> Fine Art and Calligraphy</h2>
<a href="sites/blog.html">
<img src="images/FineArts.png" id="Fine">
</a>
</div>
<div class="Service2">
<h2> Chess and Mathematics Tutoring</h2>
<a href="sites/services.html">
<img src="images/Math.png" style="width: 300px">
</a>
</div>
<div class="Services3">
<h2> Web Dev and Design </h2>
<a href="sites/services.html">
<img src="images/Web-Developer-Skills-1.jpg" style="width: 400px;">
</a>
</div>
</div>
and add the following CSS to flex-container:
.flex-container {
display: flex;
justify-content: space-between;
}
you may need to remove some of the margins and floats you have in your original CSS.
I would recommend looking into some basic flexbox concepts, here for example.
Another option to flexbox is the less well-known CSS grid (not to be confused with table). This might come in handy though especially if you plan on expanding your content.
I strongly recommend using CSS Flexbox if you are familiar with it.
I also noticed that the img in the services2 element is causing the strange offset.
I agree with what Carlos said about making a container div around all 3 elements, display: flex; justify-content: space-between; -- in this case you should get rid of those floats for sure. If you want to keep the floats in those classes, all three need to have the same float (either float: left or float: right), and then you should tamper with the margins to get the spacing you want.
Also, avoid styling width and height in the HTML, and you are better off making a div with a background-image: url("../images/FineArts.png")(in the CSS). This way you have a lot more control of the image and its size. I hope this helps.

Making text elements in HTML take up a fixed amount of space without using tables?

I have a web page that has five buttons of width 50 arranged next to each other in a row, and above each one, I want there to be a text item. However, putting each one in a <span> or a <div style="display:inline"> does not pad them correctly with either "width="50"" or adding "width:50px" to the style; they just appear next to each other. The "obvious" answer is to put each item into a table cell, but W3C says this is a Bad Thing now.
I also tried using input tags with readonly set; these space properly, but the text appears in input boxes rather than "on the page background."
Is there a way to align label elements (that can be changed in the script) evenly spaced horizontally without using a table?
There are a few possible solutions. Either you can use display: table-cell, which perfectly follows the W3C recommendations or you can use a flex box which is an even better solution. However the flex box is still quite new and you may want to support an older browser so the display: table-cell approach might work at least as a fallback.
Please, see the working fiddle.
HTML
<div class="container">
<div class="row">
<span>Text 1</span>
<span>Text 2</span>
<span>Text 3</span>
<span>Text 4</span>
</div>
</div>
CSS
.container {
display: table;
width: 100%;
}
.row {
display: table-row
}
span {
display: table-cell;
text-align: center;
}
Inline elements can't have fixed width or height. Try adding display: inline-block;.
Elements of display: inline don't take a width property, their size is dictated by their contents; to allow for elements to appear in-line with their siblings and to also accept a width switch their display property to that of inline-block.
It's because inline elements does not have fixed width. They are automatically set to fit in the space. You need to set display: inline-block to set width of an inline element.
Try this:
<style>
.btnbtnSpace{
width:50px;
display: inline-block;
}
</style>
<div class="btnbtnSpace">Text<br /><button > 1</button></div>
<div class="btnbtnSpace">Text<br /><button > 2</button></div>
<div class="btnbtnSpace">Text<br /><button > 2</button></div>
<div class="btnbtnSpace">Text<br /><button > 4</button></div>
<div class="btnbtnSpace">Text<br /><button > 5</button></div>
"display: inline-block" worked. I didn't notice it at first because the options on the "CSS Display" page at W3Schools didn't include it; inline-block gets its own page for some reason.

CSS: How to align a division with the center of another division?

I am new for web design. I am trying to making a box including an icon and a link. When the screen is laptop, the link should be in the left of the icon and align with its center vertically; when screen is tablet and mobile phone, the icon should be under the bottom of the icon and align with its center horizontally. And the icon is much bigger than the link. Here are the code and images links:
<div class="box">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-6">
<span class="info-box-icon"><i class="mega-octicon octicon-server"></i></span>
</div>
<div class="col-xs-12 col-sm-12 col-md-6">
<a class="side-box-link" href="workflow-summary" style="vertical-align:middle;">Work Flow</a>
</div>
</div>
</div>
image links:
bg and md:
http://ww3.sinaimg.cn/bmiddle/77f9deafgw1evmq9c8z5tj206703amx6.jpg
sm and xs:
http://ww3.sinaimg.cn/bmiddle/77f9deafgw1evmq9cxdttj2045041gll.jpg
I've tried many method but still cannot make the link align with the icon's center vertically, and the link is always align in top of the box. The methods I've tried:
Use table, and set the icon and link as two table data: they can align as
what I want, but since they are two column, the link cannot move
down when the screen size change.
Use table, put them in same line: align in the box top.
Use list: align in the box top.
Use margin in CSS: align well in md and bg screen but the icon and
link will have gap in sm and xs as well.
Use "vertical-align:middle" in CSS: just not working
What should I do? Any advice will be appreciate, thank you!
You can use Flexbox, there is a nice guide here A Complete Guide to Flexbox
Also take a look at this this example I made for you, hope it can help you out.
Html code:
<div class="box">
<div class="row">
<div class="icon">
<span class="info-box-icon"><i class="mega-octicon octicon-server"></i></span>
</div>
<div class="link">
<a class="side-box-link" href="workflow-summary" style="vertical-align:middle;">Work Flow</a>
</div>
</div>
And this is the CSS:
.row {
display: flex;
align-items: center;
}
.icon {
width: 200px;
height: 200px;
background-color: salmon ;
}
.link{
padding: 10px;
}
a {
text-decoration: none;
}
#media (max-width: 450px) {
.row {
flex-direction: column;
}
}
I did use fixed sizes with the icon to illustrate, but it'll work with any size.
Take a look at this live example JSFiddle Flex Example

Vertical align in bootstrap without breaking the grid

I know there've been a dozen questions to this problem already but none seems to fit my situation. Since using tables for layouting purposes is no good style, I've tried a different although similar aproach, which seems to be a common way of vertically aligning content inside a div.
I'm using a standard bootstrap grid with a row and columns inside.
<div class="row" style="height: 100px";>
<div class="col-lg-1 valign">
<img />
</div>
<div class="col-lg-6 valign">
<span />
</div>
<div class="col-lg-5 valign">
<span />
</div>
</div>
My css looks like this:
.valign
{
display: table;
height: 100%;
}
.valign > img, .valign > span
{
display: table-cell;
text-align: center;
vertical-align: middle;
}
I've seen this been used a lot of times, and I have used it a few times myself, but in bootstrap it breaks the bootstrap grid. The last column 'falls out of the row'.
I've tried adding table-layout: fixed to the elements (span and img in this case) as suggested in another thread.
I've also tried the same thing with twelve divs and the class col-lg-1 with the same result. The last column doesn't seem to fit in the row anymore and it looks like a line break is being created.
Any Ideas?
Cheers, Beejay