Responsive align list items horizontally without text wrap - html

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!

Related

How to display to two background-images side-by-side [duplicate]

This question already has answers here:
Two divs side by side - Fluid display [duplicate]
(9 answers)
Closed 3 years ago.
I'm trying to put two images side-by-side (occupying the rest of the body) bellow my header, but I didn't find I good solution yet.
I want these two images to be a link for two different pages.
I have tried different things but neither of them worked.
HTML:
<body>
<div class="header">
<div class="logo">
<img src="C:\Users\cristovao\Documents\vscode\real_estate\icons\logo_final.png">
<div class="lettering">
<h6><span class="bolder">F</span>ÁTIMA<span class="bolder">C</span>RISTÓVÃO <span class="smaller">by KELLER WILLIAMS</span></h6>
</div>
</div>
<div class="wrapper">
<div class="nav">
<ul>
<li>
Home
</li>
<li>
Projects
</li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="box">
<a href="./Atower.html">
<img src="./IMAGENS/CI02_00_SALA_P6_4K.jpg" width="100%" >
</a>
</div>
<div class="box">
<a href="./muda.html">
<img src="C:\Users\cristovao\Documents\vscode\real_estate\IMAGENS\CASA B (3).jpg" width="100%" >
</a>
</div>
</div>
</body>
CSS:
body {
margin: 0px;
padding: 0px;
}
.container {
display: inline-flex;
}
.box {
width: 50%;
}
.container {
float: left
}
.box {
width: 50%;
}
All my code is here;
It seems that using flexbox or float I cannot display each image with half of
the total width each, and 100% of the body height, I always have 50% of the
height below the images in white,
(representing body content) when I use flexbox and 50% of white space right when I use float.
Hope anyone can help me
Br
Use flexbox and set margin:0 and padding:0.
.container {
display: inline-flex;
margin:0;
padding:0;
}
.box {
width: 50%;
margin:0;
padding:0;
}
Make the <a> tag a block styled element so it neatly wraps your image. Then stretch your image to the full height and width of its parent and use object-fit: cover to make the image fill your box.
This will have the same effect as on a background-image with the background-size set to cover.
.box a {
display: block;
width: 100%;
height: 100%;
}
.box img {
display: block;
width: 100%;
height: 100%;
max-width: 100%;
max-height: 100%;
-o-object-fit: cover;
object-fit: cover;
}

How to center unordered list of images? [duplicate]

This question already has answers here:
How to center an unordered list?
(8 answers)
Closed 3 years ago.
I am trying to center an unordered list of imagines on a website.
I have tried using 'text-align: center;', margins(which works, kinda, but looks super sloppy)
#nav{
margin-left:44%;
margin-bottom: 10px;
width: 100%;
text-align: center;
background-color: transparent;
height: 40px;
margin-bottom: 100px;
}
#nav li a {
padding: 10px;
text-decoration: none;
font-weight: bold;
color: #FFFFFF;
background-color: transparent;
float: left;
text-align: center;
display: inline;
}
<div id="nav">
<ul>
<li>
<img src='./public/images/discord.png' style="height:40px;border:0;">
</li>
<li>
<img src='./public/images/youtube.png' style="height:40px;">
</li>
<li>
<img src='./public/images/twitch.png' style="height:40px;border:0;">
</li>
</ul>
</div>
The list just doesn't center properly, as expected with manually set margins.
I recommend using flex box to handle problems like these. The first example in the snippet shows centering using flex box. The second example shows centering an unordered list.
To center an unordered list, I've set the margin left and right on the containing element (#nav) to auto. For this to center the list, you will need to know the exact width of the list. I've calculated to be 3 x 40px for each image, plus 20px for spacing for each image, making 180px in total.
A common problem some run into when dealing with undordered is that lists come with default padding on the left. For perfect centering, I've removed the list style bullets, and removed the padding too.
.flex {
display: flex;
justify-content: center;
}
.flex>div {
margin-right: 20px;
}
#nav {
margin: 0 auto;
width: 180px;
}
#nav>ul {
padding-left: 0px;
}
#nav li {
list-style-type: none;
text-decoration: none;
float: left;
margin-right: 20px;
}
<h2>Display flex</h2>
<div class="flex">
<div>
<img src='https://picsum.photos/40' style="height:40px;border:0;">
</div>
<div>
<img src='https://picsum.photos/40' style="height:40px;">
</div>
<div>
<img src='https://picsum.photos/40' style="height:40px;border:0;">
</div>
</div>
<h2>Centered UL</h2>
<div id="nav">
<ul>
<li>
<img src='https://picsum.photos/40' style="height:40px;border:0;">
</li>
<li>
<img src='https://picsum.photos/40' style="height:40px;">
</li>
<li>
<img src='https://picsum.photos/40' style="height:40px;border:0;">
</li>
</ul>
</div>

