Element Width Between Float Elements - css

I'm a noob to web dev and have a question:
I have a container that contains 4 inline elements: two buttons floating to the left, one button floating to the right, and a search box in the middle. The buttons have predefined dimensions (percentages of the container). I am trying to avoid giving the search box a predefined width, instead I would like the width of the search box to occupy as much space as it can between the floating elements on each side (refer to photo) such that it is more dynamic.
Is there any width setting technique to achieve this? I've tried adding all 3 buttons in HTML before adding the search box and then I've tried using the margin-left and margin-right value to hopefully stretch the search box. I've had no luck with this. Any help?
HTML
<div class = "SearchBox">
<input type="image" src="./Home Page Resources/ChapterIcon.png" onclick="alert('Trigger Upload Page')">
<input type="image" src="./Home Page Resources/UserSearchIcon-2.png" onclick="alert('Trigger Upload Page')">
<input type="image" src="./Home Page Resources/SearchIcon-2.png" onclick="alert('Trigger Upload Page')">
<input type="text" name="search">
</div>
CSS
.SearchBox {
display: inline-block;
position: relative;
float: right;
min-width: 35%;
height: 40px;
border: thin solid #80501F;
background-image: url("../Home Page Resources/WoodBackground-2.jpg");
border-top-right-radius: 5px;
border-top-left-radius: 5px;
}
.SearchBox input:nth-of-type(1) {
position: relative;
float:left;
width: 35px;
top: 3px;
margin-left: 1%;
margin-right: 0%;
}
.SearchBox input:nth-of-type(2) {
position: relative;
float: left;
width: 38px;
top: 0px;
margin-left: 0%;
margin-right: 0%;
}
.SearchBox input:nth-of-type(3){
height: 25px;
display: inline;
float: right;
position: relative;
top: 8px;
margin-left: 1%;
margin-right: 1%;
}
.SearchBox input:nth-of-type(4){
display: inline;
padding-left: 5px;
height: 25px;
font-family: 'Arial Black';
font-size: 100%;
margin-left: auto;
margin-right: auto;
width: auto;
position: relative;
top: 4px;
border-bottom-right-radius: 5px;
border-bottom-left-radius: 5px;
border-top-right-radius: 5px;
border-top-left-radius: 5px;
}

You could use a combination of display:table on a container and display: table-cell on each nested element.
For example, here's the HTML:
<div class="content">
<ul>
<li class="icon"> </li>
<li class="icon"> </li>
<li class="search"><input type="text"></li>
</ul>
</div>
And CSS:
.content {
width: 600px;
background: green;
}
ul {
background: gray;
display: table;
margin: 0;
padding: 10px;
width: 100%;
}
li {
list-style: none;
display: table-cell;
margin: 0;
padding: 0;
}
li.icon {
background: red;
width: 10%;
}
li.search {
width: 80%;
}
li.search input {
border: 0;
width: 100%;
}
Here's a JSFiddle to demonstrate: http://jsfiddle.net/mknkz32p/

Related

CSS keep image in ::before from expanding past the pseudo-element

I have a little mark at the bottom right of my own code snippet page, which should also contain my website's favicon. I want to use ::before for this but I have no clue how to resize the image to stay inside the 1em by 1em pseudo-element.
div#snippet {
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
background-color: rgba(200,200,200,0.3);
}
a#l2020-link {
color: blue;
background-color: lightgrey;
position: absolute;
bottom: 10px;
right: 10px;
border: 0px solid grey;
border-radius: 3px;
padding: 3px;
display: flex;
flex-direcion: row;
}
a#l2020-link::before {
content: url(https://www.lampe2020.de/favicon.ico);
width: 1em;
height: 1em;
display: inline-block;
}
<div id="snippet">
<!-- Imagine the code inputs and the iFrame to show the result here -->
Lampe2020.de
</div>
I want the favicon to be fully visible but shrunk down to 1em by 1em.
I've tried CSS object-fit but it had absolutely null effect on the image no matter what I set it to. overflow: hidden or overflow: clip kinda work but they obviously just cut off what's too much of the image and don't resize the image to fit.
You can set the content to "" and use background-image instead, and set the background-size to 1em.
div#snippet {
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
background-color: rgba(200,200,200,0.3);
}
a#l2020-link {
color: blue;
background-color: lightgrey;
position: absolute;
bottom: 10px;
right: 10px;
border: 0px solid grey;
border-radius: 3px;
padding: 3px;
display: flex;
flex-direcion: row;
}
a#l2020-link::before {
content:"";
background-image: url(https://www.lampe2020.de/favicon.ico);
width: 1em;
height: 1em;
background-size:1em;
display: inline-block;
}
<div id="snippet">
<!-- Imagine the code inputs and the iFrame to show the result here -->
Lampe2020.de
</div>

