When viewed on a mobile device, the contact information bar should vertically collapse

Here is the code snippet I am working with:

<div class="topbarsection">
   <ul>
     <li class="alignleft">
       <a href="#">Office Address</a>
     </li>
     <li class="aligncenter">
       Office Timings
     </li>
     <li class="alignright">
       <a href="tel:0123456789">0123456789</a>
     </li>
   </ul>
</div>

This is how I've styled it using CSS:

.topbarsection {
background-color: #002e5b;
border-bottom: 1px solid #ddd;
color: #fff;
display: block;
font-family: Open Sans,arial;
font-size: 16px;
padding: 10px 5px;
position: fixed;
width: 100%;
z-index: 10000;
}
.topbarsection a {
color: #fff;
}
ul li {
display:inline;
list-style-type:none;
}
.alignleft {
float: left;
width:33.33333%;
text-align:left;
}
.aligncenter {
float: left;
width:33.33333%;
text-align:center;
}
.alignright {
float: left;
width:33.33333%;
text-align:right;
}

When viewed on Desktop browsers, the mobile number gets hidden as shown in screenshot 1.

On Mobile browsers, all three details appear in a single line instead of vertically stacked. It should look like this as shown in screenshot 3.

Answer №1

Your code has several issues that need attention.

  1. For better mobile responsiveness, consider using media queries.
  2. If you decide to float elements, make sure to use a clearfix to prevent parent collapsing.
  3. Remove default margin and padding on your unordered list (ul) element.
  4. Avoid unnecessary z-index values on the parent element.
  5. To ensure stacking at smaller sizes, set the width of list items (li) to 100% with display: block.
  6. Add a viewport metatag for mobile - check out WC3's recommendations.

  7. Please provide more detailed explanations in your posts!

Here is an updated version of your code - I have only made adjustments for screens under 480px. Feel free to expand on this example to enhance responsive design:

Let me know if this solution resolves your issues!

<div class="topbarsection clearfix">
   <ul>
     <li class="alignleft">
       <a href="#">Office Address</a>
     </li>
     <li class="aligncenter">
       Office Timings
     </li>
     <li class="alignright">
       <a href="tel:0123456789">0123456789</a>
     </li>
   </ul>
</div>

CSS

.clearfix:after {visibility: hidden; display: block; font-size: 0; content: " "; clear: both; height: 0; }

.topbarsection {
  background-color: #002e5b;
  border-bottom: 1px solid #ddd;
  color: #fff;
  display: block;
  font-family: Open Sans, arial;
  font-size: 16px;
  padding: 10px 5px;
  position: fixed;
  width: 100%;
}
.topbarsection a {
  color: #fff;
}
ul{
  margin-right: 2%;
  padding: 0;
}

ul li {
  display: inline;
  list-style-type: none;
}
.alignleft {
  float: left;
  width: 33.33333%;
  text-align: left;
}
.aligncenter {
  float: left;
  width: 33.33333%;
  text-align: center;
}
.alignright {
  float: left;
  width: 33.33333%;
  text-align: right;
}

@media (max-width: 480px) {
  ul li{
    display: block;
    text-align: center;
    margin: 0;
  }
  .alignleft,
  .aligncenter,
  .alignright{
    width: 100%;
    text-align: center;
  }
}

Answer №2

Dear Ranjeet, I have a suggestion for you.

Please consider adding the following code to your head section:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

Next, update your CSS with these modifications:

.topbarsection {
background-color: #002e5b;
border-bottom: 1px solid #ddd;
color: #fff;
display: block;
font-family: Open Sans,arial;
font-size: 16px;
padding: 10px 5px;
position: fixed;
width: 100%;
z-index: 10000;
}

@media (min-width: 768px){
.topbarsection a {
color: #fff;
}
ul li {
display:inline;
list-style-type:none;
}
.alignleft {
float: left;
width:33.33333%;
text-align:left;
}
.aligncenter {
float: left;
width:33.33333%;
text-align:center;
}
.alignright {
float: left;
width:33.33333%;
text-align:right;
}
}

Lastly, make sure to explore media queries in depth!

Answer №3

If you're facing an alignment issue with your content within the .topbarsection div, consider adding a secondary wrapper div inside it. The problem likely lies in the padding, which is not being taken into account when the width is set to 100%, causing misalignment.

To rectify this, avoid using padding directly on the .topbarsection div. Instead, introduce a new .wrapper div with appropriate padding for your content to sit within.

To address another issue concerning the layout of your <li> elements under certain screen widths, explore utilizing a @media query to implement different CSS rules based on the screen size.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for customizing text field appearance in Safari and Chrome

I haven't had a chance to test it on IE yet. My goal is to create a unique style for the background image and text box shape, along with borders, for the search bar on my website, [dead site]. If you check it out on Firefox or Opera and see the sear ...

The issue of CSS transitions flickering in Internet Explorer

