How can I top-align inline-block elements? What I mean is how can I make inline-block elements appear aligned from the top of the largest element instead of from the bottom. I have made a fiddle to illustrate it:
https://jsfiddle.net/shzyku6u/
When you expand the window wide enough, the 3 smaller blue images appear inline from the bottom of the larger blue image. I would like them to appear from the top of the large, blue image - if it's possible.
Here is the HTML/CSS from the Fiddle:
.topwrapper {
max-width: 1000px;
text-align: center;
}
.block {
display: inline-block;
width: 400px;
}
.listitem {
display: inline-block;
width: 100px;
}
<div class="topwrapper">
<div class="block">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/000080_Navy_Blue_Square.svg/480px-000080_Navy_Blue_Square.svg.png" width="400" height="400" class="bigimage">
</div>
<div class="block">
<ul class="list">
<li class="listitem">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/000080_Navy_Blue_Square.svg/200px-000080_Navy_Blue_Square.svg.png" height="100" width="100">
</li>
<li class="listitem">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/000080_Navy_Blue_Square.svg/200px-000080_Navy_Blue_Square.svg.png" height="100" width="100">
</li>
<li class="listitem">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/000080_Navy_Blue_Square.svg/200px-000080_Navy_Blue_Square.svg.png" height="100" width="100">
</li>
</ul>
</div>
</div>
Since both of the .block sibling elements have a display of inline-block, the vertical-align property will be respected. Just add vertical-align: top:
Updated Example
.block {
display: inline-block;
vertical-align: top;
width: 400px;
}
Related
I have horizontal unordered list with 9 <li> items total. Every <li> item has width of 342px(width defined based on <img> element inside the list). All images has the same width, and every element has margin-left: 10px.
<ul class="carousel" role="list" style="left: 0px;">
<li class="carousel-card" aria-posinset="1" aria-hidden="false" role="listitem" aria-setsize="9" style="display: inline-block;">
<span class="a-list-item">
<div data-reference-url="Url" data-reference-title="Title" class="data-item">
<a class="carousel-item" target="" rel="noopener" href="link">
<div class="image">
<img alt="image" src="myUrl" height="192px" width="342px">
<div class="someClass">...</div>
</div>
</a>
</div>
</span>
</li>
<li class="carousel-card" aria-posinset="2" aria-hidden="false" role="listitem" aria-setsize="9" style="display: inline-block;">
<span class="a-list-item">
<div data-reference-url="Url" data-reference-title="Title" class="data-item">
<a class="carousel-item" target="" rel="noopener" href="link">
<div class="image">
<img alt="image" src="myUrl" height="192px" width="342px">
<div class="someClass">...</div>
</div>
</a>
</div>
</span>
</li>
....
</ul>
Even though I have 9 items in the list, I see that for some reason, entire width of <ul> element is always no more than 1385 px. While I expect it to be:
9[items in the list]*(342px[width of <li>)] + 10px [margin of <li>]) = 3168 px.
I inspected all li elements, and confirmed that their width is correct and is always 342px for every element. What can be the reason why <ul> width is not equal to the width of all li elements that are inside of <ul>?
My CSS styles:
.carousel {
display: inline-block;
position: relative;
left: 0px;
margin: 0px 0px 14px;
padding: 0px;
display: table;
content: "";
line-height: 0;
font-size: 0;
}
.carousel-card {
display: inline-block;
margin: 0 10px 0 0;
position: relative;
overflow: hidden;
text-align: center;
min-width: 145px;
vertical-align: top;
min-height: 200px;
max-width: unset !important;
max-height: unset !important;
}
.a-list-item {
text-align: center;
position: relative;
display: inline-block;
min-width: 145px;
}
The calculated ul width is not based on the content of the li. Content can overflow the container it is in. You have to specify the width of the li elements. At first glance the outcome seems based on the min-width of the elements.
Hi I'm trying to center the 4 images in the div. As you can see they're stuck on the left.
I would like them to be perfectly centre justified with at least a 5-10px margin surrounding them (moved slightly to the right).
It's only this section I'm concerned about...not the rest of the HTML site. Thanks.
My html is:
<section id="slide-6" class="homeSlide">
<div class="bcg">
<div class="hsContainer">
<!-- <div class="hsContent">-->
<div class="img">
<a target="_blank" href="">
<img src="http://fieldtelecommunications.com/images/telecommunications_equipment.jpg" alt="Klematis" width="280" height="280">
</a>
</div>
<div class="img">
<a target="_blank" href="">
<img src="http://fieldtelecommunications.com/images/telecommunications_equipment.jpg" alt="Klematis" width="280" height="280">
</a>
</div>
<div class="img">
<a target="_blank" href="">
<img src="http://fieldtelecommunications.com/images/telecommunications_equipment.jpg" alt="Klematis" width="280" height="280">
</a>
</div>
<div class="img">
<a target="_blank" href="">
<img src="http://fieldtelecommunications.com/images/telecommunications_equipment.jpg" alt="Klematis" width="280" height="280">
</a>
</div>
</div>
</div>
</div>
</section>
Css is:
#slide-6 .bcg {
position: relative;
background-color: #efefef;
height:50%;
}
slide-6 .hsContent {
position: relative;
}
slide-6 .hscontainer {
width: 100%;
height: 100%;
display:inline;
overflow: hidden;
margin: 0 auto; position: relative;
}
div.img {
margin: 20px;
/*padding: 5px;*/
/*border: 1px solid #0000ff;*/
/*height: auto;
width: auto;*/
/*float: left;*/
display:inline;
}
add text-align: center to this div #slide-6
div.img {
display: inline-block;
margin: 20px;
}
and on parent div
text-align: center
Using a grid system like 960gs will make your life easier.
Otherwise you'll have to put the images in containers with width: 25%, 0 margin and 0 padding. You can set margins and paddings on the contents of the containers though. OR make the hsContainer fixed width and give it margin: auto;
Your wrapper needs a width with margin:0 auto in order for the inner content to be aligned center.
section {margin:0 auto;width:1290px;}
JsFiddle Example
one way to just encapsulate everything within
<center></center>
heres a fiddle :)
https://jsfiddle.net/qcoavm5r/
Provide text-align:center to the parent element 'hsContainer'. This property will center align the elements inside it.
Try display: inline-block: instead only block.
I have tried using float left/right, which puts them next to each other but I want them to be evenly spaced from left to right. I tried using padding/margin tags, but they don't work either. Any help would be appreciated!
Code:
<div class="options">
<ul style="list-style:none;">
<li>
<img src="magger.jpg" width="180" height="180"/><br> Do Stuff To His Dog
</li>
<p>
<li>
<img src="me.jpg" width="180" height="180"> <br>Examine His Checkered Past</p>
</li>
<p>
<li>
<img src="wife.jpg" width=180 height=180><br>Sexually Harass His Wife</p>
</li>
</ul>
CSS:
.options li {
float:right;
}
You're over complicating it:
HTML:
<div class="images">
<img src="1.jpg"/>
<img src="2.jpg"/>
<img src="3.jpg"/>
</div>
CSS:
.images {
text-align : center;
}
.images img {
width : 75px;
height : 75x;
margin : 10px;
}
Demo: http://jsfiddle.net/6VrJR/
add this to your css-code:
ul{
width : 550px;
margin: auto;
}
here is a jsfiddle as well
Define width and auto margin for the UL. Also, give margin and text-align center for the text.
.options ul {
width:564px;
margin:auto;
padding:0;
}
.options li {
float:right;
text-align:center;
margin:0 4px;
}
Here is the jsfiddle: http://jsfiddle.net/EASFG/
Do not use floats:
Instead of using floats for your children elements, let them flow inside an anchor tag. Anchor tags have inline as default display behavior (add display: inline; if you need to use another tag instead).
The trick that centers the links is the container text-align: center; property.
This can be translated in code as follows:
Define your container class
.container {
width: 100%;
text-align: center;
display: block;
margin: auto;
}
Create your HTML structure:
<div class='container'>
<img src="http://www.bradfordcityfc.co.uk/images/common/bg_player_profile_default_big.png" alt="">
<img src="http://www.bradfordcityfc.co.uk/images/common/bg_player_profile_default_big.png" alt="">
<img src="http://www.bradfordcityfc.co.uk/images/common/bg_player_profile_default_big.png" alt="">
</div>
Demo: http://codepen.io/manuelro/pen/Abgox
Regards,
M
I have a 100% width container I need to fill with 4 list items. Each list item has an image with text on the right of it. I need assistance aligning each image and text in the center of the list item and aligned center in the containing element. If the browser window is resized the text cannot wrap below the image and still be centered within the container.
I thought 100% container and 25% list items would work for the base sizing but I'm unsure of how to 'unwrap' the text around the image and center each item.
HTML:
<div class="container">
<ul class="products">
<li class="product">
<img src="http://placehold.it/50x50" />Asian Food</li>
<li class="product">
<img src="http://placehold.it/50x50" />Indian Food</li>
<li class="product">
<img src="http://placehold.it/50x50" />Asian Stuff</li>
<li class="product">
<img src="http://placehold.it/50x50" />Indian Stuff</li>
</ul>
CSS:
.container {
width: 100%;
margin: 0 auto;
overflow: auto;
background: grey;
}
.products {
margin: 0 auto;
list-style-type: none;
}
.product {
position: relative;
background: white;
float: left;
width: 25%;
text-align: center;
}
Here's my JSFiddle: http://jsfiddle.net/7N5HH/2/
Thank you!
I've an unordered list like this:
<ul style="float:left;">
<li class="book">
<div>title sdfdfdsfdsfds sdfsdfsdf dsfsdfsdf fdgsdfsdf</div>
</li>
<li class="book">
<div>title</div>
</li>
</ul>
The height of the li should depend partially from the content of li(div height). But if the height of the inner DIV becomes higher than another div inside another li all the div should assume the same dimension of the larger div.
I tryed with:
div { height: auto; min-height: 100%; }
and setting the li
float: left;display: inline-block; height: 100px;
but nothing changed. If the content of the first DIV causes the div to resize, the other DIV will not assume the new 100% height. How can I solve this ?
In CSS, to automatically adjust things to the same height based on their content, they must be part of a table row. Try this:
<ul class="booklist">
<li class="book">
<div>title <br>sdfdfdsfdsfds <br>sdfsdfsdf dsfsdfsdf fdgsdfsdf</div>
</li>
<li class="book">
<div>title</div>
</li>
</ul>
.booklist { display: table; float: left; }
.book { display: table-cell; border: 1px solid gray; }
Note that unlike a set of inline-block items, table rows will not auto-wrap to multiple lines, so if that is your goal, you cannot accomplish this with CSS and must add some JavaScript, such as to adjust the heights.
Maybe I'm not understanding you but this:
all the div should assume the same dimension of the larger div.
is going to happen anyway.
If you are trying to maintain some consistency between the li elements try something like this:
li {float: left; width: 100px; border: 1px solid red; overflow: hidden; }
Try the following (put the li elements inside the div element):
<ul style="float:left;">
<li class="book" >
<div style="display: inline-block; height: 100px;">
title sdfdfdsfdsfds sdfsdfsdf dsfsdfsdf fdgsdfsdf
</div>
</li>
<li class="book" >
<div style="display: inline-block; height: 100px;">
title
</div>
</li>
</ul>
DISCLAIMER: inline styles there for ease of reference:)