How to get logo, header and button in line without using margin hack.

I want a heading, a logo and a button to all be in line where the heading is centered, the logo is offset to it's right and the button is small on the right hand side.
I can hack this together if I fix the margins but this is not scalable and therefore don't want it as a final solution. What's a better way to do this?
My code is below and I also have it posted here as well: http://codepen.io/blueduckyy/pen/RpKoMJ .
HTML:
<div class="top-bar username-heading">
<img src="http://www.wonko.info/ipt/xfiles/interfaces/target.bmp" alt="Missing">
<h1>blueduckyy</h1>
<a class="button user-edit-button" href="/users/edit">Edit</a>
</div>
CSS:
.username-heading img {
float: right;
width: 100px;
height: 100px;
margin-left:-250px;
margin-right:150px;
}
.username-heading h1 {
position: relative;
top: 18px;
left: 10px;
text-align: center;
font-size: 3.5rem;
font-weight: bold;
}
.username-heading {
height: 120px;
background: yellow;
}
.user-edit-button {
float: right;
padding: 5px 5px 5px 5px;
margin-top: -20px;
margin-bottom: 1px;
You can place the items with 'position: absolute' inside the container:
.username-heading {
vertical-align: top;
position: static;
}
.username-heading img {
position: absolute;
right: 0;
width: 100px;
height: 100px;
}
.username-heading h1 {
position: absolute;
top: 18px;
margin: 0 auto;
width: 100%;
text-align: center;
font-size: 3.5rem;
font-weight: bold;
display: inline-block;
}
.username-heading {
height: 120px;
background: yellow;
}
.user-edit-button {
position: absolute;
right: 0;
top: 120px;
padding: 5px 5px 5px 5px;
margin-top: -20px;
margin-bottom: 1px;
}
Keep in mind that the '.username-heading' position must be other than 'static'.
Just use positioning,
position: relative;
top: 25px;
This is the only css youll need.... Here is the pen
.top-bar{
display: flex;
align-items: center;
justify-content: space-between;
background-color: orangered;
}
img{
height: 40px;
border-radius: 50%;
}

CSS - "p" tag background does not wrapp

This is a simple CSS question, however I don't have a clue yet. I have several p tags with a background color, my problem is that the background extends to 100% of the screen in every tag, I just want to wrapp the words, creating the effect as buttoms or blocks. I don't understand why the p background goes to all the width.
Fiddle: Exact example HERE
<div class="process_wrapper_mobile">
<div class="inter_mobile_process">
<p>Interview</p>
</div>
<div class="inter_mobile_process">
<p>Data reception</p>
</div>
<div class="inter_mobile_process">
<p>Design</p>
</div>
CSS:
.process_wrapper_mobile {
position: relative;
float: left;
width: 100%;
height: auto;
background-color: #CCC;
}
.process_wrapper_mobile .inter_mobile_process {
position: relative;
float: left;
width: 100%;
height: auto;
margin-bottom: 3%;
}
.process_wrapper_mobile .inter_mobile_process p {
text-align: center;
color: #fff;
font-size: 0.9em;
background-color: #333;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
padding: 3% 5%;
}
PLease check my fiddle.
Thanks!
The P element is a block element, which extends the whole width by default. If you change the display to display: inline-block, they will only take up the space of the text. Then you need to work on the inline positioning of the buttons.
p is block level element, you can add a span which is inline inside the p, and add your styling around that. However, you don't really need a p, just a span
.process_wrapper_mobile {
position: relative;
float: left;
width: 100%;
height: auto;
background-color: #CCC;
}
.process_wrapper_mobile .inter_mobile_process {
position: relative;
float: left;
width: 100%;
height: auto;
margin-bottom: 3%;
text-align: center; /* Move this here from the p */
}
/* Make the p into a span, it's not a paragraph */
.process_wrapper_mobile .inter_mobile_process span{
padding: 3% 5%;
display: inline-block;
color: #fff;
font-size: 0.9em;
background-color: #333;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
<div class="process_wrapper_mobile">
<div class="inter_mobile_process">
<span>Interview</span>
</div>
<div class="inter_mobile_process">
<span>Data reception</span>
</div>
<div class="inter_mobile_process">
<span>Design</span>
</div>
</div>
Update the fiddle check this link
--Added display:inline-block style to 'P' tag
--Added margin-bottom 10% to .process_wrapper_mobile .inter_mobile_process
Updated Code
.process_wrapper_mobile .inter_mobile_process {
position: relative;
float: left;
width: 100%;
height: auto;
**margin-bottom: 10%;**
}
.process_wrapper_mobile .inter_mobile_process p {
text-align: center;
color: #fff;
font-size: 0.9em;
background-color: #333;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
padding: 3% 5%;
**display:inline-block;**
}
Add float:left to .process_wrapper_mobile .inter_mobile_process p
.process_wrapper_mobile .inter_mobile_process p{
float:left
}

fixing position of div having float:left property

I have certain boxes which I want them to be side by side. I used float:left;margin-left:10px; and successfully achieve my aim.
But I want to lock their positions on screen i.e. fixed w.r.t to screen and no movements according to mouse. For that I tried to use `position:fixed', it too worked, but now it created a problem.
The problem is that the two boxes are now overlapping with each other and displaced with their location.
Her is the fiddle
.chatWindow {
display: inline-block;
position: relative;
width: 250px;
height: 280px;
bottom:0;
background: #FAFAFA;
margin-left: 10px;
margin-bottom:10px;
float: left;
border-radius: 3px;
border: 1px solid #7a7a7a;
z-index: 100000;
}
You can set the fixed property to parent div. Try this fiddle.
CSS
.chatWindow-parent{
position: fixed;
}
.chatWindow {
display: inline-block;
position: relative;
width: 250px;
height: 280px;
bottom:0;
background: #FAFAFA;
margin-left: 10px;
margin-bottom:10px;
border-radius: 3px;
border: 1px solid #7a7a7a;
z-index: 100000;
}
HTML
<div class="chatWindow-parent">
<div class="chatWindow"></div>
<div class="chatWindow"></div>
</div>
http://jsfiddle.net/2csBx/
You have to have 2 different classes. Otherwise by fixing the position you are telling them to be fixed in the same location.
Need to add a parent class
HTML
<div class="chatContainer">
<div class="chatWindow"></div>
<div class="chatWindow"></div>
</div>
CSS
body{
height: 2000px;
}
.chatContainer {
position: fixed;
bottom: 10px;
}
.chatWindow {
display: inline-block;
position: relative;
float: left;
width: 250px;
height: 280px;
bottom: 10px;
margin-left: 10px;
background: #FAFAFA;
border-radius: 3px;
border: 1px solid #7a7a7a;
z-index: 100000;
}
Try this fiddle: http://jsfiddle.net/ETwEF/2/

DIV styling problems

Hi I am currently working on a project that has lots of DIVs and sections and such.
I am currently having problems with my header. The search bar and the panes div are going down or going out from the "header" section when I'm trying to minimize the browser window.
Structure goes like this.
As you can see on the image above, the red part is the header and it has 3 divs inside it.
This is how it goes on the view:
<div id = "header" class = "fixed-top">
<div class = "wrapper">
<div id = "logo">
</div>
<div id = "search-box">
</div>
<div id = "panes">
</div>
</div>
</div>
The header's width is 100% and having a class of position-fixed.
The wrapper class has a width of 980px and margin is 0 auto/auto centre. I also made its position to absolute.
The logo style looks like this:
#logo {
width: 130px;
height: 45px;
float: left;
background:url(image.png);
position: relative;
margin: 4px 0 0 2px;
}
The search bar on the other hand looks like this:
#search-box {
width: 440px;
padding: 2px 8px;
float: left;
position: relative;
margin-left: 90px;
}
Lastly, the panes style is:
#panes {
float: right;
width: 170px;
height: 48px;
position: relative;
margin-right: 10px;
}
And by the way, the search-box div also have child divs. And panes div has a UL list and each LIs are floated left.
Is there anything I am missing out why this happens?
I also tried the "clearfix" but it is still happening.
Thanks.
Just Try This CSS code, it will work nice
*{
margin:0;
padding:0;
}
#header{
background-color:#ED1C24;
width:740px;
float:left;
}
#logo {
width: 124px;
height: 45px;
background:url(image.png);
position: relative;
margin: 4px 0 0 2px;
float:left
}
#search-box {
width: 420px;
margin-left:20px;
position: relative;
float:left
}
#panes {
width: 160px;
height: 48px;
position: relative;
margin-right: 10px;
float:left
}
Like this
DEMO
CSS
* {
margin: 0;
padding: 0;
}
#header {
background-color: #ED1C24;
display: table;
vertical-align: middle;
}
#logo {
width: 130px;
height: 45px;
background: url(image.png);
position: relative;
margin: 4px 0 0 2px;
display: table-cell;
}
#search-box {
width: 440px;
padding: 2px 8px;
position: relative;
margin-left: 90px;
display: table-cell;
}
#panes {
width: 170px;
height: 48px;
position: relative;
margin-right: 10px;
display: table-cell;
}

Resources