Can someone help me troubleshoot an issue with this code in Internet Explorer? CSS: .nav-fillpath a { width: 100px; height: 100px; position: absolute; display: block; bottom: 0%; left:0; right:0; color: #3e3e3e; margin ...

Looking for a way to scroll images without relying on the marquee tag? Whether you prefer using JavaScript, jQuery,

<marquee> <div class="client"> <img src="images/c1.png"/> </div> <div class="client"> <img src="images/c2.png"/> </div> <div class="client"> ...

Optimizing Window Width with React.js and CSS

I'm currently in the process of building a responsive website with react. I am utilizing CSS stylesheets for styling and have used @media queries to ensure responsiveness. However, I've encountered an issue while testing in Chrome where the elem ...

Is there a way to modify or alter the layout specifically for mobile devices?

Currently, my desktop version displays the image first followed by content (h1, p, button), and then repeats in that order. However, I would like to restructure the HTML for the mobile version to make it more user-friendly. How can I achieve this without d ...

When hovering or mousing over, the text remains stationary and does not follow the scroll bar movement within the

A method I am using involves displaying additional data on hover for a shortened header, like this: Hover behavior However, the text shifts across the page when the scrollbar is used, as shown here: Scrollbar interaction This table showcases HTML, CSS, ...

A div element featuring a border with transparent edges on the top right and bottom left corners

Does anyone know how to code the border for the upper right corner and lower left corner of the box (as shown in the image below)? I would really appreciate some help. Thank you in advance! This is the HTML <div class="carouselle"> <div clas ...

"Effortless CSS Formatting in VS Code - A Guide to Automatic Styling

Recently started using VS code and I'm on the lookout for a solution to automatically format CSS whenever I save my work. Strangely, my searches haven't led me to any reliable extensions or clear guides on how to achieve auto formatting in VS cod ...

Using the combined power of CSS and jQuery to create dynamic visual effects based

Having some trouble figuring out why this code isn't functioning as expected... I've got a bunch of DIVs that have the class .rightColumnButton. Some of them currently have a height set to 30px: .rightColumnButton{ height:30px; width:206px; mar ...

Can input be styled instead of using a textarea for design?

Within the confines of a PDF's single-line display, I am in need of including text that does not contain new lines. While using an input instead of a textarea seems like the ideal choice, inputs do not naturally support line breaks and utilizing keydo ...

The styles for the React calendar are not being properly applied to the calendar component due to CSS overriding

Having trouble overriding the default Calendar.css file in NextJS while creating a calendar component. Even after adding my own custom styles, they aren't being applied. Deleting the css file contents doesn't change the format either. Only when c ...

Positioning elements in CSS using percentages allows for flexible and responsive

I am new to web design and I am trying to create a webpage similar to the one shown in this image. However, I want to set the height and width of the divs using percentage values instead of pixels. Whenever I attempt this, the boxes end up overlapping or ...

Encountering a problem with Material UI where the drop-down options are appearing below the footer of the modal window

Hey there, I'm currently using Material UI and React Select. One issue I've run into is that my dropdown options are appearing below the modal window. You can check out the code sandbox demo here I've tried adjusting the z-index and changi ...

A step-by-step guide on submitting a CSV file with Ajax along with additional form information

Imagine you have an HTML form with input fields for Name, Location, Address, and Family members (to be uploaded as a CSV file), along with a Submit button. However, when you click on submit, the data from the CSV file is not being passed to the PHP script. ...

Modify the useRef value prior to the HTML rendering (React functional component)

Hello everyone, I am attempting to update the value of useRef before the HTML is rendered. I have tried using useEffect for this purpose, but it runs after the HTML is ready, making it unsuitable for my needs. What I want to achieve is resetting the value ...

Use jQuery ajax to send form data and display the received information in a separate div

I'm working on a PHP test page where I need to submit a form with radios and checkboxes to process.php. The result should be displayed in #content_result on the same page without refreshing it. I attempted to use jQuery Ajax for this purpose, but noth ...

Issue with fixed positioning not behaving as intended while scrolling downwards

I am facing an issue with an element that has the .it class and I want to keep it fixed on the screen using the position: fixed CSS property. However, as I scroll down the page, the element moves along with the screen instead of staying in place. Below is ...

What is the best way to create a responsive Material UI Modal?

I have set up a Modal with a Carousel inside, but I am struggling to make it responsive. Despite trying various CSS solutions from StackOverflow, nothing seems to be working for me. How can I resolve this issue? CSS: media: { width: "auto&quo ...

Enable the set body to have certain sections that are scrollable by setting overflow=hidden

I am currently working on a website that features the following structure: <body> <section></section> <section></section> ... </body> To prevent scroll bars on the body, I have set the CSS property body{overflow:hidden ...

Checking if children have certain text using jQuery

My task involves filtering an HTML table. In order to accomplish this, I have created an 'each' callback for all 'tr' elements and check if any of their children contain a specific pattern. $("#filter").keypress(function() { var fi ...