HTML lists and z-index on content of li

I am trying to solve an issue. I have a regular list. Within the li's I have a overlapping div, witch appears on a hover. The problem is, that the hover is always overlapped by the next li. How can i make it happen, that the content of the next li stays below the hovered div. Html and css seems to stack li's? Is this a know problem?
I already tried z-index, but that will not work on child elements.
Its impossible to add the complete code, but this basically shows it. Why is the next li-element always overlapping the last and how can I get around this?
li {
position: relative;
width: 100px;
}
img {
width: 120%;
height: auto;
position: absolute;
top: 0;
border: 1px solid red;
}
<ul>
<li style="z-index:1;"><!-- list-element 1 -->
<div style="z-index:4;"><img src="http://img.brothersoft.com/screenshots/softimage/f/free_cat_wallpaper-407575-1285474870.jpeg" style="width:100%;" /></div>
</li>
<li style="z-index:1;"><!-- list-element 2 -->
<div style="z-index:4;"><img src="http://img.brothersoft.com/screenshots/softimage/f/free_cat_wallpaper-407575-1285474870.jpeg" style="width:100%;" /></div>
</li>
<li style="z-index:1;"><!-- list-element 3 -->
<div style="z-index:4;"><img src="http://img.brothersoft.com/screenshots/softimage/f/free_cat_wallpaper-407575-1285474870.jpeg" style="width:100%;" /></div>
</li>
</ul>
Thanks
/* EDIT: My Mistake - I misunderstood your question. */
Do the positioning on the .img-wrapper. (code snippet updated below too)
See this fiddle: https://jsfiddle.net/tqtveoau/4/
/* END EDIT */
I'm guessing this isn't your production code but always try and use classes for your styles.
.list-item {
position: relative;
width: 100px;
}
.img-wrapper {
display: none;
position: absolute;
top: 0;
z-index: 1;
}
.img-wrapper img {
width: 120%;
border: 1px solid red;
}
.list-item:hover .img-wrapper {
display: block;
}
<ul>
<li class="list-item">
List Item
<div class="img-wrapper">
<img src="http://img.brothersoft.com/screenshots/softimage/f/free_cat_wallpaper-407575-1285474870.jpeg" style="width:100%;" />
</div>
</li>
<li class="list-item">
List Item
<div class="img-wrapper">
<img src="http://img.brothersoft.com/screenshots/softimage/f/free_cat_wallpaper-407575-1285474870.jpeg" style="width:100%;" />
</div>
</li>
<li class="list-item">
List Item
<div class="img-wrapper">
<img src="http://img.brothersoft.com/screenshots/softimage/f/free_cat_wallpaper-407575-1285474870.jpeg" style="width:100%;" />
</div>
</li>
</ul>
See the fiddle here:
https://jsfiddle.net/tqtveoau/3/
Move the styles from your img to the parent div and give it a class. Also set the background of the div that's absolute to white

How to top-align smaller inline-block elements next to larger elements?

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;
}

I'm having trouble making a navbar with a logo in the center, and two links on either side with equal spacing

I want to create a navbar with a logo in the center, and two navbar items (links) on either side, width equal spacing. I also need the logo to be lined up center with the navbar items.
Here's the code i have thus far. I'm using bootstrap.
HTML:
<div class="nav">
<div class="container">
<ul>
<li> Navbaritem1 </li>
<li> Navbaritem2 </li>
</ul>
<div class="logo">
<img src="Img/logo.png" class="img-responsive">
</div>
<ul>
<li> Navbaritem3 </li>
<li> Navbaritem4 </li>
</ul>
</div>
</div>
CSS:
.nav li {
display: inline;
margin-left:5%;
margin-right:5%;
}
.logo {
width: 100px;
display: inline-block;
height: auto;
}
.logo img {
float: none;
padding-top:10%;
}
ul {
display: inline;
text-align:center;
width:30%;
}
Did you try centering a div inside the container?
<div class="container">
<div class="center">
Centered content here
</div>
</div>
And the css:
.center {
display: table;
margin: 0 auto;
}
Also this post might've helped you with a quick search.